---
title: Keep-alive settings
summary: null
url: >-
  https://www.fastly.com/documentation/guides/getting-started/hosts/keep-alive-settings
---

Backend keep-alive settings control how long Fastly maintains persistent connections to your origin servers. Properly configured keep-alive settings can improve performance by reusing connections, reducing latency, and avoiding repeated connection establishment.

## About keep-alive connections

Keep-alive connections allow multiple HTTP requests to be sent over a single TCP connection, rather than opening a new connection for each request. Fastly supports two types of keep-alive settings:

- **TCP keep-alive:** Maintains the underlying TCP connection by sending periodic probes to detect dead connections and prevent intermediate network devices from closing idle connections.
- **HTTP keep-alive:** Controls how long Fastly keeps a persistent HTTP connection open to your backend between requests.

By default, Fastly uses keep-alive connections when possible. You may need to adjust these settings based on your origin server's configuration, network topology, or specific performance requirements.

## Keep-alive settings

The following keep-alive settings can be configured for each backend in your Fastly service:

| Setting                  | Type    | Description                                                                                                                                              |
| ------------------------ | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tcp_keepalive_enable`   | Boolean | Whether to enable TCP keep-alive for backend connections. Fastly defaults to using keep-alive if this is unspecified.                                    |
| `tcp_keepalive_time`     | Integer | Interval in seconds between the last data packet sent and the first keep-alive probe.                                                                    |
| `tcp_keepalive_interval` | Integer | Interval in seconds between subsequent keep-alive probes.                                                                                                |
| `tcp_keepalive_probes`   | Integer | Number of unacknowledged probes to send before considering the connection dead.                                                                          |
| `keepalive_time`         | Integer | How long in seconds to keep a persistent connection to the backend between HTTP requests. By default, Fastly keeps connections open as long as possible. |

## When to adjust keep-alive settings

You may need to modify keep-alive settings in the following scenarios:

- Your origin server has specific timeout requirements that are different than Fastly's defaults
- You're experiencing connection timeouts or connection reset errors
- Your backend uses load balancers or firewalls with aggressive connection timeout policies

For example, if your origin closes idle connections after 60 seconds but Fastly attempts to reuse connections after 65 seconds, requests may fail. Setting `keepalive_time` to 55 seconds ensures Fastly closes connections before your origin does.

## Configuring keep-alive settings

Keep-alive settings are configured at the backend level and apply to Deliver and Compute services. You can configure these settings using the Fastly API, CLI, VCL, and Compute SDKs.

### API

You can view and modify keep-alive settings using the [Backend API endpoint](https://www.fastly.com/documentation/reference/api/services/backend/). You can include the following keep-alive parameters in your API requests when creating or updating a backend:

```json
{
  "name": "my-backend",
  "address": "origin.example.com",
  "keepalive_time": 55,
  "tcp_keepalive_enable": true,
  "tcp_keepalive_time": 30,
  "tcp_keepalive_interval": 10,
  "tcp_keepalive_probes": 3
}
```

Use a `PUT` request to the `/service/{service_id}/version/{version_id}/backend/{backend_name}` endpoint to update an existing backend with these settings.

### CLI

You can update keep-alive settings using the [Fastly CLI](https://www.fastly.com/documentation/reference/cli/backend/update/) `fastly backend update` command:

```term copy
$ fastly backend update --version=latest --autoclone --http-ka-time=55 --name=my-backend
```

Replace `my-backend` with your backend's name and adjust the `--http-ka-time` value as needed. You can also use the following flags:

- `--tcp-ka-enabled` to enable TCP keep-alive
- `--tcp-ka-time` to set the TCP keep-alive time
- `--tcp-ka-interval` to set the TCP keep-alive interval
- `--tcp-ka-probes` to set the number of TCP keep-alive probes

For example, to configure all TCP keep-alive settings:

```term copy
$ fastly backend update --version=latest --autoclone --name=my-backend --tcp-ka-enabled=true --tcp-ka-time=30 --tcp-ka-interval=10 --tcp-ka-probes=3 --http-ka-time=55
```

Refer to the [Fastly CLI documentation](https://www.fastly.com/documentation/reference/cli/) for more information.

### VCL

For Deliver services, you can set keep-alive values using custom VCL by defining [backend properties](https://www.fastly.com/documentation/reference/vcl/declarations/backend/):

```vcl
backend my_backend {
  .host = "origin.example.com";
  .port = "443";
  
  .between_bytes_timeout = 10s;
  .connect_timeout = 1s;
  .first_byte_timeout = 15s;
  
  .keepalive_time = 55s;
  
  .tcp_keepalive_enable = true;
  .tcp_keepalive_time = 30s;
  .tcp_keepalive_interval = 10s;
  .tcp_keepalive_probes = 3;
}
```

Note that time values in VCL use time suffixes (e.g., `s` for seconds).

### Compute

For Compute services, you can configure keep-alive settings when creating dynamic backends programmatically. The specific implementation depends on your application's language SDK. Refer to the [SDK documentation](https://www.fastly.com/documentation/reference/compute/) for your language.

## Troubleshooting connection issues

If you're experiencing connection timeouts or errors, consider the following:

- **Origin timeout errors:** If requests fail with timeout errors, your origin may be closing connections before Fastly expects. Try reducing the `keepalive_time` value to be shorter than your origin's timeout.
- **Connection resets:** If you see connection reset errors, verify that your origin supports keep-alive connections and that any intermediate load balancers or firewalls aren't closing idle connections.
- **Performance issues:** If you notice degraded performance, ensure that your `keepalive_time` is long enough to allow connection reuse for multiple requests, but not so long that stale connections accumulate.

When adjusting keep-alive settings, make small incremental changes and monitor your service's performance and error rates to find the optimal configuration for your specific use case.

## Related content

- [Working with hosts](https://www.fastly.com/documentation/guides/getting-started/hosts/working-with-hosts)
- [Fastly CLI documentation](https://www.fastly.com/documentation/reference/cli/)
- [Backend API reference](https://www.fastly.com/documentation/reference/api/services/backend/)
