oscklm
oscklm•17mo ago

Keeping track of stats, docs count etc with Convex?

Heeey 👋 So i've grown quite curious over the last couple of days on how to count and keep track of stats. Is there a suggested way to count tables total docs and such with Convex? Scenario I go ahead and create a new post in our admin dashboard, and the newly created post then gets reflected on the front dashboard page which shows various stats about our platform, total videos, gross revenue, total users etc. all of these coming from various apis. Idea 1: Real-Time Updates Some method to increment or decrement some kind of counter in a statistics table, then each time an item gets created or deleted we find its stat and increment/decrement it. Would this be a viable approach? I have some bad feelings about this 🫣 Idea 2: Periodic Updates Some recurring scheduled task that runs every 15-30 minutes, calculating the total documents in several tables. However, I'm uncertain about how to do this, and also keep it both performant and usage-efficient in terms of cost. I would love to start a discussion about this topic and explore the different potential solutions. Any suggestions or advice are welcomed!
6 Replies
ballingt
ballingt•17mo ago
I'd love to hear about your bad feelings about idea 1! This would be my default, and I might move to idea 2 as a performance or cost optimization for specific tasks if it turned out to be necessary. I think the first approach is just easier to reason about.
oscklm
oscklmOP•17mo ago
Ohh awesome! I already kinda know how i'd go about it too. And like we are not talking about tracking hundreds of different stats, more like 10-20 at most i'd say - so maybe the cost efficiency is not something i should worry about. And yeah as u say, atleast not for now. My concerns are: - Data inconsistency, lets say if Sarah decides to add something, and Peter decides to delete something in that very instance, could the count become inconsistent?
ballingt
ballingt•17mo ago
If Sarah and Peter are users, there's no problem! Convex mutations are transactions, so this will just work! sorry big edit, I misread your question! (if Sarah and Peter are developers writing Convex functions this is a different problem — now it's an abstraction / developer communiation issue)
oscklm
oscklmOP•17mo ago
I think my "luck" as of now, is that i'm the only dev in our team. BUT this insight is really awesome and something to def keep in mind (regarding what u wrote before lol). I'm also trying my very best to prepare for when new devs get hired to our startup, so what you brought up about the developer side of things, great food for thought.
ballingt
ballingt•17mo ago
The mutations as transactions thing is pretty sweet, it means you can do what in SQL you've use locks and transactions for just by writing JavaScript code: write one row, write another, and then both will happen — see https://stack.convex.dev/dont-drop-acid and https://docs.convex.dev/database/advanced/occ for more
Don't Drop ACID
When your database supports ACID semantics, you're free to write code the intuitive way and ignore the complexities of concurrency and failure.
OCC and Atomicity | Convex Developer Hub
In Queries, we mentioned that determinism
oscklm
oscklmOP•17mo ago
That's soo cool though. That it just works 🤯 i will read resources u linked, would love to learn more about this Aaaand, yet again i get more excited about Convex. You guys rock 💙

Did you find this page helpful?