← All articles
technologysoftwarenetworksreliabilitySeptember 17, 20263 min read

Did the Payment Go Through? Send It Again and Find Out Safely

By the BrainSnail editorial team. How these articles are written and checked, and how to tell us when one is wrong.

An operation that produces the same result whether it runs once or five times is the property that makes it safe to retry a request you never got an answer to.

The property being described

An operation has this property when performing it repeatedly leaves the system in the same state as performing it once. Setting a value to seven qualifies, since setting it to seven again changes nothing. Adding seven to a value does not, since each repetition moves it further. Deleting a specific record qualifies, because the second attempt finds nothing to delete and the end state matches. The property is about the resulting state rather than about the response, so a repeat may legitimately report that there was nothing to do.

Why networks make it essential

The fundamental problem is that a lost reply is indistinguishable from a lost request:

  • A client sends a request and receives no answer
  • The request may never have arrived
  • Or it may have been processed and the reply lost on the way back
  • The client cannot tell which from where it is standing
  • If the operation is safe to repeat, it simply retries
  • If it is not, retrying may charge a customer twice

How unsafe operations are made safe

Many genuinely useful operations are not naturally repeatable, and the standard fix is to make the server recognise a repeat. The client generates a unique key for the intended action and sends it with every attempt. The server records the key along with the result the first time, and on seeing the same key again returns the stored result without performing the work a second time. Payment systems work exactly this way, which is why an interface asking you to confirm a transfer usually creates such a key before the confirmation, not after it.

Which requests are safe by convention

The standard vocabulary of the web assigns the property to particular request types by convention, and knowing which is which prevents a great deal of trouble. Fetching a resource changes nothing and is safe to repeat indefinitely, which is why browsers and caches retry it without asking. Replacing a resource wholesale with a supplied version qualifies, since sending the same version twice ends the same way. Removing a resource qualifies. Creating a new resource with a server-assigned identifier does not, because each attempt creates another, which is exactly the operation that needs a client-supplied key.

Where the assumption breaks

Several traps catch people who assume an operation is safe when it is not. An operation may be repeatable in its main effect and not in its side effects, so setting a record again is harmless while the email it triggers is sent twice. Stored keys must expire eventually or the store grows without limit, and a retry arriving after expiry is treated as new. Two different requests carrying the same key by accident cause serious confusion. And an operation that is safe on its own may not be when interleaved with a competing one, which is a separate problem requiring separate machinery.

The takeaway

An operation whose repetition leaves the same state can be retried freely, which matters because a client cannot distinguish a lost request from a lost reply. Operations that lack the property are given it by sending a unique key that the server records with its result and recognises on repeats. Side effects, key expiry and competing requests are where the assumption breaks.

Practise this

Questions from Networks and the Internet

Reading about something is not the same as being able to recall it. These are real questions from the Networks and the Internet unit in our Technology track, answers and explanations included. The unit has 120 in total across 23 steps.

  • Multiple choiceLevel 1

    1. What does an IP address do?

    • Identifies a device on a networkcorrect
    • Charges the device's battery
    • Cleans the computer screen
    • Makes the internet louder

    An IP address is a number that identifies a device on a network so data reaches the right place.

  • Choose all that applyLevel 4

    2. Which of these are services or protocols that run on the internet? Choose all that apply.

    • The World Wide Web (HTTP)correct
    • Email (SMTP)correct
    • File transfer (FTP)correct
    • A printed newspaper
    • A handwritten letter

    The web, email and file transfer all run on the internet, but printed papers and letters do not.

  • Fill the blankLevel 2

    3. Wifi connects devices ____, using radio waves instead of cables.

    • wirelesslycorrect
    • loudly
    • slowly
    • secretly

    Wifi is a wireless connection, so it links devices wirelessly.