Event Driven API Cache

Introduction

Quite often we, as developers, cache resources in memory to improve app performance and reduce server load. By storing frequently accessed data in memory you avoid using CPU cycles to fetch a fixed set of data from your database.

Traditionally the cache concept has been limited to static data or data that has a long lifespan. What about API's that serve up frequently changing data? Many times this data is not truly "dynamic", but rather constantly changing. It's possible to cache these type of requests using an event driven architecture.

Event Driven Scenario

The concept is simple - every time a data change occurs an event is triggered. This event forces your cache to flush the old data and insert the new data.

Lets use the example of an eCommerce site with inventory data that contains product SKU's, price and number of units available. This data is cached so various services, like the website and iOS app, can pull product inventory levels without putting a tremendous load on the server.

Every time a new unit is sold or additional stock is added, the cache for that product must be updated. In this example, we have made the assumption that you are using a traditional database (MySQL or Postgres) with a cache layer placed on top. It is critically important to flush and insert the new data as quickly as possible to avoid any kind of read errors on inbound requests. Given the requirement of a quick flush and insert, what caching software is the best choice?

Redis or Memcached

When it comes to in-memory caching there are two major players: Redis and Memcached. Both are key-value storage based on NoSQL architecture.

Our simple experiment is not intended to represent all use cases, but rather provide some kind of baseline. Which software will not only be able to flush data quickly, but also re-populate it quickly?

To compare the two we generated a sample data set with 500,000 records of SKU's, prices and units available. We inserted the entire data set into the cache, then flushed it. We ran this test 5 times and compiled the results below:

Redis (500,000 records)

Insert:  
        Max: 24196 ms
        Min: 23817 ms
        Avg: 24014 ms
Flush:  
        Max: 177 ms
        Min: 175 ms
        Avg: 176 ms


Memcached (500,000 records)

Insert:  
        Max: 36862 ms
        Min: 36041 ms
        Avg: 36442 ms
Flush:  
        Max: 1 ms
        Min: 0 ms
        Avg: 0 ms


We expected Redis to win outright on both tests. However, the results reveal that Memcached has near instant flush characteristics. The insert performance of Redis was significantly better than Memcached, doing it nearly 35% quicker. With a larger data set, it's likely this margin would grow further.

This analysis does not account for the actual performance of the cache when serving requests. Generally Redis is regarded as having better performance and more robust caching due to a more advanced memory management algorithm. This experiment was simply intended to benchmark the insert/flush speed of each software.

Please leave a comment below if you have any questions!