Skip to content

perf(allocate): skip the no-op header CAS for unfillable backorders (#67) - #84

Merged
JumpTechCode merged 1 commit into
mainfrom
perf/skip-noop-backorder-cas
Jun 22, 2026
Merged

perf(allocate): skip the no-op header CAS for unfillable backorders (#67)#84
JumpTechCode merged 1 commit into
mainfrom
perf/skip-noop-backorder-cas

Conversation

@JumpTechCode

Copy link
Copy Markdown
Collaborator

Closes #67.

Problem

When the sweep re-allocates a backordered order and no stock is available, allocate computed target = BACKORDERED == order.state and then called cas_state unconditionally, executing UPDATE orders SET state='backordered', version=version+1 — a real row write, dead tuple, WAL record, and version bump on every tick even though nothing changed. With B orders backordered on a zero-stock SKU (a common steady state), each order incurred one header UPDATE per interval indefinitely: WAL/IO and autovacuum pressure proportional to backlog × tick rate, dead tuples on the hot orders table, and a modestly raised OCC-conflict probability for legitimate concurrent allocate/cancel.

Fix

Track whether any line gained allocation this pass (any_allocated) and skip cas_state entirely when the state does not change and nothing was allocated. A real state change or any new line allocation still CASes — both to persist the transition and to keep the version bump that serializes concurrent allocate/cancel against the lines this pass just changed. Steady-state cost on a perpetually-unfillable order drops from O(backlog × tick) to ~0.

No invariant is affected: in the zero-available case no stock is touched (stock_locations filters available > 0, so the per-line FOR UPDATE reserve never runs).

Scope

This addresses the write-amplification core of #67. The optional read-side filter (excluding zero-available SKUs from backordered_orders) and per-order backoff are deliberately left out to keep the change minimal; the no-op-write elimination already removes the dominant steady-state cost.

Tests

  • Unit (test_allocate_handler.py): a no-progress backordered re-allocation now asserts no cas_state call; new guard test that partial progress on a backordered order still CASes; strengthened the CREATED→BACKORDERED test to assert the first-time transition still persists.
  • Integration (test_backorder_sweep.py): test_zero_stock_sweep_does_not_rewrite_the_order runs the sweep 3× against a zero-stock backordered order and asserts the header version is unchanged across ticks.

make verify green locally: 505 passed, 99.14% coverage.

🤖 Generated with Claude Code

)

A re-allocation of a backordered order that gains no stock recomputed
target == order.state (BACKORDERED) and then CASed unconditionally,
executing UPDATE orders SET state='backordered', version=version+1 -- a
real row write, dead tuple, WAL record, and version bump on every sweep
tick even though nothing changed. With B orders backordered on a
zero-stock SKU (a common steady state), cost was O(backlog x tick rate)
indefinitely with no business progress.

Track whether any line gained allocation this pass and skip cas_state
entirely when the state does not change and nothing was allocated. A real
transition or any new line allocation still CASes -- both to persist the
change and to keep the version bump that serializes concurrent
allocate/cancel against the lines just changed. Steady-state cost on an
unfillable order drops to ~0.

No invariant is affected (no stock is touched in the zero-available case).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@JumpTechCode
JumpTechCode merged commit 1bb6602 into main Jun 22, 2026
7 checks passed
@JumpTechCode
JumpTechCode deleted the perf/skip-noop-backorder-cas branch June 22, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perpetually-backordered orders are re-CASed every sweep tick (steady-state write amplification)

1 participant