Scrivio Interview library
ConceptFoundations5 min read · updated 13 November 2026

Spring Session can keep HTTP sessions in Redis so any application instance can serve any request. It offers a simple repository and an indexed one, which store different keys.

Describe what the default Redis session repository stores per session and how expiry works, and what the indexed repository adds and costs.

The default RedisSessionRepository stores each session as one hash, spring:session:sessions:<id>, with creationTime, lastAccessedTime, maxInactiveInterval and one sessionAttr:<name> field per attribute, and sets the key's TTL to the inactive interval, so expiry is plain Redis key expiry pushed forward on each save and there are no session-destroyed events. The indexed RedisIndexedSessionRepository adds an index by principal name and publishes created, deleted and expired events, paying with extra keys per session, keyspace notifications and a background cleanup task. Attributes use JDK serialization by default, and a springSessionDefaultRedisSerializer bean switches them to JSON.

Why this question is asked

Spring Session is the usual way to make a Spring application stateless at the instance level: the HTTP session moves from the server's memory to Redis, so any instance can serve any request and deployments do not log users out. Most teams add the dependency and never look at what lands in Redis, until a managed service refuses CONFIG or memory grows. Interviewers ask this to check that we know what the two repositories store and what the indexed one costs. In this answer, we will look at the default repository's keys and expiry, then the indexed repository's extra keys, events and requirements, and finally serialization.

1A Quick Recap of Spring Session

A web session holds state for one user across requests, identified by a cookie, usually SESSION. Spring Session replaces the servlet container's session storage with a SessionRepository: on each request it loads the session from the repository, and at the end of the request it saves the changes back. With Spring Boot 3, adding spring-session-data-redis and the Redis starter is enough to switch it on, and the repository type is chosen by the property spring.session.redis.repository-type, default or indexed.

2What the Default Repository Stores

The default RedisSessionRepository keeps one Redis hash per session. The key is the namespace plus the session id, by default spring:session:sessions:<id>. The hash holds three bookkeeping fields and one field per attribute, each attribute name prefixed with sessionAttr:.

Text
HGETALL spring:session:sessions:3f1c9a...
-- 1) "creationTime" 2) "\xac\xed..." (JDK-serialized Long: 1731400000000)
-- 3) "lastAccessedTime" 4) "\xac\xed..." (Long: 1731401200000)
-- 5) "maxInactiveInterval" 6) "\xac\xed..." (Integer: 1800 seconds)
-- 7) "sessionAttr:cart" 8) "\xac\xed..." (serialized Cart object)
TTL spring:session:sessions:3f1c9a...
-- (integer) 1792 seconds until the session expires

Notice that expiry is plain Redis key expiry. Each time the session is saved, which happens on every request because lastAccessedTime changes, the repository sets the key's TTL back to maxInactiveInterval, 1,800 seconds by default. If the user goes quiet for 30 minutes, the key expires and Redis removes it. Nothing in the application is told; there are no session-destroyed events. For most applications that is exactly right: one key per session, no extra traffic, nothing to configure on the server.

3What the Indexed Repository Adds

RedisIndexedSessionRepository, chosen with spring.session.redis.repository-type=indexed in Boot 3 or @EnableRedisIndexedHttpSession, adds two capabilities. It implements FindByIndexNameSessionRepository, so we can find all sessions of a user by principal name, which is what 'log out my other devices' or concurrent session control need. And it publishes SessionCreatedEvent, SessionDeletedEvent and SessionExpiredEvent, so the application can react, for example to write an audit record when a session ends.

Step throughKeys per session in the indexed repository
  1. spring:session:sessions:<id>The session hash, as in the default repository
  2. spring:session:sessions:expires:<id>Empty key whose expiry triggers the expired event
  3. spring:session:expirations:<minute>Set of ids expiring in that minute, for cleanup
  4. spring:session:index:...PRINCIPAL_NAME_INDEX_NAME:<user>Set of session ids for one principal

4 layers, from the top down. Step through them one at a time.

Key names as in Spring Session 3; check your version.

4What the Indexed Repository Costs

