Failures and antipatterns · Intermediate

The thundering-herd problem

Trace excess wakeups for limited work and design predicate checks together with notification granularity.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Many waiters wake for one opportunity
  2. Thirty-two workers compete for one item
  3. Readiness does not reserve a resource
  4. Adjust notification scope and waiting
  5. Ask Logeia about this articleCheck your understanding

Many waiters wake for one opportunity

A thundering herd occurs when many waiters wake for the same event and compete for a much smaller amount of available work. If a single item arrives but every worker is notified, most awakened workers may obtain nothing and return to sleep. Wakeup and lock-acquisition overhead can then consume substantial resources. Distinguish this notification pattern from a cache stampede, where simultaneous misses trigger redundant regeneration or fetching. They can occur together, but identifying which mechanism creates the redundant work is necessary to choose an effective remedy.

Thirty-two workers compete for one item

Consider thirty-two workers waiting on a condition variable for a queue to become nonempty. Broadcasting after one insertion wakes all of them, although only one can remove that item. The others reacquire the lock, examine the predicate and wait again. A notification does not reserve the queued item for its recipient. Checking the predicate in a loop remains essential for correctness in the presence of competition and spurious wakeups. Removing that check to reduce apparent overhead would trade a performance symptom for an incorrect synchronization protocol.

Readiness does not reserve a resource

Readiness-based I/O can produce related patterns when several waiters observe the same opportunity. Linux epoll provides EPOLLEXCLUSIVE for particular registration patterns to help limit wakeups, with restrictions on flags and registration operations. It is not a general replacement for application synchronization, and readiness still does not mean ownership of the resource. Compare wakeups with completed useful work, unsuccessful acquisitions and CPU cost. Those observations distinguish excess coordination from a system that is simply busy serving a genuinely large number of independent requests.

Adjust notification scope and waiting

When one work item only enables one worker to proceed, consider a targeted notification rather than a broadcast. A state change that enables every waiter, such as shutdown, may legitimately require broadcasting. Dispatching jobs, splitting wait queues or batching notifications can also help. Notification suppression can go too far: a broken protocol may leave available work with every worker asleep. Design state transitions and notification rules together, and test both ordinary arrivals and lifecycle events. The goal is sufficient wakeups for progress, not the smallest wakeup count regardless of behavior.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 18

Understand the systems behind the web · STEP 15

Choose a learning path