Architecture / 6 / 9
Distributed locks
A marketplace pays its sellers with a nightly job. With two pods for availability the job runs twice and every seller is paid twice. A lock in Redis makes one pod wait for the other, until a lease runs out mid-job or a paused process wakes up still believing it holds the lock. Fencing tokens checked by the database, an idempotency key at the bank and an honest look at Redlock show what a lock can and cannot promise, and when a lock in the database itself is the better tool.
- 14 min
- Advanced
- 12 steps
- Double payouts
- 0 /day
- Stale writes accepted
- 0
- Stale writes rejected
- 0
- request
- reply
- redelivery
- duplicate
- lost
- call: the reply returns on the same line
Numbers are a model scenario, not measurements
The real request
Worker A → BankRequest
02:00:00 pod-a cron settle-payouts batch b_11181
02:00:00.2 read 2 140 pending payouts2
02:00:01 POST /v1/transfers x 2 1403
02:00:18 mark 2 140 rows paid, done in 18 s4
-- one pod, one run, nobody else
cron— A timer inside the service: every night at 02:00.pending— Payouts not yet sent. The job's to-do list.transfers— A real money movement for every row.18 s— How long the job holds its work. It will matter.
Build it yourselfCheat sheetCompare lessons
Course: 74 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes
11 Case studies