What happened

High schedule latency in Tokio applications often indicates issues with how your code interacts with the asynchronous runtime.

This metric – the delay between a task becoming ready and Tokio actively processing it – is crucial for optimizing performance, particularly when dealing with network I/O or other potentially blocking operations. The key here is understanding that Tokio’s scheduler is trying to be fair and efficient; if your code isn't cooperating, it can lead to significant delays.

Why it matters

Ignoring schedule latency can dramatically impact the responsiveness and overall performance of your application.

If tasks are spending excessive time waiting for Tokio to poll them, this translates directly into slower response times, reduced throughput, and potentially frustrated users. In a high-load environment, even seemingly small latency increases can compound and create bottlenecks.

But, but, but…

The most common cause of schedule latency is the asynchronous code not yielding frequently enough back to Tokio’s scheduler. Tokio is constantly polling for ready tasks, and if your code isn't giving it anything to do during those intervals, it waits longer.

A naive implementation will read data directly off the connection while more data is available. When requests are pipelined, the entire pipelined request (or most of it) will end up in an in memory buffer. When you read frames off of it, each will be Poll::Ready (without going back to the network). This creates both long polls and unfairness between clients. The impact on throughput is usually smaller: the same number of requests are processed. Latency, however, changes dramatically because one entire pipeline can wait behind another. Explicitly yielding after each request can reduce latency by roughly 10× in this example. You can do even better by yielding only after several consecutive immediately-ready reads.

“Scheduling latency is the amount of time between your task being ready to run (e.g., because the socket has data) and Tokio actually polling the future.”
— Dr Non

The counter-take

The prevalent assumption that simply increasing Tokio’s thread count will solve schedule latency problems is a common misconception; addressing the underlying interaction between your code and Tokio's scheduler is critical.

The AI angle

AI-powered applications increasingly rely on real-time interactions. Reducing schedule latency is crucial to ensure responsiveness in conversational AI agents and other systems that require immediate feedback.