Idempotency in event-driven systems
handling at-least-once delivery.
idempotency means repeated processing yields the same end state; it matters because messages get redelivered; achieve it with idempotency keys and conditional writes.
assuming exactly-once.
WHAT THIS TESTS This checks whether you understand at-least-once delivery and can design consumers that tolerate duplicates without corrupting data, a foundational event-driven skill.
A GOOD ANSWER COVERS Idempotency means processing the same event one or many times leaves the system in the same final state. It matters because queues, streams, and webhooks almost always guarantee at-least-once, not exactly-once, delivery: network retries, visibility-timeout expiry, and consumer crashes all cause the same message to be delivered again. To design for it, attach a stable idempotency key derived from the event, such as an order ID or a producer-supplied UUID. Before performing side effects, check a durable dedup store keyed by that ID; if it has been seen, skip or return the prior result. Use conditional writes, for example insert-if-not-exists or compare-and-set, so the database itself rejects duplicates. Prefer naturally idempotent operations such as setting a field to an absolute value over relative mutations. Make the dedup record and the side effect atomic wherever possible.
COMMON WRONG ANSWERS Assuming the platform delivers exactly once. Using blind increments such as balance += amount, which double-charges on redelivery. Relying on application-level locking that does not survive a crash. Treating retries as rare rather than guaranteed. Deduplicating only in memory, which is lost on restart.
LIKELY FOLLOW-UPS Why is true exactly-once so hard? How do you make the dedup check and the write atomic? What TTL should idempotency keys have? How does idempotency interact with message ordering?
ONE CONCRETE EXAMPLE A payment consumer charges a card on each message; redelivery double-charges. Fix it by recording the event UUID in a payments table with a unique constraint, performing the insert and the charge in one transaction. A duplicate insert then fails, the charge is skipped, and the customer is billed exactly once regardless of how many times the message is delivered.
Read the original → cloud.google.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.