You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(engine): atomic increment and multi-key get and delete on the store #2960
Is your feature request related to a problem? Please describe. StoreHandler names rate-limit counters and idempotency records as its purpose, but has no atomic counter. A caller must hold a TTL-bounded lock around get and put, which costs several round trips and contends under load. Reading or removing several known keys takes one call per key.
increment atomically adds delta, creating the key at zero if absent with the TTL applied on creation. The completion receives the new value. The stored form is a decimal string, so get stays consistent.
Multi-key get returns values in key order, with null for an absent key.
Multi-key delete is best-effort and non-atomic across keys. The completion receives the count removed.
The first parameter's type differs from the single-key forms, so there is no lambda overload ambiguity.
Existing implementations keep working. Defaults are built from lock, get and put. Stores backed by external systems can override them with native commands.
The async contract is the same as the other operations: completion fires strictly later, on the caller's I/O thread.
Describe alternatives you've considered
A compare-and-set counter loop. It retries under contention.
Delete by prefix. It needs a scan on distributed stores and cannot work when keys are hashed.
Client-side locks, which is today's workaround.
Additional context
A sliding-TTL touch is a possible follow-up and is out of scope here. Tests first: memory-store unit tests for the three operations, and default-implementation tests against the test store.
Request: conditional update (replace) on StoreHandler
StoreHandler can write unconditionally (put), create only if absent (putIfAbsent), and remove (delete, getAndDelete). It has no way to update a value only if it is still the value the caller read. Any read-modify-write on a value that is not a single counter therefore has to hold a TTL-bounded lock around get and put. That has the same costs this issue already identifies for counters: several round trips, lease sizing, contention under load, and a lost update if the lease lapses between the read and the write.
The counter case is covered by atomic increment, and a compare-and-set loop is the wrong tool for it. But increment does not help for values with more than one field or non-numeric state, such as a small record whose next state depends on its current state. For those, the standard primitive is a conditional write.
Atomically sets key to value only if its current value equals expected.
The completion receives true if the value was replaced, and false if the key is absent or holds a different value.
ttl applies to the new value, or null for no expiry.
The async contract is unchanged: the completion fires strictly later, on the caller's I/O thread.
Callers retry by re-reading with get and calling replace again. Contention is handled by the caller, not blocked on a lock.
Implementations
Default: built from lock, get and put, like the other defaults, so existing implementations keep working.
Native overrides: the memory store can use ConcurrentMap.replace(key, expected, value). Stores backed by external systems can override the default with a native conditional write.
Alternatives considered
Client-side locks around get and put: this is today's workaround, with the costs above.
Version-stamped values compared by the caller: this is not atomic without the store's help.
Tests first
StoreHandlerTest: the default against the test store, covering replace on a match, a mismatch, an absent key, and the TTL.
MemoryStoreHandlerTest: the native override, including expiry and concurrent callers where exactly one wins.
None of the four has a default implementation. Every StoreHandler must implement them natively, so this is a breaking SPI change (feat(engine)!).
replace lands in the same PR: it sets the value only if the current value equals expected, and completes with true if replaced and false if the key is absent or holds a different value. ttl applies to the new value; null means no expiry.
increment, multi-key get and multi-key delete behave as described above.
The memory store and the engine test store (which now honours TTL) implement all four natively, with unit tests for each.
Activity
jfallows commentedon Nov 9, 2026
Request: conditional update (
replace) onStoreHandlerStoreHandlercan write unconditionally (put), create only if absent (putIfAbsent), and remove (delete,getAndDelete). It has no way to update a value only if it is still the value the caller read. Any read-modify-write on a value that is not a single counter therefore has to hold a TTL-boundedlockaroundgetandput. That has the same costs this issue already identifies for counters: several round trips, lease sizing, contention under load, and a lost update if the lease lapses between the read and the write.The counter case is covered by atomic
increment, and a compare-and-set loop is the wrong tool for it. Butincrementdoes not help for values with more than one field or non-numeric state, such as a small record whose next state depends on its current state. For those, the standard primitive is a conditional write.Proposal
keytovalueonly if its current value equalsexpected.trueif the value was replaced, andfalseif the key is absent or holds a different value.ttlapplies to the new value, ornullfor no expiry.getand callingreplaceagain. Contention is handled by the caller, not blocked on a lock.Implementations
lock,getandput, like the other defaults, so existing implementations keep working.ConcurrentMap.replace(key, expected, value). Stores backed by external systems can override the default with a native conditional write.Alternatives considered
getandput: this is today's workaround, with the costs above.Tests first
StoreHandlerTest: the default against the test store, covering replace on a match, a mismatch, an absent key, and the TTL.MemoryStoreHandlerTest: the native override, including expiry and concurrent callers where exactly one wins.This can land alongside #2969 or separately.
Generated by Claude Code
jfallows commentedon Nov 9, 2026
Final API in #2969, rebuilt on
develop. It supersedes the "defaults built fromlock,getandput" proposal in this issue and in thereplacecomment.StoreHandlermust implement them natively, so this is a breaking SPI change (feat(engine)!).replacelands in the same PR: it sets the value only if the current value equalsexpected, and completes withtrueif replaced andfalseif the key is absent or holds a different value.ttlapplies to the new value;nullmeans no expiry.increment, multi-keygetand multi-keydeletebehave as described above.Generated by Claude Code