I'm currently working on a new app which will use a NOSQL DB (particularly Cosmos DB) for data scalability. I need to implement a way to update counters every time a post receives a like or comment. As of now, I could think of two approaches to this problem:
- Write a JS stored procedure to check document
ETAGand increment counters only if the
ETAGis current. If not, run a loop and fetch the most recent version of the document before updating it. The document holding counters as well as like and comment records would belong to the same partition key.
- The counters should be eventually consistent. A
Functionis triggered to update them after the like or comment record is written to the database.
In each scenario, Cosmos DB would run on
Session consistency level.
Assume that a document is so popular that it begins to receive thousands of likes immediately. How would the first approach scale in this case? Since it's running in a CAS loop, it is likely to time out given time constraints. Second, is
ETAG comparison the right approach here? Does Cosmos DB write to only one replica so that we could rely on
ETAG changes to make sure we're updating the most recent version of a document?
As for the second approach, perhaps eventual consistency is fine, but I hate seeing mismatched information on websites such as (3 comments on a post, but you can read 5 total comments). If the lag was tiny, this could be tolerated, and I'm not sure how much of a lag a typical, say, social media app should expect.