Distributed lock
0-day streakReview

Your progress

Level 1

0 XP100 XP to level 2
0day streak
0best streak
0topics started
0cards reviewed
    Review cards

    Progress is stored only in this browser. No account is required.

    Layer 3 · Go deeper · 11 min

    Go deeper

    Size it, build it, then make the calls an interviewer will push you on.

    Back of the envelope

    Size it up

    Tap through each step of the arithmetic.

    How many lock operations a second at peak?

      Why give each process one lease instead of a TTL per lock?

        How many consensus groups does the lock store need?

          Can a fencing token’s counter ever run out?

            Build-up · 5 steps

            Build the high-level design

            One functional requirement at a time, on a single lock server, the way most teams start.

            Step 1 of 5

            Acquire a named lock

            Acquire is one atomic step: create the record only if the name is free, with an expiry, so two simultaneous requests can’t both win. The server returns a token; the worker then writes to the payouts DB.

            6 interview moments

            You’re the candidate

            The interviewer pushes on the hard parts. Pick an approach and see how it holds up.

            1. Deep dive 1 of 6

              Interviewer

              “Many teams want locks. Some guard nightly thumbnail jobs; others guard payout batches. What do you build for them?”

              Your move. Pick an approach:

            2. Deep dive 2 of 6

              Interviewer

              “Your lock server dies in the midnight batch rush. How do you make sure no grant is lost or doubled?”

              Your move. Pick an approach:

            3. Deep dive 3 of 6

              Interviewer

              “The lock service is perfect, but a holder freezes in a GC pause past its lease. How do you stop its late write?”

              Meanwhile another worker has got the lock and paid the batch.

              Your move. Pick an approach:

            4. Deep dive 4 of 6

              Interviewer

              “NTP steps a clock, a VM resumes after migration, a network blip drops renewals. How do you time leases safely?”

              Your move. Pick an approach:

            5. Deep dive 5 of 6

              Interviewer

              “At the 10:00 tatkal rush, 2,000 requests a second want the lock for one train, date and class. What do you do?”

              Each holder does 20 ms of work plus two lock writes of about 3 ms, so at most 38 holders a second get through.

              Your move. Pick an approach:

            6. Deep dive 6 of 6

              Interviewer

              “Merchant 48213’s payout jobs must run one at a time, in order. Do you need the lock service at all?”

              Your move. Pick an approach:

            Build-up · 7 steps

            Follow one payout batch through it

            The hot path is the client library, a frontend and one group leader. The protected stores do the fencing; everything else is off to the side.

            Step 1 of 7

            Session

            The client library opens one session per group it uses: a 15 s lease, kept alive every 5 s over one stream. Locks hang off it, so a dead process loses them all at once.