Practical technical guides / Agent-era architecture: idempotency and messaging

The Inbox Forgets Before the Sender Stops Retrying: Size Dedup Retention From the Redelivery Horizon

An inbox only stops duplicate agent commands while it remembers the key. Size retention from DLQ redrive, offset resets and re-plans, and key by business intent.

By Sandeep Belgavi · Published 2026-11-29

An inbox must outlive every redelivery path
Original explanatory diagram; synthetic values where shown.

An inbox only stops duplicate agent commands while it remembers the key. Size retention from DLQ redrive, offset resets and re-plans, and key by business intent.

Why an agent makes the old inbox question harder

An idempotent consumer, often called the inbox pattern, is a small table of processed message keys. The consumer inserts the key in the same transaction as the business change, so a redelivered message finds its key and does nothing. Most teams implement it once, pick a retention period that sounded reasonable at the time, often seven days, and purge older rows.

That choice was rarely revisited because the only thing that redelivered messages was the broker, and brokers redeliver within minutes. Put an agent in front of the producer and the set of redelivery paths grows. At the help desk of Belgavi's Book Store an agent reads CASE-4102, decides a refund is due and emits a command issue_refund with id CMD-101. The command can now arrive again through at least five paths:

  • the producer's own retry after a timeout, within seconds or minutes;
  • a dead-letter queue that someone redrives days later, after fixing the bug that made the consumer reject it;
  • a consumer group whose offsets are reset to replay a log, for example after a bad deploy;
  • a backfill job that re-sends the commands of one week;
  • the agent itself, which re-plans after a tool error and issues CMD-205 for the same refund.

The inbox only helps if it still remembers the key when the duplicate arrives, and if the key is the same. This guide shows how to size retention from the longest redelivery path instead of habit, and how to choose a key that survives re-planning. The companion example and the interactive lab replay the same scenarios.

What stays deterministic

The agent decides what should happen. Everything after the decision is ordinary, deterministic messaging and must stay that way: the command id, the business key, the inbox insert, the transaction that couples it to the refund, and the purge job. None of these should depend on the model. In particular the consumer must never ask the model whether a refund was already issued. That question has a factual answer in your own tables.

The command carries two keys:

  • the command id, unique per emission, assigned by the producer when it first publishes;
  • the intent key, which names the business effect, such as refund:CASE-4102. It is built from case facts the workflow already holds, not from text the model wrote.

The consumer records both. The command id catches transport duplicates. The intent key catches a second decision about the same effect, which is the duplicate an agent adds.

