Working example of Spring Cloud Gateway with Redis session management?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Cloud Gateway can sit in front of downstream services while Redis holds shared session state, which is useful when requests may be routed across multiple instances. The key is to combine reactive gateway routing with Spring Session backed by Redis so session data survives beyond one JVM instance and can be reused on later requests.
What the Pieces Do
In this setup:
- Spring Cloud Gateway handles routing and filters
- Spring Session stores
WebSessiondata in Redis - Redis acts as the shared backing store for session attributes
This is different from using the gateway as a stateless token-only edge. If you choose session management, the gateway needs to persist and reload session state consistently.
Minimal Dependency Set
A typical Maven setup includes the gateway starter, Redis support, and Spring Session for Redis:
The important part is using the reactive Redis stack, because Spring Cloud Gateway is built on Spring WebFlux rather than the traditional servlet model.
Application Configuration
A small application.yml can define both the Redis connection and the gateway route.
SaveSession matters when the session has been modified and you want the gateway to persist it before forwarding the request onward.
A Simple Gateway Application
A minimal Spring Boot entry point is ordinary:
The interesting behavior comes from how requests interact with the session.
Writing Session Data Through the Gateway
A small controller inside the gateway can demonstrate that the session is really stored in Redis.
Hit /session repeatedly and the counter should continue increasing for the same client session. That is the easiest proof that state is surviving outside process memory.
A Downstream Service Example
You can route authenticated or session-aware traffic to a downstream service. Here is a tiny backend service on port 8081:
With the gateway route shown earlier, a call to /api/hello on port 8080 is forwarded to the backend.
In a real application, the gateway often combines routing with Spring Security and session-backed authentication state. Redis helps because multiple gateway instances can share the same session store.
Running Redis Locally
For local testing, a disposable Redis container is usually enough:
Once Redis is running, start the gateway and call the session endpoint:
Reusing the same cookie jar should show the session counter increasing.
Common Pitfalls
The most common mistake is mixing servlet-style session assumptions into a reactive gateway project and then using the wrong Redis or session dependencies. Another is forgetting the SaveSession filter when the gateway mutates session state before forwarding. Teams also often test with one gateway instance only and miss the real point of Redis-backed sessions, which is shared state across instances. A final issue is treating session management and stateless token-based designs as interchangeable even though they produce very different gateway behavior and scaling tradeoffs.
Summary
- Spring Cloud Gateway can use Spring Session with Redis to persist shared reactive session state.
- Use reactive Redis dependencies, not the servlet-centric stack.
- Configure Redis, gateway routes, and
SaveSessionexplicitly. - A simple
WebSessioncontroller is an easy way to verify persistence. - Redis-backed sessions are most valuable when gateway instances need shared session state rather than purely stateless routing.
Related reading
- Working of physical clock synchronization in distributed systems
- Write-through cache Redis
- Write Loss in Synchronous write in Redis Cluser
- Writing object as key in Gemfire Cache
- Working of compactions work in YugaByte DB
- Would querying 60 columns from a Snowflake table would cost me more than querying 20 columns?
- Would Java indexOf brute force method be more practical for me or some other substring algorithm?
- Write string to output stream

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.