# A load balancer in front

A load balancer takes the connections of all users at one address and hands each to one of several servers. It leaves out a server that does not answer. With it, all servers of a [cluster](High-Availability.md) work at the same time, and the loss of one is the loss of a share of the capacity and nothing more.

Any balancer that passes TCP can stand in front of CrushMFT: a hardware appliance, a cloud's own, HAProxy, nginx. CrushMFT has one of its own, made for it: [CrushBalance](../CrushBalance/Main.md), with its own wiki.

## What it balances

Either the servers themselves, or the [DMZ servers](DMZ.md) in front of them. The rules are the same.

```
                 +----------------+
  users  ----->  | load balancer  |
                 +---+--------+---+
                     |        |
              +------v-+   +--v-----+
              | DMZ 1  |   | DMZ 2  |      or the servers directly
              +--------+   +--------+
```

## The settings that matter

### Pass connections through, do not open them

Let the balancer pass each connection on as it is (TCP, "layer 4"), and let CrushMFT answer TLS and SSH itself. Then:

- SFTP and FTPS work, which a balancer cannot look into anyway;
- the certificate, the ciphers and the client certificates are the server's business, set in one place;
- nothing is decrypted on the way.

A balancer that ends HTTPS itself and talks plain HTTP to the servers also works, for the web only. See [Behind a proxy](Ports-Behind-a-Proxy.md) for what the servers must then be told.

### Keep a user on one server

For the web interface, send all requests of one user to the same server: by the user's address, or by a cookie of the balancer. Otherwise a user is logged out whenever a request lands elsewhere. The other ways to deal with that are in [Session replication](Session-Replication.md).

SFTP and FTP connections stay where they are by nature.

### Tell the server who the user is

Behind a balancer, every connection seems to come from the balancer. Then banning by address bans the balancer, the log shows one address for everybody, and a user's allowed addresses mean nothing.

The cure is the **PROXY protocol**: the balancer puts the user's real address in front of each connection, and the server reads it. Turn it on in the balancer and on the listener: [PROXY protocol](Ports-PROXY-Protocol.md). It works for every protocol, SFTP and FTP included.

For web traffic through a balancer that ends HTTPS, the `X-Forwarded-For` header does the same: [Behind a proxy](Ports-Behind-a-Proxy.md).

Accept either only from the balancer's own address. A server that believes any caller's claim about its address can be told anything.

### Ask the right health question

Let the balancer ask each server `GET /WebInterface/api/readyz` and expect 200: see [Health checks](Monitoring-Health.md). It answers without a login and leaves nothing behind.

- A check that only opens the port is not enough: a server can accept connections while it is not ready.
- A check that fetches the login page starts a session every time.
- For a balancer that can only check the port it balances, such as the SFTP port, add a second check on the web port of the same server, if the balancer allows it.

A DMZ closes its ports while its server does not answer, so even a plain port check takes it out then: see [When the server is away](DMZ.md#when-the-server-is-away).

### FTP needs care

FTP opens a second connection for every transfer and every listing. Through a balancer that is trouble twice: the second connection must reach the same server, and the address the server tells the client must be the balancer's.

- Give each server its own range of passive ports, and let the balancer pass each range to its server.
- Set the listener's passive address to the public one.

See [FTP](Ports-FTP.md). SFTP has none of this, which is one more reason to prefer it.

### Timeouts

A large transfer runs for hours on one connection, and an idle SFTP session waits for minutes. Set the balancer's idle time-out higher than the server's own, or it cuts connections the server still wants.

## Taking a server out

Before maintenance, take the server out at the balancer, so that it gets no new connections, and wait for the open ones to finish. **Stop logins** in the node's detail on the [Cluster page](Cluster-Map.md#acting-on-another-node) makes sure of it on the server's side: new logins are refused and open sessions stay. Then update or restart it. While a server shuts down, its readiness answer is "not ready", so a balancer that asks that stops sending users by itself in that moment.

**Update every server, one at a time** in the Cluster section does the whole round: [The Cluster section](Cluster.md#updating-every-server).

## The balancer itself

One balancer is one machine that can fail. Run two, the way that product does it, or use a cloud's balancer, which is redundant by itself.

## Without a balancer

| Way | What it gives |
|---|---|
| [ServerBeat](ServerBeat.md) | two servers behind one address, one working and one waiting, nothing in front |
| DNS with several addresses | spreading, without noticing a dead server. Programs that try the next address cope; many do not. |

## Things to know

- **All servers must be the same**: users, settings, files, certificate, SSH host keys. See [Shared users and shared storage](HA-Shared-Users-and-Storage.md).
- **Count the licence.** Each server has its own.
- **The Cluster page shows the result**: "Where the load is" has a bar per server. Bars of very different length with a balancer in front mean the balancer is not spreading, or one partner is most of the load.
- **Nothing here was run for this page.** It is advice from the pages it links to; a balancer was not set up in front of the test server.