CREATE TABLE inbox (
  command_id   text PRIMARY KEY,
  intent_key   text NOT NULL,
  processed_at timestamptz NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX inbox_intent ON inbox (intent_key);

-- inside the consumer's transaction
INSERT INTO inbox (command_id, intent_key) VALUES ($1, $2)
ON CONFLICT DO NOTHING;
-- 0 rows inserted: a duplicate by either key; commit without applying

A unique intent key is only right when the effect can happen once per case. For effects that may legitimately repeat, such as a second partial refund approved by a person, put the approval id into the intent key, so a new approval makes a new key and a replay of the old one does not.

Size retention from every redelivery path

Retention is a promise: the inbox remembers a key for as long as any path can deliver that key again. List the paths and how long each can be.

PathLongest delay after the first send
Producer retries, capped at 30 minutes0.5 hours
Amazon SQS standard dead-letter queue, 14-day retention336 hours, counted from the original send
Amazon SQS FIFO: up to 4 days in the source queue, then 14 days in the DLQ432 hours, because a FIFO move resets the timestamp
Offset reset to the earliest offset of a 7-day log168 hours
Backfill of the previous weekwhatever the job's window is

The SQS rows come from the dead-letter queue documentation. For standard queues the expiration is based on the original enqueue timestamp, which stays unchanged when a message moves to the dead-letter queue. For FIFO queues the timestamp resets on the move. That one sentence changes the horizon by days, so read it for your own broker rather than assuming.

The required retention is the largest of these delays plus a margin for clock skew between producer and consumer. For the table above, the example's required_retention returns 433 hours. A seven-day inbox covers producer retries and the 7-day replay, and silently fails the rest.

Purging is still necessary: the table grows with traffic. If 433 hours of keys is too many rows, shorten the paths instead. Cap DLQ retention, forbid resets beyond the inbox horizon, or make the redrive tool check the inbox age before it sends.

A worked example with the numbers

The companion example inbox_retention_horizon simulates one consumer over a timeline of deliveries. In the first fixture the inbox keeps keys for 168 hours. CMD-101 (refund for CASE-4102) arrives at hour 0, its producer retry at hour 0.2, CMD-102 (a return label for CASE-4107) at hour 6, and both are redriven from the dead-letter queue at hour 200, after an engineer fixed a schema bug.

The result: the retry at 0.2 hours is skipped, but both redrives arrive after their records were purged. Four effects are applied, two of them duplicates: a second refund and a second label. The longest gap between a key's first application and a later delivery is 200 hours. Raise retention to 361 hours and the same timeline applies two effects and catches three duplicates.

The re-plan fixture shows the other failure. The agent emits CMD-201 for refund:CASE-4120, retries it once, then re-plans after a timeout and emits CMD-205 for the same refund. Keyed by command id, three effects are applied and one of them is a duplicate, although every key was remembered. Keyed by intent, two effects are applied and two duplicates are caught.

The offset-reset fixture uses a 48-hour inbox and a replay at hour 72 of three commands sent at hours 0, 30 and 60. Only CMD-301 is applied twice: the other two are still within 48 hours. That is the pattern to expect in production, where duplicates come from the oldest messages of a replay.

Refresh on duplicate, or not

A design choice the lab exposes: when a duplicate is skipped, should its record's timestamp move forward? With refresh, a key that keeps being retried stays remembered as long as retries continue. In the retry-storm fixture, with a 24-hour inbox and retries every 20 hours, refresh catches all three duplicates. Without it, the retry at hour 40 is 40 hours after the record was written and is applied again.

Refresh helps against periodic retries and does nothing against a single late redrive, which is the more common incident. It also lets a misbehaving producer keep keys alive forever. Treat it as a complement to correct retention, not a replacement.

Migration steps for an existing consumer

  1. Inventory every path that can resend a command: producer retry policy, DLQ retention and redrive tooling, log retention and offset-reset runbooks, backfill jobs, and the agent's re-plan behaviour.
  2. Compute the horizon from that list and compare it with the current purge job. Record the result next to the purge job's configuration so the next change to DLQ retention fails review.
  3. Add an intent key to the command schema, built by deterministic code from case facts. Backfill the column for recent rows from the business tables where you can.
  4. Make the redrive tool and the replay runbook refuse messages older than the inbox horizon, or route them to a person.
  5. Alert on the case the inbox cannot see: an applied effect whose intent already has an earlier applied effect. A nightly query over the business table finds these.

When an agent is unnecessary

If a rule decides the refund, as many return-window cases do, a deterministic workflow emits exactly one command per case transition and the re-plan path disappears. Keep the agent for cases that genuinely need judgement, and keep the inbox anyway: brokers and people still resend messages.

Failure modes and how to verify

The common failures are a purge job tightened during a storage incident without anyone checking the horizon, a DLQ retention raised for debugging, and a new code path that builds the intent key from the model's text. Verify with tests that replay a fixture timeline across the purge boundary, assert that the boundary is inclusive (a duplicate exactly at the retention age is still caught), and assert that two command ids with the same intent key apply once. The lab lets you change one assumption at a time and export the trace for a design review.

Interactive experiment

Inbox retention against redelivery: which duplicates get applied. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.

Primary references and authorship

Sources checked 2026-11-29. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/inbox_retention_horizon. Views are my own and do not represent any employer, past or present.