Try
↑ ↓ move↵ openesc close

Idempotency keys and the outbox lab

Take orders from clients that retry, and publish an event for each one. Compare a dual write with a transactional outbox, idempotency keys with different lifetimes, and a consumer that does or does not drop repeats.

tick 150dual-write, keys offwrong outcomes: 19% of orders

Retried requests

orders
300
duplicate orders
36
retries absorbed by a key
0
keys stored
0

Some clients send a request twice. Without a key the service cannot tell the second one from a new order. A key that has expired looks new again.

Events

events lost
20
events published twice
0
applied twice downstream
0

A dual write saves the row and then publishes, so a crash in between loses the event. The outbox saves both in one transaction. Its relay may publish twice if it crashes before it marks the row.

Problems so far, last 150 ticks
Problems so far, last 150 ticks. Text version available below the chart.013253850176150recent tickscount

What to try

Each one sets up a starting point. Then make the change it describes.

  1. Lose an event

    With the dual write, read how many events were lost.

    Look for. A crash between the database commit and the publish loses the event, and nothing notices. The order exists and no one downstream hears about it.

  2. Use the outbox

    Switch to the transactional outbox.

    Look for. No event is lost, because the event row is saved in the same transaction as the order. The cost shows up elsewhere: the relay can publish an event twice, and a plain consumer applies it twice.

  3. Drop the repeats

    Use the outbox, then set the consumer to drop seen events.

    Look for. Delivery is at least once and the consumer makes it effectively once. Events applied twice fall to zero.

  4. Turn on keys

    Set idempotency keys on with a lifetime of 600 ticks.

    Look for. A retry carries the same key, so the service recognizes it and creates no second order. Every retry in the run is absorbed.

  5. Let keys expire early

    Keep keys on and cut the lifetime to 30 ticks.

    Look for. Retries that arrive after the key was forgotten look like new orders. A shorter lifetime keeps the table small and lets late retries through.

New to the idea? Read the lesson first. Every number here comes from a seeded simulator, so the same seed gives the same run.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms