Glossary term
Event Loop Lag
Engineering definition of event loop lag covering callback delay, handler blocking, queue buildup, timeout margin, starvation and validation evidence.
Definition
metricEvent loop lag is the delay between when a single-threaded event loop should run a callback or timer and when it actually runs it.
Event loop lag appears in event-driven services, embedded gateways, message brokers, telemetry processors, UI runtimes and control software when one loop serializes timers, callbacks, I/O completions or messages. A useful review states the probe interval, expected callback time, observed callback time, handler duration, queue depth, blocking operation, timeout impact, starvation risk and validation evidence.
Event loop lag is the delay between when a single-threaded event loop should run a callback or timer and when it actually runs it. It is a direct symptom of a loop that is busy, blocked, overloaded or starved behind earlier work.
Event loops are useful because they serialize state changes and reduce locking. The tradeoff is that one slow handler can delay timers, network callbacks, cancellation, health checks, watchdog kicks and retry scheduling for unrelated work.
Lag Measurement
For a scheduled callback with expected run time:
and observed run time:
the event loop lag is:
The metric should be reported as a distribution, not only an average. A small mean can hide rare long stalls that break timeouts or watchdog windows.
Handler Load
If callbacks arrive at rate:
and the mean handler service time is:
a first event-loop utilization screen is:
As U_loop approaches one, queueing delay and lag become sensitive to bursts and handler variance. A single blocking handler can dominate the tail even when average utilization looks acceptable.
Sustained Load Screen
Suppose callbacks arrive at:
and the mean handler time is:
The loop utilization estimate is:
The average load appears feasible, but it does not prove the loop is healthy. If one maintenance callback blocks for 80 ms, every timer and I/O completion behind it waits. Event-loop review therefore needs both sustained utilization and maximum blocking duration.
Timeout Budget
Loop lag consumes caller timeouts. If a request has timeout:
and downstream work needs:
then a simple margin screen is:
The request is unsafe when:
because the timeout can fire before useful work has enough time to complete.
Worked Callback Screen
Suppose a service schedules a probe every:
One probe should run at:
but actually runs at:
The measured lag is:
For a timeout of 100 ms, handler duration 22 ms and downstream call budget 45 ms, the margin is:
The timeout budget fails even though the downstream dependency itself still fits its 45 ms allocation.
Blocking Sources
Common sources include synchronous file I/O, CPU-heavy parsing, compression, serialization, expensive logging, large JSON processing, long cryptographic work, slow database drivers, unbounded callbacks, garbage collection pauses, lock convoy effects and retry storms that schedule too much work back onto the loop.
The failure can look like network latency, but the packets may not be the bottleneck. The callback that should start the network operation is simply not running on time.
Validation Evidence
Useful evidence includes probe interval, p50, p95, p99 and maximum lag, handler duration distribution, callback queue depth, timer delay, CPU profile, blocked-stack samples, garbage collection pauses, retry count, admission state, timeout outcomes and traces around overload.
The validation workload should include bursts, slow handlers, cancellation, retry traffic, logging volume and background maintenance. Testing only the happy path can miss the condition that creates loop lag in production.
A useful release dashboard keeps loop lag beside request latency, timeout count and retry volume, because these signals often fail together.
Design Levers
Useful levers include moving CPU-heavy work to worker threads, bounding handler duration, streaming large payloads, rate-limiting retries, coalescing duplicate requests, applying backpressure, splitting critical timers onto a separate loop, avoiding synchronous I/O and measuring maximum lag in release tests.
Reducing event loop lag is not the same as adding more threads everywhere. If the bottleneck is an unbounded upstream queue, more workers can amplify overload. If the bottleneck is one blocking callback, isolating or rewriting that handler may be the right fix.
Relationship To Neighbor Terms
Latency is the end-to-end delay seen by work. Event loop lag is the delay before event-driven work even begins running on the loop. Jitter is variation in that delay. Task starvation is the broader failure where eligible work waits too long. Thread-pool saturation can cause loop lag when completion callbacks cannot be produced fast enough or when retries flood the loop.