{
  "schema_version": 2,
  "id": "integrate/spring-framework-cache/session",
  "title": "Store sessions with Spring Session",
  "url": "https://redis.io/docs/latest/integrate/spring-framework-cache/session/",
  "summary": "Keep HTTP session data in Redis with Spring Session Data Redis.",
  "tags": [
    "docs",
    "integrate",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "client"
  ],
  "last_updated": "2026-11-17T15:08:03Z",
  "page_type": "content",
  "content_hash": "cb285597a8d0c7f0ec0ebcaa9bf90289638ab0f1940bb2613ca2c8cff9d67e58",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[Spring Session](https://spring.io/projects/spring-session) replaces the servlet\ncontainer's in-memory HTTP session with one stored in Redis. Several instances of your\napplication share the same sessions, and sessions survive a restart. See the\n[session store use case](https://redis.io/docs/latest/develop/use-cases/session-store) for more\nabout this pattern."
    },
    {
      "id": "enable-redis-backed-sessions",
      "title": "Enable Redis-backed sessions",
      "role": "content",
      "text": "With Spring Boot, add the Spring Session starter for Redis next to the\n[Spring Data Redis starter](https://redis.io/docs/latest/integrate/spring-framework-cache#requirements)\nand a web starter:\n\n[code example]\n\nSpring Boot then stores your `HttpSession` data in Redis with no further code:\n\n[code example]\n\nTwo requests that share a session cookie return `1` and then `2`. Spring Session stores\nthe session as a hash under the key `spring:session:sessions:<session-id>`, with a\ntime to live (TTL) equal to the session timeout. The default timeout is 30 minutes."
    },
    {
      "id": "choose-a-repository-type",
      "title": "Choose a repository type",
      "role": "content",
      "text": "Spring Session provides two Redis repositories. Set the type with a property:\n\n[code example]\n\n| Type | Behavior |\n| --- | --- |\n| `default` | Stores each session as one hash with a TTL equal to the timeout. |\n| `indexed` | Also keeps an expiration entry for each session and a set of expirations, and supports looking up sessions by index, such as by user name. |\n\nWith the `indexed` type, Spring Session sets the Redis `notify-keyspace-events`\nconfiguration to `gxE` at startup, so it can publish session expiry events. It also\nsets the TTL of the session hash to the timeout plus 5 minutes, so the expiry handling\ncan still read the session when it expires. See\n[Keyspace notifications](https://redis.io/docs/latest/develop/pubsub/keyspace-notifications) for\nmore about the setting."
    },
    {
      "id": "configure-the-namespace-and-timeout",
      "title": "Configure the namespace and timeout",
      "role": "configuration",
      "text": "Use these properties to change the key prefix and the session timeout:\n\n[code example]\n\nWith this namespace, sessions are stored under `myapp:sessions:sessions:<session-id>`,\nand a 15-minute timeout gives the session hash a 15-minute TTL (20 minutes with the\n`indexed` type)."
    },
    {
      "id": "use-a-service-that-doesnt-allow-configuration-changes",
      "title": "Use a service that doesn't allow configuration changes",
      "role": "content",
      "text": "If your Redis service doesn't let clients change the server configuration, tell Spring\nSession not to set `notify-keyspace-events`:\n\n[code example]\n\nSpring Session then leaves the setting alone, and sessions work as before. You must\nenable keyspace notifications yourself if you want session expiry events."
    }
  ],
  "examples": [
    {
      "id": "enable-redis-backed-sessions-ex0",
      "language": "xml",
      "code": "<dependency>\n  <groupId>org.springframework.boot</groupId>\n  <artifactId>spring-boot-starter-session-data-redis</artifactId>\n</dependency>",
      "section_id": "enable-redis-backed-sessions"
    },
    {
      "id": "enable-redis-backed-sessions-ex1",
      "language": "java",
      "code": "@RestController\npublic class SessionController {\n    @GetMapping(\"/hit\")\n    int hit(HttpSession session) {\n        Integer count = (Integer) session.getAttribute(\"count\");\n        int next = count == null ? 1 : count + 1;\n        session.setAttribute(\"count\", next);\n        return next;\n    }\n}",
      "section_id": "enable-redis-backed-sessions"
    },
    {
      "id": "choose-a-repository-type-ex0",
      "language": "properties",
      "code": "spring.session.data.redis.repository-type=indexed",
      "section_id": "choose-a-repository-type"
    },
    {
      "id": "configure-the-namespace-and-timeout-ex0",
      "language": "properties",
      "code": "spring.session.data.redis.namespace=myapp:sessions\nserver.servlet.session.timeout=15m",
      "section_id": "configure-the-namespace-and-timeout"
    },
    {
      "id": "use-a-service-that-doesnt-allow-configuration-changes-ex0",
      "language": "properties",
      "code": "spring.session.data.redis.configure-action=none",
      "section_id": "use-a-service-that-doesnt-allow-configuration-changes"
    }
  ]
}
