skip to content

An entry's deadline has passed but no caller has touched it since — by what routes does the store eventually reclaim its memory?

level: middleimportance: must knowfreq 55%

answer

  1. three occasions, all opportunistic
  2. a caller, a sweep, or scarcity
  3. the sweep samples rather than enumerates
  4. no route offers a bound

basics

~20 s

Three routes: a caller touching the entry, a background sweep over entries that carry a deadline, and the store needing the room. Which of the three a store has, and how hard each works, varies between stores.

solid answer

~50 s

There are three, and a store may have any combination of them. **Reclaim on access**: a caller asks for the entry, the read path sees the deadline has passed, reports a miss, and releases it. **The background sweep**: a periodic pass over entries carrying a deadline that frees the dead ones — sampled and best-effort rather than an exhaustive walk, because it competes with request serving on the same machine. **Reclaim under allocation pressure**: the store needs room, and an entry already past its deadline is the cheapest thing to release. Some stores in this class have no background pass at all and depend entirely on the first and third. The practical consequence is that on a keyspace nothing reads back, only the sweep and pressure can return the memory, and neither of them offers a bound you can rely on.

go deeper

for a junior

Remember that the memory comes back later than the deadline, and that a read of a dead entry is one of the things that can trigger the release.

for a middle

Name all three routes and what fires each: a caller touching the entry, the store's own periodic pass, and the store needing room. Say that the pass samples rather than enumerating.

for a senior

Reason from workload to behaviour: a keyspace nobody reads back gets nothing from reclaim on access, so its resident footprint is governed by the sweep's pace and by pressure.

for a principal

The point to press is the absence of a bound. No route can be asked to return memory by a given time, so a capacity plan that depends on timely reclaim needs an explicit pass instead of a deadline.

## Why there has to be a route at all The deadline changes what a reader is served and nothing else. It is not a scheduled job, and on a tier holding millions of entries it could not cheaply be one: waking at the right instant for every entry would mean maintaining, and constantly re-ordering, a structure over every deadline in the keyspace, and paying that cost on every write. So stores in this class defer the work and pick it up opportunistically. There are three occasions when it is cheap to do, and between them they are how memory actually comes back. ## The three routes 1. **Reclaim on access.** A caller asks for the entry. The read path has to compare the deadline before it can answer anyway, so when it finds a passed deadline it reports a miss and releases the entry in passing. This is free in the sense that the comparison was already being paid for. It fires only when someone actually asks, which is the whole of its limitation. 2. **The background sweep.** The store runs its own periodic pass over entries that carry a deadline and frees the dead ones it finds. The important property is that the pass is **best-effort**: it samples rather than enumerating the whole population, and it takes a bounded amount of work per cycle so that reclaim never starves request serving. A given dead entry can survive many cycles before a sample happens to land on it. 3. **Reclaim under allocation pressure.** The store needs room for a new write. An entry whose deadline has already passed is the cheapest possible thing to give up — nobody may be served it in any case — so it goes first. This is the route that eventually catches everything, but it only fires when the tier is under pressure, and it is not the same subject as forced removal of *live* entries at a memory ceiling. ## What varies between stores | route | what makes it fire | what it does not promise | |---|---|---| | reclaim on access | a caller asking for that entry | anything at all on a keyspace nobody reads | | the background sweep | the store's own timer, on stores that have one | that it visits every dead entry, or visits soon | | reclaim under allocation pressure | the store needing the room | anything until the tier is actually pressed | Do not assume one store's combination is the model. Some stores pair reclaim on access with a sampling sweep; some run a dedicated crawler thread; some have no background pass at all and return memory only when a reader arrives or when room is needed. What is common to all of them is the shape: **the deadline marks an entry dead, and a separate, later, opportunistic mechanism turns dead into free.** ## The consequence that matters in production Put the routes against a workload and the behaviour falls out: - **A keyspace that is read constantly** reclaims well. Most dead entries are touched soon after their deadline, so the first route does most of the work and the memory tracks the live set closely. - **A write-once keyspace nobody reads back** — audit records, fan-out state, anything written for a consumer that may never come — gets nothing from the first route. Memory comes back only as fast as the sweep samples, and, where there is no sweep, not until the tier is under pressure. - **A high write rate against a slow sweep** lets the dead population grow, because a sampled pass has no mechanism for going faster just because there is more to do beyond what its budget allows. ## How to reason about it in an interview Name all three routes, and then say what each one *cannot* do. That second half is what separates someone who has operated a tier from someone who has read a summary. The strongest form of the answer adds the thing none of the three provides: **no bound**. There is no setting that says "free the memory within one minute of the deadline", because every route is triggered by something outside the deadline's control — a caller, a timer with a budget, or scarcity. If your design needs the space back at a known time, the deadline is the wrong instrument, and an explicit pass that touches or removes the entries is the right one.

  • Why would a store sample entries in its background sweep rather than walk every entry that carries a deadline?
    Because the pass runs on the same machine, and on some designs the same path, that is answering requests. Walking a keyspace of millions of entries to find the dead ones would cost more serving time than the reclaimed memory is worth, so the pass takes a bounded amount of work per cycle and is deliberately best-effort. The consequence is that a dead entry can survive many cycles.
  • Does the caller whose read triggers a reclaim pay for it?
    It pays the deadline comparison either way, since the read path has to make it before answering. The release itself is trivial for a small value. For a large one, stores differ: some free it inline on the calling path, others hand it to a helper so the caller is not held. In every case the caller is simply told there is no entry.
  • If the tier is never under pressure and nothing reads a dead entry, how long can it stay resident?
    Indefinitely, on a store with no background pass, and for an unbounded time on one whose pass samples. Nothing in the lifetime you set bounds it. That is why a tier whose workload never reads back must be sized for live entries plus a dead population, rather than for the live set alone.

saying these in an interview costs you the question

  • Names only a background pass and assumes it finds every dead entry.
  • Assumes every store schedules a timer per entry that fires at the deadline.
  • Thinks memory returns promptly on a keyspace nothing reads back.
  • Treats reclaim of dead entries as the same thing as forced removal of live ones.
  • Expects the same reclaim behaviour from every store in this class.