Skip to content
Ameen Abozaid
MID-038

Redis

  • 7 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Strings
  • hashes
  • sets
  • sorted sets
  • key design

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Use Redis strings, hashes, sets, sorted sets, and expirations for appropriate problems.
  2. Design key names and namespaces that support invalidation.
  3. Use Redis from Spring (Spring Data Redis, @Cacheable with a Redis cache manager).
  4. Explain Redis persistence options and memory limits, and what is lost on failure.

Lessons

  1. 01Redis in one picture
  2. 02Data structures and their uses
  3. 03Key design
  4. 04Redis from Spring
  5. 05Persistence, memory, and eviction
  6. 06Demo: a leaderboard with sorted sets
  7. 07Demo: caching product details with @Cacheable

Lab and exercises

Lab

MID-038-LAB1 · Catalog cache

Outcomes: 1–4 · Time: ~3 h Cache product details (by id) and category listings in Redis with different TTLs, using a RedisCacheManager. Add hit/miss metrics. Run a load test before and after and record hit ratio and p95 latency. Set maxmemory and an eviction policy for the Redis container. Acceptance: hit ratio and latency improvement measured under load.

Exercises (assessment: data-structure selection scenarios)

IDOutcomeTask
MID-038-EX011Choose a Redis structure for each: page-view counter, shopping cart, unique visitors per day, top 10 products, session token.
MID-038-EX021Using redis-cli, build a leaderboard with three products and read the ranking.
MID-038-EX032Design key names for product details, category products, and a "catalog version" that supports invalidation.
MID-038-EX043Write a RedisCacheManager bean with a 10-minute default TTL and a 1-hour TTL for categories.
MID-038-EX054Redis restarts with no persistence and holds product cache, sessions, and a pending-email queue. What is lost, and what must move elsewhere?
MID-038-EX064Pick maxmemory-policy for a pure cache and explain why not noeviction.
Show solutions
  • EX01 Counter: string (INCR). Cart: hash. Unique visitors: set (or HyperLogLog if approximate is fine). Top 10: sorted set. Session: string with TTL.
  • EX02 ZADD board 5 p1 9 p2 2 p3, then ZRANGE board 0 -1 REV WITHSCORES gives p2, p1, p3.
  • EX03 shop:product:{id}:v1, shop:category:{id}:products:v{catalogVersion}, shop:catalog:version. Bumping the version makes old category keys unreachable; they expire by TTL.
  • EX04
java
RedisCacheConfiguration base = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10));
RedisCacheManager.builder(factory).cacheDefaults(base)
    .withCacheConfiguration("categories", base.entryTtl(Duration.ofHours(1))).build();
  • EX05 Cache and sessions are lost (users logged out, cache cold, database spike). The email queue is lost permanently: move it to a durable queue or the database. Redis must not be the only copy.
  • EX06 allkeys-lru or allkeys-lfu: Redis evicts old entries to make room. noeviction rejects writes when full, so the cache stops working.

Outcomes 2-4 are also checked by the lab (key names, TTLs, memory limit).

Quiz

Outcomes: O1 data structures · O2 key design · O3 Redis from Spring · O4 persistence and memory.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Best structure for a leaderboard:O1
  2. 2Best structure to hold a cart with one field per product:O1
  3. 3Which command is atomic for counting?O1
  4. 4Which key is best designed?O2
  5. 5To find keys safely in production use:O2
  6. 6@Cacheable on a method fails to cache when:O3
  7. 7Which removes a cache entry?O3
  8. 8Per-cache TTL in Spring is configured in:O3
  9. 9RDB persistence is:O4
  10. 10For a pure cache, memory policy should typically be:O4

Practical part

Structure-selection scenarios (MID-038-EX01, EX05) plus lab MID-038-LAB1. Pass: quiz at least 8/10, correct structures for all scenarios, and a lab with measured hit ratio and latency.

Common mistakes

KEYS * in production; unbounded memory; Redis as the only copy of important data.

Assessment

Data-structure selection scenarios.

Career relevance

Caching patterns (MID-039) and rate limiting (MID-034).

Type to search levels, phases, modules and lessons.