A colleague proposes a lock for payment processing built on five independent in-memory key-value servers. A client sets a key with a time-to-live on each, and counts itself the holder if it succeeds on three within less than the time-to-live. There is no consensus among the servers. Assess it.
Sketch your own answer first, then compare it with the one below.
A strong answer
The majority count looks like consensus but is not. The servers never talk to each other, and each one decides by its own clock when the key expires. Safety therefore depends on timing: that clocks run at about the right rate, that network delay is small against the time-to-live, and that a client is not paused for long between acquiring and using the lock.
Two sequences break it with no bug involved. A clock on one of the three servers that granted client 1 jumps forward and its key expires early. Client 2 then gets that server plus two others that client 1 did not reach, and both hold a majority. Or client 1 pauses after sending its requests, the keys expire, client 2 acquires, and client 1 wakes up, reads its buffered replies and believes it holds the lock.
There is also no fencing token. The random value each client stores does not increase, so the payment store cannot order two holders.
For payments, I would reject it. Use a consensus-backed lock service that issues increasing tokens, and make the payment store check them or use idempotency keys. If the lock is only to avoid duplicated work, a single server with a lease is simpler and no less safe. The designer of this scheme has disputed these criticisms, so read both arguments before deciding.
Sources
- Martin Kleppmann (2016). How to do distributed locking.