wtf( )unctionsystem design, drawn
← all problemsDistributed SystemsMedium

Stock that drifts negative

An inventory topic is spread round-robin across twelve partitions for throughput. Decrement and increment events for the same item land on different partitions and get consumed in parallel, so they apply out of order.

Order is guaranteed inside a partition, not across them. Round-robin threw the guarantee away for throughput nobody measured.

Choose what decides which partition an event goes to.
Components — tap one, then tap a slot on the diagram
!Stock counts are drifting negative, and no individual event is wrong.

Boundaries, outermost first: Topic: Partition 3 (FAILED: out of order), Partition 1 (FAILED: out of order), Partition 2 Outside every boundary: Producer, an empty slot for the how events are routed Connections: Producer publishes to how events are routed (step 1) how events are routed publishes to Partition 1 — same item, same partition (step 2) how events are routed publishes to Partition 2 how events are routed publishes to Partition 3

Partition 3out of order
Partition 1out of order
Partition 2
Producer