A cap sits on a shelf, and the count a person made at 18:00 says zero. The system says -1. A sale arrived for an item the system already counted as zero, and the system accepted it. Two other products disagree with their shelves as well, and the program raised no error for any of the three. This lab reproduces all of it with one JSON file and python3. You clone github.com/izzudd/inva-lab, open 0008.stale-writes/, and run three commands.
Key takeaways
- A message describing 10:05 can arrive after the one describing 10:10, and a consumer that applies what it reads keeps the older number.
- Three of the eleven messages describe a state that was already gone: two older sets and one sale on a shelf counted at zero.
- The starter applies all eleven and rejects none, which is why nothing throws and why the log looks tidy.
- The answer adds three guards in order: a seen
event_id, then anevent_timenewer than the row, then a sale the shelf can cover. - The answer ends at
applied=8 rejected=3with five of five checks green, andapp/is the only directory that changes. - The lab has no broker and no concurrency, so the fix cannot be "apply them in the right order".
What the system records
Start with make demo. It replays eleven messages and compares each product with the venue count, the number written down at 18:00.
$ make demo
python3 scripts/demo.py
sku system venue verdict
CAP-03 -1 0 DIFFERENT
MUG-01 4 6 DIFFERENT
TEE-02 11 9 DIFFERENT
messages applied: 11, rejected: 0
sales attempted on an empty shelf: 1
Every product disagrees with its shelf count: MUG-01 is short by two, TEE-02 is high by two, and CAP-03 sits at -1.
The last two lines explain why nothing threw: the starter applied all eleven messages and rejected none, because it has no rule that refuses an update.
One queue, three wrong shelves
make run replays the same eleven messages and prints one line per message, in arrival order. Each message carries event_id, sku, type, quantity and event_time, and the type is either set or sale. Three of the eleven arrive later than the event they describe.
$ make run
python3 app/consumer.py
1 e-101 MUG-01 set 12 @10:00 applied MUG-01 = 12
2 e-104 MUG-01 set 6 @10:10 applied MUG-01 = 6
3 e-102 MUG-01 set 4 @10:05 applied MUG-01 = 4
4 e-201 TEE-02 set 9 @10:02 applied TEE-02 = 9
5 e-203 TEE-02 set 9 @10:09 applied TEE-02 = 9
6 e-202 TEE-02 set 11 @10:08 applied TEE-02 = 11
7 e-301 CAP-03 set 3 @10:03 applied CAP-03 = 3
8 e-302 CAP-03 sale 2 @10:07 applied CAP-03 = 1
9 e-303 CAP-03 sale 1 @10:11 applied CAP-03 = 0
10 e-304 CAP-03 set 0 @10:12 applied CAP-03 = 0
11 e-305 CAP-03 sale 1 @10:14 applied CAP-03 = -1
summary messages=11 applied=11 rejected=0
Line 3 is the bug. A message describing 10:05 lands after a message describing 10:10, and the consumer overwrites the row with whichever message arrived last. Read MUG-01 by arrival and you get 12, then 6, then 4. Read it by event time and the newest fact is the set to 6 at 10:10, which matches the shelf.
TEE-02 has the same shape. The set to 11 describes 10:08, but it arrives after the set to 9 that describes 10:09, so line 6 wins over line 5. That count was accurate at 10:08, yet arriving after 10:09 lets an outdated reading overwrite a newer one.
The lab writes the arrival order into a JSON array, which is what makes the failure reproducible and free to run. A broker passes messages in delivery order rather than event order. Prefetch hands a message to a worker that is already busy, a retry sends one that was delivered before, and two consumers reading the same partition interleave. None of those is a fault in the broker.
CAP-03 fails differently. It goes 3, then 1 after the first sale, then 0 after the second. The set to 0 keeps it there. Line 11 then takes it to -1, a state no shelf can be in. The summary at the bottom records all eleven updates as applied and zero rejected.
Your mission
scripts/check.py holds five checks with one theme: a message about the past cannot overwrite the present, and a sale cannot take the shelf below zero.
- A: every product's quantity matches the venue count.
- B: an update older than the stored
event_timeis rejected, not applied. - C: no quantity is negative, and a sale on an empty shelf is refused.
- D: a re-delivered event does not move the stock.
- E: the consumer prints a summary whose counts are the ones it actually applied.
A and C read data/warehouse.json. B and D build their own small queues, so editing the data file cannot turn them green.
A code review can read a loop that applies messages and find it reasonable. A check can feed it a late 3 after a 5 and show you the result.
$ make check
python3 scripts/check.py
FAIL A every product's quantity matches the venue count
CAP-03: system -1, venue 0; MUG-01: system 4, venue 6; TEE-02: system 11, venue 9
FAIL B an update older than the stored event time is rejected
after 5 then a late 3 from an hour earlier: MUG-01 = 3, rejected 0
FAIL C no quantity is negative, and a sale on an empty shelf is refused
one item, then two sales: CAP-03 = -1; negative rows in the replay: CAP-03: -1
FAIL D a re-delivered event does not move the stock
with e-302 delivered twice: CAP-03 -3, MUG-01 4, TEE-02 11
PASS E the consumer prints one summary line with the counts it actually applied
summary messages=11 applied=11 rejected=0
1 of 5 checks passed
make: *** [Makefile:4: check] Error 1
You start at 1 of 5. Check E passes because the summary accurately reports what the starter did: it applied every message.
What the answer changes
The answer is one file, app/store.py, on the solusi-0008-stale-writes branch of the same repo. It adds three guards: skip any event_id already seen, reject any message older than the row's event_time, and refuse any sale larger than the current stock.
With those in place, all five checks pass and the consumer prints summary messages=11 applied=8 rejected=3. Three of eleven messages are refused. Two describe an older state, and one is a sale on an empty shelf.
The order of the guards matters for the counters. A message that is both a duplicate and stale is refused at the first one, so applied plus rejected still equals the number of messages read, whatever the data looks like.
You still have to write it. HINTS.md has three levels, and the first one only asks you to put two messages side by side.
What this lab does not do
- No broker: the queue is a JSON array in arrival order, so prefetch and re-delivery are written into the file.
- No concurrency: one process and one lane, because serializing messages would hide the bug rather than fix it.
- No storefront push: the retry path from the original story is out of scope.
- Two message types only: the vocabulary is
setandsale, with no reservations, transfers, or returns.
Check which counts in your own system rely on arrival order when they should track event time.