Those capabilities cost something. Each session now has extra keys and set memberships, so writes multiply. To publish expired events the repository relies on Redis keyspace notifications, which must include expired events (notify-keyspace-events with E and x, for example Egx). By default it tries to set this itself with CONFIG SET at startup, which fails on managed services that block CONFIG, so there we disable that with spring.session.redis.configure-action=none and set the flag in the provider's settings.

Redis expires keys lazily and in a background sweep, so an expired event may arrive late. To make expiry timely, the repository also runs a cleanup task, every minute by default, that touches the keys listed in the current expirations set to force their expiry. Notifications go to every subscribed instance, and the cleanup task runs on each instance, so the indexed repository adds steady background load that grows with the number of sessions.

Default versus indexed repository
AspectDefaultIndexed
Keys per sessionOne hashHash plus expiry key, set entries
Find sessions by userNoYes, findByPrincipalName
Session eventsNoneCreated, deleted, expired
Server configurationNoneKeyspace notifications for expired events
Background workNoneCleanup task every minute

5Serialization of Attributes

By default, attribute values and bookkeeping fields are written with JDK serialization, which is why they start with the bytes \xac\xed in redis-cli. That format is unreadable to people, ties stored sessions to exact Java classes, and fails to deserialize when a class changes incompatibly between deployments. Declaring a RedisSerializer bean named springSessionDefaultRedisSerializer replaces it.

Java
@Bean
RedisSerializer<Object> springSessionDefaultRedisSerializer() {
return new GenericJackson2JsonRedisSerializer(); // JSON values, readable in redis-cli
} // with Jackson 3 in newer Spring Data Redis, check the serializer class name for your version

JSON makes sessions readable and more tolerant of added fields. Storing the Spring Security context as JSON needs Spring Security's Jackson modules registered on the ObjectMapper, so test login after switching.

6Follow-up questions

When do you actually need the principal-name index?

When the application must act on all sessions of one user: showing a user their active sessions, 'log out everywhere' after a password change, an administrator ending a user's sessions, or Spring Security's concurrent session control through SpringSessionBackedSessionRegistry, which limits how many sessions a user may have. If none of these exist, the default repository is simpler and cheaper.

Does a session read without changes still write to Redis?

Yes, at least lastAccessedTime is updated and the TTL is pushed forward on each request that touches the session, which is how sliding expiry works. Attribute fields are written only when set, under the default save mode.

Can the default repository publish any events?

No. It relies purely on key expiry, and Redis does not tell the application when a key expires unless keyspace notifications are enabled and something listens, which is exactly the machinery the indexed repository adds.

7Conclusion

In this answer, we saw that the default RedisSessionRepository stores one hash per session under spring:session:sessions:<id>, with bookkeeping fields and sessionAttr fields, and relies on the key's TTL, refreshed on each save, with no events. The indexed repository adds a principal-name index and created, deleted and expired events, at the cost of extra keys, keyspace notifications that need CONFIG or provider settings, and a cleanup task. Attributes use JDK serialization unless a springSessionDefaultRedisSerializer bean switches them to JSON.

What to say in the interviewAbout 80 seconds aloud

Open with

The default RedisSessionRepository stores each session as one hash, by default under spring:session:sessions followed by the session id. The hash has creationTime, lastAccessedTime and maxInactiveInterval, plus one field per attribute named sessionAttr: and the attribute name.

Then, if they want more

The key's TTL is set to the inactive interval, 30 minutes by default, and is pushed forward every time the session is saved on access, so expiry is just Redis key expiry and there are no session destroyed events.

The indexed repository, which in Boot 3 I select with spring.session.redis.repository-type=indexed or @EnableRedisIndexedHttpSession, adds an index by principal name, so I can find all sessions of a user, and it publishes created, deleted and expired events.

It pays for that with extra keys per session, keyspace notifications that must include expired events, which it tries to set with CONFIG SET unless I configure it in the provider, and a cleanup task that runs every minute. So I only use it if I need lookup by user or session events.

Attributes are JDK-serialized by default; I would declare a JSON RedisSerializer bean named springSessionDefaultRedisSerializer, which makes them readable and more tolerant of class changes.

About this answer

Last updated
13 November 2026
Versions it names
Spring Boot 3
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Describe what the default Redis session repository stores per session and how expiry...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/cfbafc9ccf/