Idempotency in API Design: Webhooks and Practical Examples
Idempotency ensures that repeated API calls produce the same result, which is especially critical for payments, orders, and webhooks.
Quick Overview
Idempotency is a property of operations that can be executed multiple times without changing the outcome. In API design, it’s essential for building reliable and fault-tolerant interfaces. GET, PUT, and DELETE are idempotent by nature, whereas POST typically isn’t. For non-idempotent operations like POST or external calls such as webhooks, Idempotency Keys prevent duplicate execution. Webhooks should also be designed to handle idempotent processing, allowing receivers to handle the same event multiple times without duplicating data. A solid idempotency strategy significantly reduces errors from network failures, retries, and race conditions, greatly improving API reliability.
Key Components
Idempotent HTTP Methods
GET is idempotent because it doesn’t change state. PUT is idempotent because it fully replaces a resource, regardless of how many times it’s called. DELETE is idempotent because once the resource is gone, further deletion attempts produce the same final state. POST is not idempotent because each call can create a new resource.
Idempotency Keys
An Idempotency Key is a unique value the client includes with a request. The server stores the key along with the result of the first request. On a subsequent call with the same key, the server returns the cached result instead of re-executing the operation. This is particularly important for payments, bookings, and orders.
Guaranteed Idempotency for Payments
In payment APIs, a network error might cause the client to retry. Without an Idempotency Key, the payment could be processed twice. With one in place, the server ensures the payment is only processed once, even if the client makes multiple requests.
Webhooks and Idempotency
Webhooks are server-initiated events sent to the client. They can arrive multiple times due to timeouts or retries. The receiver should handle webhooks idempotently by tracking each event via a unique Event ID and discarding or ignoring duplicates.
Retry Strategies and Idempotency
Clients should only automatically retry idempotent operations. GET and PUT can be safely retried on network failures. POST should only be retried if an Idempotency Key is used. Otherwise, unwanted duplicate charges can occur.
Storing Idempotency Keys
Idempotency Keys must be stored server-side for a defined period. Storage can be a database, cache, or dedicated service. What matters is that keys are linked to operation results and can’t be reused for different requests.
TTL and Retention
Idempotency Keys should have a defined lifespan. After the TTL expires, the key can be deleted. The client must know how long a key remains valid so it doesn’t try to reuse it too late. Typical TTLs range from minutes to days.
Avoiding Race Conditions
If two requests with the same Idempotency Key arrive simultaneously, the server must ensure only one operation executes. Locking mechanisms or atomic database operations prevent race conditions.
Error Handling with Idempotency Keys
If an Idempotency Key is invalid or expired, the API should return a clear error message. The client can then decide whether to retry the operation with a new key or resend the original request.
Monitoring and Logging
Idempotency Keys and retried requests should be logged. This helps with debugging and reveals patterns like frequent timeouts or duplicate sends. Monitoring can alert you to problems early.
Practical Example
An online shop offers an API for creating orders. The client wants to ensure an order isn’t accidentally created twice.
Request with Idempotency Key:
POST /api/v1/orders
Content-Type: application/json
Idempotency-Key: 7c9e6679-7425-40de-944b-e07fc1f90ae7
{
"customerId": 123,
"items": [
{ "productId": 42, "quantity": 2 }
]
}
First execution:
HTTP/1.1 201 Created
Location: /api/v1/orders/98765
{
"orderId": 98765,
"status": "created",
"total": 79.97
}
Repeated request with the same key:
POST /api/v1/orders
Content-Type: application/json
Idempotency-Key: 7c9e6679-7425-40de-944b-e07fc1f90ae7
{
"customerId": 123,
"items": [
{ "productId": 42, "quantity": 2 }
]
}
The server recognizes the key and returns the same result:
HTTP/1.1 201 Created
Location: /api/v1/orders/98765
{
"orderId": 98765,
"status": "created",
"total": 79.97
}
Webhook with Event ID:
POST /webhooks/orders
Content-Type: application/json
X-Event-ID: event-12345-abcde
{
"eventType": "order.created",
"orderId": 98765,
"status": "created"
}
The receiver stores the X-Event-ID and processes matching IDs only once. This keeps processing idempotent, even when the webhook is sent multiple times.
FAQ: Idempotency in API Design
1. What is idempotency?
2. Which HTTP methods are idempotent?
3. What is an Idempotency Key?
4. Why is idempotency important for payments?
5. What is a webhook?
6. Why should webhooks be idempotent?
7. How do you detect duplicate webhooks?
8. What happens when simultaneous requests arrive with the same Idempotency Key?
9. How long should an Idempotency Key be stored?
10. Can PATCH be idempotent?
11. What happens with an invalid Idempotency Key?
12. Should Idempotency Keys be used for all POST requests?
13. What’s the difference between idempotency and safe methods?
14. How do you test idempotency?
15. How does idempotency relate to retry strategies?
Next in the API Learning Path
The next article in the API learning path covers Webhook Fundamentals — how webhooks work, how to implement them securely, and what to keep in mind when verifying signatures.
References
- https://www.rfc-editor.org/rfc/rfc9110
- https://stripe.com/docs/api/idempotent_requests
- https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key
Recommended Books on API Development
If you’d like to dive deeper into idempotency, API design, and software architecture, here are some books we recommend:
API Development
Books about API design, REST, GraphQL, OpenAPI and API architecture
Designing Data-Intensive Applications von Martin Kleppmann
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
API Design Patterns von JJ Geewax
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.




