Trace gateway queue capacity, poison messages and replay
Apply a bounded queue, transformation contract, duplicate filter, dead-letter route and replay to a supplied fictional event fixture.

Gateway Gus
Use the Integration and Gateways message-queuing route to inspect how a bridge maps source records, preserves meaning and handles failures.
Predict the reading, then compare it with the measurement.
Python 3 in your browser (JupyterLite)
Python · no installOpen the notebook in your browser and run each Python cell; no install or account is needed.
Three ways to run: use JupyterLite here with no install; run main.py locally from the downloadable lab folder; or open the same notebook in Google Colab.
Steps
Step 1
- Do
- In the notebook editor, run the Step 1 notebook cell. Freeze and hash the fictional source-event fixture.
- You will see
Run the cell. You should see:
- The run prints six events including a duplicate e2 and malformed e3, plus a SHA-256 prefix.
- Why it matters
- A replay result needs a fixed source input.
Code for step 1#!/usr/bin/env python3 """Deterministic gateway queue/translation model, not a broker or protocol run.""" import argparse import hashlib import json EVENTS = [ {"id": "e1", "source": "meter-a", "watts": 120}, {"id": "e2", "source": "meter-a", "watts": 125}, {"id": "e2", "source": "meter-a", "watts": 125}, {"id": "e3", "source": "meter-b", "watts": "bad"}, {"id": "e4", "source": "meter-b", "watts": 90}, {"id": "e5", "source": "meter-a", "watts": 130}, ] def show(title, rows): lines = [title, *rows] assert 4 <= len(lines) <= 15 print("\n".join(lines)) def fixture_hash(): return hashlib.sha256(json.dumps(EVENTS, sort_keys=True, separators=(",", ":")).encode()).hexdigest() def stage1(): show("Step 1 | Freeze the supplied event fixture", [ f"events={len(EVENTS)} SHA256={fixture_hash()[:16]}…", "fields: id, source, watts; fictional meter values only.", "IDs: e1 e2 e2 e3 e4 e5; one duplicate and one bad value.", "This is a Python queue model, not a broker delivery trace.", ]) def stage2(): queue = [] rows = ["arrival queue_after_arrival decision"] for item in EVENTS[:4]: if len(queue) == 3: rows.append(f"{item['id']:>7} {' '.join(queue)} backpressure") else: queue.append(item["id"]) rows.append(f"{item['id']:>7} {' '.join(queue)} enqueued") queue.pop(0) rows.append(f"drain_e1 {' '.join(queue)} one slot freed") rows.append("Backpressured e3 is retried after drain, then validated in Step 3.") show("Step 2 | Fill a bounded intake queue", rows) def stage3(): seen = set() rows = ["event decision"] for item in EVENTS: event_id = item["id"] if event_id in seen: decision = "duplicate" elif not isinstance(item["watts"], (int, float)): decision = "dead-letter" else: decision = "accept" seen.add(event_id) rows.append(f"{event_id:>5} {decision}") show("Step 3 | Validate and deduplicate", rows) def stage4(): rows = ["id target_topic target_payload"] seen = set() for item in EVENTS: if isinstance(item["watts"], int) and item["id"] not in seen: rows.append(f"{item['id']} plant/{item['source']}/power {{'watts':{item['watts']},'unit':'W'}}") seen.add(item["id"]) rows.append("Contract maps source and watts; id must remain the idempotency key.") show("Step 4 | Translate accepted records", rows) def stage5(): show("Step 5 | Quarantine and inspect poison input", [ "e3 has watts='bad'; integer watts required by target contract.", "Dead-letter entry: id=e3 reason=invalid_watts attempts=1.", "Do not retry malformed input forever or block later messages.", "Fix source schema or record an explicit manual correction.", ]) def stage6(): show("Step 6 | Replay after correction", [ "Corrected e3 watts=95; retain original id=e3 for audit.", "Replay e3 once: accepted=1, duplicate e2 still ignored.", "Final unique accepted IDs: e1 e2 e3 e4 e5 (5).", "Original intake=6; duplicate=1; repaired dead-letter=1.", "Broker acknowledgement and crash recovery remain unmodelled.", ]) STAGES = (stage1, stage2, stage3, stage4, stage5, stage6) import platform try: import pyodide runtime = f"Python {platform.python_version()} / Pyodide {pyodide.__version__}" except ImportError: runtime = f"Python {platform.python_version()} (local or Colab)" print(f"Runtime: {runtime}") print("Runner check: Python calculation from this notebook") stage1()
Step 1 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 2
- Do
- In the notebook editor, run the Step 2 notebook cell. Fill a capacity-three intake queue.
- You will see
Run the cell. You should see:
- The fourth event e3 meets backpressure; draining e1 frees a slot.
- Why it matters
- Capacity pressure needs an explicit admission policy.
Code for step 2stage2()
Step 2 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 3
- Do
- In the notebook editor, run the Step 3 notebook cell. Validate and deduplicate the six events.
- You will see
Run the cell. You should see:
- The output marks e2 duplicate and e3 dead-letter while accepting e1, e2, e4 and e5.
- Why it matters
- Bad records must not block later valid records.
Code for step 3stage3()
Step 3 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 4
- Do
- In the notebook editor, run the Step 4 notebook cell. Translate accepted meter records.
- You will see
Run the cell. You should see:
- The output maps e1 to plant/meter-a/power with watts 120 and unit W.
- Why it matters
- A reviewable bridge specifies target topic, unit and idempotency key.
Code for step 4stage4()
Step 4 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 5
- Do
- In the notebook editor, run the Step 5 notebook cell. Quarantine the poison record.
- You will see
Run the cell. You should see:
- The dead-letter entry says id=e3 reason=invalid_watts attempts=1.
- Why it matters
- Unbounded retries of malformed input cause queue failure.
Code for step 5stage5()
Step 5 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 6
- Do
- In the notebook editor, run the Step 6 notebook cell. Replay the corrected event once.
- You will see
Run the cell. You should see:
- The run reports five unique accepted IDs after correction, one duplicate and one repaired dead letter.
- Why it matters
- Replay must preserve identity and show reconciliation counts.
Code for step 6stage6()
Step 6 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
A bridge has a retained command and a rejected message. What two failure paths need explicit policy?
Return to the chapter’s knowledge checkA team sets the highest QoS so that each command runs exactly once across the whole pipeline. Why is QoS alone not sufficient?
Return to the chapter’s knowledge check
Caution
Before you start
Read this before you start.
The fixture and queue are a deterministic Python model, not broker traffic. Confirm acknowledgements, persistence and crash recovery in a real gateway.
Technical details
- Version tested
- Version tested: Python 3.12.7 / Pyodide 0.27.6 in JupyterLite 0.6.4; Chromium 151.0.7922.34; captureSource playwright:jupyterlite. Date: 2026-10-08.
https://iotclass.org/labs/gateway-queue-poison-replay.html