In this lesson · topic 4 of 7
Topic 4
Slow is not dead: duplicates without a crash
6 min read
Every number on this page was produced by the simulator with a fixed seed and is checked by a test in the repository. Each run publishes thousands of messages. In every run half of the crashes land in the ack gap, unless the text says otherwise.
More crashes, more of the failure
With acking first, a crash chance of 5 percent per attempt lost 2.6%measured of the messages. At 15 percent it was 8.3%measured, and at 30 percent 18%measured. The loss grows a little faster than the crash rate itself.
With acking last, nothing was lost at any of those rates. The cost shows up as repeated side effects per message: 0.03measured at 5 percent, 0.09measured at 15 percent, and 0.21measured at 30 percent. Those numbers also match what the arithmetic of the model predicts, which a test checks.
Slow is not dead
Turn crashes off and let 20 percent of attempts run longer than the ack timeout. With acking last, each message caused 0.25measured extra effects on average. The broker gave up on the slow attempt, handed the message out again, and then both finished. With acking first the same slowness caused 0.00measured extra effects, because the message was already gone from the broker.
So a duplicate does not need a failure. A garbage-collection pause, a busy disk or a timeout set too low is enough. Redelivery should be treated as normal, and not as a rare event.
The key helps only if it commits with the effect
With an idempotency key written together with the effect, extra effects fell to 0.00measured and losses to 0%measured. Redelivery went on exactly as before: 0.17measured redeliveries per published message, against 0.17measured without the key. In place of repeating the effect, the store skipped it 0.09measured times per published message.
Now write the key in a separate step after the effect. A crash between the two leaves an effect with no record. The retry cannot tell, and does it again. Extra effects per message came back to 0.04measured, lower than without a key but not zero.
Limits the lab does not show
- The store grows. Keys cannot be kept forever. You expire them, and the window has to be longer than the longest delay a redelivery can arrive after. A late duplicate past the window is applied again.
- Poison messages. A message that always makes the consumer fail will be redelivered over and over. Real systems cap the number of attempts and move the message to a dead-letter queue for a person to look at.
- Producers repeat too. If a producer retries a send that did succeed, the broker holds two copies. The key must be attached before the first send.
- Ordering. Redelivery can bring a message back after later ones were processed. If order matters, the consumer must check it.
- The lab has one kind of failure. Brokers fail, networks split and acks get lost on the way back. They add more ways to repeat, and make the case for idempotence stronger.