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

What you'll need

Familiarity with standard Linux text editors such as Vim, Emacs, and GNU Nano

How will you use the codelab?

Read onlyRead and complete the exercises

How would you rate your experience with Google Cloud services?

NoviceIntermediateProficient

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

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)

Note: gcloud is the powerful and unified command-line tool for Google Cloud Platform. Full documentation is available from https://cloud.google.com/sdk/gcloud. It comes pre-installed on Cloud Shell. You will notice its support for tab-completion.

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.

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

Note: Enabling Memorystore API is required before you can create an instance. That can involve an additional step and a couple extra minutes when executing the command.

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.

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:

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.