0032. Retention sweeper for idempotency_keys and outbox_events
Status
Accepted. Closes the "known follow-up, explicitly deferred" in
ADR-0022 (no TTL job for
idempotency_keys) and the unbounded-growth consequence of
ADR-0014 (published outbox_events rows were
kept forever).
Context
Both tables are append-only bookkeeping:
idempotency_keysgets one row perIdempotency-Keyever seen onPOST /paths/{pathId}/work-units. A key only needs to outlive the client's retry window;idx_idempotency_keys_created_atwas created for exactly this cleanup.outbox_eventsgets two rows per domain event (integration + analytics topic). Once the relay has setpublished_at, the row is only useful for forensics.
Neither was ever deleted, so both grow without bound.
Decision
-
A
postgres.Housekeeperruns in thecmd/wesprocess next to the outbox relay, whenever Postgres is configured. It sweeps once at startup and then everyHOUSEKEEPING_INTERVAL. -
Rules.
idempotency_keys: delete rows withcreated_atolder thanIDEMPOTENCY_KEY_TTL. A committed row always has its outcome populated (ADR-0022 §4), so an old row is never "in progress".outbox_events: delete rows withpublished_at IS NOT NULLandpublished_atolder thanOUTBOX_RETENTION. An unpublished row is never deleted, however old — it is an undelivered event.
-
Configuration (read in
cmd/wes/main.go; all optional;0disables that table's sweep; unset/invalid/negative uses the default):Env var Default Meaning HOUSEKEEPING_INTERVAL1htime between sweeps IDEMPOTENCY_KEY_TTL24hhow long an Idempotency-Key is kept OUTBOX_RETENTION168h(7d)how long a published outbox row is kept -
Batched, idempotent deletes. Each
DELETEremoves at most 1000 rows (WHERE pk IN (SELECT pk … LIMIT n)) and loops until a batch comes back short, so a large backlog never holds one long lock. Because the statements are idempotent, replicas sweeping concurrently is harmless — no leader election. -
Shutdown. The sweeper's context is cancelled and awaited before
runreturns, so the pgx pool is never closed under a running sweep.
Consequences
- Both tables stay bounded;
idx_idempotency_keys_created_atis finally used. - An
Idempotency-Keyretried after the TTL is treated as a fresh request. 24 h is far beyond any realistic client retry window, and the ADR-0022 guarantee is explicitly "within the retention window". - Published outbox rows older than the retention can no longer be inspected;
raise
OUTBOX_RETENTIONif forensics need longer, or set it to0to keep everything. - Verified with testcontainers Postgres
(
TestHousekeeper_SweepsExpiredRowsAndNeverUnpublishedOutbox, including the batch loop and that an ancient unpublished row survives).