When you have the pleasant problem of trying to handle lots and lots of users, you quickly realize that tracking user state in the HTTP session doesn’t scale very well. To keep each session in memory could consume quite a lot of memory, for example, and distributing the session across multiple nodes is a bit cumbersome.
Many use cookies for this, but then we easily get the problem of too big and too many cookies in each request.
A solution to this can be to store the session in an external datastore, say Redis. This was the subject for the talk ‘Manage your user’s session with Spring Session’ by David Gomez.
As it turns out, it’s very easy to implement this with Spring Boot: you add the spring-session-data-redis dependency to your project, add the @EnableRedisHttpSession to your starter class, and boom: you’re done.
By default, the implementation uses session cookies, but you can change this to use HTTP Headers instead.
David Gomez made the claim that ‘The reason for the requirement on REST services to be stateless is mostly from scalability concerns, and using Redis-backed sessions lessens this concern’. I’m still not sure if it’s a good idea to introduce state into REST services, but it’s certainly an interesting thought.
I couldn’t find the slides for this presentation yet, but I hope they will show up soon.