December 1, 2026•5 min read

The Answer: Make the Two Rules Agree

Three independent fixes, measured one at a time. The one you reach for first makes the log quiet and leaves the product on sale, and that is the most useful row in the table.

Read in Indonesian

Lab repo

The lab lives in folder 0007.echo-loop

The deliberately broken code, the dataset, and the target live in a public repo. Everything runs in Docker. make check

The first fix most people write turns two checks green, quiets the log, and still leaves the mug on sale. If you stop there, the output looks solved while the core bug remains.

Key takeaways

  • Diffing before writing is necessary and not sufficient: it fixes the log and leaves the field wrong.
  • The app has to own every field the other side's rule reads, or it owns neither.
  • Drafting a product without zeroing its stock moves the failure from 'still on sale' to 'never coming back'.
  • Coalescing a queue keeps the newest event, not the first, and reads the input when the job runs.
  • Each fix moves a disjoint set of checks, which is why the table has five rows instead of two.

Measurements

Each row is one edit applied alone to the starter, then make check. Nothing is estimated:

applied green what the detail says
starter, unchanged none 0/7
1. send only what differs D, G 2/7; A still status=ACTIVE after 40 writes, C still throttles MUG-01
2. own both fields A 1/7; F turns into out: DRAFT/0, back: DRAFT/0
3. decide from the newest answer E 1/7; G still 39 redundant of 160 writes
all three A–G 7/7; writes=2 throttled=0, 0 redundant of 5 writes

Three disjoint sets: {D, G}, {A}, {E}. B, C and F need the first two together, because the loop has to stop before a second phase can even be measured.

1. Send only what differs

The starter's worker writes whatever the app wants, every time:

PYTHON
live = store.get(gid)
store.write(gid, **desired(job, live))
writes += 1

It looks right because live is fetched and passed in. It is fetched and then ignored. The store accepts a write that repeats the value it already has, and fires products/update for it anyway, so every redundant write generates a new event and runs the handler again.

PYTHON
changes = {
    field: value
    for field, value in desired(job, live).items()
    if live.get(field) != value
}
if not changes:
    continue

D and G go green, while A still fails with status=ACTIVE after 40 writes. The diff removes redundant writes, yet the app and the store automation still flip the product back and forth.

2. Own both fields, or neither

The store's automation reads inventory_quantity. The app writes status. The app is making an argument it cannot win, because the field that decides the argument is not in its payload:

PYTHON
def desired(job: Job, live: dict) -> dict:
    return {"status": "DRAFT" if not job.available else "ACTIVE"}

The fix is to satisfy the other side's invariant instead of contradicting it. A draft has to mean no stock on hand, so drafting is a two-field write:

PYTHON
if not feed.available(sku_of(gid)):
    if live["inventory_quantity"] > 0:
        _STOCK[gid] = live["inventory_quantity"]
    return {"status": "DRAFT", "inventory_quantity": 0}

Now the automation has nothing to object to, the webhook the app receives says what the app already believes, and there is no next write to make. A goes green.

F does not, and it gets worse before it gets better: out: DRAFT/0, back: DRAFT/0. The product is correctly drafted and now permanently unsellable, because the stock it had is gone and nothing remembers it. That is why _STOCK exists in the fix: without it, drafting a product permanently loses its inventory count.

3. Decide from the newest answer

The starter's handler queues the first event and drops the rest:

PYTHON
if gid in queue:
    return
queue[gid] = Job(gid=gid, available=feed.available(sku_of(gid)))

That guard drops burst traffic at the cost of keeping stale data: five supplier updates arriving before the worker runs collapse to the first, so the app acts on the oldest answer. Two changes fix it:

PYTHON
queue[gid] = Job(gid=gid)          # the newest event wins
...
return {"status": "DRAFT" if not feed.available(sku_of(job.gid)) else "ACTIVE"}

Coalesce toward the newest, and read the input when the job runs rather than when the event arrived. E goes green.

What the lab still does not teach

The lab runs on one thread with one queue, so it never exercises concurrency. In production, this bug class also requires one lane per product so two workers cannot interleave writes to the same object, even though adding locking here will not change a single check.

The forty-write cap is also a harness convenience that turns a hang into a readable log. A real store charges query cost against a shared budget, so a looping sync on one product starves every other API call the app makes for that merchant.

Before you ship

Because the three fixes are independent, you can ship them in any order and get partial green checks. Diffing first is cheap and misleading, so you need a check that verifies final product state rather than write volume alone. Beyond this lab, map every field the other system's automation reads so both sides agree on the same state.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
WebhookEvery Write Comes Back as News

A supplier file says the mug is gone, the app drafts it, the store puts it back, forty times. Seven checks, and the eager webhook handler is only the second problem.

WebhookThe Status That Changed Itself

A product's status blinks between published and draft, nothing errors, and the guard you wrote first works right up until it doesn't. The loop was never an echo.

For developers who learn by struggling

© 2026 Inva.dev.