Platforms, Microservices, DevOps and APIs · Hands-on 8

Hands-on 8 — In-Memory Cache

One shared Redis cache for every replica, so the database answers only on a miss

Three replicas, three caches?

The gateway runs as 3 replicas, each JVM with its own local cache. Replica 1 caches account 42.
The next request for account 42 lands on replica 2. What happens?

  • Replica 2 has nothing cached → another database query.
  • Each replica warms up its own cache: the database load barely drops.
  • The fix: a shared external cache — all replicas read from and write to the same store.

One cache per JVM × one shared Redis

type: simple · ConcurrentMapCache
account-1its own copy of account 42
account-2another copy, another database query
PUT through account-1account-2 keeps the old name, with no expiry
type: redis
account-1 · account-2no local copy
redis · accounts::42one copy, time to live (TTL) 300 s
PUT through account-1account-2 reads the new value
  • Per JVM: one miss per replica, and a write fixes only the replica that made it.
  • Shared: one miss for all replicas; @CachePut / @CacheEvict act on the only copy.

In-memory databases

Disk-based In-memory
Storage disk (HDD / SSD) RAM
Read latency 1–10 ms < 1 ms
Write latency 5–20 ms < 1 ms
Durability persistent by default configurable
Capacity terabytes gigabytes (RAM-bound)
Use case source of truth cache, sessions, counters

Cache-aside

Hit
GET accounts::42
→
the JSONmethod body skipped · about 0.004 s
Miss
GET accounts::42
→
(nil)
→
SELECT in PostgreSQLabout 0.058 s
→
SET accounts::42TTL 300 s · next read: a hit

The database stays the source of truth; the cache only saves round-trips.

Redis

Remote Dictionary Server: cache, session store, message broker — the de-facto distributed cache.

Type Key commands Typical use
String SET GET INCR EXPIRE cached JSON, counters, rate-limit tokens
Hash HSET HGET HGETALL session objects, configuration maps
List LPUSH RPOP LRANGE task queues, activity feeds
Set SADD SMEMBERS SISMEMBER unique visitors, tags
Sorted Set ZADD ZRANGE ZRANK leaderboards, priority queues
Stream XADD XREAD XACK durable event logs, fan-out

Unlike Memcached (strings only), Redis has native data structures with O(1) or O(log n) operations.

Persistence

Mode How it works Trade-off
RDB (snapshot) point-in-time snapshot at intervals fast restarts; may lose seconds of data
AOF (append-only file) logs every write; replayed on restart near-zero loss; larger files, slower restart
RDB + AOF both recommended for production
None pure in-memory acceptable for a pure cache

This hands-on enables AOF: --appendonly yes.

Architecture

internet
:8080 →
lbNginx
→
gateway ×3
gateway-1
gateway-2
gateway-3
→
services
auth
account ×2
→
data
redis:6379 · read first
PostgreSQLon a miss only
  • All replicas share one Redis: an entry written via replica 1 is readable via replicas 2 and 3.
  • Steps: 1 Docker Compose → 2 Spring Boot → 3 run and measure.

1 · Docker Compose

  redis:
    image: redis:7-alpine
    hostname: redis
    ports:
      - 6379:6379
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes

volumes:
  redis_data:
  account:
    # ... build, DATABASE_* ...
    environment:
      REDIS_HOST: redis
      REDIS_PORT: 6379
    deploy:
      replicas: 2
    depends_on:
      - db
      - redis

Same REDIS_HOST / REDIS_PORT on every service that uses the cache (e.g. auth).

2 · Spring Boot: dependencies and config

  • spring-boot-starter-data-redis — Lettuce, the Redis client.
  • spring-boot-starter-cache — @Cacheable, @CachePut, @CacheEvict.
  • @EnableCaching on the main class.
spring:
  data:
    redis:
      host: ${REDIS_HOST:localhost}
      port: ${REDIS_PORT:6379}
  cache:
    type: redis
    redis:
      time-to-live: 300000
      cache-null-values: false

type: redis replaces the default in-memory ConcurrentMapCache; TTL of 5 minutes; null is not cached.

2 · @Cacheable: read-through

@Cacheable(value = "accounts", key = "#id")
public AccountOut findById(String id) {
    return accountRepository.findById(id)
        .map(AccountOut::from)
        .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
  • First call for id = "42": the method runs, queries the database, stores accounts::42 in Redis.
  • Next calls: the method body is skipped; the value comes straight from Redis.

Annotations go on service-layer methods, not on controllers or repositories.

2 · Keeping the cache consistent

@CachePut(value = "accounts", key = "#id")
public AccountOut update(String id, AccountIn dto)
@CacheEvict(value = "accounts", key = "#id")
public void delete(String id)
Annotation Use on Behaviour
@Cacheable GET cached; run only on miss
@CachePut PUT/PATCH always run; update cache
@CacheEvict DELETE run; remove the key

@CacheEvict(value = "accounts", allEntries = true) clears the whole cache (bulk updates).

2 · JSON serialisation

Default Java serialisation stores unreadable binary. A RedisConfig bean switches to JSON:

@Bean
public RedisCacheConfiguration cacheConfiguration(ObjectMapper objectMapper) {
    return RedisCacheConfiguration.defaultCacheConfig()
        .serializeValuesWith(RedisSerializationContext.SerializationPair
            .fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)))
        .entryTtl(Duration.ofMinutes(5))
        .disableCachingNullValues();
}

