Skip to content

The replay that emailed every customer again

Failure story 05, in events and replay.

The team rebuilt one read model from history. Nothing else changed. Why did customers start receiving confirmation emails for last year’s orders?

Assumption Value
Event log 1.2 million OrderPlaced events, kept forever
Order-history view A read model built from those events, used by the My orders page
Handler One onOrderPlaced function that updates the view and sends the confirmation email
Task Add a delivery estimate column to the view, which means rebuilding it from the first event
Step What happens
1 An engineer adds the column, empties the view and resets the consumer’s position to the start of the log.
2 onOrderPlaced runs for the first historical event: it writes the row, then sends Thanks for your order!
3 It does the same for the next event, about 400 a second.
4 Twenty minutes later, support has 300 tickets and the email provider has throttled the account for a spike in spam complaints.
5 Someone stops the consumer. About 480,000 emails have gone, and the view is half built.

Replaying events is safe only for code whose effects are inside the thing being rebuilt. Writing a row to the view is such an effect: doing it again produces the same view. Sending an email is an effect on the outside world, and the outside world doesn’t get rebuilt. The handler mixed the two, so the only way to rebuild the view was to repeat the side effect too.

Aspect Design A: one handler Design B: projectors and reactors
Handlers onOrderPlaced updates the view and sends the email A projector updates the view. A separate reactor sends the email
Replay Runs everything Runs projectors only, into a new copy of the view
Live traffic during a rebuild Has to stop, or emails are sent twice Continues: the live reactor keeps sending emails for new orders
Switching to the rebuilt view In place, while it’s half built When the new copy has caught up, switch the My orders page to it
Guard on the reactor None Records confirmation sent for order X, and won’t send twice for the same order

The rule that separates them: a projector may run any number of times; a reactor must run once per fact. Mark every handler as one or the other, and make the replay tool refuse to run reactors.

Rebuild the view in a test environment with a fake email sink attached.

  1. The rebuilt view matches. Compare the new view with the old one, row by row, for every column that existed before. Expected: identical. The new column is populated.
  2. Nothing left the building. Expected: the email sink received zero messages during the rebuild.
  3. Live work continued. Place a new order while the rebuild runs. Expected: exactly one confirmation email for it, and the order appears in both the old and the new view.

This check is in the checks catalog as test templates in Java and Python.

  • The read model is never rebuilt from history, and you’re confident it never will be. That’s a bet against your own future.
  • The handler’s only effects are on the read model itself.

Discussion question. Which of these are safe to replay: updating a search index, incrementing a page-view counter, calling a fraud-scoring API, publishing a new event? Justify each answer.

Exercise. Under Design B, a new order arrives while the rebuild is at 60%. Trace what each component does with it, and say how many emails the customer receives.

Worked answer

The live projector adds the order to the current view, so the My orders page shows it immediately. The live reactor sends one confirmation and records that it did. The rebuild projector is reading the log from the start, so it reaches the new event later and adds it to the new view, sending nothing. When the new view catches up with the head of the log, the page switches to it, and the order is already there. The customer gets one email. On the safe-to-replay question: the search index is safe if writes are keyed by ID; the counter isn’t, unless it’s recomputed from scratch rather than incremented; the fraud API is an external query (see the replay that calculated a different invoice); publishing a new event is an external effect, because other consumers will react to it.

  • Martin Fowler’s Event Sourcing (opens in a new tab) discusses external updates during replay, and wrapping external systems in gateways that can be disabled while rebuilding.

Glossary: CQRS

Written and edited by Abhinav Taduka, enterprise software architect.