SkillSprout
Chapter 24 · Lesson 3
Advanced
3 min read

Idempotency and Chaos Engineering

Overview

Retries are only safe if repeating an operation does no harm. Idempotency makes that true. Chaos engineering then tests, on purpose, whether your resilience patterns really work.

Detailed Notes

What is idempotency

An operation is idempotent if doing it many times has the same effect as doing it once. "Set the user's email to a@x.com" is idempotent. "Add 10 dollars to the balance" is not: each repeat adds again.

In HTTP, GET, PUT and DELETE are meant to be idempotent; POST is not by default.

The problem it solves

A customer taps "Pay 50 dollars". The card is charged, but the response is lost on the way back, so the app times out and retries. Without protection, the customer is charged twice. A timeout never tells you whether the server did the work, so retrying is only safe if repeats are harmless.

How it works

Idempotency keys. The client generates a unique key per logical operation and sends it with every attempt. The server stores the key and its result (say for 24 hours); on a repeat it returns the stored result without redoing the work. A database unique constraint on the key guards against races.

Delivery guarantees. Systems that pass messages between services (covered later) usually promise at-least-once delivery: a message arrives one or more times, so receivers must be idempotent. At-most-once may lose messages. Exactly-once is very hard and is usually built as at-least-once plus idempotent processing.

Chaos engineering. Inject controlled failures to prove your timeouts, breakers and failover really work, on your terms rather than at 3 am. Define the steady state (a measurable normal, such as orders per minute), form a hypothesis ("if one server dies, steady state remains"), inject a failure, observe the metrics, then fix any gap.

Control the blast radius: start small (one instance, a test environment), keep a stop button, and run during staffed hours. A game day is a scheduled team exercise to practise failure scenarios.

A simple example

Idempotency is an elevator call button: pressing it five times does not summon five elevators.

System design example

A payment API requires an Idempotency-Key header, stores key plus result under a unique constraint, and returns the stored result for repeats. The client retries with backoff using the same key. Each month a game day kills one payment server during working hours and checks that no payment is lost or doubled.

Summary

  • Idempotent operations are safe to repeat; retries depend on it.
  • Idempotency keys let the server ignore duplicates.
  • Chaos engineering tests resilience with small, controlled failures, like a fire drill.

Architecture Diagram

Safe retries with an idempotency key
Architecture
Clientidempotency key
Payment API
Key storekey -> result

A repeated request with the same key returns the stored result instead of repeating the work.

  1. 1The client sends a request with a unique key.
  2. 2An unknown key means the API does the work and stores the result.
  3. 3A retry with the same key gets the stored result, with no second charge.

Common Interview Questions

Chapter quiz

Resilience and Error Handling · 4 questions

  1. 1.Why must remote calls have timeouts?

  2. 2.What is the purpose of jitter?

  3. 3.What does an open circuit breaker do?

  4. 4.Why use an idempotency key for a payment request?

0 of 4 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course