Multi-database architecture
Rails 8 configures four databases by default: primary, cache, queue and cable. This course explains how that setup works in one application. Models and the Solid adapters name a role, and database.yml resolves it to a location. Each database has its own schema file and its own namespaced rake tasks. The course also covers how to decide which data gets its own database. It then shows the costs: connection pools that multiply, and no transactions or joins across databases. It ends with a connection-planning exercise.
What you'll learn
- ✓How the primary, cache, queue and cable roles map to databases in database.yml
- ✓Three ways to declare connects_to for Solid Queue, Solid Cache and Solid Cable
- ✓What separate schema files and version: 1 tell you about each database
- ✓How namespaced database tasks and db:prepare behave
- ✓How to decide which data gets its own database
- ✓How to count connections across pools and processes
Four databases, one application
Rails 8 ships with four databases configured by default, and most developers never notice, because the framework hides the seam almost completely.
Read the full lesson free →Curriculum
7 lessonsFour databases, one application
Rails 8 configures four database roles by default, each with its own pool, schema and migration path.
How a model finds its database
Models inherit primary, while Solid adapters use connects_to to name a role that database.yml resolves.
A schema file per database
Each database has its own schema file, and version: 1 marks schemas that a gem ships whole.
The task namespace
Namespaced tasks act on one database, unqualified tasks act on all four, and db:prepare runs safely on boot.
Deciding what gets its own database
Two questions about growth and recoverability explain why the cache got its own database and the queue did not.
What it costs you
Pools multiply per database and per process, and transactions and joins cannot cross a role boundary.
Putting it together
A recap of role indirection and degrees of separation, plus an exercise that calculates connection headroom.