← Lesson
Queues and Retries
Joeven
Run
Reset
Python loads on first run
import random random.seed(2) def backoff(attempt, base=0.1, cap=2.0): raw = min(cap, base * (2 ** attempt)) jitter = raw * (0.5 + random.random() / 2) return round(jitter, 3) def run_with_queue(max_attempts=4): ledger = {} attempts = [] for attempt in range(max_attempts): key = "job_17:step_3:refund" if key not in ledger: ledger[key] = {"cents": 1999} wrote = True else: wrote = False crash = attempt < 2 attempts.append({"attempt": attempt, "new_write": wrote, "backoff": backoff(attempt), "crash": crash}) if not crash: return {"ok": True, "ledger": ledger, "attempts": attempts} return {"ok": False, "dlq": True, "attempts": attempts} out = run_with_queue() print("attempts:") for a in out["attempts"]: print(" ", a) print("final ledger", out.get("ledger"), "ok", out["ok"]) print("idempotency kept a single refund despite retries")
Run to execute this in your browser. Nothing is sent to a server.