Solid Queue and Solid Cache with Rails 7
Handling background jobs and caching are two of the most important parts of a production app, they allow for a more smooth experience for the users. There are many options in the market for these purposes, one of the most popular ones being Sidekiq for background jobs and Redis for cache, but with the introduction of Rails 8 two more options are going to be default for Rails, those are Solid Queue and Solid Cache. In this article, we will learn how to implement them in a Rails 7 application so you can get used to working with them and be ready when you decide to move to Rails 8 without changing much.
Let’s start with the background jobs mechanism, Solid Queue is a DB-based queuing backend for Active Job, designed with simplicity and performance in mind. Besides regular job enqueuing and processing, Solid Queue supports delayed jobs, concurrency controls, recurring jobs, pausing queues, numeric priorities per job, priorities by queue order, and bulk enqueuing (enqueue_all for Active Job’s perform_all_later). Solid Queue can be used with SQL databases such as MySQL, PostgreSQL, or SQLite, and it relies on Active Job for retries, discarding, error handling, serialization, or delays, and it’s compatible with Ruby on Rails’s multi-threading.
For the caching side, we have Solid Cache, a database-backed Active Support cache store that lets you keep a much larger cache than is typically possible with traditional memory-only Redis or Memcached stores. This is thanks to the speed of modern SSD drives, which makes the access-time penalty of using disk vs RAM insignificant for most caching purposes. Simply put, you’re now usually better off keeping a huge cache on disk rather than a small cache in memory.
For both gems, it is recommended to be on Rails 7.1 or higher since there are some issues with lower versions from different tests that I have made.
Let’s see how to incorporate Solid Queue and Solid Cache into your Rails application.
For Solid Queue and Solid Cache, you have to implement several configurations in order to use it, and those configurations can be different depending on your needs. For example, the database used for storing Solid Queue data can be separate(default configuration) or the same database that the application data is in. You can also configure the number of workers and the queues they can work on and even the polling time each group of workers can check the queue for new jobs. But one of the best features that Solid Queue provides is the ability to create cronjobs with just 3 lines of code. Here are some examples of the configuration and how to schedule a job to run in the future but you can check the full code in my example repository.
# Configuration
# config/application.rb
config.active_job.queue_adapter = :solid_queue
config.solid_queue.connects_to = { database: { writing: :queue } }
# Schedule a job
job = TestJob.set(wait_until: (Time.now + 3.minutes)).perform_later("This was scheduled at #{Time.now}")
# Cancel a job
SolidQueue::Job.find(job.provider_job_id).destroy
For Solid Cache, I had some issues with version 1.0 and couldn’t get it to run with my app, the documentation didn’t match what the application was asking for so the quickest solution for this was to bump down the version to 0.7 and everything worked fine. Hopefully, in the future, these issues will be solved and I can bump up the version again
For more updated information on the configuration for Solid Queue and Solid Cache visit the official pages.
There is a gem that works in tandem with Solid Queue called Mission Control, it is used to have a visual dashboard of everything related to the inner workings of the Queues. It is developed with a Full-Stack Rails application in mind, but as of the writing of this article, it looks like they added support to API-only apps.
That is all I wanted to share with you about Solid Queue and Solid Cache. I hope you learned something new about Solid Queue and Solid Cache with this article and I hope you can implement them in your current and future Rails applications. Thank you for reading all the way, if you like the article give it a clap and you can check out my other articles.
References
- https://github.com/rails/solid_queue
- https://github.com/rails/solid_cache
- https://github.com/rails/mission_control-jobs
- https://rubygems.org/gems/mission_control-jobs
- https://rubygems.org/gems/solid_cache
- https://rubygems.org/gems/solid_queue
- https://gorails.com/episodes/solid-queue-rails
- https://rubyonrails.org/2024/9/27/rails-8-beta1-no-paas-required
- https://github.com/mateomh/solids_test