# Capacity and tuning

- **Linux limits** (for a busy balancer; adjust to your load):
  - open files: `MAX_OPEN_FILES` at the top of `crushbalance_init.sh` (1048576 as delivered; [Install on Linux](Install-Linux.md)). `install` writes it into the systemd unit as `LimitNOFILE`, and `start` sets it with `ulimit -n`, as far as the user may go. The automatic global connection cap (`max_connections` = 0) is (open-file limit − 1024) ÷ 2, at least 1,000, so a low limit also lowers the cap;
  - `net.core.somaxconn=65535` and `net.ipv4.tcp_max_syn_backlog=65535`;
  - the ephemeral port range: `net.ipv4.ip_local_port_range="1024 65535"`. Reserve the ports of your listeners in it, passive ranges included, for example `net.ipv4.ip_local_reserved_ports="21,443,990,40000-40999"`: otherwise a connection to a backend can take a listener's port while that listener is not bound (at startup, or after a change), and the listener stays in `bind_error` until that connection closes. The default range (32768–60999) already contains common passive ranges;
  - `net.ipv4.tcp_tw_reuse=1` and `net.ipv4.tcp_max_tw_buckets` of at least 2,000,000;
  - conntrack: `net.netfilter.nf_conntrack_max` sized for 120 seconds of connections at the peak rate, or conntrack disabled for the balanced ports;
  - `fs.file-max` above twice the number of sessions.
- **Heap (preliminary).** The final sizing table comes with the load tests on reference hardware (three Linux hosts, 100,000 sessions from 8 client addresses). Until then use these figures: **`-Xmx1024M` covers about 100,000 plain or about 40,000 TLS sessions, or about 30,000 re-encrypting HTTPS sessions** (`backend_tls`, which holds a second TLS engine per session).
  - Local load tests, with at most 4,480 sessions from one client address, confirm the first two figures with margin. They measured this heap per idle session: about 2.3 KB for a plain `tcp` session, about 8 KB for a TLS-terminated one, about 8.4–9.1 KB for HTTPS with plain back legs, and about 14.1–14.6 KB for HTTPS re-encrypting to CrushMFT. Each distinct client address adds roughly 0.2–0.3 KB (measured with IP stickiness, without a per-client cap or GeoIP, which keep an entry per address of their own). For a mix, add the costs.
  - Keep that live heap, about 8 MB plus the sessions, at or below about 45 % of `-Xmx`: the rest absorbs TLS handshakes in flight (about 3 KB each) and the short-lived garbage of TLS transfers.
  - Plan the process's memory as `-Xmx` plus about 0.4 GB (thread stacks and the garbage collector), plus the direct memory of the data buffers in use (below), plus the kernel's socket buffers.
  - To raise the heap on Linux, edit `-Xmx1024M` in `JAVA_OPTS` at the top of `crushbalance_init.sh`; on Windows, run `-i [heapMB]` again.
  - Data buffers live in direct memory (a quarter of the JVM's maximum direct memory, 64 MiB–4 GiB); by default the JDK sizes direct memory like the heap, so raising `-Xmx` raises it too.
- **Very high churn to one backend.** When the client closes first, the balancer's connection to the backend waits in TIME_WAIT. With default settings that limits new sessions to one backend address and port to roughly 470 per second; the settings above raise it, and several backend addresses or ports raise it further.
- **Windows:** widen the dynamic port range (`netsh int ipv4 set dynamicport tcp start=10000 num=55535`) and shorten `TcpTimedWaitDelay` (registry value under `HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters`).
