Testing idempotent order imports with duplicate event identifiers

2026-11-19来源 dev.toEnglish

By YX Fulfillment

A warehouse importer can see the same order event again after a retry. It can also see two genuine events that happen to request the same item. Both events belong in the ledger when their upstream identities differ. Deduplicating by SKU or matching payload would erase valid work.

This offline example assumes upstream IDs are unique within a source. An exact replay is acknowledged without inserting another record. Reuse of the same ID with changed data is a discrepancy that stops the batch. The fixture stores only synthetic event and item values.

import sqlite3 db = sqlite3.connect(":memory:") db.execute(""" CREATE TABLE received_event ( source TEXT NOT NULL, event_id TEXT NOT NULL, sku TEXT NOT NULL, qty INTEGER NOT NULL, PRIMARY KEY (source, event_id) ) """) def import_batch(events): inserted = 0 with db: for source, event_id, sku, qty in events: cursor = db.execute(""" INSERT INTO received_event VALUES (?, ?, ?, ?) ON CONFLICT(source, event_id) DO NOTHING """, (source, event_id, sku, qty)) if cursor.rowcount: inserted += 1 continue original = db.execute(""" SELECT sku, qty FROM received_event WHERE source=? AND event_id=? """, (source, event_id)).fetchone() if original != (sku, qty): raise ValueError(f"Conflicting event {source}/{event_id}") return inserted data = [("shop-a", "E-1", "S-7", 2), ("shop-a", "E-2", "S-7", 2)] first, second = import_batch(data), import_batch(data) rows = db.execute("SELECT COUNT(*) FROM received_event").fetchone()[0] assert (first, second, rows) == (2, 0, 2) print("First insert, retry insert, stored rows:", first, second, rows) try: import_batch([("shop-a", "E-3", "S-7", 1), ("shop-a", "E-1", "S-7", 3)]) except ValueError as error: print(error) else: raise AssertionError("conflicting payload was accepted") assert db.execute("SELECT COUNT(*) FROM received_event").fetchone()[0] == 2 

Output from a local run on Python 3.9.6

First insert, retry insert, stored rows: 2 0 2 Conflicting event shop-a/E-1 

The second import stores zero new rows. The two original records remain even though their item and quantity match. The conflicting batch includes a new event before the conflict, so the last assertion also checks that the transaction rolls back that insert.

Keep the deduplication record and any resulting database updates in the same transaction. An acknowledgment sent before commit can cause the source to stop retrying while the warehouse has yet to store the event. Use a durable outbox or another explicitly idempotent mechanism for work that crosses the database boundary. This test exercises SQLite transaction behavior in one process, and covers neither network retries nor distributed delivery.

原文:https://www.a8q9.cn/en/article/testing-idempotent-order-imports-with-duplicate-event-identifiers-abe0fd.html

相关文章