Keep history across restarts (SQLite)
With a broker, Cabby keeps a history of each stream and the state of each session, so clients
that reconnect catch up on what they missed. The in-memory broker (--broker=memory, which
--presets=broker picks) loses that state when Cabby restarts. The SQLite broker also writes it
to a single file, so it survives a restart, without Redis or NATS to run.
Turn it on
The path is required: --broker=sqlite without one stops Cabby at startup. You can set it as
CABBY_SQLITE_PATH instead, or in the configuration file:
# cabby.toml
[broker]
adapter = "sqlite"
[sqlite]
path = "/var/lib/cabby/cabby.db"
Everything else works as with the in-memory broker. Clients are still served from memory, and
Cabby writes to the file in the background, so delivery doesn't wait for the disk. History is
kept within --history_limit and --history_ttl, and sessions within --sessions_ttl, as
before. Configuration lists them.
What survives a restart
A graceful restart, where Cabby is stopped with SIGTERM or SIGINT (docker stop, an
upgrade, kamal accessory reboot), keeps everything. Cabby writes what's left before it exits.
On the next start it keeps the same epoch, the stream offsets, the history and the sessions,
each session with its original expiry. Reconnecting clients resume from their offsets and
restore their sessions as if Cabby had never stopped.
After a crash (the process was killed, or the machine lost power), the last few writes may not have reached the disk. The same goes for a final write that failed, or history Cabby had to drop because its write queue was full. Cabby can't tell which messages are missing, so it starts with a new epoch. Reconnecting clients notice and resync cleanly, exactly as after a restart with the in-memory broker. They never skip a message silently.
The startup log says which case it was: the line starting with restored ends with
clean shutdown, new epoch after an unclean shutdown or new database.
Presence isn't saved. Every connection drops when Cabby stops, so a saved member list would only show people who are gone.
With Kamal, Cabby runs as an accessory, and accessories aren't restarted when you deploy your app. The SQLite broker helps when Cabby itself restarts: an upgrade, a reboot of the accessory or of the server.
Kamal
Give the accessory a volume, so the file outlives the container:
# config/deploy.yml
accessories:
cabby:
image: ghcr.io/danig2k/cabby:0.5.3
# host, port and the rest as in the Rails guide
env:
clear:
CABBY_BROKER: sqlite
CABBY_SQLITE_PATH: /var/lib/cabby/cabby.db
volumes:
- "cabby_data:/var/lib/cabby"
The image creates /var/lib/cabby for its non-root user, with mode 0700, and a new named
volume mounted there takes on that owner, so Cabby can write to it without extra steps.
The rest of the accessory is in the Rails guide, under "Deploying with Kamal".
Files and the lock
- Cabby creates the directory with mode
0700if it's missing, and the database files with mode0600: they hold session state and the messages of private streams. - Next to
cabby.dbyou'll seecabby.db.lock, and SQLite'scabby.db-walandcabby.db-shmwhile Cabby runs. After a graceful stop,cabby.dbholds everything, so you can back it up or snapshot the volume. - One Cabby process per file. A second one waits up to 10 seconds for the lock, then stops with an error that names the lock file.
- If the file is corrupt, Cabby moves it aside as
cabby.db.corrupt-<timestamp>and starts a new one.
The metrics sqlite_queue_size, sqlite_batch_write_ms, sqlite_write_errors_total and
sqlite_dropped_total show how the background writes are doing.
One node only
The SQLite broker is for a single Cabby node: the file is local, and only one process can use it. If
you run several Cabby nodes, use Redis, PostgreSQL or NATS instead. Each of them can carry
broadcasts between the nodes (--pubsub=redis, --pubsub=postgres or --pubsub=nats) and keep
history and sessions for all of them (--broker=redis, --broker=postgres or --broker=nats).