Problem Statement
When an event is delivered multiple times due to retries or network glitches, processing it repeatedly can cause incorrect data updates or side effects, leading to inconsistent system state and user confusion.
Jump into concepts and practice - no test required
This diagram shows events flowing from the source to the consumer, where an idempotency check ensures each event is processed only once before updating the data store.
### Before: Non-idempotent consumer processed_events = set() def consume(event): # No check for duplicates process_event(event) ### After: Idempotent consumer processed_events = set() def consume(event): if event.id in processed_events: return # Skip duplicate event process_event(event) processed_events.add(event.id)
idempotent event consumer in microservices?processed_events = set()
def consume(event):
if event.id in processed_events:
return "Skipped"
process(event)
processed_events.add(event.id)
return "Processed"
What will be the output if the same event with id=42 is consumed twice?