Sub topic 14
Time sources and clock skew across nodes
This chapter assumes you know the fixed window and token bucket algorithms from earlier chapters, and that you can read short Python and Lua snippets. Every algorithm so far depends on "now". With one process that is easy. With many application servers, each server has its own clock, and those clocks disagree.
Two kinds of clock
A machine exposes two different clocks, and they answer different questions.
- Wall clock (
time.time()): the calendar time. Network time sync (NTP) can step it forward or backward at any moment. - Monotonic clock (
time.monotonic()): a counter that only moves forward. Its zero point is arbitrary, so it is only meaningful within one process on one machine.
Use the monotonic clock for in-process limiters, as the token bucket does. A wall-clock step backward would produce a negative elapsed and could stall refills. Never send monotonic values to another node. They cannot be compared.
What skew does to a shared limiter
Clock skew is the difference between two machines' wall clocks. It matters as soon as limiter state is shared and the application servers supply the timestamps.
WINDOW = 60
def window_index(now):
return int(now // WINDOW)
true_now = 119.0
node_a = true_now # accurate clock
node_b = true_now + 3.0 # clock runs 3 seconds fast
print(window_index(node_a), window_index(node_b)) # 1 2At the same real instant, node A counts the request in window 1 and node B counts it in window 2. A client whose requests alternate between the two nodes gets its counts split across windows. The boundary burst problem from the fixed window chapter gets worse. Token buckets have a related problem. If a node's timestamp is older than the one stored by another node, elapsed is wrong. It may be clamped to zero, which loses refill time, or it may be too large, which grants extra tokens.
Use one time source for the decision
The most reliable fix is to let the shared store decide what time it is. The Lua script from the atomicity chapter does this with redis.call('TIME'):
local t = redis.call('TIME')
local now_ms = tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)Every request from every application server is then measured against the same clock. Skew between application servers no longer matters. The store's own clock can still jump, but that is one clock to monitor rather than many.
The alternatives are weaker:
- Rely on NTP. Well-run fleets usually stay within a few milliseconds, but a misconfigured node can drift by seconds.
- Use coarse windows. A skew of 50 ms is irrelevant for a 1-hour quota and significant for a 1-second burst limit.
- Use the client's timestamp. Never do this. A client can claim any time it likes.
Practical rules
- Use
time.monotonic()for single-process limiters. - Use the store's time (
TIME) for shared limiters. - Clamp elapsed time to zero or above.
- Keep NTP running everywhere and alert on large offsets.
Exercise
Two app servers share a fixed window limiter in Redis and each passes its own time.time() as the window timestamp. One server's clock is 5 seconds ahead. Name one problem this causes and one fix.
Show answer
The same client's requests are counted in different windows depending on which server handles them, so the effective limit can be exceeded around window boundaries. The fix is to take the time from the shared store, for example with redis.call('TIME') inside the Lua script, so every decision uses one clock.