Deploy a Ruby app on Heroku using Redis


Profile picture for Ajeet Raina
Author:
Ajeet Raina, Developer Marketing Manager at Redis

Heroku is a popular PaaS offering that allows software developers to easily deploy their code without worrying about the underlying infrastructure. By using a simple 'git push heroku' command, developers are able to deploy their application flawlessly. This platform offers support for a wide range of programming languages such as Java, Ruby, PHP, Node.js, Python, Scala, and Clojure.

Here's a quickstart guide to deploy Ruby apps on Heroku using Redis. We will be deploying a sample Leaderboard app and will be using company valuation and stock tickers as its domain.

Step 1. Create a Redis Enterprise Cloud Database#

Create your free Redis Enterprise Cloud account by visiting this link.

recloud

Follow this link to create a Redis Enterprise Cloud subscription and database as shown below:

heroku

The database endpoint URL is unique so will be different in your case. Save it for future reference.

Step 2. Create a Heroku account#

If you are using Heroku for the first time, create your new Heroku account through this link.

heroku

Step 3. Install the Heroku CLI on your system#

brew install heroku

Step 4. Login to Heroku#

heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/XXXXXXXXXXA
Logging in... done
Logged in as your_email_address

Step 5. Connect your application to Redis Enterprise Cloud#

For this demonstration, we will be using a Sample Redis Leaderboard app.

Clone the repository#

git clone https://github.com/redis-developer/basic-redis-leaderboard-demo-ruby

Run the commands below to get a functioning Git repository that contains a simple application as well as a app.json file.

heroku create
Creating app... done, ⬢ thawing-shore-07338
https://thawing-shore-07338.herokuapp.com/ | https://git.heroku.com/thawing-shore-07338.git

Step 6. Setting up Environment Variables#

Go to the Heroku dashboard, click "Settings" and set REDIS_HOST and REDIS_PASSWORD under the Config Vars. Refer to Step 1 for the correct values to use.

heroku

You now have a functioning Git repository that contains a simple application as well as a app.json file, which is used by Node’s dependency manager.

Step 7. Deploy your code#

$ git push heroku

Wait for few seconds and you will see the messages below:

remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> console, rake, web
remote:
remote: -----> Compressing...
remote: Done: 125.9M
remote: -----> Launching...
remote: Released v10
remote: https://thawing-shore-07338.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/thawing-shore-07338.git
* [new branch] master -> master

Step 8. Accessing the application#

Open https://thawing-shore-07338.herokuapp.com/ to access your application on the browser. Please note that the Web URL is unique, hence it will be different in your case.

heroku

How does it work?#

How the data is stored:#

  • The AAPL's details - market cap of 2.6 triillions and USA origin - are stored in a Redis hash like this:

    HSET "company:AAPL" symbol "AAPL" market_cap "2600000000000" country USA
  • The market capitalization for each company is also stored in a ZSET (Redis Sorted Set).

    ZADD companyLeaderboard 2600000000000 company:AAPL

How the data is accessed:#

  • Top 10 companies:

    ZREVRANGE companyLeaderboard 0 9 WITHSCORES
  • All companies:

    ZREVRANGE companyLeaderboard 0 -1 WITHSCORES
  • Bottom 10 companies:

    ZRANGE companyLeaderboard 0 9 WITHSCORES
  • Between rank 10 and 15:

    ZREVRANGE companyLeaderboard 9 14 WITHSCORES
  • Show rank for AAPL, FB and TSLA:

    ZREVRANGE companyLeaderBoard company:AAPL company:FB company:TSLA
  • Add 1 billion to the market cap of the FB company:

    ZINCRBY companyLeaderBoard 1000000000 "company:FB"
  • Reduce the market cap of the FB company by 1 billion:

    ZINCRBY companyLeaderBoard -1000000000 "company:FB"
  • How many companies have a market cap between 500 billion and 1 trillion?:

    ZCOUNT companyLeaderBoard 500000000000 1000000000000
  • How many companies have a market cap over a Trillion?:

    ZCOUNT companyLeaderBoard 1000000000000 +inf

Next Steps#