awesome-everything RU
↑ Back to the climb

Queues, Streams, Eventing

Delivery guarantees: code and log reading

Crux Read real consumer code, SQL, and a metrics line, predict the delivery-guarantee behaviour, and pick the highest-leverage fix a senior engineer makes first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Delivery-guarantee bugs are found in the consumer code, the SQL, and the metrics line — rarely in the broker. Read each snippet, predict where the duplicate or the loss comes from, then choose the fix a senior engineer reaches for first.

Goal

Practise the loop you run in every duplicate-charge incident: read the handler, find the crash window or the race, and reach for the structural fix rather than nudging a timeout.

Snippet 1 — the SELECT-then-act consumer

def handle(msg):
    if db.execute("SELECT 1 FROM processed WHERE msg_id=%s", msg.id):
        return ack(msg)              # already done, skip
    stripe.charge(msg.amount)        # side effect
    db.execute("INSERT INTO processed (msg_id) VALUES (%s)", msg.id)
    ack(msg)
Quiz

Two consumers receive the same message during a rebalance. What happens, and what is the highest-leverage fix?

Snippet 2 — the transactional dedup

BEGIN;
  INSERT INTO processed (msg_id) VALUES ('msg-7a3f');  -- UNIQUE(msg_id)
  UPDATE orders SET status = 'paid' WHERE id = 'O-123';
COMMIT;
-- on UNIQUE violation: ROLLBACK, log "duplicate", ack the broker
Quiz

The consumer COMMITs this transaction, then crashes before acking the broker. On redelivery, what happens?

Snippet 3 — the external-API consumer

def handle(msg):
    resp = stripe.charge(msg.amount,
                         idempotency_key=msg.id)   # external dedup
    db.execute("UPDATE intents SET charge_id=%s WHERE msg_id=%s",
               resp.id, msg.id)
    ack(msg)
Quiz

The Stripe call succeeds, then the process dies before the UPDATE and the ack. The broker redelivers minutes later. What is the actual behaviour?

Snippet 4 — the dedup metric

metric: dedup_check_hit_rate
  steady state: 0.05%
  14:02-14:12 : 8.0%   (no deploy in this window)
  14:13+      : 0.06%
Quiz

Reading this metric, what most likely happened — and is it a correctness emergency?

Recap

Every delivery-guarantee incident is read in code, SQL, and metrics: SELECT-then-act races under concurrent delivery; INSERT-first in one transaction makes redelivery harmless; an Idempotency-Key derived from a stable message ID extends dedup across an external API; and a dedup_hit_rate spike means the broker is redelivering, not that dedup failed. Diagnose the crash window or the race, apply the structural fix (atomic dedup, stable idempotency key), then confirm the duplicate path is closed — before touching a single timeout.

Continue the climb ↑Delivery guarantees: build a crash-proof payment consumer
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.