Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB and Redis as cache layer architecture

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?

like image 610
Afshin Mehrabani Avatar asked Jun 26 '14 05:06

Afshin Mehrabani


1 Answers

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?

like image 96
generalhenry Avatar answered Sep 18 '22 18:09

generalhenry