Home > Software engineering >  Indexing frequently updated counters (e.g., likes on a post and timestamps) in Firebase
Indexing frequently updated counters (e.g., likes on a post and timestamps) in Firebase

Time:01-21

I'm new to firebase and I'm currently trying to understand how to properly index frequently updating counters.

Let's say I have a list of articles on a news website. Every article is stored in my collection 'articles' and the documents inside have a like counter, a date when it was published and an id to represent a certain news category. I would like to be able to retrieve the most liked and latest articles for every category. Therefore I'm thinking about creating two indices, one for category type (in ASC order) and likes (DESC order) and one of the category type and the published date (DESC order).

I tried researching limitations and on the best practices page I found this, regarding creating hotspots with indices:

Creates new documents with a monotonically increasing field, like a timestamp, at a very high rate.

In my example I'm using articles which are not created too frequently. So I'm pretty sure this wouldn't create an issue, correct me if I'm wrong please. But I do still wonder if I could run into limitations or high costs with my approach (especially regarding to likes which can change frequently, while the timestamp is constant).

Is my approach to indexing likes and timestamps by category a sound approach or am I overseeing something?

CodePudding user response:

If you are not adding documents at a high rate, then you will not trigger the limit that you cited in your question.

From the documentation:

Maximum write rate to a collection in which documents contain sequential values in an indexed field: 500 per second

If you are changing a single document frequently, then you will possibly trigger the limitation that a single document can't be updated more than 1 times per second (in a sustained burst of updates only, not a hard limit).

From the documentation on distributed counters:

In Cloud Firestore, you can only update a single document about once per second, which might be too low for some high-traffic applications.

That limit seems to (now) be missing from the formal documentation, not sure why that is. But I'm told that particular rate limit has been dropped. You might want to start a discussion on firebase-talk to get an official answer from Google staff.

Whether or not your approach is "sound" depends entirely on your expected traffic. We can't predict that for you, but you are at least aware of when things will go poorly.

  • Related