In a new Rails 8.1 app, which Action Cable adapter does config/cable.yml set for development, test and production, and why?
answer
- one adapter per environment
- development: async, one process only
- test: the recording test adapter
- solid_cable:install overwrites the file
- Rails' own template alone says redis
basics
~20 sA new Rails 8.1 app's config/cable.yml uses async in development, test in test and solid_cable in production, connected to a separate cable database. rails new runs solid_cable:install, which overwrites Rails' own template whose production entry was redis.
solid answer
~40 s`config/cable.yml` names one pub/sub **adapter** per environment. Rails' own template writes `async` for development, `test` for test and `redis` (with `REDIS_URL` and a `channel_prefix`) for production, but `rails new` then runs `solid_cable:install`, which overwrites the file. The result: production uses `adapter: solid_cable` with `connects_to: { database: { writing: cable } }`, `polling_interval: 0.1.seconds` and `message_retention: 1.day`, storing messages in a `cable` database. `async` keeps subscribers in an in-memory map inside one process, so it is fine for `bin/rails server` but a broadcast from a separate `bin/rails console` never reaches the browser. `test` behaves like `async` and also records each broadcast so test assertions can read it. With `--skip-solid` the production entry stays `redis`.
code
yaml · 13 linesdevelopment:
adapter: async
test:
adapter: test
production:
adapter: solid_cable
connects_to:
database:
writing: cable
polling_interval: 0.1.seconds
message_retention: 1.daygo deeper
Recall the three generated adapters, async, test and solid_cable, and that async works only inside one process.
Explain why the file comes from two generators, what the solid_cable entry points at, and why console broadcasts vanish in development.
Show you check cable.yml and the cable database early in a deploy plan, and know the redis fallback when the adapter key is missing.
Decide whether the database-backed default is enough for the app's real-time load, or whether a dedicated pub/sub service is worth adding to the stack.
## What config/cable.yml decides Action Cable delivers a broadcast to every process that has a subscriber for it. The component that carries messages between publishers and those processes is the **subscription adapter** (also called the pub/sub adapter). `config/cable.yml` chooses it per environment, under an `adapter:` key, and Action Cable reads the current environment's section through `config_for` at boot. If the section has no `adapter:` key, `pubsub_adapter` falls back to `redis`. ## What a new Rails 8.1 app gets Two generators write this file. Rails' application template writes a first version; then `rails new` runs `solid_cable:install` (unless Action Cable is skipped), and that installer copies its own `cable.yml` with `force: true`, replacing the first. | Environment | Rails' template alone | After `solid_cable:install` (what a new app has) | |---|---|---| | development | `async` | `async` | | test | `test` | `test` | | production | `redis`, `url` from `REDIS_URL`, `channel_prefix: <app>_production` | `solid_cable`, `connects_to: { database: { writing: cable } }`, `polling_interval: 0.1.seconds`, `message_retention: 1.day` | The installer also writes `db/cable_schema.rb`, which creates the `solid_cable_messages` table. Solid Cable 4 adds `writer_batch_size` and `writer_batch_delay` to the generated production block; `polling_interval` and `message_retention` are the two defaults its README documents. ## Why each environment gets what it gets - **development: `async`.** It needs no service at all. Subscriptions live in an in-memory map, and broadcasts are posted onto Action Cable's event loop in the same process. That is enough when the browser, the channel and the code that broadcasts all run inside `bin/rails server`. - **test: `test`.** The test adapter is a subclass of `async` that also appends every broadcast to a per-name list. `ActionCable::TestHelper` installs one in each test that includes it, and `assert_broadcasts` and `assert_broadcast_on` read that list. - **production: `solid_cable`.** A real deployment runs several processes, often on several machines, so the adapter must be shared. Solid Cable stores each broadcast as a row and every process polls for new rows, which removes Redis as a required service; the 8.0 release notes list it among the new defaults. ## The async trap The generated template warns about it in a comment, and the Action Cable guide repeats it: 1. You open the scoreboard page in a browser; its subscription lives in the `bin/rails server` process. 2. In a terminal you run `bin/rails console` and call `ScoreboardChannel.broadcast_to(match, home: 2, away: 1)`. 3. The console is a different process with its own `async` adapter and no subscribers, so the message goes nowhere and nothing raises. To test a broadcast by hand in development, trigger it inside the server process (a controller action, or the in-page web console), or switch development to `solid_cable` with its own `connects_to` block. ## How the adapter name becomes code `ActionCable::Server::Configuration#pubsub_adapter` turns the `adapter:` value into a require path, `action_cable/subscription_adapter/<name>`, and the matching class. The built-in names `async`, `inline`, `test`, `redis` and `postgresql` live in Action Cable itself; `solid_cable` works because the `solid_cable` gem ships a file at `action_cable/subscription_adapter/solid_cable` defining `ActionCable::SubscriptionAdapter::SolidCable`. The adapter object is built lazily, the first time the server needs it, so a misspelled name or a missing gem shows up on the first connection or broadcast rather than at boot. ## Changing the defaults - `rails new --skip-solid` leaves the production entry on `redis` and adds a commented-out `redis` gem line to the Gemfile with the comment "Use Redis adapter to run Action Cable in production". - `rails new --skip-action-cable` (`-C`) writes no `cable.yml` at all. - Adding Solid Cable to an older app is `bundle add solid_cable` followed by `bin/rails solid_cable:install`; the `cable` database entry in `config/database.yml` is separate configuration that the README tells you to add. ## Why interviewers ask it The question checks that a candidate's picture of Action Cable is current. Saying "you need Redis for Action Cable in production" describes the Rails 7 default. Knowing that `async` is single-process explains the most common development surprise, and knowing that `solid_cable` points at its own database explains why a production deploy needs that database prepared before WebSockets work.
- What happens if the production section of cable.yml has no adapter key?`ActionCable::Server::Configuration#pubsub_adapter` fetches `adapter` with a default of `"redis"`, so Action Cable loads the Redis adapter. Without the `redis` gem in the bundle, the first time Action Cable builds its adapter it raises a `LoadError` beginning "Error loading the 'redis' Action Cable pubsub adapter. Missing a gem it depends on?"
- How do you check a broadcast by hand in development with the async adapter?Trigger it inside the web server's process: from a controller action, or from the web console rendered in the page, as the generated cable.yml comment suggests. Alternatively give development a `solid_cable` entry with its own `connects_to`, so a console process and the server share the messages table.
- A new Rails 8.1 app needs a Redis server for Action Cable in production.
- A broadcast from bin/rails console reaches development browsers through the async adapter.
- The async adapter is fine in production because it shares messages across Puma workers.
- solid_cable:install appends a production entry and keeps the redis one as a fallback.
- The test adapter only records broadcasts and never delivers them to subscribers.
Assumes Rails 8.1, whose rails new runs solid_cable:install; the Solid Cable default for production arrived with Rails 8.0, and earlier apps generated redis.