An order flow holds a lock across several service calls for 2 seconds: how do you choose its scope, what throughput does one contended key allow, and when does the lock store's write rate limit you?
answer
- one holder at a time
- hold time sets a key's ceiling
- which calls touch the contended state
- writes per lock: create, release, renewals
- coarse means rare acquisition, not broad work
basics
~20 sOne key admits at most 1/hold-time acquisitions per second, 0.5 at 2 seconds, so hold it only across calls touching that state. Splitting one lock into several adds writes; the store limits you when orders times writes per order nears its capacity.
solid answer
~50 sI size it from the hold time, with illustrative figures beyond the stated 2 seconds. One key admits at most `1 / hold time` acquisitions per second however large the fleet, so a 2-second lock on a popular product's key allows 0.5 orders per second, and demand of 3 per second just builds a queue. So I hold that key only across the calls touching its state: a 200 ms step gives 5 per second, a 50 ms call gives 20. The cost comes when other calls still need cover, because one lock becomes several, with more partial states and more writes. At two writes per lock, five locks cost 10 writes per order and eight cost 16, so 1,000 orders per second means 10,000 or 16,000 writes. The store is the limit when that total nears its measured write capacity, and its rising latency usually lengthens every hold before then.
code
pseudocode · 33 lines// Illustrative names and durations. Hot key: lock:product:P.
// Scope A: whole order. Hot key held about 2 s, at most 0.5 orders/s on P.
token = new_random_token()
if not set_if_absent_with_expiry("lock:product:P", token, lifetime = 10 s):
return RETRY_LATER // nothing done yet, nothing to undo
try:
validate_cart() // 150 ms
quote_reservation(P) // 150 ms
change_product_record(P) // 50 ms
if not take_payment(): // 1,200 ms
undo_reservation(P) // still under the claim
return PAYMENT_FAILED
write_order() // 150 ms
notify_fulfilment() // 300 ms
finally:
delete_if_value_matches("lock:product:P", token)
// Scope B: only the 50 ms on P's record. At most 20 orders/s on P.
validate_cart()
quote_reservation(P)
token = new_random_token()
if not set_if_absent_with_expiry("lock:product:P", token, lifetime = 2 s):
return RETRY_LATER // nothing changed yet, nothing to undo
try:
change_product_record(P)
finally:
delete_if_value_matches("lock:product:P", token)
if not take_payment(): // no claim held for this call
undo_reservation(P) // must claim the key again: two more writes
return PAYMENT_FAILED
write_order()
notify_fulfilment()go deeper
Recall that a lock key has one holder at a time, so the longer each holder keeps it, the fewer workers get through per second. One divided by the hold time is the number to remember.
Explain the arithmetic: a 2-second hold gives 0.5 acquisitions per second on one key, whatever the fleet size. Then explain the write count: each lock is a conditional create and a release, plus any renewals.
Show the judgment: find which calls touch the contended state, compare per-key demand with each scope's ceiling, then multiply orders by writes per order against the store's measured capacity. Name the partial states that narrowing exposes.
Frame the tradeoff: narrowing trades a limit on one key for more load on a store that many services may share. A long-held claim that hands key groups to owners removes the per-order write, but concentrates load and adds reassignment.
A **distributed lock** here is a key claimed with a lifetime in a shared store, so that one worker at a time acts on something. Its **scope** is how much work runs between claim and release, and which key the claim names. Scope sets two limits: what one contended key can pass per second, and how many writes the store absorbs per order. This assumes the lock is already justified. ## The worked example Every figure is this example's unless a source is named. An order makes five service calls totalling 2 seconds: cart validation 150 ms, stock reservation 200 ms (50 ms of it changes the product's record), payment 1,200 ms, order write 150 ms, fulfilment notice 300 ms. One popular product takes 3 orders per second at peak, so its key is the contended one. The catalogue as a whole takes 1,000 orders per second. ## One key's ceiling Holds on one key run in series, so it passes at most `1 / hold time` acquisitions per second. This is arithmetic, not a vendor's claim. - Whole order, 2 s: **0.5 per second**. - Reservation step, 200 ms: **5 per second**. - The one call on the record, 50 ms: **20 per second**. Three limits on that number: - **Workers do not move it.** A larger fleet adds waiters, not acquisitions. - **It binds one key.** Other products have their own ceilings, unless one key covers the whole catalogue. - **It is an upper bound.** Use the mean hold; the gap between a release and the next claim lowers it further. Against 3 orders per second, the 2-second hold falls behind by 2.5 orders every second. After 60 seconds, 150 orders are waiting and a new arrival would wait about 300 seconds. The 200 ms hold runs the key at 60% of its ceiling, the 50 ms hold at 15%. ## Choosing the scope 1. **Find the contended state.** Name the key workers fight over and the calls touching what it covers. Here payment never touches the product's record, yet it is 60% of the whole-order hold. 2. **Compare demand with the ceiling.** Keep peak demand on the hottest key well below `1 / hold time`, because waits climb steeply near it. 3. **Decide what others may see.** A long lock keeps other workers out of in-between states in the common case; a narrow one lets them see stock reserved but unpaid. 4. **Count the store writes** for the scope left standing. ## What narrowing costs Narrowing one lock to one call adds no writes. The cost arrives when the other calls still need cover, so one lock becomes several. - **More acquisitions:** each is a conditional create and a release, plus a write per renewal. - **More exposed partial states:** the undo for a failed payment exists under any scope, but with a narrow lock it runs in the open and must claim the key again. - **Several keys at once:** an order holding three product keys needs one agreed acquisition order, and a key taken early is held while waiting for later ones, so take the hottest last. A shorter hold also allows a shorter lifetime, so a crashed holder blocks the hot key for less time. ## When the store's write rate is the limit `store writes per second = orders per second x locks per order x (2 + renewals per lock)` These holds sit far below a 10-second lifetime, so renewals are zero, and the order is taken to touch eight records: | Scope | Hold on hot key | Key ceiling | Locks per order | Writes per order | Writes/s at 1,000 orders/s | Orders/s at 30,000 writes/s | |---|---|---|---|---|---|---| | Whole order | 2 s | 0.5/s | 1 | 2 | 2,000 | 15,000 | | One per step | 200 ms | 5/s | 5 | 10 | 10,000 | 3,000 | | One per resource | 50 ms | 20/s | 8 | 16 | 16,000 | about 1,900 | The last column is an order of magnitude only: one consensus-backed coordination store documents, on its reference hardware, a three-member cluster completing more than 30,000 requests per second under heavy load, with commit latency limited by network and disk IO. On a real store, reads and other users share that capacity. Budget half of it (this example's choice), 15,000 writes per second, and the per-step scope fits at 1,000 orders per second while the per-resource scope does not. Measure your own store, and watch latency first: the same documentation says average latency usually rises with throughput, and each hold includes at least one commit wait, so a busy store lowers every key's ceiling, narrow holds most. Partitioning the store can raise its total write rate but leaves one key's ceiling alone. So narrowing trades a limit on one key for a limit on the whole store. ## What coarse means in the literature A published coarse-grained lock service's design paper calls a lock fine-grained when it may be held only briefly, seconds or less, and expects coarse use instead; its example is a lock electing a primary that then serves for perhaps hours or days. By that wording the 2-second lock is fine-grained, however broad it sounds. The paper says that in fine-grained use the rate at the lock service grows with clients' combined transaction rate, and that brief unavailability can stall many clients. That is its expectation about its own service, not a law. The escape it sketches stops writing per transaction: a long-held claim assigns a group of locks to one application-specific server. The limit moves to that server, and a lost claim means reassigning the group.
- Suppose payment takes 1.2 of a whole-order lock's 2 seconds. What happens to each scope if payment slows to 10 seconds?The whole-order hold becomes 10.8 seconds, so the product key's ceiling drops to about 0.09 per second, and a lifetime sized at 10 seconds now needs renewals, each one a further write. Holds of 200 ms or 50 ms that exclude payment stay at 5 and 20 per second. Only a lock around the payment step itself gets slower.
- An order holds three product keys at once. How does the order of acquisition affect the popular key's throughput?A key's hold runs from its claim to its release, so a key taken first is also held while the worker waits for the others. Their contention lengthens its hold and lowers its ceiling. Taking the hottest key last keeps its hold close to the work done on it. Every worker must use the same order, or two can block each other until a lifetime ends.
- The lock store is near its write limit and a whole-order lock is off the table. What are the options?Cut locks per order first: remove locks on keys nobody contends, since each costs two writes. Partition the store, which can raise total write capacity but leaves one key's ceiling unchanged. Or stop writing per order: a long-held claim gives a group of keys to one process, which orders that work itself. That moves the limit to the process and adds reassignment when its claim is lost.
- How do production numbers tell you whether one key or the whole lock store is the limit?A key limit is local: failed claims and waits concentrate on a few keys, whose acquisition rate sits near one over their mean hold, while other keys and the store's latency look normal. A store limit is global: claim latency rises on every key, including uncontended ones, as the store's write rate approaches what you measured it can sustain.
- Adding more workers raises the throughput of one contended lock key.
- A 2-second lock around a whole order is coarse-grained use, so its load is independent of order rate.
- The narrowest lock is always best, because only the contended key's ceiling matters.
- A coordination store's documented 30,000 requests per second is a lock budget on any hardware.
- Partitioning the lock store raises the ceiling on one popular key.
- Renewing a lease more often lets the same hold serve more orders on that key.