Skip to content

Write ordering without a lock

Status: finding, 2026-11-12. Replaces, for now, the "gate against no gate" throughput measurement planned for stage 1. Test: tests/ordering/test_write_ordering_trace.py. Versions: langgraph 1.2.20, langgraph-checkpoint 4.3.1, langgraph-checkpoint-postgres 3.2.0; citations are path:line inside those installed packages.

Why not a throughput number yet

The design then kept a per-(thread, namespace) gate so writes land in the order the graph submitted them, and stage 1 was to measure gate against no gate. Upstream's AsyncPostgresSaver takes one saver-wide lock around every cursor (langgraph/checkpoint/postgres/aio.py:428), so before our lock-free saver exists (stage 2) any throughput number measures that lock, not the gate. The prior question is which orderings a graph can produce at all when nothing serialises the saver. This is that trace.

Method

A tracing saver extends upstream's InMemorySaver with no lock. Before and after the moment each aput, aput_writes and aget_tuple takes effect it yields to the event loop a seeded random number of times (0–8), and it records, per (thread_id, checkpoint_ns), each call's start, commit and end on one clock plus the checkpoint id, parent id and (task_id, idx) keys it carried. Every node is async, so no thread pool is involved and a seed replays the same interleaving: three runs printed identical counts (measured).

Six real graphs, each under durability sync, async and exit, 50 seeds per cell:

  • linear — three nodes in a row, invoked twice on one thread;
  • fanout — three parallel nodes writing one reducer channel and one channel each, then a join;
  • delta — three parallel nodes writing a DeltaChannel;
  • subgraph — a compiled subgraph (its own checkpoint_ns) that interrupts twice, resumed twice;
  • interrupts — one node that calls interrupt() twice, resumed twice;
  • concurrent — two ainvoke of the linear graph on the same thread_id at once.

Each run's history (aget_state_history: values, next, interrupt values) is compared with the same scenario run on a plain InMemorySaver, whose async methods never yield and so apply calls in submission order.

Findings (measured, runs out of 50 seeds)

Columns: (1) two aput overlap; (2) aput commits out of submission order; (3) two aput share a parent; (4) aput_writes overlaps aput; (5) two aput_writes overlap; (6) a write lands before the aput of its own checkpoint; (7) a write for checkpoint N lands after the aput of N's child; (8) one (checkpoint, task, idx) key written by two calls; (9) the same, by two overlapping calls; (10) aget_tuple overlaps aput; (11) history differs from the ordered saver.

scenario / durability 1 2 3 4 5 6 7 8 9 10 11
linear / sync 0 0 0 50 19 34 44 0 0 0 0
linear / async 0 0 0 50 50 50 13 0 0 0 0
linear / exit 0 0 0 0 0 0 0 0 0 0 0
fanout / sync 0 0 0 50 50 34 48 0 0 0 0
fanout / async 0 0 0 50 50 50 10 0 0 0 0
fanout / exit 0 0 0 0 0 0 0 0 0 0 0
delta / sync 0 0 0 50 50 36 0 0 0 0 0
delta / async 0 0 0 50 50 50 0 0 0 0 0
delta / exit 0 0 0 50 50 34 0 0 0 0 0
subgraph / sync 0 0 0 50 9 37 45 50 0 0 0
subgraph / async 0 0 0 50 50 48 42 50 0 0 0
subgraph / exit 0 0 0 50 0 34 0 50 0 0 0
interrupts / sync 0 0 0 50 47 22 38 50 0 0 0
interrupts / async 0 0 0 50 50 38 31 50 0 0 0
interrupts / exit 0 0 0 50 0 22 0 50 0 0 0
concurrent / sync 50 37 7 50 50 32 49 0 0 50 50
concurrent / async 50 37 7 50 50 50 8 0 0 50 50
concurrent / exit 41 9 0 0 0 0 0 0 0 0 20

The test asserts the set of non-zero columns per row, so a LangGraph upgrade that changes any of them fails loudly.

