A counter must restart from zero each hour, and its first increment creates the entry; what must the caller get right?
answer
- the reset is the entry disappearing
- the first change creates it without a deadline
- a gap there makes the counter immortal
- later operations may or may not disturb it
- eviction looks exactly like a reset
basics
~20 sA lifetime belongs to the entry, and the entry does not exist until the first change creates it, so the deadline has to be attached by the caller that created it. Miss that step and the counter never resets.
solid answer
~50 sThe reset is not an operation — it is the entry disappearing. So the deadline has to be attached to an entry that only came into existence with the first change, which makes the first caller responsible for something none of the later callers do. The usual shape is: apply the change, and if the number handed back shows this was the first one, attach the lifetime. Between those two operations the process can die, leaving a counter with **no deadline at all** that climbs forever, and nothing reports it. Whether a later change disturbs an existing deadline **varies by store and by operation** — arithmetic on an existing entry commonly leaves it alone, while a whole-value write may drop it — so check the rule rather than assume it. And because the store keeps no history, once the entry is gone the previous period's number is unrecoverable.
go deeper
Recall that a lifetime belongs to the whole entry, so a counter resets by its entry disappearing rather than by anything writing zero into it.
Explain who attaches the deadline and why only the caller that created the entry should: attaching it on every change pushes the period out for as long as traffic continues.
Name the silent failure in the gap between the first change and the deadline, and say that whether later operations disturb an existing deadline varies by store and by operation.
Rule on what a periodic counter owes its readers: if a period's total must survive the boundary, require it to be copied out before the deadline, because the tier keeps no history and can drop the entry early.
## The reset is a disappearance, not a zeroing A counter that starts again each period is not reset by an operation that writes zero. It is reset by its **entry ceasing to exist**: the deadline passes, the entry goes, and the next change recreates it from nothing. That single fact produces every consequence in this answer, and it is the thing most candidates skip past. The mechanics of how a deadline is attached and enforced are a subject of their own. What belongs to the counter is narrower and more practical: the entry is the unit the deadline attaches to, and the entry is born from the first change. ## The gap between the change and the deadline On a store that creates a missing counter at zero, the first change brings the entry into being **without a lifetime**. Attaching one is a second operation, and only the caller that created the entry should do it — otherwise every subsequent change pushes the deadline further out and the period never ends. 1. Apply the change. 2. Look at the number handed back; if it indicates this was the first change, attach the deadline. 3. Every later caller does step 1 only. The failure lives between 1 and 2. If the process dies, is killed, or times out there, the entry exists with **no deadline** and no later caller will add one, because none of them sees a first change. The counter climbs for as long as the tier is up, every reading covers an unbounded period instead of an hour, and nothing anywhere reports an error. It is a silent, permanent defect in a counter that looks healthy. Two defences are worth naming: - Make the create and the deadline **one unit of work**, if the store offers a way to apply both together or to run the pair as a single server-side action; what is available varies across this class. - Failing that, have a reader that notices a counter with no deadline and attaches one, accepting that the first period will be long. ## Whether an ordinary change disturbs the deadline This is where an answer that assumes one store gets caught. The honest statement is that **it depends on the store and on the operation**: - Arithmetic against an existing entry is a modification and commonly leaves an existing deadline untouched. - A whole-value write may replace the entry outright and take the deadline with it, making the counter permanent. - Some stores offer a way to keep the existing deadline on an operation that would otherwise clear it. The practical rule that survives every variant: if **any** code path writes the counter key whole — a repair script, a reset job, a second service — it can quietly change whether the counter ever resets. Know the rule for your store, for each operation you use, and keep whole-value writes off counter keys. ## What the period boundary costs you | At the boundary | What is true | |---|---| | The old number | gone; the store keeps no history of an entry that expired | | A reader arriving after it | sees no entry, which it must read as zero, not as an error | | A total spanning the boundary | unrecoverable, unless something copied the number out first | | An early disappearance | indistinguishable from an ordinary reset | That last row is the one that matters on this tier. The entry can also disappear **before** its deadline — evicted under memory pressure, or lost with a restart — and it leaves exactly the same trace as a scheduled reset: nothing. A counter whose meaning depends on covering a full period can therefore be wrong at a moment nobody chose, and no reader can tell. If the previous period's number is needed, it has to be copied somewhere durable before the deadline, because afterwards there is nothing to copy. ## One deadline, one entry The deadline covers the entry, not the numbers inside it. If several counters are held as named fields of one entry, they share one lifetime and reset together; they cannot be given individual deadlines. Counters that need to reset on different cadences need separate entries — which also means separate per-entry overhead, and that cost belongs to the memory side of the design. ## Answering it well Lead with the reset being a disappearance. Name the creator's responsibility and the silent failure in the gap between the change and the deadline. Say explicitly that whether later operations disturb the deadline varies by store and by operation. Then close on what the boundary destroys: no history, and an early loss that looks exactly like a scheduled reset.
- How does a caller know it was the one that created the counter?From the number the change handed back: where the operation returns the result, a value equal to the amount just added means the entry did not exist a moment ago. That is enough to decide who attaches the deadline. It is not a general-purpose lock — two callers cannot both see it — and it is only available where the operation returns its result, which varies.
- Why not simply attach the deadline on every change?Because then the deadline moves forward every time the counter is used, and a counter that is used continuously never reaches it. The period would end only during a lull, which is the opposite of the intended behaviour. A deadline meant to bound a period is attached once, by the caller that created the entry, and left alone.
- Several counters are held as named fields of one entry. Can they expire separately?No. The lifetime is a property of the entry, so every field inside it shares one deadline and they all disappear together. Counters that must reset on different cadences need separate entries, which costs per-entry overhead but buys independent lifetimes. Grouping only makes sense when the numbers genuinely share a period.
saying these in an interview costs you the question
- Attaches the deadline on every change, so an active counter never resets
- Assumes every operation preserves an existing deadline
- Believes a reset writes zero rather than removing the entry
- Expects the previous period's number to still be readable after the reset
- Treats a missing counter as an error instead of as zero
- Assumes an early disappearance under memory pressure would be visible