# Scaling

Three ways, which work together. All are under the service's **Scaling**, with the servers it runs on and what it does when nobody uses it: a bigger [size](/docs/usage#sizes) (more memory and cores for each container, given to it as it runs, with no restart), more instances, and more containers under load. Each container is billed at its size while it's awake.

```diagram scaling
Three ways to scale, which work together. A bigger size gives each container more memory and cores, as it runs, with no restart (m to xl, say). More instances run one in each region you pick, each on a server of its own, like web in Europe and in the US; visitors reach the nearest awake one. More containers start under load, up to 20 per instance (#1, #2, #3 on one server), and when its servers are full another server in the region runs it too. Each container is billed at its size while it's awake.
```

## Regions: an instance in each

Under **Scaling → Regions**, pick the [regions](/docs/servers#choosing-regions) it runs in: it runs one instance in each, on a server of its own there (Hobby up to 3, Business up to 10). Visitors reach the nearest awake one; if a server goes down, the others answer. **Anywhere** is one instance, on the server Alf picks. [Where services run](/docs/placement) says how it chooses, and when instances move.

The API and [alf.toml](/docs/alf-toml) can say more: `instances` (several, spread by themselves) or `region_instances` (`{"Europe": 2}`), for more than one in a region.

## Under load: more containers

Each instance of a public service can start more containers when it's busy (**Scaling → Under load**):

- **Up to so many containers per instance** (up to 20).
- **Requests per second per container**: when the rate goes above this, another container starts; the proxy sends each request to the least busy one.

When load stays lower for a minute, extra containers drain (finishing their open requests) and stop. **Scaling → Where it runs** lists each container under its server: its own (#1) and the ones added under load (#2 and up), with how long each has been up, its open requests, memory and CPU, and the requests per second they're for. The **Replicas** chart on its **Overview** shows how many over time. Each is billed at its size while it runs.

### Looking at one container

The **Container** menu at the top of a service's **Overview**, **Logs** and **Terminal** picks one of them (or **Logs** and **Terminal** beside it under **Where it runs**):

- **Overview** shows that container's numbers now, and its charts: the requests it answered, its response times, memory, CPU and traffic, and the time it ran. With all containers the charts are the service's: requests and traffic add up, memory and CPU are per container (the most any one held, what they used on average), as a size limits each.
- **Logs → Live** follows that container's output; **History** shows only what it wrote. With all containers, each line in History says which one wrote it (`#2`).
- **Terminal** opens in that container itself, which isn't stopped while the terminal is open.

Coding agents pass `container` to `get_logs` and `search_logs` the same way.

## When its servers are full: another server in the region

A server starts an extra container only while it has the memory for one. When every server a service is placed on in a region wants more containers than it has memory for, and has for half a minute, another server in that region with room runs it too:

- It's **added under load**: it isn't one of the instances you asked for, and doesn't count against them. **Scaling → Where it runs** shows it with that label under its region.
- Visitors near that region are spread over its servers there, the added one included, so it takes its share at once. Its first container may take a little longer if that server hasn't got the image yet.
- It runs up to the containers left of the service's limit in that region: the service's **containers per instance** for each instance placed there, as it could run if its servers had the room. Another is added half a minute later if the region is still short.
- A minute after the region can carry the load without it, it gets no new visitors, finishes the open requests, and goes.
- It's billed like any container: its size, by the hour it's awake.
- Only ever in a region the service already runs in. A new region is yours to choose: a server across the world would answer slower than one awake nearby.

Services with a volume and private services run one container per instance and are never added to.

Services with a volume and private services run one container per instance; a service with a volume runs one instance, where its data is.

During a [deploy](/docs/deployments), the new version starts as many containers as are running now before it takes over, and with several instances one server switches first.

## Where to look

- **Metrics → Requests per second** and **Replicas** for one service.
- **Scaling → Where it runs** for each server it runs on, by region: where it is, whether it was added under load, how many containers it runs of how many it may, and each of them.
- **Servers** for where each service runs and on how many servers, and whether a server is full right now. See [The servers page](/docs/capacity).
