Explain Lease vs Lock.
A lease is a time-limited lock that automatically expires after a set duration, while a lock remains held until explicitly released.
When to Use
Use a lease in distributed systems where failures may prevent a process from releasing a lock. Use a lock in local systems or short-lived tasks where explicit release is guaranteed.
Example
If a service holds a 10-second lease on a file and crashes, the file unlocks after 10 seconds. With a normal lock, the file may remain locked forever.
Want to dive deeper?
Strengthen your prep with:
- Grokking System Design Fundamentals
- Grokking the Coding Interview
- Mock Interviews with ex-FAANG engineers
Why Is It Important
Locks risk deadlocks if processes crash without releasing them. Leases improve fault tolerance by expiring automatically, ensuring resources aren’t stuck.
Interview Tips
In interviews, define both clearly: locks are indefinite until released, leases are time-bound. Use examples, highlight failure scenarios, and explain why leases help avoid deadlocks.
Trade-offs
Leases provide safety but add overhead in managing timeouts and renewals. Locks are simpler but risk indefinite blocking if not managed carefully.
Pitfalls
Common mistakes include setting lease times too short (expiring mid-task) or too long (wasting resources). With locks, the pitfall is forgetting to release them, leading to deadlocks.
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78