Overview
To ensure that API requests are handled safely without accidental duplication, Zenskar supports idempotency. This is especially useful in scenarios where a network interruption occurs, and you are unsure if your request reached our servers. By retrying the request with the same idempotency key, you guarantee that the operation is performed only once.Currently, idempotency is supported exclusively for Usage Event APIs. Support for other Zenskar APIs will be rolled out in a phased manner.
How idempotency works
To make an idempotent request, include a unique key in theIdempotency-Key header of your HTTP request.
- First Request: Zenskar processes the request normally and saves the resulting status code and response body.
- Subsequent Requests: If you send another request with the same
Idempotency-Key, Zenskar skips processing and returns the cached response from the first successful execution.
Key lifecycle and TTL
- TTL (time to live): Idempotency keys are stored for 24 hours. After 24 hours, a request with the same key will be treated as a new request.
- Scope: Keys are scoped to your Organization. You cannot reuse a key across different organizations, but you should ensure uniqueness within your own environment.
- Payload consistency: If you retry a request with the same key but change the request body, Zenskar will return an error to prevent accidental data mismatches.
Workflow diagram
The following diagram illustrates how Zenskar handles requests containing an idempotency key:Example request
To report usage idempotently, add theIdempotency-Key header to your request. We recommend using a V4 UUID or another high-entropy string.
Error handling and retries
Idempotency is a critical tool for building resilient integrations. Here is how to handle common scenarios:Network timeouts
If a request times out or the connection is dropped before you receive a response, you can safely retry the exact same request with the sameIdempotency-Key. If Zenskar received the first request, you will get the cached result. If we didn’t, we will process it for the first time.
4xx and 5xx Errors
- 4xx Errors: If a request fails with a validation error (e.g.,
400 Bad Request), the idempotency key is generally not cached. You should fix the request payload and you can choose to use a new key or the same one depending on the fix. - 5xx Errors: If you receive a
500 Internal Server Erroror503 Service Unavailable, Zenskar does not cache the result. You should retry these requests with the same idempotency key.