Setup Solid Queue/Cable/Cache in Rails 8 to share a single database

An alternative to Sidekiq and Redis

Rails 8 ships with Solid Queue, Solid Cache, and Solid Cable.

The following setup will configure the app to run the app's primary database, along with Solid queue / cache / cable, all in a single Postgresql database.

For most small apps, this setup should be fine. As apps grow, we can split them out to separate databases easily.

Note: As usual, I set up both production and staging environments.

  • Create config/environments/staging.rbif you haven't already.


Update database.yml

  • I set up all 3 databases (development, production, staging) exactly the same way.

  • production and staging each specify a url for the database, which points to <%= ENV["DATABASE_URL"] %>. that ENV variable must be set in the staging and production app on whichever hosting service we're using.

  • Replace projectname with the actual project name.

# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  primary: &primary_development
    <<: *default
    database: projectname_development
  cache:
    <<: *primary_development
  queue:
    <<: *primary_development
  cable:
    <<: *primary_development

  # The specified database role being used to connect to PostgreSQL.
  # To create additional roles in PostgreSQL see `$ createuser --help`.
  # When left blank, PostgreSQL will use the default role. This is
  # the same name as the operating system user running Rails.
  #username: projectname

  # The password associated with the PostgreSQL role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: projectname_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
#   production:
#     url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
  primary: &primary_production
    <<: *default
    url: <%= ENV["DATABASE_URL"] %>
  cache:
    <<: *primary_production
  queue:
    <<: *primary_production
  cable:
    <<: *primary_production

staging:
  primary: &primary_staging
    <<: *default
    url: <%= ENV["DATABASE_URL"] %>
  cache:
    <<: *primary_staging
  queue:
    <<: *primary_staging
  cable:
    <<: *primary_staging

Staging, Production, Development environment configs

The following are the config files I used on instrumental.dev. You can use these as a starting point and tweak as needed.

config/environments/staging.rb:

config/environments/production.rb:

config/environments/development.rb:


Setup Puma.rb

Find the solid_queue plugin line and replace it with this:

This will ensure that solid queue runs inside the main app server.


config/cable.yml

config/cache.yml

config/queue.yml


Move Solid Queue, Solid Cache, and Solid Cable database tables into regular migrations for the main (sole) database

By default, Rails 8 assumes we will add Solid Queue / Cable / Cache to its own separate databases. Since we're running it on the primary database, we need to move the table migrations into regular migrations.

Generate 3 migrations:

In each of those migration files, inside of the change block, copy/paste all of the contents of the db/queue_schema.rb , db/cache_schema.rb , and db/cable_schema.rb respectively — not including the wrapping ActiveRecord::Schema[7.1].define(version: 1) do ...

Then run

Now all of the Solid tables should be in the primary database.


Install Mission Control

(as of this writing, Mission Control isn't yet part of Rails Core, but it's from the same people who built Solid Queue)

Mission Control gives you an admin web interface for viewing jobs info.

Follow install instructions here


Run process

To run the process, you'll need to run

As of this writing, I found that you won't actually see any output after running this.

In fact, I think that with that puma plugin :solid_queue active, the bin/dev isn't even needed.


Create a background job

Here's an example of a simple job that updates the title of of a post.

jobs/update_post_title_job.rb

And the post.rb for this:

Last updated