DEV Community

Nwajichukwu Goodness
Nwajichukwu Goodness

Posted on

What Happens When an API Request Times Out After the Server Has Already Processed It?

Imagine your application sends an API request to create an order.

The server receives the request, creates the order and commits the operation to its database.

But before the response reaches your application, the network connection times out.

Your application receives a timeout error.

What should it do?

Retry the request?

Wait for the original operation?

Check whether the order already exists?

This is a common problem in distributed application design: the client knows that it did not receive a response, but it does not necessarily know whether the server completed the operation.

Understanding this situation is important when building payment integrations, order processing systems, resource creation workflows and other applications that depend on state-changing API requests.

  1. A timeout creates an uncertain outcome

Consider this request lifecycle:

  1. The client sends a POST request.
  2. The server receives and processes it.
  3. The server commits the operation.
  4. The server prepares a response.
  5. The network connection fails before the client receives that response.

From the client's perspective, the request failed.

From the server's perspective, the operation may have succeeded.

These are two different outcomes.

A timeout tells the client that it did not receive a response within the expected period. It does not establish that the server rolled back its work.

This creates an uncertain operation state.

The client cannot safely conclude that the resource was never created simply because its HTTP request timed out.

  1. Why immediately retrying can create duplicates

Suppose an application creates an order using:

POST /api/orders
Content-Type: application/json

{
"customer_id": "cus_123",
"product_id": "prod_456",
"quantity": 1
}

The server processes the request, creates order "ord_001", but the response never reaches the client.

The application retries the same request.

If the API treats the second request as an entirely new operation, it could create another order.

The result might look like this:

Request| Server outcome
First POST| Creates "ord_001"
First response| Lost
Second POST| Creates "ord_002"
Second response| Received

The client intended to create one order but now has two.

This is why retry logic cannot be designed around network errors alone.

It must also consider the effect of repeating the operation.

  1. HTTP idempotency and why it matters

HTTP defines an important property called idempotency.

According to RFC 9110, an operation is idempotent when multiple identical requests have the same intended effect on the server as a single request.

For example:

  • GET is idempotent.
  • PUT is idempotent.
  • DELETE is idempotent.

POST is not inherently idempotent.

This distinction matters because HTTP's specification allows clients to retry idempotent requests after communication failures, even when the original request might have succeeded.

For non-idempotent requests, automatic retries require additional safeguards or a way to establish that the original operation was not applied.

An HTTP method alone does not tell you everything about an API's retry behaviour. The endpoint's actual semantics also matter.

Reference: https://www.rfc-editor.org/rfc/rfc9110.html

  1. Using idempotency keys for state-changing requests

One common solution is an idempotency key.

An idempotency key is a unique identifier associated with a particular logical operation.

Instead of treating every incoming request as a new operation, an API that supports idempotency can use the key to recognize repeated attempts.

Stripe provides a practical example.

When creating or updating an object through its API, a client can supply an "Idempotency-Key" header.

For example:

curl https://api.stripe.com/v1/customers \
-u sk_test_...: \
-H "Idempotency-Key: 7f8c2a90-1234-4567-8901-123456789abc" \
-d description="Customer account"

If the connection fails, the client can retry the same logical request using the same key.

Stripe documents that it saves the first request's status code and response body after endpoint execution begins. Subsequent requests using the same key return the saved result.

This includes cached "500" responses in API v1.

There are also important details:

  • Keys can be up to 255 characters.
  • Stripe recommends sufficiently random keys, such as UUID v4 values.
  • Reusing a key with different parameters results in an error.
  • API v1 can automatically remove keys after they are at least 24 hours old.
  • Validation failures and certain concurrent request conflicts are not saved as completed idempotent results.

Therefore, idempotency is not simply adding a random header to every request. The client must understand the API provider's actual implementation.

Documentation: https://docs.stripe.com/api/idempotent_requests

  1. The retry strategy still matters

Idempotency helps prevent duplicate operations, but it does not mean applications should retry indefinitely.

Retry behaviour should account for:

Transient failures

Temporary network interruptions, throttling and service unavailability may justify another attempt.

Non-transient failures

Invalid parameters, authentication failures and other permanent errors generally require correction rather than repeated requests.

Retry limits

Applications should define a maximum number of attempts.

Backoff

Exponential backoff increases the waiting period between attempts, reducing unnecessary pressure on an unavailable service.

AWS documents this approach in its retry-with-backoff guidance and recommends designing operations to be idempotent when using retries.

Reference: https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/retry-backoff.html

  1. What if the API does not support idempotency keys?

Not every API provides an idempotency mechanism.

In that situation, developers need another way to determine the outcome of an uncertain operation.

Depending on the API, possible approaches include:

  • Querying the resource using a client-generated reference.
  • Retrieving the operation status through a separate endpoint.
  • Checking an existing transaction or order record.
  • Using webhooks to receive asynchronous operation updates.
  • Reconciling the result against application-owned records.

For example, an order service might accept a unique external reference supplied by the client.

If the initial request times out, the application can query that reference before deciding whether another creation attempt is appropriate.

The exact approach depends on the API's capabilities.

If there is no reliable way to identify or retrieve the original operation, the client may not be able to establish its outcome immediately.

  1. Designing for uncertain outcomes

A robust API integration should account for the possibility that the operation succeeds while its response is lost.

A useful implementation checklist includes:

Area| What to consider
Request identity| Can repeated attempts be associated with the same logical operation?
Idempotency| Does the API support idempotency keys or equivalent safeguards?
Retry policy| Which failures justify another attempt?
Backoff| How long should the client wait between attempts?
Operation status| Can the client retrieve the original operation's outcome?
Reconciliation| How does the application resolve uncertain results?
Persistence| Is the operation recorded independently of the HTTP response?

These decisions become particularly important when an API operation creates financial transactions, orders, subscriptions or other resources that should not be duplicated.

Conclusion

An API timeout is not always an indication that the operation failed.

Sometimes the server has already completed the work, and the client simply never received confirmation.

This makes retry behaviour an important part of application design.

HTTP idempotency, API-supported idempotency keys, controlled retries and operation-status checks provide different ways to handle this uncertainty.

The key engineering lesson is to treat the outcome of an interrupted request as potentially unknown until the application has enough information to establish what actually happened.

A reliable integration must account for both the request and the possibility that its response never arrives.

Further reading

Top comments (1)

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Good framing: a timeout tells you nothing about what happened server-side.