liveBook · Manning

Idempotency

As explained in Microservices Patterns, Second Edition

The property of an operation that repeated application of the same input produces the correct effective outcome.

An operation is idempotent when applying the same input repeatedly produces the correct effective outcome. In a CQRS view, an event handler may receive the same event more than once, so its create, update, and delete methods must tolerate duplicate application. section 13.3.3

Choose a response based on the update and the cost of temporary staleness:

  • Set a field to a value from the event when repeating that assignment produces the correct outcome. section 13.3.3
  • Detect and discard duplicates for non-idempotent updates, such as adding a delivery time to a running sum. One approach records processed event IDs. section 13.3.3
  • Consider duplicate detection even for idempotent updates if replaying older events can make the view temporarily stale. section 13.3.3

Duplicate delivery events

An order history view receives DeliveryPickedUp and DeliveryDelivered, then receives both again after the broker redelivers from an earlier point. Processing the second pickup temporarily returns the order to an outdated state; the second delivery restores it. Discarding duplicates avoids that temporary regression. section 13.3.3

Figure 13.9 The DeliveryPickedUp and DeliveryDelivered events are delivered twice, which causes the order state in the view to be temporarily out of date.
DuplicateEventsOutOfDateState

The event sequence shows how duplicate processing can temporarily regress an order view. section 13.3.3

Duplicate handling is separate from handling events that arrive out of order. For example, an upsert can create a missing view record or update an existing one when customer and order events arrive in either order. Maintaining a CQRS view also requires replay support and adds development and operational overhead. section 13.3.3 section 13.2.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines idempotent and non-idempotent updates and recommends recording processed event IDs, without detailing the storage implementation.
Examples
Contrasts setting an event-derived field with adding delivery time to a running sum, and traces repeated pickup and delivery events.
What you can do after
Evaluate whether a view update needs duplicate detection for correctness or to avoid temporary staleness.
How it connects
Connects idempotency with CQRS, event replay, duplicate detection, and out-of-order event handling.

Also explained in

Compare how 20 books explain idempotency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.