I’m following an online course which covers this topic on caching with Redis and MongoDB. The tutor mentioned about overriding query.exec()
in mongoose by inserting the cache check logic each time before executing a query.
The cache stores data as key-value pairs with the query object (obtained from .getQuery()
) & the collection name as the key, and the query result as the value. If a same query has been evaluated before, return saved result. Otherwise, call query.exec()
.
Regarding his approach, I have following concerns:
- Overriding query.exec()
Are we sure we want to consult redis before sending every single query? That likely would cause extra overhead to queries that are rarely revisited.
- Super long keys in redis
The amount of keys saved in cache have to be set with a limit right? Redis is gonna be bloated very soon if the application receives a high traffic.
Are my concerns sensible? Are there ways to walk around the issues I mentioned above? What’s the most common approach for caching with Redis and Mongo? Assuming the application we are developing is a blog with high daily traffic.