# Change a health check or sticky sessions

Change how a service checks its targets, or pin each client to one target with a cookie.

Only the health check and sticky sessions of a service can change. Protocol, ports and
certificate cannot: remove the service and add it again.

## Prerequisites

- Permission for `lb:UpdateService`.

## Change the settings

Only the fields you send change; the rules across fields are checked against the service as it
then is.

**Console:** not available. The console cannot change a service; use the CLI, API or Terraform.
**CLI**
```sh
dcloud lb services update lb-id service-id \
  --health-check-interval 3 --health-check-timeout 2 --health-check-retries 1
```

Sticky sessions on an http or https service of an `lb11`–`lb31` or `application` load balancer
(a `dedicated` one accepts the setting but does not apply it):

```sh
dcloud lb services update lb-id service-id --sticky-sessions \
  --sticky-cookie-lifetime 3600
```

**API**
```http
PATCH /lb/api/load-balancers/lb-id/services/service-id
{ "healthCheckInterval": 3, "healthCheckTimeout": 2, "healthCheckRetries": 1 }
```

**Terraform**
Change `health_check_*` or `sticky_*` on the `dcloud_lb_service` and apply: these change in
place.

| Field                  | Values                                     | Default |
| ---------------------- | ------------------------------------------ | ------- |
| `healthCheckProtocol`  | `http` or `tcp`                            | `http`  |
| `healthCheckPath`      | starts with `/`, at most 256 characters    | `/`     |
| `healthCheckInterval`  | 1–60 s; at least 3 on `lb11`–`lb31`        | 15      |
| `healthCheckTimeout`   | 1–60 s, at most the interval               | 10      |
| `healthCheckRetries`   | 0–5                                        | 3       |
| `stickySessions`       | `true` or `false`; `http` and `https` only | `false` |
| `stickyCookieName`     | letters, digits, `-` and `_`, at most 64   | `EWSLB` |
| `stickyCookieLifetime` | 60–604800 seconds                          | 300     |

With sticky sessions, the load balancer sets the cookie on a client's first response and sends
the client's later requests carrying it to the same target.

## Result

`200` with the service as now set. On `application` and `dedicated` the fleet picks up the change
within seconds. On `lb11`–`lb31` it is sent to the provider in the background, and a refusal
there is recorded as an event on the load balancer; an `https` service still waiting for its
certificate gets the settings when the certificate is served.

## When it is refused

- `400` `Nothing to change: send at least one field`.
- `400` `healthCheckInterval must be at least 3s on …` for an interval under 3 seconds on
  `lb11`–`lb31`.
- `400` `healthCheckTimeout (…s) must not exceed healthCheckInterval (…s)`.
- `400` `Sticky sessions work on http and https services only: a tcp service cannot set a cookie`.
- `404` `Service not found`.
- `409` for a load balancer another service owns.

## Related

- [Health checks](health-checks.md)
- [Add or remove a service](add-or-remove-a-service.md)
