Redis with ASP.NET Core for distributed sessions and horizontal scaling Back

Redis with ASP.NET Core for distributed sessions and horizontal scaling

Posted on 25/5/2021 under .net redis

Horizontal scaling is usually the preferred scaling solution to adopt, but they may not be feasible for web apps that relies on in-memory stores/caches for tracking session state or data. These apps needs to be re-architected to adopt the use of a distributed cache such as Redis to keep session data consistent across servers. Redis is usually use as a distributed cache or as a key-value in-memory database. 

I'll provided the same source on how to set them up for the 2 difference use case. I will try to make some videos to explain step by step. Stay tune for updates.

Redis for distributed caching

Github source code here

For this case, Redis is configured as a cache and sessions is set up as well (refer to startup.cs). Any data set or read via HttpContext.Session (refer to the home controller) is set/read to/from the redis cache. Session data are typically used in web applications.

Redis as key-value database

Github source code here

In this case, Redis is set up and used like a database. The RedisContext class connects to Redis and provides all the get/set/delete functions to directly access the specificed keys in Redis. This implementation is more applicable to APIs, where session data are not likely to be used since APIs are stateless.