When a Rails app's Action Cable runs on four app servers, which cable.yml adapters deliver every broadcast, and how do solid_cable, redis and postgresql differ?
answer
- async cannot cross processes
- solid_cable: rows plus polling
- redis: url, channel_prefix, redis gem
- postgresql: NOTIFY, 8000-byte payloads
- missing adapter key falls back to redis
basics
~20 sAcross four servers any shared adapter works, never async: solid_cable writes each broadcast as a row that every process polls for, redis publishes through a Redis server, and postgresql uses NOTIFY on the app's own database, with payloads capped near 8000 bytes.
solid answer
~40 sEach server process keeps its own subscribers, so a broadcast made on server A must reach processes on B, C and D through a shared adapter; `async` (in-memory, one process) silently loses most of them. `solid_cable` inserts a row into `solid_cable_messages` and a listener thread in every process polls for new rows every `polling_interval`, so it needs only a database, ideally a separate `cable` one via `connects_to`. `redis` needs the `redis` gem, a `url`, optionally `channel_prefix` to share one Redis server between apps, and `ssl_params` for TLS. `postgresql` needs the `pg` gem and uses Active Record's connection to the app's database: `NOTIFY` for broadcasts and a dedicated listening connection, with an 8000-byte payload limit and stream names over 63 bytes hashed. Pick on existing infrastructure, payload size and latency.
code
yaml · 6 linesproduction:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") %>
channel_prefix: league_production
ssl_params:
ca_file: /etc/ssl/certs/redis-ca.crtgo deeper
Remember that async works in one process only, and that production with several servers needs solid_cable, redis or postgresql.
Explain how each shared adapter moves a message: polled rows, Redis publish, or NOTIFY and LISTEN, with the settings each needs.
Choose using concrete limits: the 8000-byte NOTIFY payload, Solid Cable's polling delay and table, Redis as an extra service, and channel_prefix when sharing.
Weigh adding a pub/sub service against database-backed delivery for the whole platform, including failure modes when the shared backend is down.
## Why the adapter matters once there is more than one process In Action Cable every server process holds its own WebSocket connections and its own channel subscriptions. On a sports-league site served by four app servers, the browsers watching one match's live scoreboard are spread across all four. When a score update is broadcast from a controller on server A, the **subscription adapter** in `config/cable.yml` must carry it to the processes on B, C and D, which then transmit it to their own subscribers. That rules one adapter out immediately: - `async` keeps the subscriber map in memory inside one process. It cannot see the other servers, or even other Puma worker processes on the same machine, so most viewers miss most updates, and nothing raises. The three shared adapters are compared below. ## The three shared adapters | | `solid_cable` | `redis` | `postgresql` | |---|---|---|---| | Extra service | none, a database | a Redis server | none, the app's PostgreSQL | | Gem | `solid_cable` (generated in Rails 8) | `redis` | `pg` | | How a broadcast travels | row inserted; every process polls for new rows | published to Redis; one subscriber connection per process | `NOTIFY` on the database; one `LISTEN` connection per process | | Latency | up to `polling_interval` (0.1 s by default) | push | push | | Size limit | binary column | none that Rails sets | about 8000 bytes per payload | | Key settings | `connects_to`, `polling_interval`, `message_retention` | `url`, `channel_prefix`, `ssl_params` | `channel_prefix`; uses `database.yml` | ## solid_cable The default in a new Rails 8 app. `ActionCable::SubscriptionAdapter::SolidCable` writes each broadcast as a row in `solid_cable_messages`, together with a hash of the stream name. Every process runs a `solid_cable_listener` thread that, every `polling_interval`, selects rows with an id above the last one it saw, for the stream hashes it is subscribed to, and delivers them. `connects_to` points the `SolidCable::Record` models at a separate database (the generated config uses one named `cable`) so polling and trimming stay off the primary. Old rows are deleted according to `message_retention`. ## redis `ActionCable::SubscriptionAdapter::Redis` requires the `redis` gem (versions 4 to 6) and passes the rest of the section (`url`, `ssl_params`, `reconnect_attempts` and so on) to `Redis.new`. Each process holds one connection subscribed to the streams it needs plus one for publishing. `channel_prefix` is prepended to every stream name with a colon, so two apps or two environments can share one Redis server without hearing each other. ## postgresql `ActionCable::SubscriptionAdapter::PostgreSQL` requires the `pg` gem and uses `ActiveRecord::Base.connection_pool`, that is the app's primary database from `config/database.yml`. Broadcasts check out a pooled connection and run `NOTIFY`; each process opens one extra connection, outside the pool's normal checkout, that `LISTEN`s for its streams. Two limits shape it: 1. PostgreSQL caps a `NOTIFY` payload at 8000 bytes, which the Rails guide and the Solid Cable README both call out; large Turbo Stream HTML can exceed it. 2. Channel identifiers are limited to 63 bytes, so the adapter replaces longer stream names with their SHA1 hex digest. The check counts bytes, so multibyte names are hashed when they need to be. ## How to choose for the scoreboard site - **Already running Redis** for other work, or needing the lowest latency at high message rates: `redis`. - **Only PostgreSQL**, small payloads, no wish to add a table: `postgresql`. - **Fewest moving parts**, any supported database, larger payloads, and a tolerable 0.1-second polling delay: `solid_cable`, which is what a new app already has. Whichever you choose, every process must use the same adapter and the same backing service, and `channel_prefix` (supported by all three) should differ between apps that share that service. A section with no `adapter:` key falls back to `redis`. A misspelled adapter name raises "Could not load the '...' Action Cable pubsub adapter", and a missing driver gem raises "Error loading the '...' Action Cable pubsub adapter. Missing a gem it depends on?", both when Action Cable first builds its adapter.
- Does Action Cable need sticky sessions at the load balancer when it runs on four servers?Not for broadcasts: each WebSocket stays on the server that accepted it, and the shared adapter carries every broadcast to all four. Each reconnect simply opens a new socket wherever the balancer sends it, and the consumer resubscribes. What the adapter cannot fix is a single process holding too many sockets.
- Why might a Turbo Stream broadcast work with solid_cable but fail with the postgresql adapter?Turbo Stream broadcasts often carry rendered HTML. The postgresql adapter sends each payload through `NOTIFY`, which PostgreSQL limits to 8000 bytes, so a large partial can be rejected. Solid Cable stores the payload in a binary column instead, which is why its README suggests it for larger payloads.
- The async adapter shares broadcasts between Puma workers on the same host.
- The postgresql adapter needs its own cable database entry, like solid_cable.
- solid_cable pushes new rows to listeners through database triggers.
- The redis adapter works without the redis gem because Rails bundles a client.
- A cable.yml section without an adapter key falls back to async.