Bitpath

Boss lesson · Unit 04

Charged twice

Retries, idempotency and what a timeout really means

13 exercisesabout 3 min

What you practise

  • A retry after a timeout runs the work twice
  • Idempotent operations, idempotency keys, at-least-once vs at-most-once, making a handler safe to repeat

Try a question from this lesson

Pick one

The reply to a payment request is lost, so the client times out and sends the same request again. The server treats every request as new. What does it do?

client                   server
POST /charge 30  ---->   charges 30
       x  <----------    "done" (lost)
(no reply: timeout)
POST /charge 30  ---->   ?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

A timeout tells you nothing

When a request times out, one of three things happened:

The request never arrived. It arrived, and the work failed. Or the work was done, and only the reply was lost.

The client cannot tell these apart. After a timeout, the honest answer to "did it happen?" is "maybe".

So a retry is only safe if doing the work twice is harmless.

Concept

Idempotent

An operation is idempotent if doing it twice leaves the same result as doing it once.

*Set* the balance to 70: idempotent. Run it five times, the balance is 70.

*Subtract* 30: not idempotent. Every run changes the result.

Reading a page, setting a value and deleting item 42 are all safe to retry. Adding, appending and charging are not.

Concept

How many times

At most once: send, and never retry. Nothing happens twice, but some things never happen.

At least once: retry until the other side confirms. Nothing is lost, but some things happen twice.

Exactly once cannot be promised over a network that loses messages. What you can build is its effect: at-least-once delivery, plus a handler that ignores repeats.

9 more exercises in the app

  • 3× type the answer
  • 2× multiple choice
  • 1× true or false
  • 1× fill the blanks
  • 1× match the pairs
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy