Course contentsShow
← Linux
Lesson 605 of 1,8196. Systemd Internals and Unit OrchestrationFree lesson

Timer Accuracy and Randomization

Configure OnCalendar events with jitter to prevent stampeding herd issues in distributed systems.

In this lesson, you will learn how to introduce temporal randomness into your systemd timers to prevent system performance degradation caused by synchronized task spikes.

The Problem: The "Stampeding Herd"

When you deploy a fleet of servers or run multiple background maintenance scripts using OnCalendar timers, you might set them to run at the top of the hour (e.g., *:00:00). If dozens of services all trigger at the exact same second, your system experiences a "stampeding herd" effect. This causes sudden, massive spikes in CPU and I/O usage, potentially leading to timeouts or service instability.

Adding Jitter with RandomizedDelaySec

To solve this, systemd timers include the RandomizedDelaySec directive. This parameter adds a random offset to the start time of a unit. When a timer is triggered, systemd waits for a random amount of time between zero and the value specified before actually starting the associated service unit.

Implementation Example

Suppose you have a backup script that should run daily, but you want to ensure it doesn't collide with other heavy maintenance tasks:

[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
RandomizedDelaySec=300
AccuracySec=1us

[Install]
WantedBy=timers.target

In this example, RandomizedDelaySec=300 tells the system to pick a random number of seconds between 0 and 300 (5 minutes) to delay the task. By spreading these starts out, you flatten the resource usage curve across your infrastructure.

AccuracySec and Efficiency

Note the AccuracySec field as well. By default, systemd timers are accurate to within one minute to allow the kernel to group events together and keep the CPU in a low-power sleep state longer. If you need high precision, you can lower this value (as shown above), but be aware that overly frequent wakeups increase power consumption and system jitter. Use RandomizedDelaySec to distribute the load of scheduled events, and AccuracySec to balance the trade-off between power efficiency and execution timing.

Key Takeaway: Use RandomizedDelaySec to add a buffer window to your timers, effectively diffusing synchronized "stampeding herd" spikes across your system.