Run Multiple Server Instances¶
Multiple Gradient servers can run behind a load balancer. They share one PostgreSQL database and one S3 bucket. One server instance can fail. Builds continue.
Requirements:
- PostgreSQL 19 on the network, reachable from all server hosts
- S3 bucket for NARs, see
s3.* - Load balancer with WebSocket support in front of all server instances. This page doesn't cover setup of the load balancer itself.
1. Shared Database¶
All server instances connect to the same database. Set postgres.enable only on the host with the database, not on the server instances.
# configuration.nix of every server instance
services.gradient = {
postgres.enable = false;
database.urlFile = "/var/lib/gradient-secrets/database_url"; # (1)!
};
-
One line such as
postgresql://gradient:<password>@db.example.com/gradient. Generate the password withopenssl rand -base64 32. -
Connect directly or through PgBouncer in session mode. Avoid PgBouncer in transaction mode. Session locks don't survive transaction mode.
- Every server instance can open up to 61 database connections with the default settings.
| Purpose | Connections |
|---|---|
The three pools of database.* |
56 |
| Refresh of waiting evaluations | 2 |
Liveness lock, lock on shared maintenance passes and the listener for NOTIFY messages |
3 |
| Total per server instance | 61 |
- Set
max_connectionsto at least the number of server instances × 61, plus headroom for autovacuum andpsql.
2. Same Storage and Secrets¶
All server instances need the same S3 bucket, the same secret files and the same host keys for SSH.
# configuration.nix of every server instance
services.gradient = {
s3 = {
enable = true;
bucket = "gradient";
endpoint = "https://s3.example.com";
accessKeyId = "GK...";
secretAccessKeyFile = "/var/lib/gradient-secrets/s3_secret";
};
secrets = {
jwtFile = "/var/lib/gradient-secrets/jwt"; # (1)!
cryptFile = "/var/lib/gradient-secrets/crypt"; # (2)!
};
ssh.hostKeyFile = "/var/lib/gradient-secrets/ssh_host_ed25519_key"; # (3)!
};
- Copy the file from the first server to every other server. Different keys on one server instance sign users out on every request to that instance.
- Copy the file from the first server. Only this key can read secrets in the database.
- Generate the key once with
ssh-keygen -t ed25519 -N "" -f ssh_host_ed25519_key, then copy the file to all servers. SSH clients behind the load balancer see one host key.
Server instances don't start next to a running instance with a different bucket or other secrets. The log can contain is up with a different NAR storage or a different secret. Local NAR storage under baseDir can hold the NARs of a single server instance only.
3. Load Balancer¶
# configuration.nix of every server instance
services.gradient = {
listenAddr = "0.0.0.0";
reverseProxy.nginx.enable = false;
http.trustedProxies = [ "10.0.0.10/32" ]; # (1)!
};
- Address of the load balancer. Without this entry all clients share the address of the load balancer for rate limits.
The load balancer must meet these requirements.
- Let requests for a WebSocket upgrade pass through. Workers connect through
/proto. UI connections use WebSocket. - Keep idle connections open for a long time (for example, one day). Worker connections stay open between jobs.
- Send the request to another server instance after a failed connection attempt.
- Forward TCP port
2222to all server instances for Build over SSH.
4. First Start¶
- Stop every server instance of the previous release first. Then start the new release on all hosts.
- The start order of the new release is free. Only one server instance can migrate the schema at a time. The others wait and start after the migration.
- A new server instance can check the workers that remain on the server from the previous release. The instance can wait up to 4 times
metrics.workerSampleIntervalSecs(60 seconds at the default) for recent metric samples of those workers. A newer sample within that time can stop startup with an error.
Failure of a Server Instance¶
Server instances hold a liveness lock in the database for their entire lifetime. Other server instances monitor these locks.
| Failure | Detection |
|---|---|
Process crash or SIGKILL |
Within seconds. PostgreSQL can release locks for closed connections. |
| Host crash or network outage | After about 30 seconds of TCP keepalive with no response. |
| Loss of database connection at the server instance itself | The instance can shut down within 6 seconds. systemd can relaunch it as a new instance. |
The remaining server instances take over afterwards.
- Failed server instances receive an end time. Worker connections of those instances close.
- Workers reconnect to another server instance through the load balancer. Running builds continue there and complete on the first attempt.
- A multi-node exec job can continue if all node workers reconnect to the same server instance within the grace period. Node workers on different server instances restart the job. This restart won't use up retries.
- Jobs of workers that stay disconnected longer than
proto.assignmentReclaimGraceSecsgo back to the queue. - Remaining server instances send the pending deliveries of the failed instance (commit status, for example).
Per-Instance Limits¶
- Draining applies to all server instances. Restarts don't reset it.
- Rate limits and per-IP connection limits have separate counts for each server instance. Two server instances allow twice as many requests as one.
- Build logs reach the database every 10 to 30 seconds. Other server instances can show a live build log up to 30 seconds late. A killed server instance can lose the last 30 seconds of log of its running builds.
Verify Deployment¶
The query can show one row for each running server instance. Stopped server instances leave the list within about 30 seconds.
Next steps¶
- Configuration for every option of
database.*,s3.*andhttp.* - Back Up and Restore for the shared database and secrets
- Monitor Gradient for metrics of each server instance
- Upgrade Notes before each upgrade