Try
↑ ↓ move↵ openesc close

Interview prep / Locks, leases and fencing

Junior · Locks, leases and fencing · System design interview question

A worker holds a 30-second lease and renews it every 10 seconds. Why renew so early instead of just before the lease ends?

Sketch your own answer first, then compare it with the one below.

A strong answer

A renewal is a network call, and it can be slow, lost or retried. If the worker waits until 29 seconds, a single dropped packet means it loses the lease before it can try again.

Renewing at one third of the lease leaves room for two failed attempts. Renewal at 10 seconds that fails can be retried at 15 and 20, and the lease does not end until 30. The rule of thumb is to renew at between a third and a half of the lease length, and to retry quickly after a failure.

There is a limit to what early renewal protects. If the worker is paused for 40 seconds, it renews nothing, and the lease expires however carefully it was timed. What it can do is stop work as soon as it fails to renew. A worker that cannot reach the lock service for 20 seconds should assume it may have lost the lease, stop starting new protected operations, and re-acquire the lock if it wants to continue, rather than assuming everything is fine.

Sources

Learn it by running it

The locks, leases and fencing lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms