Chapters & contents Lesson 9 of 20
3 min read
System design fundamentalsExpanded reading

Timeouts, retries, and idempotency

Recover from uncertain network outcomes without multiplying side effects.

What you’ll learn

  • Explain why a timeout does not prove an operation failed
  • Bound retries
  • Design a duplicate-safe operation

A missing response is ambiguous

Imagine a client asking an account service to mark a lesson complete. The server may save the change and then lose the connection before the response reaches the client. From the client's perspective, the request timed out. That observation does not prove the server did nothing. Distributed systems regularly face this kind of uncertainty because a request, a response, or an intermediate component can fail independently. The retry design must account for an operation that may already have succeeded.

Use timeouts as a resource decision

A timeout limits how long a caller waits for a particular operation. Choose it in the context of the user's action and the behavior of downstream dependencies. An unlimited wait can consume threads, connections, or memory long after the result is useful. A timeout that is too short can produce unnecessary failures and retries during ordinary variation. Measure latency distributions, include connection setup where relevant, and budget time across the complete request rather than giving every dependency the entire user-visible deadline.

Bound and space retries

Retrying immediately and indefinitely can turn a small outage into a much larger load spike. Use a limited attempt count or deadline, increase delays when appropriate, and introduce jitter so many clients do not retry at the same instant. Retry only failures that may be transient and only when the operation's semantics permit it. A validation error is usually not fixed by repeating the same input. Communicate when the system has stopped trying and provide an understandable next step.

Design the state change to tolerate repetition

An idempotent operation has the same intended effect when repeated as when performed once. Setting a lesson's completed state to true can be naturally duplicate-safe if the stored model prevents repeated rows for the same learner and lesson. In contrast, incrementing a completion counter every time a request arrives can overcount retries. For operations that are not naturally idempotent, a scoped idempotency key and a durable record of its result can help, provided concurrent requests are coordinated correctly.

Test the uncertain case

Test more than an ordinary success and an obvious server failure. Simulate a successful write whose response is lost, then repeat the request. Send the same operation concurrently and verify the final state. Decide how long an idempotency record remains valid and what happens if the same key is reused with a different request body. Logging a request identifier helps investigation, but a log entry alone does not enforce duplicate safety. The guarantee must come from the state model and update mechanism.

Try it yourself

A client retries an enrollment request after a timeout. How can the service avoid creating two enrollments?

Show a possible solution

Define enrollment uniqueness for a learner-course pair and enforce it atomically in storage. Return the existing enrollment for a repeated create intent, or use an appropriately scoped idempotency key whose result is stored durably. Handle concurrent requests through the storage constraint rather than a separate unprotected read-then-write check.

Check your understanding

Try 14 questions across system design fundamentals. Read each explanation and revisit anything that needs another pass.

Practise this subject

Go to the source

Use these primary references to check details and explore the topic further.

Saved in this browser. Signed-in progress syncs to your account.