Kamal Rails Series Part 4: Configure Redis and Sidekiq
This article covers adding Redis and Sidekiq to your Kamal deployment. Sidekiq is a popular background job processor for Rails, and it uses Redis as its data store. We’ll build upon our existing Kamal configuration to add these services.
Step 1: Add Redis to Your Gemfile
1
2
# Gemfile
gem "redis", "~> 4.0"
Step 2: Add Sidekiq (Skip if Already Configured)
Add Sidekiq to your Gemfile:
1
bundle add sidekiq
Create a sample job:
1
rails generate sidekiq:job dummy
Step 3: Configure Redis and Sidekiq in deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#config/deploy.yml
# Name of your application. Used to uniquely configure containers.
service: rubypodcatcher
# Name of the container image.
image: joshio1/rubypodcatcher
# Deploy to these servers.
servers:
web:
hosts:
- <SERVER_IP>
options:
network: "private"
..other configuration..
job:
hosts:
- <SERVER_IP>
cmd: bundle exec sidekiq -q default -q mailers
options:
network: "private"
env:
clear:
HOSTNAME: rubypodcatcher.com
DB_HOST: <SERVER_IP>
RAILS_SERVE_STATIC_FILES: true
RAILS_LOG_TO_STDOUT: true
REDIS_URL: "redis://rubypodcatcher-redis:6379/0"
secret:
- RAILS_MASTER_KEY
accessories:
redis:
image: redis:latest
host: <SERVER_IP>
directories:
- data:/data
options:
network: "private"
- If you haven’t created a private Docker network on your remote server, see the previous article for instructions.
1
docker network create -d bridge private
- Note: Any other configuration in your
deploy.yml(like Postgres) doesn’t need to be modified.
Step 4: Deploy Redis
Redis is configured as an accessory. Deploy it with:
1
2
kamal env push
kamal accessory boot redis
Step 5: Deploy Your Application
Deploy your application with the new Redis and Sidekiq configuration:
1
kamal deploy
Verify the Setup
To verify Redis is working:
1
2
3
4
irb(main):008:0> redis = Redis.new
=> #<Redis client v4.8.1 for redis://rubypodcatcher-redis:6379/0>
irb(main):010:0> redis.set("sample_key", "sample_value")
=> "OK"
To verify Sidekiq is working:
1
2
3
irb(main):005:0> DummyJob.perform_async
2024-01-18T15:39:15.194Z pid=7 tid=2jb INFO: Sidekiq 7.2.0 connecting to Redis with options {:size=>10, :pool_name=>"internal", :url=>"redis://rubypodcatcher-redis:6379/0"}
=> "2ab8740502724b5d107182cd"
Summary
This article demonstrates how to deploy Sidekiq and Redis with Kamal on a Rails application. This configuration works with both the Hetzner and AWS series. Note that Rails 8 introduces SolidQueue, which uses the database instead of Redis for job storage. I’ll cover SolidQueue with Kamal in a future article.
Hetzner Series:
- Part 1: Deploy Rails application on Hetzner
- Part 2: Add Postgres to your deployed Rails application
- Part 3: Enable HTTPS and SSL Certificate
AWS Series:
Listen to this podcast where DHH talks about Rails and Kamal.
If you would like to search for specific terms or concepts or names in Ruby/Rails podcasts, check out rubypodcatcher.com