# Replicas

Source: https://openpanel.sh/docs/load-balancing/replicas

How many replicas a service runs, which servers they are placed on, and how a deploy replaces them.

A replica is one running copy of a service: one container on one of your servers. A service runs 1 to 100 replicas. More than one needs the [load balancer](/docs/load-balancing) on a public service, and is not allowed on a private service or one with a volume.

## Where replicas are placed [#where-replicas-are-placed]

You choose the servers a service may run on. At each deploy its replicas are spread over them in turn, in a fixed order (by the servers' ids), so the same settings always place the same way:

| Servers chosen | Replicas | Placed |
|---|---|---|
| 1 | 3 | All three on that server |
| 2 | 3 | Two on the first server, one on the second |
| 3 | 3 | One on each |

A server that has been disconnected is skipped. A service with a volume runs on its volume's server only; see [Volumes](/docs/volumes#where-its-data-lives).

<Callout type="info" title="A service built from Git on several servers">
A Git service placed on more than one server is built once, pushed to your organization's own registry, and pulled from it by every server it is placed on. The registry travels over the private network, so every one of those servers must be in it: OpenPanel refuses such a deploy while one of them is not, for example a server added with One command. A service that runs a Docker image is pulled from its registry on every server and can span any of them.
</Callout>

## Change how many [#change-how-many]

<Tabs items={["CLI","API"]}>
<Tab value="CLI">
```sh
openpanel services update web --project shop --replicas 4 --server eu-1 --server eu-2
openpanel deploy web --project shop
```

`--server` replaces the list of servers: name every server the service may use.
</Tab>

<Tab value="API">
```sh
curl -X PATCH https://openpanel.sh/api/v1/services/$SERVICE_ID \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"replicas": 4, "serverIds": ["'"$SERVER_1"'", "'"$SERVER_2"'"]}'
```
</Tab>
</Tabs>

The new count takes effect at the next deploy.

## How a deploy replaces them [#how-a-deploy-replaces-them]

On each server, the OpenPanel agent rolls its own replicas:

<Steps>
<Step>
### New replicas start beside the old ones [#new-replicas-start-beside-the-old-ones]

The old replicas keep serving.
</Step>

<Step>
### The edge moves to the new ones [#the-edge-moves-to-the-new-ones]

From the moment a new replica is ready, the edge sends traffic only to new replicas.
</Step>

<Step>
### The old ones stop [#the-old-ones-stop]

Once every new replica on that server is ready, the agent waits 5 seconds for requests in flight to finish, then stops the old ones.
</Step>
</Steps>

If a new replica exits or fails its health check, the old ones keep serving until OpenPanel marks the deployment failed and gives the service back to the deployment before it. See [Health checks](/docs/projects/health-checks).

A service with a volume is the exception: its one replica stops before the new one starts.

## Reaching a replica [#reaching-a-replica]

From outside, a public service's replicas are reached only through the edge, never directly: your servers publish no container port. Inside a project, other services on the same server reach the service by its name.

<Cards>
<Card href="/docs/load-balancing" title="Load balancer on or off" description="Which services can run several replicas." />

<Card href="/docs/projects/settings" title="Service settings" description="Servers, resources and the rest." />
</Cards>