Resource starvation

Also known as: starvation · thread starvation

On this page 7

Resource starvation is a condition in concurrency where a thread, process or request is ready to make progress but is repeatedly denied the CPU time, lock, memory or other resource it needs, because other contenders keep receiving it first. Unlike a deadlock, the system as a whole keeps working; only the starved task stalls, sometimes indefinitely.

In programming, starvation is also called thread starvation when the victim is a thread, and it violates the liveness property known as bounded waiting: the guarantee that every request is eventually served within a finite number of turns.

How resource starvation arises

Starvation results from an allocation policy that is not fair, combined with sustained demand from other contenders. A scheduler or lock that always picks the highest priority, the newest arrival or simply the fastest thread can leave a slower contender waiting forever, even though the resource becomes free many times.

Three ingredients are usually present. First, there is contention: more requesters than the resource can serve at once. Second, the selection rule has no aging or ordering guarantee. Third, the favored group generates continuous demand, so the gap the victim needs never appears. Remove any one of these and starvation usually disappears.

The table below lists the resources that are most often starved and how the symptom typically appears.

ResourceTypical causeVisible symptom
CPU timeStrict priority schedulingLow-priority jobs never finish
Mutex or monitorUnfair lock bargingOne thread waits while others re-acquire
Read-write lockContinuous stream of readersWriters never obtain the lock
Thread pool workerLong or blocking tasks fill the poolQueued tasks time out
Connection poolLeaked or long-held connectionsRequests wait for a free connection
Network or disk bandwidthOne heavy flow dominatesLatency spikes for small flows
Resources that can be starved

Starvation compared with deadlock and livelock

Starvation, deadlock and livelock are all liveness failures, but they differ in who is affected and whether anything progresses. In a deadlock, a cycle of tasks each holds what another needs, so all of them stop. In a livelock, tasks keep changing state in response to one another without completing work. In starvation, the system progresses but an individual task does not.

FailureSystem progressAffected tasksTypical remedy
Resource starvationYesOne or a fewFair queuing, aging
DeadlockNo, for involved tasksAll tasks in the cycleLock ordering, timeouts
LivelockNo useful workAll interacting tasksRandomized backoff
Priority inversionPartialHigh-priority taskPriority inheritance
Liveness failures side by side

Priority inversion is closely related: a high-priority task waits for a lock held by a low-priority task, which is itself starved of CPU by medium-priority work. The result is that the most important task is effectively starved, which is why real-time systems implement priority inheritance.

Scheduling and locking patterns that cause starvation

Most cases of starvation can be traced to a small set of recurring patterns. Recognizing them during design review is much cheaper than diagnosing a production stall.

  • Strict priority scheduling without aging lets high-priority work monopolize the processor.
  • Reader-preferring read-write locks allow overlapping readers to block writers indefinitely.
  • Barging locks, where a releasing thread can immediately re-acquire, favor threads already running on a CPU.
  • Shortest-job-first policies can postpone long jobs forever under continuous short arrivals.
  • Spinlocks on contended cores favor the thread with better cache locality.
  • Bounded thread pools with blocking tasks let dependent tasks wait for workers that never free up.

The last pattern deserves attention in asynchronous programming. When every worker in a pool blocks on a result that must be computed by another task queued in the same exhausted pool, the queued task is starved, and the symptom can resemble a deadlock even though no lock cycle exists.

Preventing and mitigating starvation

Prevention means guaranteeing bounded waiting through the allocation policy, not hoping that load stays low. The appropriate technique depends on which resource is contended and how much throughput the system can trade for fairness.

TechniqueHow it worksTrade-off
AgingRaises the priority of tasks the longer they waitAdds scheduler bookkeeping
Fair locksGrant ownership in FIFO orderLower throughput under contention
Writer-preferring locksBlock new readers once a writer waitsCan starve readers instead
Ticket locksServe requests by issued ticket numberPoor scaling on many cores
Separate thread poolsIsolate blocking or dependent workMore threads to tune
Weighted fair queuingShare capacity by assigned weightsRequires weight configuration
Mitigation techniques and their trade-offs

Fairness has a cost. A strictly fair lock forces a context switch to the next waiter instead of letting the running thread continue, which can reduce throughput substantially under heavy contention. Many runtimes therefore default to unfair but fast locks and offer a fair variant, such as the fairness flag of Java’s ReentrantLock, for cases where bounded waiting matters more than raw speed.

Detecting starvation in running systems

Starvation is hard to detect because averages look healthy while a minority of tasks wait. Monitoring should therefore focus on tail behavior and per-task waiting time rather than mean throughput.

SignalWhat it suggestsNext diagnostic step
Rising p99 latency, stable medianA subset of requests waits too longBreak latency down by request class
Growing queue lengthWorkers never reach queued tasksInspect pool saturation and blocking calls
Threads stuck in waiting stateLock acquisition never succeedsTake thread dumps over time
Watchdog or timeout alarmsTasks exceed bounded waitingCorrelate with lock hold times
Signals that point to starvation

A practical rule is to record how long each request waits for a lock, a worker or a connection, and to alert on the maximum, not the average. Repeated thread dumps that show the same thread waiting while others cycle through the lock are strong evidence of lock-level starvation.

The idea in brief

Resource starvation is like standing at a busy counter where the clerk always serves whoever shouts loudest. The shop stays busy and many customers leave happy, but one quiet customer may never be served.

In a computer, many tasks share processors, locks and connections. If the rules for sharing always favor certain tasks, others can wait without end. The fix is to add a fair rule, such as taking turns or giving extra priority to anyone who has waited too long.

A practical scenario

An inventory service caches product data behind a reader-preferring read-write lock. During a sales event, about 4,000 read requests per second keep at least one reader inside the lock at all times. A background job that applies price updates needs the write lock, but it waits for more than 40 minutes, so customers see outdated prices even though every server reports normal CPU use.

Engineers notice that the median read latency is fine while the update job never completes. Thread dumps show the writer blocked on the same lock in every sample. The team switches to a writer-preferring lock policy, which blocks new readers as soon as a writer is waiting, and adds an alert on maximum lock wait time. Price updates now finish within a few hundred milliseconds, with only a small increase in read latency during each update.