Database / ValKey Interview questions
How does Valkey handle expired key deletion internally?
Expiration is enforced two ways at once. Passive (lazy) expiration checks a key's TTL whenever it's accessed and deletes it on the spot if the time has passed, before returning any result to the caller.
Active expiration runs independently from a periodic background cycle: it samples a subset of keys that carry a TTL, deletes any found expired, and increases its sampling rate when a high proportion of the sample turns out expired, so memory isn't held indefinitely by keys nobody happens to access.
Replicas don't independently decide a key has expired; they wait for the primary to propagate an explicit DEL/UNLINK for that key, which keeps primary and replica data consistent.
More Related questions...