The @RedisHash annotation marks a Java class as an aggregate root that Spring Data Redis stores as a Redis hash. Each instance becomes one hash whose fields are the object's properties, and the annotation's value defines the keyspace (the key prefix) that groups all instances of that type. With it you can use ordinary Spring Data repositories such as CrudRepository against Redis, without writing commands by hand.
The final Redis key for an entity is the keyspace plus the entity id, for example user:42. The annotation also controls expiry through its timeToLive attribute.
Given an entity annotated with @RedisHash("user") and an id of 42, Spring Data Redis writes:
user:42, with one field per property (name, age, and so on).42 added to a set at the key user, which the repository uses to list all entities of that type.A common misunderstanding is that all users are stored under the single key user. Only the set of ids lives there; each object has its own hash key. The mapping is done by the MappingRedisConverter, which flattens nested objects and collections into hash fields such as address.city. It is not JSON, although you can store a JSON string in a field if you want.
Add spring-boot-starter-data-redis. In Spring Boot, Redis repositories are auto-configured when a Redis connection is available, so no extra annotation is needed in most applications. Outside Boot, add @EnableRedisRepositories.
After saving, you can inspect Redis with redis-cli:
Every @RedisHash entity needs an identifier. Spring Data recognises a property annotated with org.springframework.data.annotation.Id, or a property literally named id. The id is not a separate field name you choose for the key; it is the suffix appended to the keyspace.
If the id is null when you call save, Spring Data generates one for String and Long ids. The _class field is also stored by default so the type can be restored on read. When you supply the id yourself, that value is used directly, which makes saves idempotent: saving the same id again overwrites the hash.
Redis can delete keys after a number of seconds, and @RedisHash exposes this declaratively:
Every saved UserSession expires after 1,800 seconds (30 minutes). To choose the TTL per instance instead, annotate a numeric property or method with @TimeToLive. When an entity has a TTL, Spring Data also keeps a short-lived shadow copy of the hash so it can clean up secondary indexes after expiry. Listening for expiry events needs keyspace notifications; set enableKeyspaceEvents on @EnableRedisRepositories or configure notify-keyspace-events on the server.
Redis is a key-value store, so queries on anything but the id need an index. Annotating a property with @Indexed makes Spring Data maintain a Redis set for each value, such as user:email:[email protected], which contains the ids of matching users. Derived finder methods like findByEmail use these sets.
Keep these limits in mind:
LIKE or joins.For complex querying, use a database or Redis modules built for search, and keep @RedisHash for lookups by id and a few indexed fields.
| Good fit | Poor fit |
|---|---|
| Sessions, tokens, short-lived state | Large, relational data with joins |
| Lookups by id or one indexed field | Range queries, sorting, full-text search |
| Entities that expire automatically | Data that must never be lost without persistence settings |
| Fast access to small objects | Very large objects with deep nesting |
If you only need a cache with TTLs, the Spring cache abstraction with RedisCacheManager is often simpler. Use @RedisHash when you want a repository-style model for objects that live primarily in Redis.
No. The object is flattened into hash fields by MappingRedisConverter. Nested objects become fields with dotted names, and collections are stored under indexed field names.
@RedisHash goes on the class and sets the keyspace, plus optional TTL. @Id (or a property named id) goes on a field and supplies the identifier appended to the keyspace to form the key.
user is the set of known ids, and the user:email:... keys are the secondary index sets created by @Indexed. Spring Data uses them to implement findAll and finder methods, so do not delete them manually.
Records with a single canonical constructor can be read by Spring Data's object mapping in current versions, but since records are immutable you must supply the id yourself or use a wither pattern. Test persistence of your exact version before relying on it.
@RedisHash turns a Java class into a Redis hash entity: its value sets the keyspace, @Id supplies the key suffix, timeToLive or @TimeToLive sets expiry, and @Indexed enables equality finders. Pair it with a CrudRepository for simple object storage with expiry, and choose a different store when you need rich queries.