Skip to content

Commit b1e87b3

Browse files
authored
docs: Memorystore documentation improvements (#2737)
This PR contains Memorystore documentation improvements as described in #2735.
1 parent b33abb1 commit b1e87b3

File tree

1 file changed

+85
-13
lines changed

1 file changed

+85
-13
lines changed
Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,106 @@
11
== Cloud Memorystore for Redis
22

3-
=== Spring Caching
4-
53
https://cloud.google.com/memorystore/[Cloud Memorystore for Redis] provides a fully managed in-memory data store service.
6-
Cloud Memorystore is compatible with the Redis protocol, allowing easy integration with https://docs.spring.io/spring-boot/docs/3.2.x/reference/html/io.html#io.caching[Spring Caching].
4+
Cloud Memorystore for Redis is compatible with the Redis protocol, allowing both programmatic access to the data store as well as easy integration with Spring caching.
75

8-
All you have to do is create a Cloud Memorystore instance and use its IP address in `application.properties` file as `spring.data.redis.host` property value. (Read more about this property in https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.application-properties.data[Spring Boot documentation]. )
9-
Everything else is exactly the same as setting up redis-backed Spring caching.
6+
Cloud Memorystore for Redis documentation can be found https://cloud.google.com/memorystore/docs/redis/[here].
7+
Use this documentation to create a Cloud Memorystore for Redis instance for your application.
108

119
[NOTE]
1210
====
13-
Memorystore instances and your application instances have to be located in the same region.
11+
Cloud Memorystore instances and your application instances have to be located in the same region.
1412
====
1513

16-
In short, the following dependencies are needed:
14+
=== Spring Data Redis
15+
16+
Because Cloud Memorystore is compatible with Redis, your application can use Spring Data Redis to connect to a Cloud Memorystore instance.
1717

18+
Add the Spring Boot Data Redis starter for a blocking I/O application:
19+
20+
.pom.xml
1821
[source,xml]
1922
----
2023
<dependency>
2124
<groupId>org.springframework.boot</groupId>
22-
<artifactId>spring-boot-starter-cache</artifactId>
25+
<artifactId>spring-boot-starter-data-redis</artifactId>
2326
</dependency>
27+
----
28+
29+
Or use the reactive starter for a reactive application:
30+
31+
.pom.xml
32+
[source,xml]
33+
----
2434
<dependency>
2535
<groupId>org.springframework.boot</groupId>
26-
<artifactId>spring-boot-starter-data-redis</artifactId>
36+
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
2737
</dependency>
2838
----
29-
For reactive applications, you can also use `spring-boot-starter-data-redis-reactive` instead.
3039

31-
And then you can use `org.springframework.cache.annotation.Cacheable` annotation for methods you'd like to be cached.
40+
Refer to Memorystore guides for instructions to find the address of your Cloud Memorystore instance in the https://cloud.google.com/memorystore/docs/redis/create-instance-console#creating_a_redis_instance[Google Cloud Console], or via the https://cloud.google.com/memorystore/docs/redis/create-instance-gcloud#creating-redis[gcloud command].
41+
42+
Configure the host and port of the Cloud Memorystore instance like this:
43+
44+
.src/main/resources/application.properties
45+
[source]
46+
----
47+
spring.data.redis.host: <cloud_memorystore_host> <1>
48+
spring.data.redis.port: <cloud_memorystore_port> <2>
49+
----
50+
<1> Replace `<cloud_memorystore_host>` with the actual IP address of the Memorystore instance.
51+
<2> Replace `<cloud_memorystore_post>` with the actual port of the Memorystore instance.
52+
53+
(Read more about these configuration properties in the https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties[Spring Boot documentation].)
54+
55+
==== Authentication
56+
57+
If you've enabled https://cloud.google.com/memorystore/docs/redis/about-redis-auth[authentication] for the Cloud Memorystore instance, you will need to provide the auth string via the `spring.data.redis.password` configuration property.
58+
You can find the auth string for your instance following https://cloud.google.com/memorystore/docs/redis/manage-redis-auth#getting_the_auth_string[this guide].
59+
60+
Remember to never commit plain-text secrets in your configuration properties file, so use an alternative method to provide this value to the application during startup.
61+
You can for instance load it from Secret Manager, use a Kubernetes secret, or provide it to your application via an environment variable.
62+
63+
==== In-transit encryption
64+
65+
If you've enabled https://cloud.google.com/memorystore/docs/redis/about-in-transit-encryption[in-transit encryption] for the Cloud Memorystore instance, you will need to configure the server CA certificate file via an SSL bundle.
66+
Follow guide in https://cloud.google.com/memorystore/docs/redis/manage-in-transit-encryption#downloading_the_certificate_authority[this page] to download the certificate `server-ca.pem` file.
67+
You can include this file in your project as a resource file, e.g. `src/main/resources/redis/server-ca.pem`.
68+
69+
Configure an SSL bundle to use the `server-ca.pem` file:
70+
71+
.src/main/resources/application.properties
72+
[source]
73+
----
74+
spring.data.redis.ssl.bundle: cloud-memorystore
75+
spring.ssl.bundle.pem.cloud-memorystore.truststore.certificate: classpath:redis/server-ca.pem <1>
76+
----
77+
<1> You can replace this location with a local file path if you don't want to add this file to your application's classpath, e.g. `file:/path/to/server-ca.pem`.
78+
It is also possible to put the contents of the `server-ca.pem` file here directly.
79+
80+
Spring Boot automatically enables encrypted access, when an SSL bundle is configured.
81+
82+
See the Spring Boot https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.ssl[SSL] documentation for more information about SSL bundles.
83+
84+
==== Programmatic access
85+
86+
See https://docs.spring.io/spring-boot/docs/current/reference/html/data.html#data.nosql.redis.connecting[connecting to Redis] in the Spring Boot documentation for ways to programmatically interact with Cloud Memorystore.
87+
88+
=== Spring Caching
89+
90+
Optionally, Cloud Memorystore can be used as a (Redis) backend for https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.caching[Spring caching] by adding this starter dependency (in addition to the Spring Data Redis starter):
91+
92+
.pom.xml
93+
[source,xml]
94+
----
95+
<dependency>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-starter-cache</artifactId>
98+
</dependency>
99+
----
100+
101+
Add the `@EnableCaching` annotation on your `@SpringBootApplication` class, or one of its `@Configuration` classes to enable caching support.
102+
103+
This lets you use the `org.springframework.cache.annotation.Cacheable` annotation for methods you'd like to be cached.
32104
[source,java]
33105
----
34106
@Cacheable("cache1")
@@ -37,6 +109,6 @@ public String hello(@PathVariable String name) {
37109
}
38110
----
39111

40-
If you are interested in a detailed how-to guide, please check https://codelabs.developers.google.com/codelabs/cloud-spring-cache-memorystore/[Spring Boot Caching using Cloud Memorystore codelab].
112+
See the https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.caching.provider.redis[Redis cache provider documentation] for information about configuring cache names, time-to-live, etc.
41113

42-
Cloud Memorystore documentation can be found https://cloud.google.com/memorystore/docs/redis/[here].
114+
If you are interested in a detailed how-to guide, please check https://codelabs.developers.google.com/codelabs/cloud-spring-cache-memorystore/[Spring Boot Caching using Cloud Memorystore codelab].

0 commit comments

Comments
 (0)