Cloud Memorystore for Redis is a fully managed Redis service for Google Cloud Platform. Applications running on Google Cloud Platform can achieve extreme performance by leveraging the highly scalable, highly available, and secure Redis service without the burden of managing complex Redis deployments.

It can be used as a back-end for Spring caching to improve performance of Spring-based applications. The following steps demonstrate the setup.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Codelab-at-a-conference setup

If you see a "request account button" at the top of the main Codelabs window, click it to obtain a temporary account. Otherwise ask one of the staff for a coupon with username/password.

These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.

Note that all these accounts will be disabled soon after the codelab is over.

Use these credentials to log into the machine or to open a new Google Cloud Console window Accept the new account Terms of Service and any updates to Terms of Service.

Here's what you should see once logged in:

When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.

Run the following command in Cloud Shell to confirm that you are authenticated:

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

Start Cloud Shell if you haven't already.

After Cloud Shell launches, you can use the command line to create a new Memorystore instance:

$ gcloud redis instances create myinstance --size=1 --region=us-central1

If Memorystore API was not enabled, you will be asked if you would like to enable it. Answer y.

API [] not enabled on project [204466653457].
Would you like to enable and retry (this will take a few minutes)?
(y/N)?  y
Enabling service on project 204166153457...
Waiting for async operation operations/tmo-acf.c8909997-1b4e-1a62-b6f5-7da75cce1416 to complete...
Operation finished successfully. The following command can describe the Operation details:
 gcloud services operations describe operations/tmo-acf.c8909997-1b4e-1a62-b6f5-7da75cce1416
Create request issued for: [myinstance]
Waiting for operation [operation-1538645026454-57763b937ad39-2564ab37-3fea7701] to complete...done.
Created instance [myinstance].

Once this operation completes, your instance will be ready to use.

Get the redis host ip-address of the instance by running the following command. You will use this later in configuring your Spring Boot application.

$ gcloud redis instances describe myinstance --region=us-central1 \
  | grep host

If you navigate to the Storage > Memorystore section of the GCP console you should be able to see your instance in "ready" state :

Create a Cloud Compute instance in the same region:

$ gcloud compute instances create instance-1 --zone us-central1-c

Once this operation completes, your instance will be ready to use.

Connect to your instance via SSH by navigating to the Compute > Compute Engine > VM instances and clicking SSH in the Connect column:

In the VM shell (not Cloud Shell) Install OpenJDK, Maven, telnet:

$ sudo apt-get install openjdk-8-jdk-headless maven telnet

Wait for the installation to complete and then proceed to the next step.

Let's create a new Spring Boot project with web, redis and cache dependencies:

$ curl \
  -d dependencies=web,redis,cache -d language=java -d baseDir=cache-app \
  | tar -xzvf - && cd cache-app

Edit file to configure the application to use the ip address of the Memorystore instance for redis host:

$ nano src/main/resources/

Add this line with your own Memorystore Redis IP address (from a couple of steps ago) :<memorystore-host-ip-address> 

Make sure to add a new line after this one.

Create a REST controller java class:

$ nano src/main/java/com/example/demo/

Put the following content into the file:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloWorldController {
private StringRedisTemplate template;

public String hello(@PathVariable String name) throws InterruptedException {
  return "Hello " + name;

The @RequestMapping annotation exposes the method as an HTTP endpoint and maps part of the path to a method parameter (as indicated by the @PathVariable annotation).

The @Cacheable("hello") annotation indicates that the method execution should be cached, and the cache name is "hello". It is used in combination with the parameter value as a cache key. You will see an example further in this code lab.

Also, we need to enable caching in the Spring Boot application class.


$ nano src/main/java/com/example/demo/

Import org.springframework.cache.annotation.EnableCaching and annotate the class with this annotation. The result should look like that:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

public class DemoApplication {

public static void main(String[] args) {, args);

Now you are ready to run the application!

$ mvn spring-boot:run

Open another SSH connection to your instance the same way you did a few steps back. In the new SSH window, access the /hello/ endpoint multiple times, passing "bob" as name:

$ time curl http://localhost:8080/hello/bob 
Hello bob!

real        0m5.408s
user        0m0.036s
sys        0m0.009s

$ time curl http://localhost:8080/hello/bob 
Hello bob!

real        0m0.092s
user        0m0.021s
sys        0m0.027s

Notice, the first time the request took 5 seconds, but the next one was significantly faster, despite the fact we have Thread.sleep(5000) invocation in the method. This is because the actual method was executed only once, and the result was put in the cache. Every subsequent call returns the result straight from the cache.

We can actually see exactly what the application has cached. From the same terminal you used in the previous step, connect to Memorystore Redis host using telnet:

$ telnet <memorystore-host-ip-address> 6379

To see the list of cache keys, use this command:


As you can see, the cache name is used as a prefix for the key, and the parameter value is used as the second part.

To retrieve the value, use the GET command:

$ GET hello::bob
   Hello bob!

Use the QUIT command to exit.

To clean up, from Cloud Shell, delete Compute and Memorystore instances.

Delete the compute instance:

$ gcloud compute instances delete instance-1 --zone us-central1-c

Delete the Redis instance:

$ gcloud redis instances delete myinstance --region=us-central1

In this codelab, you've created Cloud Memorystore and a Compute Engine instance. Also, you've configured a Spring Boot application to use Memorystore with Spring Caching!

Learn More


This work is licensed under a Creative Commons Attribution 2.0 Generic License.