In Rails Action Cable, what runs on the worker pool sized by config.action_cable.worker_pool_size, and how does that size affect database connections?
answer
- default 4 threads per process
- connect, commands, stream callbacks
- event loop holds the sockets
- one connection per busy worker
- mount_path nil for a standalone server
basics
~20 sEach process's Action Cable worker pool (worker_pool_size, default 4) runs connect and disconnect, every subscribe and perform command, and each stream callback that transmits a broadcast. Each busy worker can hold an Active Record connection, so the database pool must cover them.
solid answer
~50 sAction Cable splits work in each server process. An event loop thread holds every socket, reads frames and runs the heartbeat timer; a `Concurrent::ThreadPoolExecutor` worker pool of `config.action_cable.worker_pool_size` threads (default 4) runs the application code: `connect` and `disconnect`, every incoming `subscribe`, `unsubscribe` and `perform` command, and each stream handler that decodes a broadcast and transmits it. Each unit of work is wrapped in the Rails executor, so an Active Record connection is checked out while a worker uses it and returned afterwards. Peak demand from Action Cable is therefore about `worker_pool_size` connections per process, on top of Puma's request threads when Action Cable is mounted in the same process at `mount_path` (`/cable`). In Rails 8.1 the `database.yml` key for that pool is `max_connections` (`pool` is the old name the Action Cable guide still uses). Raising the size helps IO-bound actions, not CPU-bound Ruby.
code
ruby · 5 lines# config/application.rb
config.action_cable.worker_pool_size = 8
# Each process may now need up to 8 extra Active Record connections
# for channel work, beyond its Puma request threads.go deeper
Recall that Action Cable has a small worker pool, 4 threads by default, that runs channel code.
Explain the split between the event loop holding sockets and the worker pool running connect, commands and stream callbacks.
Size database pools from worker_pool_size per process plus Puma threads, spot pool timeouts, and know when a standalone cable process helps.
Decide whether WebSockets share processes with web traffic or run as a separate service, given connection counts, deploy cadence and database limits.
## Two kinds of threads in each process Action Cable runs inside an ordinary Rack server process (Puma in a new app). In every such process it keeps: - **An event loop thread** (`ActionCable::Connection::StreamEventLoop`) that holds every open WebSocket, reads incoming frames and runs timers such as the 3-second heartbeat ping. It never runs your code. - **A worker pool**, a `Concurrent::ThreadPoolExecutor` with at most `config.action_cable.worker_pool_size` threads, default **4** (set in `ActionCable::Server::Configuration`). The `ActionCable::Server::Worker` class itself defaults to 5 when built directly, but the server always passes the configured value. The split lets one process hold thousands of idle sockets on one thread while a handful of threads run application code. ## What runs on the worker pool 1. `connect` when a socket opens, and `disconnect` when it closes. 2. Every command a browser sends: `subscribe` (so `subscribed` and its callbacks), `unsubscribe` (so `unsubscribed`), and `message` (so the action called by `perform`). 3. Every stream delivery: when a broadcast arrives from the pub/sub adapter, each subscribed channel's handler is posted to the pool, where it decodes the JSON (or runs your custom block) and calls `transmit`. On a sports-league site, a goal broadcast to a match watched by 2,000 viewers spread over four servers means about 500 small handler runs on each server's pool. Most are fast, but a stream block that queries the database runs once per subscriber. ## Database connections The Action Cable engine wraps every unit of worker work in the Rails executor, so Active Record connections are checked out when the code needs one and returned when the work ends. That gives a simple upper bound: - **Per process**, Action Cable can need up to `worker_pool_size` connections at once, in addition to Puma's request threads when cable is mounted in the same process. - **Across the fleet**, the source comment's arithmetic applies: 5 servers × 5 Puma workers × an 8-thread worker pool is at least 200 database connections. - **If the pool is smaller** than the threads that want it, workers block waiting for a connection, and every socket on that process slows down. In Rails 8.1 the `config/database.yml` key for a pool's size is `max_connections`; `pool:` is the old name, still read as a fallback. The Action Cable guide's advice to raise "`pool`" predates the rename. Adapters add their own connections: the `postgresql` adapter opens one extra listening connection per process and checks out pooled ones for `NOTIFY`, and `solid_cable` polls and writes through the `cable` database named in `connects_to`, which has its own pool. ## Sizing the pool | Symptom | Likely cause | Lever | |---|---|---| | Broadcast delivery lags under load | stream handlers or actions waiting on IO | raise `worker_pool_size`, then the DB pool to match | | Connection-pool timeouts in cable logs | pool smaller than Puma threads plus cable workers | raise `max_connections` or lower one side | | High CPU, no IO wait | CPU-bound Ruby in handlers | more processes, not more threads | On CRuby, threads share the global VM lock, so more workers help when they wait on the database or network, not when they compute. ## Where Action Cable is mounted - `config.action_cable.mount_path` defaults to `/cable`. The engine prepends `mount ActionCable.server => "/cable"` to the app's routes, so cable sockets share Puma's processes with web requests. - Setting `mount_path = nil` skips that route. You then run Action Cable as its own Rack app, as the guide shows, with a `cable/config.ru` that loads the environment and calls `run ActionCable.server`, started by its own Puma, and point browsers at it with `config.action_cable.url` (rendered by `action_cable_meta_tag`). - A standalone cable process is sized on its own: its Puma threads barely matter, and its database needs are the worker pool plus the adapter. The choice is operational: mounted is simpler; standalone isolates long-lived sockets from request-serving capacity.
- What does setting config.action_cable.mount_path to nil do?It stops the engine from prepending the `mount ActionCable.server` route, so the web app no longer accepts WebSocket upgrades. Action Cable itself still works: you run `ActionCable.server` as a separate Rack app (for example `cable/config.ru` under its own Puma) and set `config.action_cable.url` so browsers connect there.
- Why can a slow block passed to stream_for hurt every viewer on that server?Each broadcast runs that block once per subscriber, on the shared worker pool. If the block queries the database, 500 subscribers on one server mean 500 queries competing for 4 worker threads and the pool's connections, so unrelated deliveries and actions wait. Prepare the payload once before broadcasting instead.
- worker_pool_size is the number of WebSocket connections a process can hold.
- Each subscription gets its own worker thread for as long as it lives.
- Action Cable reuses the upgrade request's database connection, so it needs no extra connections.
- More worker threads let CPU-bound channel code run in parallel on CRuby.
- Setting mount_path to nil turns Action Cable off entirely.
Assumes Rails 8.1, where database.yml's pool size key is max_connections; pool: is the old name, still read as a fallback and still used in the Action Cable guide.