Configure a timeout policy

Updated at:

A timeout defines how long the Envoy sidecar proxy waits for a response from an upstream service before terminating the request. Without a timeout, a slow or unresponsive service can hold connections open indefinitely, consuming resources and degrading mesh reliability.

The following walkthrough configures a 2-second timeout on the HTTPBin service and verifies it with controlled delays.

Prerequisites

Before you begin, make sure that you have:

  • The HTTPBin and sleep services deployed in your cluster. For details, see Preparations

Step 1: Verify that services respond without a timeout

Confirm that both services handle delayed responses normally before you apply a timeout policy.

  1. Use kubectl to connect to the Container Service for Kubernetes (ACK) cluster based on the information in the kubeconfig file, and open a shell in the sleep pod:

    kubectl exec -it deploy/sleep -- sh
  2. Send a request with a 1-second delay:

    curl -I httpbin:8000/delay/1

    Expected output:

    HTTP/1.1 200 OK
    server: envoy
    date: Fri, 11 Aug 2023 09:57:36 GMT
    content-type: application/json
    content-length: 481
    access-control-allow-origin: *
    access-control-allow-credentials: true
    x-envoy-upstream-service-time: 1006

    The x-envoy-upstream-service-time: 1006 header confirms that Envoy waited approximately 1 second for the response, and the request succeeded.

  3. Send a request with a 2-second delay:

    curl -I httpbin:8000/delay/2

    Expected output:

    HTTP/1.1 200 OK
    server: envoy
    date: Fri, 11 Aug 2023 09:57:26 GMT
    content-type: application/json
    content-length: 481
    access-control-allow-origin: *
    access-control-allow-credentials: true
    x-envoy-upstream-service-time: 2004

    Both requests return HTTP 200, which confirms that no timeout is enforced.

Step 2: Apply a 2-second timeout through a VirtualService

Create a VirtualService that routes traffic to HTTPBin with a timeout of 2s. For more information about managing virtual services, see Manage virtual services.

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: httpbin-vs
  namespace: default
spec:
  hosts:
    - httpbin
  http:
    - route:
        - destination:
            host: httpbin
      timeout: 2s

The timeout: 2s field tells Envoy to wait no more than 2 seconds for a response from the HTTPBin service on this route. Requests that take longer are terminated with an HTTP 504 status code.

Step 3: Verify that the timeout policy takes effect

  1. Use kubectl to connect to the ACK cluster based on the information in the kubeconfig file, and open a shell in the sleep pod:

    kubectl exec -it deploy/sleep -- sh
  2. Send a request with a 1-second delay (within the timeout):

    curl -I httpbin:8000/delay/1

    Expected output:

    HTTP/1.1 200 OK
    server: envoy
    date: Fri, 11 Aug 2023 09:56:30 GMT
    content-type: application/json
    content-length: 481
    access-control-allow-origin: *
    access-control-allow-credentials: true
    x-envoy-upstream-service-time: 1005

    The 1-second delay is within the 2-second timeout, so the request succeeds.

  3. Send a request with a 2-second delay (at the timeout boundary):

    curl -I httpbin:8000/delay/2

    Expected output:

    HTTP/1.1 504 Gateway Timeout
    content-length: 24
    content-type: text/plain
    date: Fri, 11 Aug 2023 09:56:35 GMT
    server: envoy

    Envoy returns HTTP 504 Gateway Timeout because the HTTPBin service could not respond within the 2-second limit.

What happened

The cause-and-effect chain in this exercise:

  1. Without a timeout (Step 1): Both the 1-second and 2-second delayed requests completed successfully because Envoy had no upper bound on response time.

  2. With a 2-second timeout (Steps 2--3): The 1-second request still succeeded because it finished before the deadline. The 2-second request failed because the actual response time (slightly over 2 seconds due to processing overhead) exceeded the configured limit, triggering a 504 Gateway Timeout.

This pattern applies directly to production services. Set the timeout based on the expected response time of your upstream service -- long enough to accommodate normal variance, but short enough to fail fast when a service is genuinely unresponsive.