Idempotent Write
Problem
ION’s event-driven backbone is at-least-once — Service Bus redelivers, retries happen after failures, replays are a first-class feature of event sourcing. Without idempotency, duplicate deliveries corrupt state: an incident is closed twice, a projection is over-counted, an audit event is emitted repeatedly.
Every write path — command handler, event handler, projection updater — must therefore treat repeated delivery of the same logical operation as a no-op after the first successful apply.
Solution
Assign a stable idempotency key to every logical write and persist the outcome of first application; on repeat, short- circuit to the same outcome.
Key sources (pick one, be consistent):
messageIdon Service Bus messages — natural key for consumers.- Domain identity —
{aggregateId, sequenceNumber}for event appends to Cosmos. - Business key —
{tenantId, externalRequestId}for ingested commands (Sentinel incident ID). - HTTP header —
Idempotency-Keyheader on write APIs, passed through by the client.
Application patterns:
- Event store append — enforce uniqueness on
{aggregateId, sequence}; a duplicate append fails on the unique constraint and is caught + treated as success. - Projection updater — track last processed
{partition, offset}per projection; skip anything ≤ last. - Command handler — persist the command’s idempotency key with its outcome (result + status); repeated calls read and return the stored outcome.
- External call side-effects — combine with the outbox pattern so external side effects publish via the same transactional write that records idempotency.
When to use
- Every message consumer (Service Bus, Event Grid).
- Every event append to the event store.
- Every external write API that customers can retry.
- Every projection / read-model updater.
Not needed for read-only paths.
Consequences
Positive
- Duplicate deliveries and manual replays are safe.
- Enables aggressive retry policies without corruption risk.
- Compatible with event-sourced replay and DR restore.
Negative
- Requires storage for idempotency state (small; TTL-able for keys older than replay window).
- Adds a lookup on every write — negligible when indexed.
- Wrong key choice leaves gaps — the key must uniquely identify the logical operation, not the message.
Failure modes
- Key too broad (e.g.
tenantId) — collapses legitimate distinct operations. - Key too narrow (e.g.
messageIdwhen messages carry multiple logical ops) — allows duplicates. - Non-durable storage for idempotency state — restarts break the guarantee.
References
references/Design Pattern- Idempotent Write.md- Related patterns: Dual Writes, ECST, DLQ, Async REST
Applied in
- Event store appends (event-store DR)
- Watchtower incident ingestion (Sentinel deduplication)
- Cogs / Temporal activity retries
- All Service Bus consumers (platform SDK enforces)
- Adaptive card action handlers (Pigeon → BFF)