Unravel โบ Java concepts โบ NoSQL and caches
Redis clients and data structures
Redis is an in-memory data store with simple structures (strings, hashes, lists, sets, sorted sets).
At a glance
A whiteboard next to the desk.
- 1Spring RedisTemplate and StringRedisTemplate expose each data type through opsForValue(), opsForHash(), opsForList() and so on: ops.set("session:42", json, Duration.ofMinutes(30))
- 2Lettuce (default in Spring) and Jedis are the client librariesLettuce shares one connection across threads; Jedis needs a pool because each connection is not thread safe.
- 3Keys should have a clear naming scheme and almost always a time to live (TTL)Without it, memory fills up with stale data.
- 4Atomic commands (INCR, SETNX, ZADD) and Lua scripts make counters, locks and leaderboards safe under concurrency
- Choose the serializer on purpose
- Never run KEYS * in production: it blocks the single-threaded server
- The server runs commands on one thread, which is why each command is atomic and why one slow command stalls everyone
public boolean tooManyAttempts(String user) { String key = "login:attempts:" + user; Long count = redis.opsForValue().increment(key); if (count != null && count == 1) { redis.expire(key, Duration.ofMinutes(15)); } return count != null && count > 5;}- line 3 uses opsForValue, the core of Redis clients and data structures
Read the full explanation (about 2 min)
Redis is an in-memory data store with simple structures (strings, hashes, lists, sets, sorted sets). It is used for caches, sessions, counters, rate limits, queues and locks because every operation is fast and atomic.
A mental model
A whiteboard next to the desk. Reads and writes take no time, and everyone in the office sees the same board, but if the power goes out the writing may be gone unless you copy it somewhere permanent.
How it works
Spring RedisTemplate and StringRedisTemplate expose each data type through opsForValue(), opsForHash(), opsForList() and so on: ops.set("session:42", json, Duration.ofMinutes(30)).
Lettuce (default in Spring) and Jedis are the client libraries. Lettuce shares one connection across threads; Jedis needs a pool because each connection is not thread safe.
Keys should have a clear naming scheme and almost always a time to live (TTL). Without it, memory fills up with stale data.
Atomic commands (INCR, SETNX, ZADD) and Lua scripts make counters, locks and leaderboards safe under concurrency.
Going deeper
Choose the serializer on purpose. The JDK default writes binary Java objects that other languages cannot read and that break when the class changes; JSON or strings are more portable and debuggable.
Never run KEYS * in production: it blocks the single-threaded server. Use SCAN with a cursor.
Redis is a cache first. Persistence (RDB snapshots, AOF) reduces loss but does not make it a safe system of record unless you accept those trade-offs.
Under the hood
The server runs commands on one thread, which is why each command is atomic and why one slow command stalls everyone. Clients speak the RESP protocol over TCP and can pipeline many commands without waiting for replies, which is how Lettuce gets high throughput from a single connection.
Common mistakes
- Keys without TTL.
- Using KEYS in production.
- Default JDK serialization for shared data.
- Storing huge values or huge collections under one key.
How Unravel recognises it
When you paste code, Unravel looks for these signs of redis clients and data structures. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@RedisHash - Types:
RedisTemplateStringRedisTemplateJedisJedisPoolJedisPoolConfigRedisClientStatefulRedisConnectionRedisCommandsRedisConnectionFactoryLettuceConnectionFactoryRedisCacheManagerRedisStandaloneConfigurationReactiveRedisTemplateRedisSerializerand 5 more - Method calls:
opsForValue()opsForHash()opsForList()opsForSet()opsForZSet()
Part of a learning path
Related concepts
Frequently asked questions
What is Redis clients and data structures?
Redis is an in-memory data store with simple structures (strings, hashes, lists, sets, sorted sets). It is used for caches, sessions, counters, rate limits, queues and locks because every operation is fast and atomic.
What are the most common mistakes with Redis clients and data structures?
Keys without TTL. Using KEYS in production. Default JDK serialization for shared data. Storing huge values or huge collections under one key.
How can I see Redis clients and data structures in real code?
Paste your own Java into Unravel and it will point out where redis clients and data structures appears, explain how it works at the detail level you choose, and draw the flow of the code around it.