~learn.
0:00 / 1:10
LLD · Key-Value Store

An expired key nobody reads never leaves memory

transcript

This key expired a minute ago. It may never leave memory. Unless your store expires keys two ways.

Asha's OTP is set with a 30-second TTL. At 29 seconds: 4821. At exactly 30: gone. Deleted by the read itself. That's lazy expiry.

But here's the hole. What if nobody reads a key again? Lazy expiry never sees it. Its memory is never freed.

At 30 seconds, the demo writes 500 session keys. Half for a minute. Half for ten minutes. At 91 seconds, one sweep removes the 250 one-minute keys. Nobody read them. 251 keys remain.

Scan every key? That holds the lock for the whole scan. Instead, a heap of deadlines. Pop only what's due. At most a budget per sweep, so writers never wait long. Key rewritten since? Its version won't match. Skipped.

Expire lazily on read. And sweep from a heap of deadlines. So memory is freed even for keys nobody reads again.

Full breakdown in the lecture.