QA Interview Hub · Knowledge base · Quizzes · Українська

System Design › Scaling and reliability

Idempotency, retries and rate limiting

Idempotency means a repeated call has the same effect on server state as the first one; by RFC 9110 GET, PUT and DELETE are idempotent and POST is not. For payments a POST is made safe by an idempotency key: the server stores the result under the key and returns it on a repeat. Retries with backoff and jitter apply to idempotent operations or keyed requests, while rate limiting answers 429.

System design · Scaling

The client sent a transfer, the response got lost, and it retried. How many times was the money debited?

The network is unreliable. The client sent a POST for a transfer, the server executed it, but the response never arrived. The client does not know what happened and repeats the request. Without protection the money is debited twice. Three mechanisms work together: idempotency makes a repeat safe, retries with backoff make it sensible, rate limiting protects the server from repeats that are too frequent.

How it works and why

Idempotency: a repeated call gives the same result as the first one. By the HTTP specification (RFC 9110) GET, PUT and DELETE are idempotent and POST is not. For payments POST is made idempotent with an idempotency key: the client generates a unique key and passes it in a header (the common name is Idempotency-Key). The server stores the result by key and on a repeat returns the same result without creating a new payment.

POST /transfers
Idempotency-Key: 7c1f2b9e-5d44-4b6a-9f10-3a0d1e2c8b77
Content-Type: application/json

{"from":"acc-1","to":"acc-2","amount":100}

The server ties the key to the request and its result. The same key with the same body: the stored response is returned. The same key with a different body: a client error, not a reason to silently perform a different payment. Hence the set of tests.

Example: a race of two identical requests

The trickiest case: the repeat arrives while the first request is still running. If the check "was this key seen?" and the storing of the result are not atomic, both pass the check and both execute.

const key = crypto.randomUUID();
const send = () =>
  request.post('/transfers', {
    data: { from: 'acc-1', to: 'acc-2', amount: 100 },
    headers: { 'Idempotency-Key': key },
  });

const [a, b] = await Promise.all([send(), send()]);
const ids = new Set([(await a.json()).id, (await b.json()).id]);

expect(ids.size).toBe(1); // one transfer
expect(await balance('acc-1')).toBe(900); // from 1000: one debit

Assumption: the starting balance is 1000. Another acceptable outcome: the second request gets 409 because the first is still running. Then check that there is one debit and that after completion a repeat returns the same transfer. The product-side fix: a unique index on the key or a lock, not a "read first" check in code.

Test cases for the key

  • A repeat with the same key and body: the same transfer, one debit.
  • The same key with a different body: an error, not silent ignoring.
  • Two parallel requests with one key: one transfer.
  • A repeat after the key retention period: behaviour per the service specification.

Retries with backoff and jitter

The client repeats the request with a growing pause (1, 2, 4 seconds) and a random offset so thousands of clients do not retry at the same moment and finish off a server that just recovered. Only idempotent operations, or operations with an idempotency key, may be retried. The number of attempts and the total time should be limited too.

Rate limiting

It limits a client's requests per time, for example 100 per minute. Exceeding it gives 429 Too Many Requests, often with a Retry-After header. Algorithms: token bucket, sliding window. Tests cover the limit boundary (the 100th passes, the 101st does not), window reset, independence of limits of different clients and that on 429 the client waits instead of retrying at once.

Limits and alternatives

An idempotency key only protects against a repeat of the same request: two different requests for the same amount with different keys are both executed. Keys are kept for a limited time, so a repeat after the retention period runs the operation again. A rate-limit counter in one instance's memory does not see requests to the others: behind a load balancer it is kept in a shared store such as Redis.

Common mistakes

  • Retrying POST without a key: each "safe" attempt can create a duplicate.
  • Backoff without jitter: all clients come back at once in waves.
  • Testing the limit with one client and not checking that limits are independent per key or user.

Check yourself

The same key arrived with a different body. What should the server do and why?

Answer

Reject with an error (often 422 or 409): it is either a client bug or a key collision. Quietly returning the old response means the client thinks a different payment was done.

Why are retries without jitter dangerous even with backoff?

Answer

Clients that failed at the same time retry at the same moments and create synchronised waves of load. A random offset spreads them out.

In an interview. Junior: An idempotent operation gives the same result on a repeat. For payments an idempotency key is used so a repeat does not debit twice. Middle/Senior: I test a repeat, the same key with a different body and parallel requests with one key because a race is what breaks a naive implementation. The client retries with exponential backoff and jitter only idempotent operations (GET, PUT, DELETE) or POST with a key, and I check rate limiting at the limit boundary and the client's reaction to 429 and Retry-After.

More articles in this chapter