Centralized Session Management with Spring Session & Redis

Problem

Probable Solutions

  1. Centralized Sessions — Maintain sessions in a common location for all instances to share.
  2. Consistent Hashing — Load balancer decides which instance to send the request to.

Architecture for Microservices

  1. Zuul Gateway
  2. UI Microservice
  3. Eureka Microservice

How does Centralized Sessions work in Spring?

How does this work?

  1. We inform Spring that sessions will now be cached in Redis.
  2. Spring receives a request.
  3. Spring Security kicks in and user is authenticated.
  4. Spring Session object is serialized and saved in the cache.
  5. Client gets a cookie with the Session ID.
  6. Client then sends the session id for further requests.
  7. Any instance of the UI Service will check in the cache for a session object against the Session ID provided by the client.
  8. Session object is de-serialized and reused.

Implementation

UI Service

Gradle dependencies for UI Service
Spring-Boot Main Class
bootstrap.properties or application.properties

Testing

New keys generated for user thussaid

--

--

--

I like building stuff.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

When (and why) not to go serverless

Uploading objects to S3 using one-time pre signed URLs

How I host all my personal projects for £10 / month (<$15) on AWS

Learning From Our Mistakes 📈

Azure function Eventhub trigger: Checkpoint

Branch and Artifact Filters in Azure Pipelines

Deploy a React/Node App Using TravisCI and AWS

Soft Skills for Software Engineers — Part 2: Get Things Done

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Sharma

Gaurav Sharma

I like building stuff.

More from Medium

Reactive Programming Using Spring Webflux

How to implement Reactor RabbitMQ in your reactive Spring Boot application — Part 1

Serverless with Spring

How to Integrate GraphQL API in Spring Boot?