Suppose we have a social network app (using NodeJS, Express) and MongoDB as the primary database engine.
In most of API calls from clients (mobile app, web app, etc.) I don't want to make a complex query for each request. These sort of requests can be replied from cache layer, Redis for instance.
But my question is how/when should I update the cache layer, because all write operations are performed in MongoDB database, not the cache layer (Redis). What is the correct approach/architecture to address this problem?
It really depends on your needs, but here's a fairly common one:
on_get_request
if data_in_redis
serve_data_from _redis
else
get_data_from_mongo
set_data_in_redis
set_expire_in_redis
serve_data_from_memory
The data will be a bit stale at times, but that's ok for most use cases. It works well in combination with some cache invalidation when important data is written:
on_important_data
delete_invalid_redis_keys
But that all assumes low write, high read, and a stable set of queries.
What does your high load use case look like?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With