skip to content

With Redlock across five independent Redis masters and a 10-second auto-release, how does a client decide it holds the lock, what validity is left, and why do Martin Kleppmann and its author disagree about relying on it for correctness?

level: seniorimportance: should knowfreq 38%

answer

  1. two conditions, both required
  2. same key and value on every master
  3. clock read before the first attempt
  4. auto-release minus elapsed, then drift margin
  5. rising number; semi-synchronous model

basics

~20 s

A Redlock client holds the lock after setting it on at least three of five Redis masters within the auto-release; validity is auto-release minus elapsed time. Kleppmann argues that is unsafe when correctness depends on it; its author disputes that.

solid answer

~50 s

The client reads the time, then tries all five independent masters in parallel with one key name and one random value: set only if absent, with a 10-second expiry and a per-master timeout of about 5-50 ms. It holds the lock only if at least three accepted and elapsed time is below the auto-release; otherwise it unlocks all five. With my example's 50 ms elapsed, validity is 10,000 - 50 = 9,950 ms, and a 5 ms drift margin leaves 9,945 ms. Kleppmann, framing locks as for efficiency or correctness, argues that for correctness Redlock falls short: it produces no number that rises on each acquisition, and its safety rests on timing assumptions. Its author, Salvatore Sanfilippo, replies that it needs only processes counting time at about the same rate, and that the missing counter is common to auto-release locks. I would report both positions.

code

pseudocode · 24 lines
pseudocode
// Illustrative names. Figures are the scenario's.
AUTO_RELEASE_MS = 10000
TIMEOUT_MS      = 50
DRIFT_MS        = 5
MAJORITY        = 3   // of 5 masters

function acquire(key):
    value = new_random_value()
    t1 = now_ms()

    accepted = in parallel, for each master:
        set key to value only if absent,
        expiry AUTO_RELEASE_MS, timeout TIMEOUT_MS

    t2 = now_ms()
    validity = AUTO_RELEASE_MS - (t2 - t1)

    if count(accepted) >= MAJORITY and validity > 0:
        return held(value, finish_by = t2 + validity - DRIFT_MS)

    for each master:   // all five, not only those in accepted
        remove key only if its value equals value
    wait random_delay()
    return not_held

go deeper

for a junior

Recall the shape: one key and one random value go to several independent Redis masters, a majority must accept, and the lock lasts only as long as its expiry minus the time spent getting it.

for a middle

Explain the two conditions together, majority and elapsed time, and compute validity on the spot from an auto-release and an elapsed time. Be ready to say why a failure means unlocking every master.

for a senior

Carry the numbers through to a finish-by time, then say what each author argues and what the argument rests on. Name the timing assumption your deployment would lean on, and do not declare a winner.

for a principal

The tradeoff to own is what a wrong result would cost here against what running five independent masters costs. Present the published positions as positions, then state which assumptions your team will operate on.

