Skip to content

Spring + Redis Cache

Java/Spring Boot application using Spring Cache abstraction with Redis.

Overview

Tech Stack Java 21 / Spring Boot + Redis
Services 1
Cache The managed Redis cache
Pattern Cache integration, Spring Cache abstraction

The appmecca.yaml

containers:
  - name: spring-redis-cache
    dockerfile: Dockerfile
    context: .

deploy:
  - name: web
    container: spring-redis-cache
    port: 8080
    health_check:
      path: /health
      startup_timeout: 300
    resources:
      memory: "512m"

redis:
  enabled: true

The top-level cache.enabled: true block provisions the managed Redis cache and injects REDIS_URL into the container. It is off by default — you have to ask for it.

Two different things called cache

The top-level cache: block above is the managed Redis cache your code connects to. The per-service deploy[].cache: block is something else entirely: it configures the AppMecca edge cache in front of the service's HTTP responses. Unlike the top-level block, edge caching is on whenever a deploy[].cache block is present.

JVM services need resources.memory

Spring Boot will not start inside the 128 MB platform default — the container is killed with exit code 137 partway through boot. 512m is a sane floor, and startup_timeout: 300 gives a cold JVM time to pass its first health check (the default is 120 seconds). The unit suffix is required: a bare 512 means 512 bytes, and 512mb is not recognised.

Key Concepts

Spring Cache Abstraction

Spring's @Cacheable annotation transparently caches method return values in Redis. The first call to /api/compute/:key takes ~2 seconds (simulated expensive operation); subsequent calls return instantly from the cache.

Cache Eviction

The @CacheEvict annotation removes specific entries from the cache. Call DELETE /api/compute/:key to evict a cached computation and force a fresh calculation on the next request.

API Endpoints

Method Path Description
GET /health Health check (verifies Redis connectivity)
GET /api/compute/:key Compute result (cached after first call)
DELETE /api/compute/:key Evict cached result
POST /api/cache Store a key-value pair
GET /api/cache/:key Retrieve a cached value
GET /api/stats Redis database statistics

Step-by-Step Deployment

1. Register the app

mecca register git@gitlab.com:appmecca-sample-apps/spring-redis-cache.git \
  -e dev

2. Build

mecca build trigger \
  -e dev \
  -g git@gitlab.com:appmecca-sample-apps/spring-redis-cache.git

3. Deploy

mecca app deploy -e dev spring-redis-cache

4. Verify

# First call (slow ~2s)
curl https://spring-redis-cache-web.dev.yourdomain.io/api/compute/test

# Second call (instant, from cache)
curl https://spring-redis-cache-web.dev.yourdomain.io/api/compute/test

What's Next