Real-Dev-Squad / website-backend

The backend code for all our website-related apps
https://api.realdevsquad.com/
MIT License
54 stars 255 forks source link

[RFC] Generating auto incrementing sequence for task number #800

Open YashJain24-chief opened 2 years ago

YashJain24-chief commented 2 years ago

https://github.com/Real-Dev-Squad/todo-action-items/issues/77

Approach 1

When a new task is created, update the counter by reading from and writing to the taskCounter collection that will store the previous value of the counter.

Pros

  1. Lesser number of reads and writes as we have a separate collection. Basically, only 1 read for getting the previous value and 1 write for updating the value in collection everytime a new task is created.

Cons

  1. Creating a whole new collection just for storing the previous counter value
  2. Two operations are performed - Reading the old counter value and then using it to increment the value and storing back to the taskCounter collection.

Approach 2

Getting QuerySnapshot of the task collection and using size property

Pros

  1. No need to maintain a collection just to maintain a single count value. Good for medium-sized collections that contain around 100-1000 documents.

Cons

  1. The main problem is the cost, by executing this query we will be charged for one read for each doc of the sub-collection.

Approach 3

Using the count aggregation query.

Pros No need to read and write to the firestore every time. It will basically return the number of documents in a collection which can then be incremented and stored when a new task is created. We are charged a small number of reads for a large number of matched entries.

Cons If a count() aggregation cannot resolve within 60 seconds, it returns a DEADLINE_EXCEEDED error. Performance depends on the size of the dataset.

Also what happens to the tasks that are already in the DB? How do we have a task number for those tasks?

ankushdharkar commented 1 year ago

I am in favour of Approach 3, small to medium docs usually take less than 50ms. I think by the time we get to so large collection sizes that it starts exceeding, we would have migrated to better infrastructure or could do so quickly.