Redis
On this page 8 topics
Overview
Topics
- Strings
- hashes
- sets
- sorted sets
- key design
Learn first
3 h of practice (estimate)
Learning outcomes
- Use Redis strings, hashes, sets, sorted sets, and expirations for appropriate problems.
- Design key names and namespaces that support invalidation.
- Use Redis from Spring (Spring Data Redis,
@Cacheablewith a Redis cache manager). - Explain Redis persistence options and memory limits, and what is lost on failure.
Lessons
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)
| ID | Outcome | Task |
|---|---|---|
| MID-038-EX01 | 1 | Choose a Redis structure for each: page-view counter, shopping cart, unique visitors per day, top 10 products, session token. |
| MID-038-EX02 | 1 | Using redis-cli, build a leaderboard with three products and read the ranking. |
| MID-038-EX03 | 2 | Design key names for product details, category products, and a "catalog version" that supports invalidation. |
| MID-038-EX04 | 3 | Write a RedisCacheManager bean with a 10-minute default TTL and a 1-hour TTL for categories. |
| MID-038-EX05 | 4 | Redis restarts with no persistence and holds product cache, sessions, and a pending-email queue. What is lost, and what must move elsewhere? |
| MID-038-EX06 | 4 | Pick 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, thenZRANGE board 0 -1 REV WITHSCORESgives 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
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-lruorallkeys-lfu: Redis evicts old entries to make room.noevictionrejects 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.
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).