1. Quickstarts
  2. Rails with Sidekiq

Deploy Rails with Sidekiq on Render


In this guide, we will show you how to configure your existing Rails and Sidekiq application on Render.

While processing web requests, you may have to offload tasks to an asynchronous, background process (typically called a worker). Sidekiq is a popular task processing framework for Ruby. Render makes it easy to use Sidekiq with Rails.

At the end, you’ll have 3 services:

  1. Rails Web Service to handle web requests
  2. Sidekiq Background Worker to process tasks
  3. Redis instance for the persistence and communication of Sidekiq tasks

Deploy to Render

There are 2 ways to deploy. We will first walk through manually setting up your services using the dashboard. We will then walk through declaring your services within your repository with Blueprints.

Deploy Manually

Redis

Create a new Redis instance with the following settings using the Redis deployment guide.

Maxmemory Policynoeviction (recommended for queues/persistence)
PlanStarter

We choose the noeviction maxmemory policy to ensure Redis does not delete tasks when its memory is full - Redis will instead prevent the creation of new tasks.

We choose the Starter instance type as it is the smallest instance type with persistence. This means that the tasks will be written out to disk and will be retained even if Redis restarts.

Copy the Internal Redis URL which will look like redis://red-xxxxxxxxxxxxxxxxxxxx:6379

Rails Web Service

Create a new Web Service for your Rails application. You may need to adjust the Build Command and Start Command for your service.

If you already have a Rails app deployed and are just adding Sidekiq, you can configure the REDIS_URL environment variable and skip the rest of this step.

RuntimeRuby
Build Commandbundle install; bundle exec rake assets:precompile; bundle exec rake assets:clean;
Start Commandbundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}

Under Advanced, add the following environment variable and any other environment variables you may need:

KeyValue
REDIS_URL<Internal Redis URL>, the Internal Redis URL from step 1.
RAILS_MASTER_KEYYour Rails application’s RAILS_MASTER_KEY

Sidekiq Background Worker

Create a new Background Worker to process Sidekiq tasks.

RuntimeRuby
Build Commandbundle install
Start Commandbundle exec sidekiq

Under Advanced, add the following environment variables and any other environment variables you may need:

KeyValue
REDIS_URL<Internal Redis URL>, the Internal Redis URL from step 1.
RAILS_MASTER_KEYYour Rails application’s RAILS_MASTER_KEY

If your Sidekiq worker needs access to a shared database or other service, environment groups are useful for sharing environment variables across services on Render.

Test it out!

Wait for these three services to finish deploying and then visit your Rails .onrender.com URL and trigger some tasks.

You should be able to see the Sidekiq Background Worker processing tasks in its logs. Background Worker start and done Logs in Render Dashboard

On the Redis metrics page, you should be able to see Active Connections in use. Graph of Active Connections against time where Active Connections increase from 0 to 12

Blueprints

Render enables users to deploy their services as code in a render.yaml file with Blueprints.

The following is an example render.yaml to configure and deploy all 3 services.

services:
  - type: redis
    name: sidekiq-redis
    region: ohio
    maxmemoryPolicy: noeviction
    ipAllowList: [] # only allow internal connections

  - type: worker
    name: sidekiq-worker
    runtime: ruby
    region: ohio
    buildCommand: bundle install
    startCommand: bundle exec sidekiq
    envVars:
      - key: REDIS_URL
        fromService:
          type: redis
          name: sidekiq-redis
          property: connectionString
      - key: RAILS_MASTER_KEY
        sync: false
  - type: web
    name: rails-web
    runtime: ruby
    region: ohio
    buildCommand: bundle install; bundle exec rake assets:precompile; bundle exec rake assets:clean;
    startCommand: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
    envVars:
      - key: REDIS_URL
        fromService:
          type: redis
          name: sidekiq-redis
          property: connectionString
      - key: RAILS_MASTER_KEY
        sync: false
  1. Copy the above code into render.yaml in the top level of your repository.

  2. Modify the render.yaml to suit your application. Remember, if you change a service name you need to update the corresponding reference in the .envVars that access variables from the service with the new name.

  3. Change the region fields to your chosen region.

  4. Commit and push your changes.

  5. On the Render Dashboard, go to the Blueprint page and click the New Blueprint Instance button. Select your repository (after giving Render the permission to access it, if you haven’t already).

  6. Enter your Rails application’s RAILS_MASTER_KEY for the background worker and web service.

  7. In the deploy window, click Approve.

  8. Test it out with the steps in the previous section.