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 needsBackground Jobs and a popular framework for this isSidekiq. Sidekiq uses Redis as a data store. This means we need to deploy 4 services to Render to run our app:
- Rails web service
- Sidekiq background service
- PostgreSQL database
- Redis
Render supports "infrastructure as code" so we're going to define these services in a YAML file so it's tracked ingit
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 aRender maintainedDockerfile
.
Firstly, we need to update our database and Puma configurations for production. Followthis 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 calledrender-build.sh
in yourbin
directory and add the following contents:
#!/usr/bin/env bash# exit on errorset-o errexitbundleinstallbundleexecrake assets:precompilebundleexecrake assets:cleanbundleexecrake db:migrate
This script is also from Render'sRails deployment guide.
Ensure the script is executable by running the following command:
chmoda+x bin/render-build.sh
Lastly, we create our infrastructure specification. In your project root, create a file calledrender.yaml
and paste in the following:
services:-type:webname:myapp-webenv:rubyregion:frankfurt# or oregonplan:starternumInstances:1buildCommand:./bin/render-build.shstartCommand:REDIS_URL="redis://${REDIS_HOST}" bundle exec puma -C config/puma.rbdomains:-example.com# replace with your domain nameenvVars:-key:DATABASE_URLfromDatabase:name:myapp-dbproperty:connectionString-key:REDIS_HOSTfromService:name:myapp-redistype:pservproperty:hostport-key:RAILS_MASTER_KEYsync:false-type:workername:myapp-sidekiqenv:rubyregion:frankfurt# or oregonplan:starterbuildCommand:bundle install && bundle exec rake assets:precompilestartCommand:REDIS_URL="redis://${REDIS_HOST}" bundle exec sidekiq -e productionenvVars:-key:DATABASE_URLfromDatabase:name:myapp-dbproperty:connectionString-key:REDIS_HOSTfromService:name:myapp-redistype:pservproperty:hostport-key:RAILS_MASTER_KEYsync:false-type:pservname:myapp-redisenv:dockerregion:frankfurt# or oregonrepo:https://github.com/render-examples/redis.gitnumInstances:1disk:name:myapp-redis-datamountPath:/var/lib/redissizeGB:1databases:-name:myapp-dbplan:starterregion:frankfurt# or oregon
The full Render YAML specification isavailable 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 theREDIS_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 theredis://
protocol directive to the URL on our own.
I found that the easiest way to do this was interpolate theREDIS_HOST
environment variable that Render gives us to create aREDIS_URL
variable in thestartCommand
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, clickNew in the top right corner and selectBlueprint.
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
- Render's guide to deploying Rails
- Render's guide to deploying Sidekiq
- Render's guide to deploy persistent Redis
- Render vs Heroku
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)
For further actions, you may consider blocking this person and/orreporting abuse