Run Redis on Google Cloud Platform

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker.

It's easy to get started developing Redis apps running on Google Cloud Platform. And because the apps you create will be running on the same infrastructure that powers all of Google's products, you can be confident that they will scale to serve all of your users, whether there are a few or millions of them.

This tutorial gets you going fast by deploying a simple Redis app. This tutorial assumes that you are familiar with Node.js programming and that you have already Prepared Your Environment for Node.js Development.

Before you begin

Check off each step as you complete it.

  1. check_box_outline_blank check_box Create a project in the Google Cloud Platform Console.
    If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.
    1. Open the Cloud Platform Console.
    2. In the drop-down menu at the top, select Create a project.
    3. Click Show advanced options. Under App Engine location, select a United States location.
    4. Give your project a name.
    5. Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
  2. check_box_outline_blank check_box Enable billing for your project, and sign up for a free trial.

    If you haven't already enabled billing for your project, enable billing now, and sign up for a free trial. Enabling billing allows the application to consume billable resources such as running instances and storing data. During your free trial period, you won't be billed for any services.

  3. check_box_outline_blank check_box Install the Google Cloud SDK.

    If you haven't already installed the Google Cloud SDK, install and initialize the Google Cloud SDK now. The SDK contains tools and libraries that enable you to create and manage resources on Google Cloud Platform.

Download and run the app

After you've completed the prerequisites, you can download and deploy the Redis sample app. The following sections guide you through getting the Redis app up and running.

Clone the Redis app

The code for the Redis sample app is in the GoogleCloudPlatform/nodejs-docs-samples repository on GitHub. If you haven't already, copy the repository to your local machine:

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

Go to the directory that contains the sample code:

cd nodejs-docs-samples
The Redis sample app is in the appengine folder:

cd appengine/redis

Alternatively, you can download the sample as a zip and extract it.

Start a Redis server

There are multiple options for creating a new Redis server:

  • Host a Redis instance on Google Compute Engine
  • Use Redis Labs to create a free Redis-as-a-Service account

Using Compute Engine

To use a hosted Redis instance on Compute Engine:

  1. Launch a Compute Engine virtual machine with Redis pre-installed by Bitnami.
  2. After the virtual machine launches, reserve a regional static IP address. Set the IP address region to the location of your Redis virtual machine instance. Then, select the instance in the Attached To dropdown, and click Reserve. This IP address is your Redis hostname.
  3. Under your Redis virtual machine instance in Compute Engine, find your Bitnami Base Password under Custom Metadata. Use this as your Redis key.
  4. By default, your Redis port number is 6379. You can verify this by using SSH to connect to your Redis virtual machine instance and then typing redis-cli.
  5. After you have completed these steps, create a configuration file and connect to your hosted Redis instance. For example:

    {
      "redisHost": "1.2.3.4",
      "redisPort": 6379,
      "redisKey": "bitnami_base_password"
    }
    

Using Redis Labs

To create a new Redis instance using Redis Labs:

  1. Create a new Redis subscription.
  2. Choose the cloud GCE/us-central1.
  3. Choose the free tier.
  4. Click select.
  5. Enter a resource name and password.
  6. Go to My ResourcesManage and click on your Redis instance. You should see a URL that looks similar to the one below.

    pub-redis-12345.us-central1-2-3.gce.garantiadata.com:12345

    Your Redis hostname consists of everything before the colon, and your Redis port number is the number after the colon. Use your Redis table password as the key value. For example, if your Redis table had a password of password, the URL above would be configured as follows:

    {
      "redisHost": "pub-redis-12345.us-central1-2-3.gce.garantiadata.com",
      "redisPort": 12345,
      "redisKey": "password"
    }
    
  7. Using this information, create a configuration file and connect to your Redis Labs instance.

Connecting to a Redis server

Create a configuration file

Create a JSON file named keys.json that contains your Redis host name, port, and password. Do not check your credentials into source control. Add keys.json to your .gitignore file.

    {
      "redisHost": [YOUR_REDIS_HOSTNAME],
      "redisPort": [YOUR_REDIS_PORT_NUMBER],
      "redisKey": [YOUR_REDIS_PASSWORD]
    }

Replace [YOUR_REDIS_HOSTNAME], [YOUR_REDIS_PORT_NUMBER], and [YOUR_REDIS_PASSWORD] with your Redis hostname, port number, and password respectively.

Run the app on your local computer

  1. Install dependencies:

    npm install
    
  2. Run the start script:

    npm start
    
  3. In your web browser, enter this address:

    http://localhost:8080
    

You can see the sample app displayed in the page. This page is delivered by the Redis web server running on your computer.

When you're ready to move forward, press Ctrl+C to stop the local web server.

Redis code review

The server.js file contains the JavaScript code to start a server and respond to requests.

The following code creates a Redis client and connects it to a Redis server.

// Connect to a redis server provisioned over at
// Redis Labs. See the README for more info.
const client = redis.createClient(
  nconf.get('redisPort') || '6379',
  nconf.get('redisHost') || '127.0.0.1',
  {
    'auth_pass': nconf.get('redisKey'),
    'return_buffers': true
  }
).on('error', (err) => console.error('ERR:REDIS:', err));

Running Redis on Google Cloud Platform

The following diagram shows the process of deploying the app on Cloud Platform.

Hello World app deployment process and structure

The App Engine flexible environment runs your application in containers that can automatically scale to handle your application's load. Behind the scenes, this architecture uses both Compute Engine and Docker. To learn more, see App Engine flexible environment.

Deploy the app to Google Cloud Platform

Enter this command to deploy the sample:

gcloud app deploy

Wait for the message that notifies you that the app update has completed.

See the app run in the cloud

In your web browser, enter this address:

https://<your-project-id>.appspot.com

This time, the page is delivered by a web server running in the App Engine flexible environment.

If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources. For information about deleting or stopping your VM instances, see Cleaning up.

For convenience, you can use an npm script to run the gcloud command. Add these lines to your package.json file:

"scripts": {
  "start": "node ./server.js",
  "deploy": "gcloud app deploy  --project <your-project-id>"
}

Now you can run this command to deploy your application:

npm run deploy

Configuration

Every app that runs in the App Engine flexible environment requires an app.yaml file to describe its deployment configuration.

runtime: nodejs
env: flex

This minimal app.yaml file sets the runtime to nodejs with the flexible environment. There are many other configuration values you can specify in app.yaml that allow you to customize resources, scaling, and other settings. For details about the configuration settings for the flexible environment, see App Engine flexible environment.

Bookshelf app logo

Redis documentation

Redis has documentation to help you get started.

Cleaning up

If you're done with the tutorials and want to clean up resources that you've allocated, see Cleaning Up.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...