When Does a Key Actually Disappear? TTL Expiration Without Divergence
Expiring a key looks like the most trivial feature a cache has. You set a time-to-live, a clock ticks down, the key goes away. SET session:abc {...} EX 3600, and an hour later it is gone. On a single process that really is all there is to it — store the deadline next to the value, and anyone who asks after the deadline gets a miss.
The moment there are two copies of that key, the triviality evaporates. Say the key lives on a partition owner and is replicated to one follower. Both of them hold the same value and the same deadline. The hour passes. Now ask the obvious question: which of them deletes the key, and when? If the answer is "both, independently, when their own clock crosses the deadline," you have just built a data-divergence bug. The two machines do not share a clock. One is a few hundred milliseconds ahead, one is mid-GC-pause, one ran its expiry sweep two seconds later than the other. For that window the key is gone on one node and present on the other, and a client that reads the owner then fails over to the follower — or vice versa — sees the key come back from the dead. Read-your-own-writes, the guarantee people most take for granted, is broken by the feature that was supposed to be trivial.
So in a replicated store, expiry is not a timer. It is a decision, and the whole design question is who gets to make it.
Lazy and active, and why you need both
Before the distributed part, the single-node part still has to be right, and it has two halves.
Lazy expiration checks the deadline when a key is touched. A GET on an expired key finds the deadline in the past, returns a miss, and schedules the key for deletion. It costs nothing until someone asks — perfectly efficient for keys that are read often. Its failure mode is the key nobody ever reads again: a one-hour session token that the user abandons, sitting in memory forever because no access ever triggers the check. Lazy alone is a slow memory leak dressed as an optimization.
Active expiration is a background sweep that walks keys with deadlines and reaps the ones that are past due, whether or not anyone asked. It plugs the leak — abandoned keys are collected on a timer — but it is the half that can hurt you if it is naive, because it does real work proportional to how many keys have TTLs, and it does it on a server that is simultaneously trying to serve reads and writes at a few million ops a second.
Every serious cache runs both: lazy to be free on the hot path, active to bound memory. Zaris is no different there. What is different is where the active sweep is allowed to run, and what it is allowed to conclude.
The owner is the only clock that counts
Zaris partitions the keyspace, and every partition has an owner — the node authoritative for that slice of keys. The rule that keeps expiry from diverging is a single sentence: only the owner expires a key, and when it does, the deletion travels to the replicas as a replicated operation, not as an independent local decision.
Concretely, the active sweep runs on the owner. When it finds a key past its deadline, it does not merely drop the key from its own memory — it emits a delete into the same operation log that carries every other write. Replicas apply that delete the way they apply a SET or a DEL: as an ordered, authoritative instruction from the owner, at the position in the log where the owner decided it. The replica never looks at its own wall clock to decide a key has expired. It holds the key, deadline and all, until the owner's delete arrives and tells it the key is gone. The two copies change state at the same logical point — the op-log position — rather than at the same wall-clock instant, and wall-clock agreement is exactly the thing distributed machines cannot have.
What about lazy expiration on a replica? A follower serving a read can still observe that a key is past its deadline — and it will correctly return a miss, because handing back a value whose TTL has passed would be wrong. But observing is not authority. The replica reports the miss and does not delete the key on its own; the authoritative removal is the owner's to make and to replicate. Observation and authority are deliberately separated: a replica may decline to serve a stale value, but only the owner may declare it dead for everyone. That separation is the whole trick. It lets every node give a correct answer to a read without any node making an independent deletion that another node hasn't agreed to.
Keeping the sweep off the hot path
An active sweep that is correct but greedy is still a problem. If reaping expired keys contends with live traffic for the worker pool, a burst of expirations — a million sessions all minted in the same minute and dying in the same minute — turns into a latency spike for every unrelated request that happened to land during the reap. A store that stalls user reads to collect its own garbage has traded a memory problem for a tail-latency problem.
So the sweep is bounded, not exhaustive. It works in capped batches rather than scanning the whole keyspace in one pass, yields between batches so live operations keep their share of the worker pool, and spreads the reaping across sweeps instead of trying to reclaim everything the instant a deadline passes. The guarantee it offers is a bounded-lag one: an expired key does not linger indefinitely, and memory is kept in check — but a key is not promised to vanish at the microsecond its TTL elapses. Lazy expiration already covers the correctness edge, since any read of a past-deadline key returns a miss regardless of whether the sweep has reached it yet. The active sweep's job is purely to bound memory, and it is allowed to do that calmly.
That split — lazy for correctness, active for memory, both subordinate to the owner's authority — is what lets expiry scale to millions of TTL'd keys without either leaking memory or stalling the data plane.
What this buys you
The payoff is that TTLs behave the way you assumed they did before you knew any of this. A key with a one-hour deadline is a live value to every node for an hour and a miss to every node afterward, and the transition from one to the other happens at a single agreed point rather than smeared across each node's private clock. Fail a client over from the owner to a replica the instant after expiry and it does not see the key resurrect, because the replica never carried a value the owner had already decided was dead — it carried it until the owner's replicated delete said otherwise, and not one operation longer. The expired-key notifications that keyspace listeners subscribe to fire once, from the authoritative delete, not once per replica that happened to notice independently.
Expiry on one machine is a timer. Expiry across a cluster is an agreement about when, routed through the one node allowed to answer. Get that right and the trivial feature stays trivial from the outside, which is the only place it was ever supposed to be.