Implementing Timed Tasks in RPC Using a Timing Wheel
This article explains how to use a timing wheel to efficiently handle RPC timeout processing, startup timeouts, and heartbeat tasks, reducing thread proliferation and CPU waste by organizing timed tasks into hierarchical time slots.
Hello everyone, I'm Chen. This article introduces how to use a timing wheel for timed tasks in RPC, such as client-side timeout handling and periodic heartbeats.
Before discussing the timing wheel, we examine the problems caused by naive timed task implementations, like creating a thread per Future which leads to excessive thread counts under high concurrency.
We then explore a more efficient approach that uses a single thread to scan all timed tasks at fixed intervals, but note that this can still cause unnecessary CPU cycles due to frequent scanning of many tasks.
The timing wheel solves these issues by mapping tasks to time slots, similar to a clock's ticks, allowing tasks to be executed only when their slot is reached, thus minimizing redundant scans.
We describe the timing wheel mechanism: slots represent time granularity, and multiple wheel layers provide coarser granularity for longer delays. A task placed in a slot is executed when the wheel pointer reaches that slot.
An example scenario with three tasks demonstrates how tasks are placed in slots, executed at the correct times, and how the wheel advances across layers.
Applying the timing wheel to RPC, we can handle client request timeouts, startup timeouts, and periodic heartbeats efficiently, even under high load, by reducing thread usage and CPU overhead.
When a repeated task like a heartbeat finishes, it can be rescheduled by resetting its execution time and re‑inserting it into the wheel.
In summary, the timing wheel mimics a real clock to organize timed tasks, offering precise timing, reduced thread count, and lower CPU consumption for RPC frameworks.
Key considerations include choosing appropriate slot granularity and number of slots to balance timing accuracy and scanning overhead.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.