skip to content

With Solid Cable as the Rails Action Cable adapter, how do polling_interval and message_retention trade broadcast latency against database load and table growth?

level: seniorimportance: nice to knowfreq 20%

answer

  1. one listener thread per process
  2. polling_interval default 0.1.seconds
  3. message_retention default 1.day
  4. autotrim: about two rows deleted per write
  5. retention is catch-up, not replay

basics

~20 s

Each process polls the messages table every polling_interval (0.1 seconds by default), so a shorter interval cuts latency but multiplies queries. message_retention (1 day) is how long rows survive before automatic trimming deletes them; it bounds table size, not what clients can replay.

solid answer

~40 s

Solid Cable stores every broadcast as a row in `solid_cable_messages`. In each server process a `solid_cable_listener` thread wakes every `polling_interval` (default `0.1.seconds`) and selects rows newer than the last id it delivered, for the streams that process has subscribers on. Latency is therefore up to one interval plus the query, and load is roughly one query per process per interval, idle or not; with four servers each running several processes that is dozens of queries a second. `message_retention` (default `1.day`) is the cut-off for trimming: with `autotrim` on, writes trigger `SolidCable::TrimJob` runs that delete expired rows in batches of `trim_batch_size` (100). Retention only lets a listener that fell behind catch up; a browser subscribing later starts from the newest id and replays nothing.

code

yaml · 8 lines
yaml
production:
  adapter: solid_cable
  connects_to:
    database:
      writing: cable
  polling_interval: 0.1.seconds
  message_retention: 1.hour
  trim_batch_size: 500

go deeper

for a junior

Recall that Solid Cable stores broadcasts in a table and that each server checks it every polling_interval, 0.1 seconds by default.

for a middle

Explain the listener thread, last-id polling, and that message_retention with autotrim bounds table growth through SolidCable::TrimJob.

for a senior

Size it: queries per second from all processes, rows held for one retention period, a dedicated cable database, and why retention is not client replay.

for a principal

Decide when database polling stops being the right transport for the product's real-time traffic, and what metric would trigger moving to a push-based backend.

## How Solid Cable moves a message Solid Cable is a database-backed Action Cable subscription adapter, selected with `adapter: solid_cable` in `config/cable.yml`. It replaces push-based pub/sub with a table and polling: 1. A broadcast, such as a goal on a live scoreboard, is handed to a background writer thread (`solid_cable_writer`), which inserts it into `solid_cable_messages` with the stream name, a 64-bit hash of that name, the payload and `created_at`. 2. Every server process runs one listener thread (`solid_cable_listener`). In a loop it selects rows whose `channel_hash` is among the streams it has subscribers for and whose `id` is greater than the last id it saw, then sleeps for `polling_interval`. 3. For each new row it calls the subscribers' handlers, which transmit to the browsers over their WebSockets. When a process first subscribes to a stream it records the current maximum id, so a new subscription receives only rows written afterwards. ## polling_interval: latency against load `polling_interval` (default `0.1.seconds`) sets how long the listener sleeps between queries. - **Latency**: a row written just after a poll waits up to one interval plus the next query's time before delivery. - **Load**: each process issues one query per interval whether or not anything was broadcast. A league site with four app servers, each running three Puma workers, makes about 120 polls a second at the default. - **Logs**: `silence_polling` (default true) hides these queries from the Active Record log. The Solid Cable README reports that at `0.01` seconds it becomes comparable to Redis in its benchmark, at ten times the query rate. For a scoreboard that changes every few seconds, the default is usually fine; the cheaper lever is a separate `cable` database (via `connects_to`) so polling never competes with the primary. ## message_retention and trimming: table growth Rows are not deleted when delivered, because Solid Cable does not track who has read what. Instead: - `message_retention` (default `1.day`) marks rows older than that as trimmable; - with `autotrim` (default true), each batch of writes schedules `SolidCable::TrimJob.perform_now` on a small background thread, on average about two trim runs per `trim_batch_size` writes, so deletion keeps pace with writes; - each run deletes up to `trim_batch_size` (default 100) expired rows, locking them with `FOR UPDATE SKIP LOCKED` when `use_skip_locked` is true (the default) so concurrent trimmers do not block each other; - with `autotrim: false`, nothing is deleted until you run `SolidCable::TrimJob.perform_later` yourself, for example on a schedule. The table therefore holds about one retention period of broadcasts. A busy site that sends 50 updates a second keeps roughly 4.3 million rows at the default one day; a shorter retention such as `1.hour` keeps it far smaller. ## What retention does not do Retention is easy to misread as message history. It is not: | Situation | Gets old rows? | |---|---| | A browser that subscribes after a broadcast | No, its process starts from the newest id | | A browser that reconnects after ten minutes offline | No, its resubscription starts from the newest id | | A listener thread that was slow or briefly lost its database connection | Yes, it resumes from the last id it delivered, if those rows still exist | So retention needs to cover the worst listener lag you expect, which is seconds or minutes, not a day. Scoreboard state after a reconnect must come from the page or an HTTP request, not from the cable table. ## Tuning checklist 1. Keep `connects_to` pointing at a dedicated database so polling and trimming stay off the primary. 2. Lower `polling_interval` only when measured latency matters, and count the extra queries per process. 3. Shorten `message_retention` on high-volume sites; it bounds table size. 4. Leave `autotrim` on unless you schedule `SolidCable::TrimJob` yourself. 5. Remember the version: these defaults come from Solid Cable's `Configuration`; Rails 8.1.4's own lockfile resolves 3.0.12, while the current release is 4.1.

  • A scoreboard viewer's browser reconnects after ten minutes offline. Does message_retention: 1.day let it catch up?
    No. On resubscription the process records the current maximum message id for that stream and delivers only newer rows. Retention lets a lagging listener thread catch up, not a client. The page must fetch the current score over HTTP, or the app must send a full state message after subscribing.
  • Why does Solid Cable trim on writes instead of deleting rows once delivered?
    It does not know which processes still need a row: every process polls independently and remembers only its own last id. Time-based retention is safe for all of them, and trimming on writes ties deletion to traffic, so the table stays near one retention period of broadcasts without a scheduler.

Solid Cable works like staff checking a notice board on a timer: every tenth of a second each office walks over and reads only the notices pinned since its last visit. Old notices are taken down after a day to keep the board clear, but someone joining the office today starts reading from the newest notice, not yesterday's.

  • message_retention lets a browser that reconnects tomorrow replay the day's broadcasts.
  • Lowering polling_interval costs nothing because idle polls return no rows.
  • Solid Cable deletes each message as soon as every subscriber has received it.
  • With autotrim: false, Solid Cable stops storing messages in the table.
  • polling_interval controls how often browsers poll the server over HTTP.

Assumes Rails 8.1 with Solid Cable 4.1, whose Configuration supplies these defaults; Rails 8.1.4's own lockfile resolves Solid Cable 3.0.12, and the 4.x installer also writes writer_batch_size and writer_batch_delay.