Cache data from a Spring Boot app with Memorystore

Memorystore for Redis is a fully managed Redis service for Google Cloud. Apps running on Google Cloud can achieve extreme performance by leveraging the highly scalable, available, and secure Redis service without the burden of managing complex Redis deployments. It can be used as a backend for data caching to improve the performance of Spring Boot apps. The codelab explains how to set it up.

What you'll learn

  • How to use Memorystore as a cache backend for a Spring Boot app.

What you'll need

  • A Google Cloud project
  • A browser, such Google Chrome
  • Familiarity with standard Linux text editors such as Vim, Emacs, and GNU Nano

How will you use the codelab?

Read only Read and complete the exercises

How would you rate your experience with Google Cloud 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:

Screenshot from 2016-02-10 12:45:26.png

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

[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.

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 wasn't enabled, then you'll 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].

After the 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 it again later when configuring your Spring Boot app.

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

If you navigate to Storage > Memorystore in Google Cloud Console, then you should be able to see your instance in "ready" state :

Create a Compute Engine instance in the same region.

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

After the operation completes, your instance will be ready to use.

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

In the virtual machine (VM) instance 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.

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 the application.properties file to configure the app to use the IP address of the Memorystore instance for Redis host.

$ nano src/main/resources/application.properties

Add the following line with your Memorystore for Redis IP address (from a couple steps ago):

spring.redis.host=<memorystore-host-ip-address> 

Add a new line after that and 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's used in combination with the parameter value as a cache key. You'll see an example later in the code lab.

Also, you need to enable caching in the Spring Boot app 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 this:

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're ready to run the app!

$ mvn spring-boot:run

Open another SSH connection to your instance the same way that you did earlier. 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 that the first time the request took five seconds, but the next one was significantly faster despite the fact that you have Thread.sleep(5000)invocation in the method. That's because the actual method was executed only once and the result was put into the cache. Every subsequent call returns the result directly from the cache.

You can actually see exactly what the app cached. From the same terminal that you used in the previous step, connect to Memorystore for Redis host using telnet:

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

To see the list of cache keys, use the following 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, delete Compute Engine and Memorystore instances from Cloud Shell.

Delete the compute instance:

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

Delete the Memorystore for Redis instance:

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

You created Memorystore for Redis and a Compute Engine instance. Also, you configured a Spring Boot app to use Memorystore with Spring Boot caching!

Learn More

License

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