Overview
Tyk Gateway enforces a maximum wait time for upstream responses. When the timeout expires, Tyk Gateway terminates the request and returns504 Gateway Timeout to the client with the response body {"error": "Upstream service reached hard timeout."}. This prevents slow or unresponsive upstream services from blocking connections and degrading overall system performance.
If you are using the Service Discovery option and an enforced timeout is triggered, the service discovery module will refresh the host or host list.
Timeout Levels
Timeouts can be configured at three levels. The most specific level takes precedence:| Level | Tyk OAS | Tyk Classic | Scope |
|---|---|---|---|
| Gateway | proxy_default_timeout in tyk.conf | proxy_default_timeout in tyk.conf | All APIs on the Gateway |
| API (from Tyk Gateway 5.14.0) | upstream.enforceTimeout | global_enforce_timeout in version_data | All endpoints in the API |
| Endpoint | enforceTimeout in operations | hard_timeouts in extended_paths | A specific endpoint |
The Gateway-level
proxy_default_timeout also acts as an upper bound. It runs concurrently with the API- and endpoint-level timeouts, and the request is terminated by whichever expires first. An API- or endpoint-level timeout can therefore only shorten the effective timeout. To set one longer than the Gateway default, increase proxy_default_timeout accordingly.Duration Format
From Tyk Gateway 5.14.0, API-level and endpoint-level timeouts accept human-readable duration strings. Accepted units arems, s, and m as follows:
| Format | Example | Meaning |
|---|---|---|
| Milliseconds | "500ms" | 500 milliseconds |
| Seconds | "1.5s" | 1.5 seconds |
| Minutes | "2m" | 2 minutes |
| Legacy integer | 3 | 3 seconds (Gateway and endpoint level only) |
HTTP Server Write Timeout
Enforced timeouts measure only the time spent waiting for the upstream service to respond.http_server_options.write_timeout in the Gateway configuration (default: 120 seconds) covers the entire request lifecycle: Gateway middleware processing, the upstream wait, and writing the response back to the client. Because it includes Gateway-side processing time on top of the upstream wait, it is always the outer bound on any enforced timeout.
If an enforced timeout is set to a value close to or longer than write_timeout, the HTTP server may close the connection before the enforced timeout fires. To use enforced timeouts longer than 120 seconds, increase http_server_options.write_timeout to at least the same value.
Configuration
Gateway Level
The Gateway-level timeout is configured usingproxy_default_timeout in tyk.conf. It applies to all APIs on the Gateway and accepts an integer value in seconds.
API Level
Available from Tyk Gateway 5.14.0
- Tyk OAS
- Tyk Classic
Configure
upstream.enforceTimeout in the Tyk Vendor Extension:| Field | Description |
|---|---|
enabled | Enable the API-level timeout. |
duration | Timeout duration as a human-readable string (for example "500ms", "1.5s", "30s"). |
Endpoint Level
An endpoint-level timeout applies to a specific path and method, overriding any API-level or Gateway-level timeout for that endpoint.- Tyk OAS
- Tyk Classic
Configure
Tyk Dashboard automatically rounds
enforceTimeout in the operations section of the Tyk Vendor Extension for the appropriate operationId:| Field | Description |
|---|---|
enabled | Enable the timeout for this endpoint. |
duration | Timeout duration as a human-readable string (for example "500ms", "1.5s", "2m"). Added in Tyk Gateway 5.14.0. |
value | Timeout in whole seconds. Deprecated in favor of duration from Tyk Gateway 5.14.0 but remains fully supported. |
duration up to the nearest whole second and stores that in value when the API is saved, ensuring that in a distributed (MDCB) deployment the timeout continues to be enforced on older Data Plane Gateways. Tyk Gateway 5.14.0 or later will ignore value and apply the more granular duration.Configuring the Endpoint-Level Timeout in the API Designer
- Tyk OAS
- Tyk Classic
Step 1: Add an EndpointFrom the API Designer add an endpoint that matches the path and method to which you want to apply the middleware.


Step 2: Select the Enforce Timeout MiddlewareSelect ADD MIDDLEWARE and choose the Enforce Timeout middleware from the Add Middleware screen.
Step 3: Configure the MiddlewareSet the timeout duration that you wish to enforce for requests to the endpoint.
Select ADD MIDDLEWARE to apply the change to the middleware configuration.Step 4: Save the APISelect SAVE API to apply the changes to your API.






