Atomic Primitives
Two single-attribute primitives that every product was writing by hand:
| Primitive | Shape | Use it for |
|---|---|---|
increment(key, attribute, by) |
one UpdateItem with ADD and ReturnValues UPDATED_NEW |
a per-partition sequence, a redelivery counter, a usage tally |
advance(key, attribute, value) |
one conditional UpdateItem guarded by attribute_not_exists(x) OR x < value |
“how far retention has passed this partition”, a high-water mark |
Both address one numeric attribute of one item, never a read-modify-write.
They refuse to touch anything that is not theirs: a key attribute, the
optimistic-lock version, a managed created_at/updated_at, the ttl
attribute and any encrypted field are rejected before a request is built, and so
is a non-numeric attribute.
In Go both helpers take any core.DB, so every standard handle serves them: the
*tabletheory.DB returned by New, and the *tabletheory.LambdaDB handles
returned by LambdaInit, NewLambdaOptimized and WithLambdaTimeout. The
counters run on the same client, table and endpoint as every other operation on
that handle.
increment
Adds by and returns the value the write produced. An absent attribute is a
counter that has not been incremented yet, so the first increment of a fresh item
returns by — the item does not have to exist. Concurrent increments cannot lose
each other because DynamoDB performs the addition.
::: code-group
next, err := tabletheory.Increment(ctx, db, &Usage{PK: "TENANT#1", SK: "MONTH#2026-11"}, "events", 1)
// next is the value this write produced: 1, then 2, then 3, ...
const next = await db.increment(
'Usage',
{ PK: 'TENANT#1', SK: 'MONTH#2026-11' },
'events',
1,
);
// next is the value this write produced: 1, then 2, then 3, ...
next_value = table.increment("TENANT#1", "MONTH#2026-11", "events", by=1)
# next_value is the value this write produced: 1, then 2, then 3, ...
:::
advance
Moves the attribute to value only when that moves it forward, and reports
whether this call moved it. Calling it with a value that is not greater is a
no-op that reports advanced = false, never an error, so a floor can be
advanced from any number of concurrent writers and converges on the highest
proposal they made.
::: code-group
floor, err := tabletheory.Advance(ctx, db, &Tenant{PK: "TENANT#1", SK: "RETENTION"}, "floor", 1_767_225_600)
if floor.Advanced {
// this call moved the floor to floor.Value
}
const floor = await db.advance(
'Tenant',
{ PK: 'TENANT#1', SK: 'RETENTION' },
'floor',
1_767_225_600,
);
if (floor.advanced) {
// this call moved the floor to floor.value
}
floor = table.advance("TENANT#1", "RETENTION", "floor", 1_767_225_600)
if floor.advanced:
... # this call moved the floor to floor.value
:::
value is an epoch-seconds int64, matching the units the ttl role uses, so a
retention floor and a TTL can be reasoned about together.
What a no-op reports
advance reports the value the attribute now holds. When the condition failed —
because another writer already moved the floor further — that value comes from a
read performed after the condition failed. The read only reports: DynamoDB
already made the decision atomically inside the conditional write, so the read
never participates in it.
A whole-item write replaces the counter
::: warning
Both primitives change one attribute of one item. A PutItem replaces the
whole item, so a whole-item write on that same item drops the counter or the
floor unless the value it writes already carries the current one. Increment 50
times while an ordinary upsert of the same row writes a copy that predates the
counter, and the counter is gone.
Keep counters and floors on their own item, or write the rest of that item with
UpdateItem (a model-shaped update with explicit fields) so the attribute the
primitive owns is never part of a replacement.
:::
This is DynamoDB’s replace semantics, not a TableTheory choice. PutItem has
nothing to merge with, so a read immediately before the put does not close the
race either: an increment landing between the read and the put is lost by both
shapes. Only an UpdateItem — which merges, and which fails closed under a
version or condition guard — leaves the counter alone.
Contract
The two primitives are pinned across all three runtimes by
contract-tests/serialization/atomic-primitives.yml.
Every fact in that table is asserted for Go, TypeScript and Python by each
runtime’s own suite, and the shared fixture itself is integrity-pinned, so a
dropped row or a runtime that stopped reading it fails the always-on
cross-runtime gate rather than passing quietly.
Runnable example
examples/atomic-primitives/main.go
drives both primitives — and the opt-in timestamp rules above — against a real
DynamoDB Local endpoint, asserting every claim. See
the example’s page.
Related
- Optimistic Locking — when you need a guarded item write rather than a guarded attribute
- Update Builder — the low-level
ADD/conditional surface these primitives wrap - Transactions — when the counter has to move with other writes atomically