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
- Cary G. Gray, David R. Cheriton (1989). Leases, an efficient fault-tolerant mechanism for distributed file cache consistency.