← Lessons

quiz vs the machine

Silver1120

Concurrency

The Distributed Semaphore

Limiting how many nodes hold a scarce resource at once across a cluster.

4 min read · intro · beat Silver to climb

Counting permits across machines

A semaphore holds a fixed number of permits. A node must acquire a permit before using a shared resource and returns it when done. A distributed semaphore enforces this limit across many machines using a shared store.

Why limit access

Some resources tolerate only so many concurrent users. A database may allow forty connections, or a paid API may cap calls per second. A distributed semaphore of size forty ensures the whole fleet, not just one process, respects the cap.

  • A node tries to acquire one of the available permits.
  • If none are free, it waits until another node releases.
  • On finishing, the node releases the permit back to the pool.

The hard part

The count must be updated atomically. If two nodes read four free permits and both grab one, you can overshoot. The shared store must make acquire a single atomic step so the total in use never exceeds the limit.

Key idea

A distributed semaphore caps how many nodes use a scarce resource at once by handing out a fixed pool of permits, relying on atomic updates so the limit is never exceeded.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a distributed semaphore limit?

2. Why must acquire be atomic?