Skip to content
cabby

Scale across nodes with Redis

With several Cabby nodes, a client that reconnects may land on a different node, and a broadcast received by one node has to reach the clients of all of them. The Redis broker covers both with the Redis you already run. Stream history and sessions live in Redis, so every node shares them, and Redis pub/sub carries broadcasts from node to node.

Turn it on

cabby --rpc_host=http://localhost:3000/_anycable --secret=change-me \
  --broker=redis --redis_url=redis://redis.internal:6379/0 --broadcast_adapter=http,redisx

Run every node with the same Redis URL and the same key prefix. --broker=redis picks Redis pub/sub too, so you don't need --pubsub=redis. To use NATS between the nodes instead, set --pubsub=nats; the NATS guide covers it.

Every key the broker writes starts with a prefix, __cabby__ by default. To share one Redis between apps, give each app its own with --redis_broker_key_prefix, with CABBY_REDIS_BROKER_KEY_PREFIX, or in the configuration file:

# cabby.toml
broadcast_adapters = ["http", "redisx"]

[broker]
adapter = "redis"

[redis]
url = "redis://redis.internal:6379/0"

[redis_broker]
key_prefix = "myapp"

A prefix can't be empty or contain { or }. History is kept within --history_limit and --history_ttl, and sessions within --sessions_ttl, as with the other brokers. Configuration lists them.

Since the state lives in Redis, you can restart or replace a Cabby node at any time. Its clients reconnect to any node, catch up on what they missed and get their sessions back.

Broadcasting

A broadcast is stored in history when it goes through the broker, and that depends on the adapter it arrives by:

--broadcast_adapter Delivered live Stored in history
http Yes Yes
redisx (Redis Streams) Yes Yes
redis (the legacy adapter, PUBLISH) Yes No

Connected clients get a redis broadcast right away, but a client that reconnects later can't catch up on it. Use http or redisx when you want history. If you list redis anyway, Cabby says so at startup:

WRN broadcasts through the redis adapter are delivered live and not stored in history; use http or redisx to keep history

The broker stores history in Redis, but it doesn't read broadcasts from Redis: that's what redisx is for. With HTTP-only broadcasts, which is what the Docker image does by default, Cabby logs a reminder in case your app broadcasts through Redis:

WRN broadcasts are HTTP only; the Redis broker does not read broadcasts from Redis (set CABBY_BROADCAST_ADAPTER to add redisx)

Setting up Redis

Memory policy. Use maxmemory-policy noeviction on the Redis that holds the broker's keys. Every one of them has a TTL, so the volatile-* policies evict them as readily as the allkeys-* ones. With noeviction, a full Redis makes broadcasts fail with an error instead of dropping history quietly.

Persistence. With RDB snapshots or AOF, the stored messages and the sessions come back after Redis restarts, so clients restore their sessions. Without persistence, a restart empties Redis: streams start again from offset 1, and clients authenticate again as new sessions.

After a Redis restart or a failover, Cabby can't tell whether the last writes made it. So each stream gets a new epoch at its next broadcast, with or without persistence. Reconnecting clients notice and resync cleanly, exactly as after a restart with the in-memory broker. They never skip a message silently. Cabby logs a warning about it, at most once a minute.

Sentinel and Cluster

The broker connects the way Redis pub/sub and the Redis broadcast adapters do, with the same flags. Only those are shown here.

For Sentinel, list the sentinels in --redis_sentinels and put the master's name in --redis_url:

cabby --broker=redis --redis_url=redis://mymaster \
  --redis_sentinels=sentinel-1.internal:26379,sentinel-2.internal:26379,sentinel-3.internal:26379

For Redis Cluster, list the nodes in --redis_url, separated by commas. One node is enough too: Cabby detects that it belongs to a cluster.

cabby --broker=redis \
  --redis_url=redis://redis-1.internal:6379,redis://redis-2.internal:6379,redis://redis-3.internal:6379

A stream's keys always share one hash slot, so a broadcast is a single script on a single cluster node, and different streams spread over the cluster. When one cluster node restarts or fails over, only the streams it holds get a new epoch.

For TLS, use a rediss:// URL and the --redis_tls_* flags.

With Sentinel, rediss:// in the first --redis_sentinels URL turns TLS on for the master only: the connections to the Sentinel nodes don't use TLS, as in AnyCable. For Sentinel nodes that accept TLS only, add --redis_sentinel_tls. Upgrading from 0.4.2 or earlier: those versions also used TLS for the Sentinel nodes with a rediss:// URL, so if yours accept TLS only, you now need that flag.

Redis ACL

All the broker's keys start with <prefix>:, so you can limit a Redis user to them. The broker needs these commands:

ACL SETUSER cabby on >secret ~myapp:* resetchannels +eval +evalsha +script|load +info +time +hmget +hset +exists +xinfo|stream +xadd +xrevrange +expire +get +set

Add +select for a database other than 0. This list covers the broker alone. Redis pub/sub and the Redis broadcast adapters need their own commands and channels on top: SUBSCRIBE, PUBLISH and the stream commands of redisx.

Limits

  • No presence yet. Presence commands get an error, Pusher presence- channels can't be subscribed to, and the users endpoints of the API and of the Pusher REST API answer 501. If you need presence, use the in-memory broker on a single node.
  • Redis has to be reachable. Cabby connects at startup and stops if it can't, with failed to start the Redis broker in the error. After that, each operation gets --redis_broker_timeout seconds, 5 by default. While Redis is down, a broadcast fails with an error and isn't delivered, and reconnecting clients resync.

Logs and metrics

At startup, Cabby says which Redis it uses and when the broker is ready:

INF Using Redis broker: redis.internal:6379 (key prefix: myapp, history limit: 100, history ttl: 300s, sessions ttl: 300s)
INF Redis broker is ready (Redis 7.4.12, epochs per stream) context=broker provider=redis
Metric Meaning
redis_broker_errors_total Failed operations, timeouts included
redis_broker_timeouts_total Operations that ran out of time
redis_broker_reconnects_total Connections opened again after a lost or failed one
redis_broker_epoch_rotations_total Streams that got a new epoch because their state was lost or their Redis instance changed
redis_broker_operation_ms How long the most recent operation took, in milliseconds