Get Started with Sidekiq and Upstash Redis on Fly.io
Sidekiq is a popular background processing library in Ruby language. With this library, we can distribute our processes to what is called Sidekiq workers
instead of doing the heavy work on the server itself.
What these workers do basically is they fetch the enqueued or scheduled tasks from the database. Then, execute them whenever the time comes and the worker is idle.
This way, we can make sure that our server remains responsive for incoming requests, without being blocked by heavy or recurring processes.
We will create a sample app using Sidekiq on Fly.io using Upstash Redis. It will enqueue and/or schedule some notification for users, depending on the inputs provided.
Fly.io Setup
Upstash Redis Setup on Fly
- Run
flyctl redis create
. - Set your connection link (after the
.toml
creation):flyctl secrets set UPSTASH_REDIS_LINK=<upstash-redis-connection-link>
Application Code
bundle init
andbundle add sidekiq sinatra
to initiate Gemfile and install dependencies. `- Create
sendEmail.rb
file:
- Create a simple Sinatra server
app.rb
:
- Create
config.ru
file:
Launch App
- First, run
flyctl launch
to create.toml
template. - There, update
internal_port = 9292
under[[services]]
. It should look something like this:
- Create a file named
Procfile
so that we can run both the Sinatra server and the Sidekiq Worker:
- run
flyctl deploy
to deploy.
App Deployed
-
send
get register/id1/enterprise
viacurl <fly-domain>/register/:id/:plan
-
send
get register/id2/paid
viacurl <fly-domain>/register/:id/:plan
-
send
get register/id3/free
and thenget update/id3/paid
viacurl <fly-domain>/register/:id/:plan
andcurl <fly-domain>/update/:id/:newPlan
Closing Words
In this example, we have implemented an app, using Sidekiq background processing library. In the sample, we have demonstrated a simple usecase which you can customize depending on your needs. This way, we have decreased the workload of our main server and with that enabled for more responsive experience for users.
For the complete source code, you can look at our repo.
You can give your feedbacks on any of our services via twitter or discord, which will be greatly appreciated.