0
votes

We're working on an app that makes use of Firebase Firestore. Every day, at midnight, we'd like to set the value of a counter property to 1 for every User document in a collection.

Cloud functions seemed like a reasonable choice for this, and we quickly found a means to schedule Cloud Functions:

exports.incrementCounterNightly = functions.pubsub.schedule('0 0 * * *')
  .timeZone('Europe/London')
  .onRun((context) => {
  console.log('This will be run every day at 00:00 AM London Time!');
});

The path to the documents we'd like to write to is collections/users/*.

Our questions are:

  • What's the best means to achieve this within Cloud Functions?
  • Is this something Cloud Functions can suitably achieve?
  • Is this scalable? What if the users collection grows to 100,000, or even 5,000,000 documents?

Many thanks in advance,

Ed

1
What does the counter do? What does it count? This is not scalable at the moment. Partly due to programming restraints but mainly due to cost. Writes are relatively expensive so writing to a 1,000,000 user documents is gonna cost. If you can explain what you’re trying to do with the counter there might be a better way to do it. - Fogmeister
Quick edit on the above comment. 5,000,000 writes at the start of each day would cost you around $900,000 a day. 😂 so yeah, writes are expensive. - Fogmeister
Hey @Fogmeister, appreciate the quick response. We're working with a dating app, and this counter essentially tracks how many available "superlikes" the user has. I had considered that this could theoretically just be tracked locally on the device, but it would be nice to keep a representation on the server too - for customer support, etc. Thanks also for highlighting the cost - an extremely important consideration with Firestore ! And yes, as extortionate as expected perhaps! 😂 - Edd677
BTW I don't know how but I misread the pricing (which I thought sounded a bit much) but it's $0.18 per 100,000 writes. So it would be $9 a day. LOL! Slight difference but still significant if you don't want to be spending several hundred dollars a month on it. - Fogmeister

1 Answers

1
votes

Due to cost and programming restraints I think you should consider structuring your data in a different way.

What you could do for your use case is not just store a raw count of the “superlikes” used per user but maybe store daily superlikes in a separate document with the date.

So if you want to query how many super likes a user has used today you could query the users “superlikes” subcollection. have a single document in there with the date of the last superlike and how many were used.

So when querying how many they have used today... get the document, if it has a different date then 0 (because it’s not today’s date) if it has today’s date then look at the count.

Then when the user uses a superlike you can go back to that document. Either set the count to 1 and the date to today’s date. Or just increment the count of the date is already today.

Let me know if you have any questions about what I’ve tried to explain.