DEV Community

Ayush Newatia
Ayush Newatia

Posted on

Deploy Rails and Sidekiq to Render.com using YAML

Render.com is a new Platform-as-a-service offering that's a great alternative to Heroku. Rather than think in terms of "apps" as Heroku does; Render has the concept of "services". So your Rails app would be a service; your database would be another service; Redis would be another service etc. Services could also potentially be shared between multiple apps.

Any non-trivial Rails app these days needs Background Jobs and a popular framework for this is Sidekiq. Sidekiq uses Redis as a data store. This means we need to deploy 4 services to Render to run our app:

  1. Rails web service
  2. Sidekiq background service
  3. PostgreSQL database
  4. Redis

Render supports "infrastructure as code" so we're going to define these services in a YAML file so it's tracked in git along with our application code. Ruby environments are supported natively and they also have a managed PostgreSQL database offering. For anything else we need to use a Docker image, so using Redis is a tiny bit trickier.

At the time of writing, Render is working on a managed Redis offering. When that's live, it should be easier to deploy and manage a Redis service.

Render supports 3 service types:

  • Web service [exposed to the internet via https on port 80]
  • Private service [exposed only to all your other services]
  • Background worker [not exposed to the network at all]

Rails on Render

We'll deploy the Rails web app as a web service, the Sidekiq worker as a background worker and Redis as a private service using a Render maintained Dockerfile.

Firstly, we need to update our database and Puma configurations for production. Follow this section from Render's Rails deployment guide and then return to this post.

Then, we need to add a build script that will be run when the app is deployed. Create a file called render-build.sh in your bin directory and add the following contents:

#!/usr/bin/env bash
# exit on error
set -o errexit

bundle install
bundle exec rake assets:precompile
bundle exec rake assets:clean
bundle exec rake db:migrate
Enter fullscreen mode Exit fullscreen mode

This script is also from Render's Rails deployment guide.

Ensure the script is executable by running the following command:

chmod a+x bin/render-build.sh
Enter fullscreen mode Exit fullscreen mode

Lastly, we create our infrastructure specification. In your project root, create a file called render.yaml and paste in the following:

services:
  - type: web
    name: myapp-web
    env: ruby
    region: frankfurt # or oregon
    plan: starter
    numInstances: 1
    buildCommand: ./bin/render-build.sh
    startCommand: REDIS_URL="redis://${REDIS_HOST}" bundle exec puma -C config/puma.rb
    domains:
      - example.com # replace with your domain name
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: myapp-db
          property: connectionString
      - key: REDIS_HOST
        fromService:
          name: myapp-redis
          type: pserv
          property: hostport
      - key: RAILS_MASTER_KEY
        sync: false

  - type: worker
    name: myapp-sidekiq
    env: ruby
    region: frankfurt # or oregon
    plan: starter
    buildCommand: bundle install && bundle exec rake assets:precompile
    startCommand: REDIS_URL="redis://${REDIS_HOST}" bundle exec sidekiq -e production
    envVars:
      - key: DATABASE_URL
        fromDatabase:
          name: myapp-db
          property: connectionString
      - key: REDIS_HOST
        fromService:
          name: myapp-redis
          type: pserv
          property: hostport
      - key: RAILS_MASTER_KEY
        sync: false

  - type: pserv
    name: myapp-redis
    env: docker
    region: frankfurt # or oregon
    repo: https://github.com/render-examples/redis.git
    numInstances: 1
    disk:
      name: myapp-redis-data
      mountPath: /var/lib/redis
      sizeGB: 1

databases:
  - name: myapp-db
    plan: starter
    region: frankfurt # or oregon

Enter fullscreen mode Exit fullscreen mode

The full Render YAML specification is available here.

Most of the above file should be self explanatory. I recommend changing the service names to something specific to your app. The only tricky bit is providing the REDIS_URL environment variable to our Rails and Sidekiq services. Unlike PostgreSQL which is a managed service, Redis is a generic private service; so the only the host name and port is given to us in environment variables by the Render platform. This means we need to add the redis:// protocol directive to the URL on our own.

I found that the easiest way to do this was interpolate the REDIS_HOST environment variable that Render gives us to create a REDIS_URL variable in the startCommand for our services. You can how this is done in the Rails and Sidekiq services above.

If you run the Rails console in the Render dashboard, you'll need to invoke it using REDIS_URL="redis://${REDIS_HOST}" bundle exec rails console otherwise the console process won't be able to find the Redis service.

And finally, all we need to do is connect this YAML file to Render. Go to the Render dashboard, click New in the top right corner and select Blueprint.

Follow the on screen instructions to connect your repository and you should be good to go!

Conclusion

I'm using the above approach in my app: Scattergun. So far I'm really happy with it. I'm excited to see what the Render team has in store for the future and personally I plan to use them for all my projects in the foreseeable future.

Further reading

Scattergun is the easiest way to collect email addresses on your landing page and send emails to your mailing list. Get started for free!

Top comments (0)