If you are thinking about introducing Redis into your tech architecture, this tutorial will give you a list of points that you should consider. When you use Redis to store important data, like product pricing, Redis becomes a key part of your tech estate. If your Redis connection goes, your whole site will likely go tits up. I've used Redis on numerous projects now and I've definitely experienced a few hidden gotchas along the way. If you want to be a champ at your work and avoid the same mistakes I made, then read on.

Make Sure You Have A Fallback Option If Redis Goes Down

I introduced Redis within Azure on this website for a brief period. To save on hosting costs, I used the free demo credits you get with your MSDN subscription to pay for it. I didn't realise that when you get a Redis server on your development license, halfway through the month it cuts out. When my Redis server cut off my whole site died instantly. When I noticed this happened I was on holiday so there wasn't even an easy way to fix the issue without doing a code deployment.

If you are considering using Redis, I assume you won't be a penny pinch like me. The point of the story is to plan for what happens if Redis dies. One option is to pay a little extra and get a clustered Redis to ensure you always have a connection. The other option is to have a quick and easy way to recover your website in case Redis goes down/isn't available. In my instance, I changed my code so I could turn it on and off via the Azure portal. If something goes wrong again, I can set a flag that will bypass Redis.

Like SQL, a Redis instance can be clustered. As of Redis 4.0.0 a number of improvements were made to Redis clustering and this is probably one of the easier options to ensure that your Redis server is always available and has good resilience.

Be aware, Redis Can Take Your Site Down Just Trying To Establish A Connection

Recently I had really bad performance issues on my website. I had disabled my Redis cache, however, I forgot that on application start I was still establishing the Redis connect. Even though I wasn't trying to add anything to Redis, or read any value from it the call to GetDatabase() produced a memory leak in my website. This caused the site to fall over. After an IISRESET the site would come back online. After a few hours, the site would get slower and slower until IIS was rebooted.

If you're using Redis be aware that establishing a failed connection to Redis can cause issues. If you create a cache manager class then ensure that it's a singleton and that only one instance of the class can be created, otherwise, you could experience the same application tool hanging issues I ran into.

The Azure Redis monitor can be an indicator that your website is having issues

I worked for one client who had the production Azure Redis metrics running on a TV located in the client's IT area. Whenever there was a spike in Redis, someone would come over and ask if there was anything wrong with Redis. As I explained to them, Redis is simply an advanced key/value store. If you have a spike in Redis then it means something is happening on your website. If Redis spike this means your website might be getting a sudden spike. If Redis can not handle spikes in load, you may need to look at getting bigger Redis instances with more processing power. When implementing Redis, and trust it!

My Redis Database Size keeps getting increased, should I be worried?

Yes and No. First, if you're used to using a session cache then you'll probably understand that if you could visualise the total amount of memory the session cache used in real-time, you would see a graph that would constantly jump up and down as new cache items get added and expire from the cache. Redis doesn't necessarily work this way, which can be confusing. Redis can be set to use a policy to only expire the cache when it's full. This means your cache might grow until it hits your max size and only at that point it will it start to clean up the cache. Redis can be configured in different ways (which is beyond this article) but it is something worth being aware of.

On the other hand, the full key/value store must fit in RAM at all times, so depending on how your Redis is set-up, if you hit your max size you could encounter fatal issues. This is one of the reasons you need semi-good knowledge about how to configure Redis. You need enough RAM to hold your entire set of data. My recommendation would be to test hitting your max limit locally and see what happens before releasing Redis into the wild!

Redis Isn't SQL

I've had a number of developers propose using Redis for the wrong thing. For example, as a log file storage which we could then 'query'. Redis is a key/value store and consequently, the paradigm of getting data in and out of it is completely different. Don't expect to be able to query data like in a database. If you need to query your data then it does not live within Redis!

Only Store Temporary Data In Redis

This isn't a hard and fast Redis rule, however, my personal preference is to only use Redis to store temporary data that you can afford to lose. I tend to use Redis to store things like HTML fragments, user data etc. I wouldn't use Redis to store data that I want to keep forever. The way I recommend you architect my projects is that any data stored in Redis is fine if it gets deleted. Any data that is vital to the running of the system, store in a database, an application setting, or if I'm working with a CMS, within the CMS itself.

Use Your Redis Tables

When you start talking to Redis it is very likely that you'll be talking to database 0 by default. It is possible to create different databases to allow better segregation of data within your application, all for free! In case you're wondering how many extra Redis databases you can create, then the answer is apparently 'unlimited'. The limiting factor is the available memory in the cluster. If your total Redis size is bigger than your allocated amount, then you need to worry.

In the past I've tended to use one database for user sessions data, one to store HTML partial fragments etc... If money isn't a factor, you can also consider using two or more Redis instances, rather than using a different database within a single Redis instance. For reference, one or more Redis databases can be used in a single instance (e.g. one cost), if you use two instances in the cloud, then you'll have to pay for two Redis servers. This separation is probably overkill for most situations, however, it means one Redis could go down while the other one is up.

Redis Server Takeaway

Redis is a cool tool that I've used on a number of projects to great success. Redis is a lot more configurable compared to using something like a normal in-memory session cache. Anyway, I hope this article helps prepare someone out there, good luck! Happy Coding 🤘