## What is being decided **Redlock** is the distributed lock algorithm on the Redis documentation's distributed-locks page. Instead of trusting one server, a client asks several **independent masters**: Redis servers with no replication or other coordination between them. The page's example uses five. Each master holds a key that expires by itself, so a crashed client cannot keep the lock forever. That expiry is the **auto-release** time. **Validity** is the window in which the client may treat the lock as its own. This answer's scenario is five masters, a 10-second auto-release and a job that runs once. ## How the client decides it holds the lock 1. Read the current time in milliseconds; call it T1. 2. Try all five masters in parallel with the same key name and the same random value: set the key only if absent, with a 10-second expiry. Each attempt has a timeout small against the auto-release, and the page suggests about 5-50 ms for 10 seconds. 3. Compute elapsed time by reading the clock again (T2) and subtracting T1. 4. The lock is acquired **only if** at least three masters accepted (N/2+1 with the division rounded down) **and** elapsed time is below the initial validity time, which is the auto-release. 5. If acquired, validity is the initial validity minus elapsed time. 6. Otherwise the client tries to unlock **all five**, including masters it believes it failed to lock, and retries after a random delay. Reasoning from the mechanism: a timed-out attempt may still have set its key, with only the reply lost. The per-master timeout stays small so an unavailable master cannot eat the window. At 50 ms, even five attempts made one at a time would cost 5 x 50 = 250 ms, or 2.5% of the auto-release. A 3,000 ms timeout with one dead master would leave a client that waits it out at most 7,000 ms, even in parallel. ## One validity calculation | Input | Value | Whose figure | |---|---|---| | Auto-release | 10,000 ms | scenario, also the page's example | | Per-master timeout | 50 ms | scenario, top of the page's range | | Masters that accepted | 3 of 5 | scenario, two masters are down | | Elapsed (T2 - T1) | 50 ms | scenario, the client waits out both timeouts | | Drift allowance | 5 ms | scenario, the page says only a few milliseconds | Validity is 10,000 - 50 = **9,950 ms**. The page limits its mutual-exclusion guarantee to a holder that finishes within validity minus a few milliseconds for clock drift, so the client plans on 9,950 - 5 = **9,945 ms** after T2. A 9-second job leaves 945 ms spare; a 10-second job does not fit. The page argues for the same bound from the keys themselves. They land between T1 and T2, and the earliest could have been set right at T1, so the first to expire lasts at least `MIN_VALIDITY`: the key's time to live, minus (T2 - T1), minus clock drift. That is why the clock starts before the first attempt. Now the failing path. The same three masters accept, but the client stalls and measures 10,200 ms. Validity would be 10,000 - 10,200 = -200 ms, so a majority is not enough: it counts the lock as not acquired and unlocks all five. ## The published dispute Martin Kleppmann's 2016 article separates locks for **efficiency**, where a failure costs a little duplicate work, from locks for **correctness**, where a failure corrupts data, and aims its critique at the second. Salvatore Sanfilippo (antirez), Redlock's author, published a reply disagreeing with the analysis. | Point | Kleppmann argues | Sanfilippo replies | |---|---|---| | Ordering | Redlock produces no number guaranteed to increase on each acquisition, and has no facility for fencing tokens | That is common to any auto-release lock without a monotonic counter; a unique random value can support a check-and-set | | Clocks | Safety depends on keys expiring after about the right time | Processes need only count time at about the same speed, with bounded error, not synchronized absolute clocks | | Delays | Safety depends on network delay being small against expiry and pauses being much shorter than it | The elapsed-time check rejects a slow acquisition; a delay after it is a problem every auto-release lock shares | - **Kleppmann's clock example.** A client holds masters A, B and C; C's clock jumps forward and its key expires; a second client takes C, D and E; both believe they hold the lock. He accepts that a well-behaved datacenter meets these assumptions most of the time, and argues that is not enough for correctness. - **Sanfilippo's concession.** He says operators can avoid such jumps, and agrees that implementations should move to a monotonic time API. ## Reporting it in a design discussion - Give the two conditions and the arithmetic as facts, because they are what the page states. - Attribute each position to its author, with what it rests on: Kleppmann on a system where timing cannot be assumed, Sanfilippo on a semi-synchronous model. - Add what the page says today: it links both articles, tells readers concerned about correctness to implement fencing tokens, and states that key expiry does not use a monotonic clock, so a wall-clock shift may let more than one process acquire a lock. - Name which timing assumption your own deployment would be relying on.

  • Why does Redlock's elapsed-time clock start before the first master is contacted, not when the third master accepts?
    Keys land at different moments between T1 and T2. At worst the first was set right at T1, so it expires first and every other key expires later. Counting from T1 gives a bound that holds for the earliest-expiring key: the documentation's `MIN_VALIDITY`, the key's time to live minus (T2 - T1) minus clock drift. Counting from the third acceptance would overstate the window.
  • In Redlock, what does a 5 ms drift allowance on a 10-second auto-release assume about clocks?
    It assumes processes differ in rate by at most 5 / 10,000, or 0.05%, over that window. That figure is derived here, not documented; the Redis page says only a few milliseconds. Sanfilippo illustrates his model with counting 5 seconds at a maximum 10% error. At that rate, 10 seconds gives 1,000 ms, leaving 8,950 ms rather than 9,945 ms. Size the allowance from the rate error you can defend.
  • A Redlock master crashes and restarts having lost its key: how can a second client then also reach three of five?
    The first client locked A, B and C. C restarts empty, so C, D and E are all lockable by someone else, which the Redis page calls a violation of exclusivity. Its remedy keeps a crashed master out for a bit longer than the longest auto-release in use, at an availability cost. Kleppmann notes that this delay relies on measuring time; Sanfilippo calls it optional if each write reaches disk first.
  • How does Sanfilippo answer the point that Redlock produces no rising number on each acquisition?
    He argues the objection applies to any auto-release lock without a monotonic counter, and that a store able to reject old numbers could generate them itself. He says the unique random value can drive a check-and-set: mark the resource with it, then write only if it is unchanged. He adds that many resources check nothing, such as an external API. Kleppmann's position is that a random value gives no ordering.

  • Three of five masters accepted, so the client holds the lock however long that took.
  • Validity is the full auto-release, because every key was set with that expiry.
  • After a failed acquisition, only the masters that answered need unlocking.
  • Redlock needs perfectly synchronized wall clocks across all of its masters.
  • Redlock's random value already rises with each acquisition, so it orders lock holders.
  • The Kleppmann and Sanfilippo argument is settled, so you just cite the winner.