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

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

New users of Google Cloud Platform are eligible for a $300 free trial.

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 the 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 the 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

[core]
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 [redis.googleapis.com] not enabled on project [204466653457].
Would you like to enable and retry (this will take a few minutes)?
(y/N)?  y
Enabling service redis.googleapis.com 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
host: 10.0.0.4

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

After 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 https://start.spring.io/starter.tgz \
  -d dependencies=web,redis,cache -d language=java -d baseDir=cache-app \
  | tar -xzvf - && cd cache-app

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

$ nano src/main/resources/application.properties

Add this line with your own Memorystore Redis IP address (from a couple of steps ago) :

spring.redis.host=<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/HelloWorldController.java

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.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
@Autowired
private StringRedisTemplate template;

@RequestMapping("/hello/{name}")
@Cacheable("hello")
public String hello(@PathVariable String name) throws InterruptedException {
  Thread.sleep(5000);
  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.

Edit DemoApplication.java:

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

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;

@SpringBootApplication
@EnableCaching
public class DemoApplication {

public static void main(String[] args) {
  SpringApplication.run(DemoApplication.class, 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. Connect to Memorystore Redis host using telnet:

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

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

KEYS *
hello::bob

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

License

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