Skip to content

Pool

A pool is a group of machines, called nodes, that run your Durable Objects. Each class runs on one pool. When a room wakes, the control plane starts it on one of its pool’s nodes.

A pool is a Layer, given the control plane it joins:

src/cluster.ts
export const Default = RivetDocker.SwarmPool("Default").pipe(Layer.provide(Cluster));

The nodes connect out to the control plane and wait for rooms to run. Nothing connects in to them, so they need no public address and no load balancer.

A class names its pool by providing it:

src/room.ts
export default class Room extends Rivet.DurableObject<Room>()(
"Room",
{ main: import.meta.url },
Effect.gen(function* () {
// ...
}).pipe(Effect.provide(Default)),
) {}

Each deploy builds one program for the pool, holding every class it runs, and rolls it out to the nodes.

Classes with similar needs share a pool and its machines. The rooms and the greeter run on the same nodes:

One pool is the right start. Fewer pools means fewer machines idling.

A class with different needs gets a pool of its own, so it can’t crowd out the rest. Rooms that transcode audio need bigger machines than rooms that only chat:

src/cluster.ts
export const Default = RivetAWS.EcsPool("Default").pipe(Layer.provide(Cluster));
export const Media = RivetAWS.EcsPool("Media", { cpu: 2048, memory: 4096 }).pipe(
Layer.provide(Cluster),
);

Both join the same control plane, so a room on Default still calls a class on Media by name.

More nodes run more rooms at once:

src/cluster.ts
export const Default = RivetDocker.SwarmPool("Default", { replicas: 3 }).pipe(Layer.provide(Cluster));

A single room still runs on one node at a time, so more nodes help a pool with many busy rooms, not one very busy room.

A pool works with any control plane it can reach. Its nodes only connect out, so the control plane can be anywhere:

src/cluster.ts
export const Cluster = RivetHetzner.ServerControlPlane("Rivet");
export const Default = RivetRailway.ServicePool("Default").pipe(Layer.provide(Cluster));

Keep the two close when you can: every call to a room passes through the control plane.

A process Alchemy didn’t build, such as an existing server or a test, can join a pool as one of its nodes. RivetNode.serve hosts classes in that process until it’s interrupted, and RivetNode.connect reaches any class’s instances through the gateway. Both read the connection from RIVET_ENDPOINT, RIVET_TOKEN, RIVET_NAMESPACE and RIVET_POOL, the variables a pool sets on the nodes it builds:

src/server.ts
import * as RivetNode from "@alchemy.run/rivet/Node";
import * as Effect from "effect/Effect";
import Room from "./Room.ts";
export const main = Effect.gen(function* () {
yield* RivetNode.serve([Room]).pipe(Effect.forkScoped);
const rooms = yield* RivetNode.connect(Room);
yield* rooms.getByName("lobby").join();
}).pipe(Effect.scoped);

Pass endpoint, token, namespace or pool to either one to set a field in code instead. Each class’s outer Effect runs once, in serve, with the context it’s given, so provide the services the classes yield. serve also takes RivetKit’s registry settings, actor options by class, which win over the ones a class sets, the shutdown grace period, and a connected Effect that runs once the node has registered. The process owns its signals: interrupt serve to stop the node. connect takes headers, computed for each call, such as the caller’s trace.

Next, choose a platform: Docker, AWS, GCP, Kubernetes, Fly, Hetzner, Railway or Rivet Cloud.