Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodically run a cloud function every second without client input

I am working on an app, where I need to periodically (every second) need to write a new timestamp in a field in firestore, this write should be performed when a specific property of the document equals true, if not the periodic execution should stop - how can I do that?

like image 242
CondorW Avatar asked Mar 05 '26 11:03

CondorW


1 Answers

The easiest solution I can offer is to use Cloud Task. When a user create a session, create a dedicated task queue with a rate limit of 1 per seconds and a bunch of task in that queues (for instance 3600 task per hour).

That task will trigger a HTTP endpoint (typically a Cloud Functions or a Cloud Run endpoint) that will increment the counter.


The main question that I had was about the firestore choice. As far as I understand, if you have 10 users in parallel, you have 10 counter and you write 10 times the same thing in firestore. not really efficient.

I have 2 propositions here:

  1. Can you use a single counter and have several user object using the same count down?
  2. Did you consider Cloud Memorystore to use in memory database to perform your per-user-timestamp-write and save only the result in Firestore document.
like image 111
guillaume blaquiere Avatar answered Mar 07 '26 08:03

guillaume blaquiere