← Lessons

quiz vs the machine

Gold1450

Concurrency

The Herd Effect On Locks

Why naive distributed locks wake every waiter at once and how to avoid it.

5 min read · core · beat Gold to climb

A stampede on release

A simple distributed lock has every waiter watch the same lock node. When the holder releases it, the service notifies all of them at once. They all wake, all retry, and all but one fail again. This stampede is the herd effect, and it wastes work and spikes load.

The cost

With one hundred waiters, releasing the lock triggers one hundred notifications and one hundred retries, ninety nine of which immediately lose. As contention grows, the service spends more effort waking losers than granting the lock.

The queued fix

The cure is to give each waiter a unique place in line and have it watch only the node ahead of it.

  • Each client creates a sequential node, getting a unique number.
  • The lowest number holds the lock.
  • Each waiter watches only the node with the next lower number.

Now releasing the lock notifies exactly one waiter, the next in line. No herd, just an orderly handoff.

Key idea

The herd effect happens when releasing a lock wakes every waiter, so well designed distributed locks queue waiters and have each watch only its predecessor, waking just one node per release.

Check yourself

Answer to earn rating on the learn ladder.

1. What causes the herd effect on a lock?

2. How does the queued design avoid the herd?

3. Who holds the lock in the queued design?