What is idempotency and why is it critical in microservices?

Answer

An operation is idempotent if performing it multiple times produces the same result as performing it once. In microservices, network failures and retries mean a message or request can be delivered more than once — idempotency ensures that duplicate deliveries do not cause duplicate side effects (e.g., charging a customer twice). Common techniques include: using a unique idempotency key (request ID) that the server stores after processing — if the same key arrives again, return the cached result without reprocessing; using database unique constraints to prevent duplicate inserts; or designing operations that are naturally idempotent (like "set status to SHIPPED" rather than "decrement inventory by 1"). Idempotency is essential for at-least-once messaging systems like Kafka and for retries in circuit breakers.