This is already implemented in the reference architecture for the open source MongoDB project called Socialization , although it is in Java, not node.js, so my answers are based on my experience and stress testing this code.
As you can see from its implementation of the status feed, the feed has an option fanoutOnWrite cache , which will create a cache (document with a limited size) for active users, limiting the number of recent entries in the cache document (this number is configurable).
The main principles of this implementation are that the requirements for the content are actually different from the requirements for cache caches, and first the database is written to the content, as this is a recording system for all content, and then you update the cache (if it exists ) This part can be performed asynchronously , if necessary. The update uses "limited arrays", aka updating the $ slice function to atomize the new value / contents into an array and slice the oldest of them at the same time.
Do not create a cache for the user if he does not already exist (if they never log into the system, then you are wasting your time). If you wish, you can use caches based on some TTL parameter.
When you start reading the cache for the user when you log in, but he is not there, then go back to "fanoutOnRead" (which requests all the contents of the users that they follow), and then builds their cache from this result.
The Socialite project used MongoDB for all third-party developers, but during comparative testing we found that the timeline cache does not need to be replicated or saved, therefore its MongoDB servers are configured only in memory (no log, no replication, no disk flushing), which is similar use of redis. If you lose the cache, it will simply be restored from the on-demand persistent content database.
Asya kamsky
source share