{"success":true,"name":"own-every-derived-number-decide-where-totals-and-counters-ar","description":"Storing a count or total that summarizes other records (members count, total paid)","category":"ai-brimind","path":"ai-brimind/own-every-derived-number-decide-where-totals-and-counters-ar/SKILL.md","file":"SKILL.md","content":"---\nname: own-every-derived-number-decide-where-totals-and-counters-ar\ndescription: \"Storing a count or total that summarizes other records (members count, total paid)\"\n---\n\n# Own Every Derived Number: Decide Where Totals and Counters Are Computed and What Keeps Them True\n\n## When to use\nYou have (or are adding) a stored field that duplicates what other records already imply — memberCount, totalRevenue, lastPaymentDate — and no single mechanism guarantees it stays in sync.\n\n## Example request\nclub.memberCount stored for fast lists, updated in the 'add member' endpoint → the import script and the delete flow forget it; six months later the number is fiction and nobody knows since when. The count computed live from the members collection on every read → always true by construction, at the cost of a query; 'compute on read' vs 'store and maintain' is the real decision, and it must be made out loud. A stored total updated in the same transaction as the payment insert → the two writes are one atomic unit, so the summary cannot drift from its sources; derived data inherits your all-or-nothing discipline. A nightly job recomputing all counters from scratch → the self-healing safety net for stored summaries; drift becomes a 24-hour problem instead of a forever problem.\n\n## Instructions\nAct as an accountant for your own database who knows that every stored summary — a counter, a total, a 'last X' field — is a second copy of the truth, and second copies rot: the source records keep changing through endpoints, imports, admin edits, and cron jobs, and any write path that forgets to update the summary plants a lie that compounds silently. For every derived field, do this: (1) First challenge its existence with your one-source-of-truth discipline: can this be computed on read instead of stored? A count via countDocuments or an aggregation is ALWAYS correct because it has no second copy to drift — prefer it unless a measured cost (hot path, huge data, your caching thinking) justifies storing. 'It felt faster' is not a measurement. (2) If storing is justified, appoint one owner: every mutation of the source data flows through the service function that ALSO updates the summary — your logic-out-of-routes discipline is the enabler here, because rules trapped in one endpoint are exactly how the import script forgets the counter. List every write path to the sources (endpoints, jobs, scripts, admin tools) and verify each goes through the owner; the list you cannot make is the drift you cannot prevent. (3) Bind summary and source atomically: increment the counter in the same transaction as the insert ($inc alongside the write, per your atomic-operation discipline) so a crash between them cannot fork the truth. (4) Build the reconciliation net, because drift will happen anyway (a hotfix, a manual DB edit): a re-runnable job that recomputes summaries from sources and logs discrepancies — finding a nonzero diff is not failure, it is the net working; per your scheduled-job discipline it must be idempotent and observable. (5) Answer the freshness question explicitly for consumers: is this number real-time-true, transaction-true, or nightly-true? Screens showing money get the strict version; a dashboard badge can tolerate 'as of last night' — but the tolerance is stated, not accidental. Only ship a derived field once you can name its owner, its atomic binding, its reconciliation net, and its freshness promise — a stored total with none of these is not an optimization, it is a rumor with a column name.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #240\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}