Skip to content

MiroTalk LK horizontal scaling

By default MiroTalk LK runs as one app instance (SCALING_MODE=standalone): the few things it keeps between requests live in memory, which is all a single VPS needs, and nothing below is required. When one server is no longer enough, every part can be scaled out on its own:

Part Scales by Shares
App instances More instances behind a load balancer Redis (SCALING_MODE)
LiveKit nodes More nodes, optionally in several regions Redis (LiveKit's own config)
Egress workers (recording) More worker machines Redis, S3
Captions workers (captions) More worker machines, a GPU speech-to-text box –

The app itself is light; the media is LiveKit's. Usually LiveKit, Egress and captions need more machines first, and several app instances are for redundancy (restarts and updates without downtime) and very busy sites.

Standalone vs. distributed

Standalone (the default): one server runs the app, LiveKit and, optionally, Egress and captions. The app keeps rate limits, room locks, the waiting room and uploads in progress in memory, and saves recordings to ./recordings (or S3). The Redis in the stack is only LiveKit's.

MiroTalk LK standalone: one server runs a single app instance, which keeps rate limits, room locks, the waiting room and uploads in memory and saves recordings to the local recordings folder, plus one LiveKit server with its own Redis and optional Egress and captions; browsers reach the app and LiveKit through HTTPS and WSS reverse proxies, and media goes straight to LiveKit

Distributed (SCALING_MODE=distributed): any number of app instances and LiveKit nodes behind load balancers, with Egress and captions on their own machines. What one app instance kept in memory moves to the shared Redis, and recordings go to S3 (or a shared disk), so any instance can answer any request.

MiroTalk LK distributed: browsers reach several app instances through an HTTPS load balancer and several LiveKit nodes through a WSS load balancer, with media going straight to the LiveKit node of the room; app instances, LiveKit nodes and Egress workers share Redis for rate limits, room locks, the waiting room and uploads; app instances and Egress workers store recordings in S3; captions workers connect to LiveKit

Several app instances

What is shared

With SCALING_MODE=distributed, what one instance kept in memory moves to Redis, so any instance can answer any request (no sticky sessions):

State Standalone Distributed (Redis keys under REDIS_KEY_PREFIX)
Rate limits Memory rl:*: per client and limiter, the same whichever instance answers
Per-room locks (claiming the host role, room setting changes, starting and stopping recordings, live streams, captions) In-process lock:*: extended while held, released after; one held by an instance that died expires within 15 seconds
Waiting room Memory wr:*: a guest may knock on one instance, poll another and be let in by a host on a third
In-browser recording uploads in progress Memory and the local disk upload:*: each piece may reach any instance; the pieces themselves go to S3 or a shared disk
Periodic checks (meeting time limit, people who left the waiting room, abandoned uploads) Run by the instance task:*: run by one instance at a time

The rest was already shared or stateless: meeting settings are LiveKit's room metadata, host sign-in and single sign-on use signed cookies, and LiveKit sends each webhook once, to whichever instance the load balancer picks.

Configuration

Give every instance the same .env (the LiveKit keys, SESSION_SECRET, HOST_USERS / OIDC_*, S3_*, WEBHOOK_*...), plus:

INI
SCALING_MODE=distributed
REDIS_URL=redis://:<password>@redis.internal:6379   # rediss:// for TLS
# REDIS_KEY_PREFIX=mirotalk-lk:                       # keeps the app's keys apart from LiveKit's in a shared Redis
# INSTANCE_ID=app-1                                   # shown in logs; defaults to the host name
TRUST_PROXY=loopback, uniquelocal                     # the load balancer's (and proxies') addresses

An instance only starts once Redis answers, and logs scaling=distributed instance=<id>. If Redis becomes unreachable later, API requests fail right away (instead of hanging) and /readyz answers 503 with "redis": "down", until the connection is back on its own. Meetings in progress go on: media never goes through the app.

Redis: any Redis 6 or newer compatible server with one endpoint: the redis container in docker-compose.yml, Valkey, or a managed one (AWS ElastiCache, Google Memorystore, Azure Cache, Upstash...), with a replica and automatic failover for high availability. Redis Cluster (sharded) mode is not supported. It can be the Redis LiveKit uses. Persistence is not needed: if Redis restarts empty, guests in the waiting room are asked to knock again and uploads in progress can't continue (what reached a disk is kept as a recording when an instance next starts). Keep it off the internet, set a password, and use TLS (rediss://) between machines.

Keep the servers' clocks in sync (NTP): instances compare each other's timestamps.

Load balancer

  • Any HTTP load balancer works (Caddy, Nginx, HAProxy, Traefik, a cloud one): round robin, no sticky sessions.
  • Health check: GET /readyz (LiveKit and Redis reachable). GET /healthz only says the process is up.
  • Allow request bodies of at least 20 MB (in-browser recordings are uploaded in 16 MB pieces) and long requests on /api/recordings/ (finishing an upload puts the whole recording together, and moves it to S3).
  • Set TRUST_PROXY to the addresses of the load balancer and any proxy in front of it, so each client is rate limited by its own IP. TRUST_PROXY=true trusts only one proxy.
  • Point LiveKit's webhook (webhook.urls in livekit.yaml) at the load balancer or any instance.
  • Instances can be restarted one at a time (e.g. for updates); each finishes its requests on SIGTERM.

On one host (Docker Compose)

docker-compose.scale.yml runs several app replicas next to LiveKit and Redis, with a small Caddy load balancer on the app's usual port, so the reverse proxy in front (Caddy, Nginx) stays as it is:

Bash
# .env: TRUST_PROXY=loopback, uniquelocal  (requests pass two proxies now)
APP_REPLICAS=3 docker compose -f docker-compose.yml -f docker-compose.scale.yml up -d --build

It sets SCALING_MODE=distributed and REDIS_URL=redis://redis:6379 for the replicas. They share ./recordings, so uploads need nothing else. Requires Docker Compose 2.24+.

On several machines

Run the app container on each machine with the same .env and a shared REDIS_URL, published on its private address (APP_BIND_ADDRESS=10.0.0.11), and put a load balancer in front, e.g. Caddy:

Text Only
meet.example.com {
    request_body {
        max_size 20MB
    }
    reverse_proxy 10.0.0.11:3030 10.0.0.12:3030 10.0.0.13:3030 {
        lb_policy round_robin
        health_uri /readyz
        health_interval 10s
    }
}

or Nginx:

Nginx Configuration File
upstream mirotalk_lk {
    server 10.0.0.11:3030 max_fails=3 fail_timeout=10s;
    server 10.0.0.12:3030 max_fails=3 fail_timeout=10s;
}
server {
    server_name meet.example.com;
    client_max_body_size 20m;
    location / {
        proxy_pass http://mirotalk_lk;
        proxy_next_upstream error timeout http_502 http_503;
        proxy_read_timeout 300s;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    # listen 443 ssl ... as in the single-server setup (self-hosting.md)
}

Uploads

In-browser recordings uploaded to the server arrive in pieces, any of which may reach any instance. Where the pieces collect is CLIENT_RECORDING_STAGING:

  • s3 (the default when distributed and S3_BUCKET is set): each piece is an object under uploads-in-progress/<upload id>/ in the bucket. The instance that finishes the upload downloads them in order into CLIENT_RECORDING_DIR, makes the file seekable, moves it to S3 as before and deletes the pieces. No shared disk is needed, only room on each instance's disk for one recording while it's put together. Add a lifecycle rule that expires uploads-in-progress/ after a few days, for pieces of uploads lost with Redis:
    Bash
    aws s3api put-bucket-lifecycle-configuration --bucket <bucket> --lifecycle-configuration \
      '{"Rules":[{"ID":"mirotalk-uploads","Filter":{"Prefix":"uploads-in-progress/"},"Status":"Enabled","Expiration":{"Days":3}}]}'
    
  • disk (the default otherwise): the pieces collect in CLIENT_RECORDING_DIR, which every instance must share (NFS, AWS EFS, CephFS... or the same host directory, as with docker-compose.scale.yml). The app warns at startup when it's distributed with disk staging, as it can't tell whether the directory is shared. Set CLIENT_RECORDING_STAGING=disk to use a shared disk even with S3 (recordings still move to S3 when finished).

CLIENT_RECORDING_MAX_UPLOADS counts uploads in progress across all instances; CLIENT_RECORDING_MIN_FREE_MB is checked on the disk the instance writes to.

LiveKit cluster

LiveKit nodes that share a Redis form a cluster on their own: each new room is placed on a node with capacity, and a participant can connect to any node, which relays signalling to the room's node. A room must fit on one node, so more nodes mean more meetings, not bigger ones. See LiveKit's distributed setup.

  1. Run LiveKit on each node with the same keys and the same livekit.yaml, whose redis points at the shared Redis:
    YAML
    port: 7880
    redis:
        address: redis.internal:6379
        password: <password>
        # use_tls: true
    rtc:
        tcp_port: 7881
        udp_port: 7882
        use_external_ip: true # or --node-ip <this node's public IP>
    room:
        empty_timeout: 300
        departure_timeout: 20
        max_participants: 100
    webhook:
        urls:
            - https://meet.example.com/api/webhooks/livekit
        filter_params:
            include_events: [room_started, room_finished, egress_ended]
    
    Each node needs a public IP with 7881/tcp and 7882/udp open, since media goes straight to the node hosting the room.
  2. Put a load balancer for livekit.example.com in front of the nodes' port 7880 (HTTPS and WebSocket upgrades, long idle timeouts), with a health check on /.
  3. Point the app at the load balancer: LIVEKIT_URL=wss://livekit.example.com and LIVEKIT_API_URL=https://livekit.example.com (or an internal address of the load balancer). Every node answers the server API for every room.
  4. Scale down by stopping nodes with SIGTERM: LiveKit drains them, keeping their meetings running and placing new ones on other nodes until they're empty.

For TURN/TLS (restrictive networks) each node needs its own TURN domain and certificate; see LiveKit's deployment guide.

Several regions (optional)

To connect people to nearby servers, run nodes in several regions with one shared Redis, a load balancer per region, and a latency- or geo-aware DNS (Route 53, Cloudflare...) sending livekit.example.com to the nearest one. Give each node its region and the region-aware node selector:

YAML
region: eu-central
node_selector:
    kind: regionaware
    sysload_limit: 0.7
    regions:
        - name: eu-central
          lat: 50.11
          lon: 8.68
        - name: us-east
          lat: 39.04
          lon: -77.49

LiveKit places a new room in the region closest to the node that creates it. The app creates the room through LiveKit's server API when the host joins, so it lands near the app instance that handled the request: to start meetings near their hosts, run app instances in each region too (with the same geo DNS for meet.example.com) and set each region's LIVEKIT_API_URL to its own regional LiveKit load balancer. Everyone else joins the meeting where it is. All regions share one Redis (LiveKit's and the app's), so keep it where round trips from every region are short: each app request makes a few.

Dedicated Egress workers

Recordings and live streams run in LiveKit Egress, which takes jobs from LiveKit over Redis. Workers on their own machines keep recordings from competing with live meetings for CPU, and more workers mean more recordings at once (each one takes ~2-6 CPUs; give workers at least 4 CPUs and 4 GB of memory).

On each worker machine:

Bash
docker run -d --name egress --restart unless-stopped \
  -e EGRESS_CONFIG_BODY="$(cat <<'EOF'
api_key: <LIVEKIT_API_KEY>
api_secret: <LIVEKIT_API_SECRET>
ws_url: wss://livekit.example.com
redis:
    address: redis.internal:6379
    password: <password>
health_port: 8080
prometheus_port: 9090
EOF
)" livekit/egress:v1.15.0
  • Workers take jobs while they have CPU to spare and balance them on their own; livekit_egress_available (on prometheus_port) helps autoscaling.
  • Use S3 for recordings (S3_BUCKET in the app's .env, which hands it to each job): a local RECORDING_DIR would leave files on whichever worker recorded them.
  • On the main server, drop --profile recording (and recording from COMPOSE_PROFILES) so no local egress runs; keep RECORDING_ENABLED / STREAMING_ENABLED on in the app.

Dedicated captions workers

The captions agent registers with LiveKit under CAPTIONS_AGENT_NAME, and LiveKit gives each meeting with captions on to an available worker. Run it on as many machines as you need:

Bash
docker build -t mirotalk-lk-agent ./agent
docker run -d --name captions --restart unless-stopped \
  -e LIVEKIT_URL=wss://livekit.example.com \
  -e LIVEKIT_API_KEY=<key> -e LIVEKIT_API_SECRET=<secret> \
  -e CAPTIONS_AGENT_NAME=mirotalk-lk-captions \
  -e CAPTIONS_IDLE_PROCESSES=2 \
  -e STT_PROVIDER=whisper -e WHISPER_URL=http://whisper.internal:8000/v1 \
  mirotalk-lk-agent
  • CAPTIONS_AGENT_NAME must match the app's; CAPTIONS_IDLE_PROCESSES keeps processes warm so captions start quickly.
  • Self-hosted Whisper is the expensive part: run it on a GPU machine (WHISPER_IMAGE=ghcr.io/speaches-ai/speaches:0.8.3-cuda) and point every worker at it with WHISPER_URL, or use a cloud provider (STT_PROVIDER=openai / deepgram).
  • On the main server, drop --profile captions / --profile whisper; keep CAPTIONS_ENABLED=true in the app.

Checklist

  • SCALING_MODE=distributed and the same REDIS_URL and .env on every app instance; each logs scaling=distributed at startup.
  • Redis private, with a password (and TLS between machines); a managed one with failover for high availability.
  • The load balancer checks /readyz, allows 20 MB bodies, and TRUST_PROXY lists its addresses.
  • Uploads go to S3 (with a lifecycle rule on uploads-in-progress/) or a disk all instances share.
  • LiveKit's webhook points at the app's load balancer; LiveKit nodes, Egress and captions workers share the keys.
  • Clocks in sync (NTP) on every machine.