Scaling query with counters

Actually, I think I now understand after reading How to update post count and vote count counters - #4 by Bryan_Fauna.

I can see how this could work in my case, by adding a hitLog collection, and a aggregatedHits collection.

The hitLog would be just using insert during a redirect, right after I queried the current key/value from my first post.

And a scheduled Netlify function could populate the aggregatedHits collection, at specific intervals. (As often as possible).

Questions and concerns I have now:

  • Should those two collections also have indexes?
  • Can the hitLog & aggregatedHits be built by day?

EDIT: As Netlify’s scheduled functions aren’t production ready, maybe a solution could be:
Not have an aggregatedHits collection, only a hitLog.

Then flow could look like:

  • User clicks link
  • Query current data, join with hitlog data from today
  • select key with least hits
  • Write new new document to hitlog
  • Do redirect

Then set documents in hitlog to have a ttl of ~48 hours.

Does that make any sense and would allow me to avoid contention entirely?

An alternative idea:
Don’t have a hitlog either, only the two collections I have now.

  • user clicks link
  • Get the latest document for the uuid that was requested
  • Grab the key with the least hits
  • Increment the value in JS
  • Create a new Document with incremented values
  • Redirect user

I hope this makes sense? But unsure if this would solve the contention issues at all? And maybe have another process deleting documents older than 48 hours.