Spring Boot on GCP
  • Introduction
  • Getting Started
    • Google Cloud Platform
    • Cloud Shell
    • gcloud CLI
    • Hello World!
      • Cloud Shell
      • App Engine
      • Cloud Run
      • Kubernetes Engine
      • Compute Engine
      • Cloud Functions
  • Application Development
    • Development Tools
    • Spring Cloud GCP
    • Cloud Services
      • Databases
        • Cloud SQL
        • Cloud Spanner
        • Cloud Firestore
          • Datastore Mode
          • Native Mode
      • Messaging
        • Cloud Pub/Sub
        • Kafka
      • Secret Management
      • Storage
      • Cache
        • Memorystore Redis
        • Memorystore Memcached (beta)
      • Other Services
    • Observability
      • Trace
      • Logging
      • Metrics
      • Profiling
      • Debugging
    • DevOps
      • Artifact Repository
  • Deployment
    • Runtime Environments
    • Container
      • Container Image
      • Secure Container Image
      • Container Awareness
      • Vulnerability Scanning
      • Attestation
    • Kubernetes
      • Kubernetes Cluster
      • Deployment
      • Resources
      • Service
      • Health Checks
      • Load Balancing
        • External Load Balancing
        • Internal Load Balancing
      • Scheduling
      • Workload Identity
      • Binary Authorization
    • Istio
      • Getting Started
      • Sidecar Proxy
  • Additional Resources
    • Code Labs
    • Presentations / Videos
    • Cheat Sheets
Powered by GitBook
On this page
  • Memorystore Redis Instance
  • Enable API
  • Create an Instance
  • Get Instance IP Address
  • Connect to Instance
  • Spring Boot Cache
  • Dependency
  • Configuration
  • Enable Caching
  • Cacheable
  • Spring Boot Session
  • Dependency
  • Configuration
  • Enable HTTP Session
  • Samples

Was this helpful?

  1. Application Development
  2. Cloud Services
  3. Cache

Memorystore Redis

Memorystore Redis Instance

Enable API

gcloud services enable servicenetworking.googleapis.com
gcloud services enable redis.googleapis.com

Enabling this API may take a few minutes.

Create an Instance

Create an instance and attach it to the default VPC.

gcloud redis instances create orders-cache \
  --size=1 --region=us-central1

Creating a Redis instance may take a few minutes.

Get Instance IP Address

gcloud redis instances describe orders-cache \
  --region=us-central1 --format="value(host)"

The IP address is not a static IP address. If you create the instance, the IP address may be different.

Connect to Instance

Computing Environment

Compute Engine

Kubernetes Engine

App Engine Flexible

App Engine Standard

Cloud Run

Cloud Function

You can test quickly by creating a Compute Engine instance in a zone within the same region:

gcloud compute instances create test-memorystore-vm --zone=us-central1-c

SSH into the machine:

gcloud compute ssh test-memorystore-vm --zone=us-central1-c

Install redis-cli:

sudo apt-get update && sudo apt-get install -y redis-tools

Connect to the instance:

redis-cli -h <MEMORYSTORE_REDIS_IP>

You can try different Redis commands, for example:

> PING
PONG
> SET greeting Hello
OK
> GET greeting
"Hello"

Spring Boot Cache

Dependency

Add the Spring Data Redis starter:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-cache'
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-data-redis'

Configuration

Configure the Redis instance to connect to:

application.properties
spring.redis.host=<MEMORYSTORE_REDIS_IP>

# Configure default TTL, e.g., 10 minutes
spring.cache.redis.time-to-live=600000

Enable Caching

Turn on caching capability explicitly with the @EnableCaching annotation:

@SpringBootApplication
@EnableCaching
class DemoApplication {
  ...
}

Cacheable

Once you configured the Spring Boot with Redis and enabled caching, you can use the @Cacheable annotation to cache return values.

@Service
class OrderService {
  private final OrderRepository orderRepository;
  
  public OrderService(OrderRepository orderRepository) {
    this.orderRepository = orderRepository;
  }
  
  @Cacheable("order")
  public Order getOrder(Long id) {
    orderRepository.findById(id);
  }
}

Spring Boot Session

Dependency

Add the Spring Data Spanner starter:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
compile group: 'org.springframework.cloud', name: 'spring-session-data-redis'
compile group: 'org.springframework.cloud', name: 'spring-boot-starter-data-redis'

Configuration

Configure the Redis instance to connect to:

application.properties
spring.redis.host=<MEMORYSTORE_REDIS_IP>

# Configure default TTL, e.g., 10 minutes
spring.cache.redis.time-to-live=600000

Enable HTTP Session

Turn on caching capability explicitly with the @EnableSpringHttpSession annotation:

@SpringBootApplication
@EnableSpringHttpSession
class DemoApplication {
  ...
}

Samples

PreviousCacheNextMemorystore Memcached (beta)

Last updated 4 years ago

Was this helpful?

See to see how to connect to a Memorystore instance from different computing environments.

See for more information.

Spring Boot can to .

Read Spring Boot documentation on and for more information.

Spring Boot can with .

redis-cli documentation
use Redis
cache with annotations
Cacheable
Redis
use Redis for session data
Spring Session Data Redis
Spring Boot Session Data Redis sample
Guide
Guide
Guide
Guide
Guide
Guide
Memorystore connectivity options