What the table says, with the code that explains it:

  1. Within one run, aput calls never overlap and never reorder — 0 of 750 single-run runs (measured). LangGraph chains them itself: each put is submitted with the previous put's future and awaits it first (langgraph/pregel/_loop.py:1396, langgraph/pregel/_loop.py:2009). This holds in subgraph namespaces too.
  2. aput_writes overlaps aput and lands on either side of it in every sync and async cell (measured). Writes are submitted separately with no wait (langgraph/pregel/_loop.py:532). A write for checkpoint N can land after the put of N+1 (column 7), and a write can land before the put of its own checkpoint (column 6) — even under sync, which waits for the put only after each step (langgraph/pregel/main.py:3549), not after the input checkpoint (inferred).
  3. DeltaChannel writes never land after the child's put — 0 in every delta cell (measured): before each put LangGraph awaits that checkpoint's delta writes (langgraph/pregel/_loop.py:2007). The one ordering a reader depends on is already enforced by the graph, not the saver.
  4. A write key is rewritten only across invocations, never by two overlapping calls (column 8 against 9, measured). The rewrites are the special channels (__interrupt__, __resume__), which upstream upserts; they come from the resumed invocation, which starts after the previous one returned.
  5. Every single-run history equals the ordered saver's — 0 of 750 (measured). With no lock at all, no reader-visible result depended on write order.
  6. Two concurrent runs on one thread are a different problem. Puts overlap, commit out of order, and in 7 of 50 runs per durability two puts share a parent, forking the thread (measured). The history never matched the ordered saver's interleaving, and no saver-side gate could make it: the two runs interleave at the graph level. That is the second-writer fence (feature 8, stage 5), not the gate.

Limits (inferred): latency here is event-loop yields, not network time, so column counts are not rates; the structural facts (1, 3) come from awaits in LangGraph's code and hold for any latency. InMemorySaver applies each call at one instant, which matches a Postgres saver only if each call is one transaction. Not traced: aget_delta_channel_history (it bypasses aget_tuple), concurrent runs that interrupt, and the sync saver.

Recommendation

A per-(thread, namespace) gate is not needed for any single-run ordering (inferred from findings 1–5): LangGraph already serialises puts and the delta writes it reads back, and nothing else's order changed a result. The design README's rule that writes may commit before or after their checkpoint, so no foreign key from writes to checkpoints is legal, is now measured (finding 2); stage 3's patch bookkeeping must not assume checkpoint N's row exists when N's writes arrive. Concurrent runs on one thread need the fence, which a gate does not replace.

Stage 2 should build the saver without the gate, then before the design drops it:

  • run this trace against our saver on Postgres (the same scenarios and assertions, the history compared with upstream's), and add concurrent runs that interrupt;
  • measure the gate's cost on our lock-free saver, under the benchmark rule: put and put_writes p50/p95 for one thread under sync (the gate sits on the critical path), throughput for many threads on one saver (the gate should cost nothing across keys), and two runs on one thread (where it serialises).

If either shows an ordering that changes a read, the gate stays and its cost is the number recorded.

Stage 2 re-run on our saver (2026-11-12)

The same scenarios and durabilities, on our lock-free AsyncOpenPostgresCheckpointer against PostgreSQL 16: every call a real round trip plus 0–8 seeded event-loop yields before it, 5 seeds per cell, each history compared with the ordered in-memory saver's. Test: tests/integration/test_write_ordering_postgres.py.

  • Every single-run history matched: 0 of 75 runs differed across linear, fanout, delta, subgraph and interrupts under sync, async and exit (measured). The test asserts it; with aput_writes made a no-op the interrupts cells fail, so it can.
  • Two concurrent runs on one thread diverged, as the trace found: 5/5 (sync), 5/5 (async), 2/5 (exit) runs (measured). Concurrent runs that interrupt, which the trace did not cover: 5/5, 5/5 and 0/5 (measured). Recorded, not asserted.

So stage 2 ships without the gate (inferred from these runs and findings 1–5): no single run needed it, and concurrent runs on one thread remain the second-writer fence's job (stage 5). The gate's cost was not measured, because there is no gate to measure.