perf(allocate): skip the no-op header CAS for unfillable backorders (#67) - #84
Merged
Merged
Conversation
) 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #67.
Problem
When the sweep re-allocates a backordered order and no stock is available,
allocatecomputedtarget = BACKORDERED == order.stateand then calledcas_stateunconditionally, executingUPDATE 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. WithBorders 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 tobacklog × tick rate, dead tuples on the hotorderstable, and a modestly raised OCC-conflict probability for legitimate concurrent allocate/cancel.Fix
Track whether any line gained allocation this pass (
any_allocated) and skipcas_stateentirely 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 fromO(backlog × tick)to ~0.No invariant is affected: in the zero-available case no stock is touched (
stock_locationsfiltersavailable > 0, so the per-lineFOR UPDATEreserve 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
test_allocate_handler.py): a no-progress backordered re-allocation now asserts nocas_statecall; 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.test_backorder_sweep.py):test_zero_stock_sweep_does_not_rewrite_the_orderruns the sweep 3× against a zero-stock backordered order and asserts the headerversionis unchanged across ticks.make verifygreen locally: 505 passed, 99.14% coverage.🤖 Generated with Claude Code