DEV Community

Cover image for Smart Caching with Redis
Carlos Almonte
Carlos Almonte

Posted on

Smart Caching with Redis

Why would you want your products list to load faster? Time is arguably the most valuable resource, saving as much time as possible is important so that we can spend it doing the things we love. Furthermore, top e-commerce websites; Amazon, Zara, and Lulus are very efficient at saving customers' time. Their products lists load very fast, allowing customers to browse quicker and make a decision faster. Because of these other websites customers expectations are very high when they visit your less popular e-commerce site. In fact, there is a term in the industry called Jakob's Law, "Users spend most of their time on other sites. This means that users prefer your site to work the same way as all the other sites they already know.". Therefore, serving your products list is important since it will save time and make your store a more familiar place for online shoppers.

Elon Musk

In order to serve your products list faster there is a technique called caching which allows the products list data to be saved 'in memory' instead of the hard drive. This allows for data to be retrieved and served significantly faster. When the CPU has to communicate to the RAM it does it through a 'bus' which is a term given to pretty much the cable between the CPU and the RAM. This cable can communicate 64bits per CPU cycle for example, so if the list is 128bits in size it would take 2 cycles, on the other hand, the cable between the CPU and the hard disk can communicate 8bits per CPU cycle, meaning that it would take 16 cycles to deliver the same amount of data. So if a CPU had a speed of 1 cycle per second it would take 2 seconds using caching and 16 seconds without caching.

Redis is a popular tool used for caching. It provides an API to save your data in the RAM. Also provides many other features that are useful once you start getting into the depths of caching, data invalidation, updating data in the cache, expiring data, and more.

RAM pickup truck

I titled this post with Smart Caching because you could either cache the entire product list, paginated, and filtered, or you could go into the logic that creates the list and cache specific data that is used. Example, products list change with season, sales dates, and product availability, sometimes it also changes depending on the place you're visiting the website from. Given all of these variables, you can cache the data for the seasons data, the data for the sales, and the data for locations, this is more accurate than saving the entire product list because if you do regular caching you will end up creating a many different products list, one for summer+bogo+east, another one for fall+bogo+west, winter+bogo+south, and so on. If your products list is hundreds of products long it will create too much data. On the other hand, if you have the data you need on the cache you can create the list at runtime which allows to save less data in the RAM and create faster user experience than using data from the hard drive.

In conclusion, customers spend most of their time using other websites, websites that have spent a lot of money in making sure customers have the best experience shopping. This translates into customers saving time which they can spend doing more of the things they love. Redis is a tool that enables this by serving data from a faster storage in the computer.

References:

https://lawsofux.com/jakobs-law/

https://redis.io/learn/howtos/solutions/caching-architecture/common-caching/write-behind-vs-write-through

Top comments (0)