Jitter cache TTLs so keys do not expire together
A cache warmed at deploy writes every key in the same second with the same TTL. An hour later they all expire in the same second, and the database gets the whole working set at once. Spread the expiry out when you write.
const ttl = 3600 + Math.floor(Math.random() * 600);
await redis.set(key, value, { EX: ttl });Ten minutes of spread on an hour turns the spike into a trickle. Coalescing in-flight misses won't help here, because every key is a different key and there's nothing to coalesce.
cachingperformance