Set Request Timeouts
Give up on a request when the service behind the API takes too long, instead of keeping the caller waiting.
How it works
The gateway stops waiting for the backend after a set time and answers 504. Without a timeout, a backend that hangs holds connections and callers indefinitely.
Shorter than your callers
Set the request timeout below the timeout of the applications that call the API, so that they get a clear 504 rather than giving up first.
Every parameter and its default: reference.
Use cases
Always, on an API in production: without a timeout, a slow service holds connections until the caller gives up.
Prerequisites
- An API instance that is deployed and answers.
Procedure
- In API Manager, go to API Instances and open the API. To cover everything published on a gateway instead, open the gateway.
- Open its Backend traffic policies and add the rule timeout.
- In Request Timeout, give the longest a whole request may take, such as
30s. - Optionally give a Connect Timeout and an Idle Timeout.
- Deploy.
Parameters
| Field | What it is | Default |
|---|---|---|
| Request Timeout | The longest a whole request may take. | 30s |
| Connect Timeout | The longest the gateway waits to open a connection to the service. | — |
| Idle Timeout | How long a connection may stay idle before it is closed. | — |
Verification
A request that takes too long
curl -i https://orders.example.com/orders/slow
Answer
HTTP/1.1 504 Gateway Timeout
Behavior and limits
- Set the timeout shorter than the one of the caller, so that the gateway answers before the caller gives up.
- A timeout does not stop the work already started in your service: make long operations asynchronous.
See also
- Policies: every rule at a glance
- Retry failed requests