Skip to content
Cloudflare Docs

Stateless Instances

Run multiple instances across Cloudflare's network

To simply proxy requests to one of multiple instances of a container, you can use the getRandom function:

import { Container, getRandom } from "@cloudflare/containers";
const INSTANCE_COUNT = 3;
class Backend extends Container {
defaultPort = 8080;
sleepAfter = "2h";
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// note: "getRandom" to be replaced with latency-aware routing in the near future
const containerInstance = getRandom(env.BACKEND, INSTANCE_COUNT);
return containerInstance.fetch(request);
},
};