Cached classes (AccountOut) must be JSON-serialisable: no-arg constructor, public fields or getters, @JsonIgnoreProperties(ignoreUnknown = true).

3 · Run

docker compose up -d --build
docker compose ps -a
NAME                STATUS     PORTS
store-redis-1       running    0.0.0.0:6379->6379/tcp
store-lb-1          running    0.0.0.0:8080->80/tcp
store-gateway-1     running
store-gateway-2     running
store-gateway-3     running
store-account-1     running
store-account-2     running
store-auth-1        running
store-db-1          running    0.0.0.0:5432->5432/tcp
  • The new container is redis; account now runs 2 replicas, the gateway 3.

3 · First request: miss · second: hit

curl -s http://localhost:8080/accounts/42 | jq    # 1st: miss, hits the database
docker compose exec redis redis-cli
> KEYS accounts::*                           # 1) "accounts::42"
> GET "accounts::42"                         # {"id":"42","name":"Ada Lovelace",...}
curl -s http://localhost:8080/accounts/42 | jq    # 2nd: hit
docker compose logs account --tail 20        # no database query this time

3 · Measure

docker compose exec redis redis-cli DEL "accounts::42"   # or the "miss" is a hit too
curl -o /dev/null -s -w "Cache miss: %{time_total}s\n" http://localhost:8080/accounts/42
curl -o /dev/null -s -w "Cache hit:  %{time_total}s\n" http://localhost:8080/accounts/42
  • Typical: miss 0.058 s, hit 0.004 s — 10–15× faster, no database round-trip.
  • Redis answers in < 1 ms and the response goes only through the gateway.

3 · TTL and hit rate

docker compose exec redis redis-cli
> TTL "accounts::42"          # seconds left; at 0 the key is removed
docker compose exec redis redis-cli MONITOR
docker compose exec redis redis-cli INFO stats | grep -E "keyspace_(hits|misses)"
# keyspace_hits:8
# keyspace_misses:2
  • After the TTL, the next read is a miss again: the cache stays eventually consistent.
  • MONITOR streams every GET (hit or nil) and SET (miss + store).
  • Healthy: > 90 % hits on read-heavy loads. Too many misses? Raise the TTL or pre-warm the cache.

Pitfalls

A cache can serve stale data: @CachePut / @CacheEvict on writes and a TTL keep it eventually consistent.

  • Forgetting @EnableCaching → the annotations do nothing; type: simple → one cache per JVM, none shared.
  • Binary values in redis-cli → JSON serialiser missing.
  • MONITOR logs every command: use with caution in production.
  • Port 6379 is published only for development; in production keep Redis inside the Docker network.

Without Docker: the simulator

Runmiss, then a hit through account-2 · 0.058 s × 0.004 s · TTL 287
local × sharedtwo misses, then a stale read after a PUT
TTL and stale dataa psql UPDATE stays hidden for up to 300 s
  • The pages embed the lab's stack as a simulator: same key, TTL, annotations and outputs.
  • Play runs a page step by step; the buttons (curl, redis-cli, psql) work at any time.
  • After the Run page, INFO stats reads keyspace_hits:8, keyspace_misses:2: 80 % hits.

Takeaways

  1. With replicas, a local cache per JVM is not enough: use a shared cache (Redis).
  2. Cache-aside: check the cache, go to the database only on a miss, then populate the cache.
  3. Redis is in-memory (< 1 ms) with rich data structures; AOF adds durability.
  4. Spring Cache: @Cacheable reads, @CachePut updates, @CacheEvict invalidates.
  5. A TTL bounds staleness; JSON serialisation keeps the cache inspectable.
  6. Verify with redis-cli (KEYS, GET, TTL, INFO stats) and compare miss × hit latency.
  7. The database behind the cache is still one container: hands-on 9 turns it into a cluster.

Outline: (1) why a shared cache once the gateway scales; (2) in-memory databases and cache-aside; (3) Redis data structures and persistence; (4) compose; (5) Spring Cache annotations; (6) run, measure.

The `account` service runs two replicas in the lab's compose (deploy.replicas: 2); that is where the cache lives. The overview page draws both cases as a step-by-step diagram, and the simulator plays them ("local × shared").

The simulator sits on the overview (opening on "local × shared") and at the top of Run.