Caching an API with Redis
In this section, we will cover how to add a caching mechanism to our API. Let's imagine that we have a tremendous number of recipes in our MongoDB database. Every time we try to query a list of recipes, we struggle with performance issues. What we can do instead is use an in-memory database, such as Redis, to reuse previously retrieved recipes and avoiding hitting the MongoDB database on each request.
Redis is consistently faster at retrieving data because it is always in RAM – that's why it's an excellent choice for caching. On the other hand, MongoDB might have to retrieve data from disk for advancing queries.
According to the official documentation (https://redis.io/), Redis is an open source, distributed, in-memory, key-value database, cache, and message broker. The following diagram illustrates how Redis fits in our API architecture:
Let's say we want to...