Usage Quota Stale-Period and Race Automation: Last Month's Number, Still Counted

A counter that only resets on the next write is not actually reset until then
Storing a running total that represents "usage this billing period" and physically resetting it to zero only when the next increment happens to arrive is a common, reasonable-looking simplification — it avoids needing a separate scheduled job whose only purpose is zeroing counters at the start of every period. The simplification has one real cost: for the entire stretch of time between a new period starting and that organization's first metered action of the new period, the counter row on disk still holds the PREVIOUS period's final total. Any code reading that counter directly, without separately checking whether the period it belongs to has actually rolled over, is reading a number that is real, but stale — describing usage that has already legally expired against the plan's own terms.
How the underlying problem shows up before you fix it
A usage or quota counter is designed to reset only as a side effect of the next write to it, rather than being actively reset by a scheduled process at the start of each new period.
A check enforcing a limit against that counter reads its stored numeric value directly, with no comparison against the counter's own stored period start date and the actual current date.
An organization or user with genuinely zero usage in a brand-new billing period can be denied a plan-entitled action, specifically in the window between the period starting and their first metered action of that period, because the stored counter still reflects the prior period's total.
Incrementing a shared numeric counter is implemented as two separate steps in application code — read the current value, then write back current-plus-amount — rather than as one atomic database operation.
Two legitimate, back-to-back actions from the same user or organization, close enough together to both read the counter before either one's write lands, can result in only one increment being recorded even though two metered actions genuinely occurred.
How a counter that lied about the calendar and a counter that could lose an increment were both closed
Centriu Axiom enforces a monthly usage quota per organization and per metered resource by keeping a single counter row — an organization ID, a resource name, a running numeric value, and the date that value's current accounting period began. A check runs before any metered action, comparing the counter's stored current value against the plan's own configured limit for that resource, and denies the action if the limit has already been reached.
Before this fix, that check read `counter?.current_value ?? 0` directly and used it as-is. The counter itself is only ever physically zeroed as a side effect of the NEXT increment call — there is no separate, scheduled process that resets every organization's counters the moment a new calendar month begins. The practical consequence: for any organization that had not yet performed a single metered action since a new month started, the counter row still held the FINAL accumulated total from the PREVIOUS month, potentially sitting right at or near the plan's own limit — and the entitlement check, reading that value with no awareness that its own period had already expired, would treat a fresh, genuinely-zero-usage month as if the organization were already at or near its cap. A paying customer, entitled to a full new month of usage under their plan, could be denied an action in the opening days of that new month for no reason connected to their actual current usage at all.
A second, entirely independent defect lived in the same counter's own increment path. Incrementing it was implemented as two separate steps inside the application's own code: fetch the counter's current value with one query, compute the new value as current-plus-amount in JavaScript, then `upsert` that computed value back with a second query. Two increments for the same organization and resource arriving close enough together — a realistic scenario for any metered action a user is able to trigger twice within a short window, whether deliberately or through a retried request — could both execute their initial read before either one's write lands, both computing their "new" value from the SAME starting number. The second upsert to complete would overwrite the first's result, silently discarding one genuine increment and leaving the counter undercounting real usage — the opposite failure mode from the stale-period bug, letting real usage run ahead of what the counter reports rather than behind it.
Both were fixed, independently, on their own sides of the problem. The entitlement check now computes the start of the current calendar month in UTC and compares it against the counter's own stored period-start value; when the counter's period is older than the current month, the check treats current usage as zero, regardless of whatever value is still physically sitting in the `current_value` column until the next real increment gets around to resetting it. Separately, and more fundamentally, the increment path itself moved from a two-step application-level read-then-write to a single, atomic Postgres function — `axiom_quota_check_and_increment`, running with elevated (`SECURITY DEFINER`) privilege specifically so it can perform the operation server-side in one statement — using an `INSERT ... ON CONFLICT DO UPDATE` that reads, resets-if-stale, and increments the counter as one indivisible database operation, closing the race independently of the client-side stale-period compensation. The same investigation also corrected the separate decrement and reset code paths' own `upsert` conflict target, which had been written against a two-column key (`organization_id, resource`) that did not match the table's actual three-column composite unique constraint (`organization_id, resource, period`) already used correctly elsewhere in the same file — bringing all three write paths (increment, decrement, reset) onto a consistent, correct conflict target.
What is actually built today
Centriu Axiom's entitlement check compares a usage counter's own stored billing period against the current calendar month before trusting its stored value, treating a counter from an already-expired period as zero rather than as current usage.
Incrementing a usage counter runs as a single, atomic Postgres function (`axiom_quota_check_and_increment`) performing the read, period-aware reset, and increment as one indivisible database statement — closing a race where two near-simultaneous increments could result in only one being recorded.
The atomic increment function auto-resets a stale counter server-side when its stored period no longer matches the current month, complementing the client-side stale-period check on the read path.
The counter table's decrement and reset write paths now target the same, correct three-column composite conflict key (`organization_id, resource, period`) the increment path already used — consistent across all three operations.
A paying organization in the opening days of a fresh billing period, with genuinely zero usage so far, is no longer at risk of being denied a plan-entitled action because of a counter row that had not yet been touched since the period rolled over.
A brand-new month, denied by last month's own total (illustrative framing of the actual measured finding)
Before the fix, an organization on the first day of a new billing month, having taken zero metered actions so far that month, could still have its counter row showing the previous month's final total — say, a value sitting at the plan's own configured limit. The very first legitimate action of the new month would be checked against that stale number and denied, even though the organization's real usage for the new period was genuinely zero. After the fix, the same check recognizes the counter's stored period no longer matches the current month, treats current usage as zero, and allows the action — with the counter itself correctly reset the moment that first action is actually recorded.
What changes operationally
Centriu Axiom's monthly usage-quota enforcement now correctly recognizes when a stored counter belongs to an already-expired billing period, and performs every increment as a single atomic, period-aware database operation — closing a gap where a paying organization could be denied a plan-entitled action based on a prior month's stale total, and an independent race where two near-simultaneous increments could silently undercount real usage.
When this is not the right fit
This automation governs the internal correctness of Centriu Axiom's own monthly usage-counter enforcement — it does not change what any plan's actual usage limits are, which remain a separate, published commercial decision, and applies specifically to metered resources tracked on a monthly period; a resource with no monthly reset concept at all would not be exposed to the stale-period side of this specific defect.
Resetting a counter lazily on next write vs. checking the period on every read
Resetting a usage counter only as a side effect of its next increment, rather than through a separate scheduled process, avoids needing extra infrastructure whose only job is zeroing counters at the start of every period — a real simplification. The cost of that simplification is that any code reading the counter directly, without ALSO checking whether its stored period has actually expired, is trusting a number that can be real but stale. Adding that period comparison on the read path, and moving the write path to a single atomic, period-aware database operation, keeps the original simplification (no separate reset job is needed) while closing both the stale-read and the increment race it otherwise leaves open.
Related systems
Main system: Centriu Axiom.
What it does NOT do
- Does not change what any Centriu Axiom plan's actual monthly usage limits are — those remain a separate, published commercial decision; this fix corrects only whether the counter measuring usage against that limit is read and written correctly.
- Does not retroactively correct any specific organization's historical usage figures from before this fix shipped — a team with that concern would need its own review of the relevant counter and increment history for the affected window.
- Does not add a separate scheduled process to reset counters at the start of each period — the fix keeps the original lazy-reset-on-next-write design, and instead makes every reader and writer of the counter correctly aware of period staleness.
- Does not overlap with the other two fixes shipped in the same commit (the Health/Errors dashboard's own mock-data disconnect, and a data-retention cleanup that fabricated its own deleted-row count) — both are covered on their own companion pages.
- Does not claim every usage or billing counter in Axiom shared this exact defect — this fix is scoped specifically to the monthly usage-quota counter and its increment/decrement/reset paths described above.
Security and governance
Centriu Axiom's monthly usage-quota enforcement now correctly distinguishes a current-period counter from a stale, already-expired one, and performs every increment as a single atomic, period-aware database operation — closing a gap that could deny a plan-entitled paying customer an action, and an independent race that could undercount real usage. Full detail on access control and audit trails lives at /governanca and /iso.
Pricing and contracting
Available by monthly subscription, with tiered plans. Values and terms come from the official pricing table at /precos (Centriu's central source — never restated here).
Frequently asked questions
Could this have caused a customer to be denied an action they had genuinely paid for?
Yes — specifically in the window between a new billing month starting and that organization's first metered action of the new month, if the prior month's counter value happened to be at or near the plan's limit.
Could this have let a customer exceed their real quota undetected?
The separate race condition (two near-simultaneous increments silently dropping one) could undercount real usage, which functionally has that effect — real usage running ahead of what the counter reports. This is the opposite direction from the stale-period defect.
Why wasn't the counter just reset automatically every month?
The design deliberately resets it lazily, as a side effect of the next real increment, avoiding a separate scheduled process whose only job would be zeroing every organization's counters. The fix adds a period-aware CHECK on the read side and an atomic, period-aware RESET on the write side, rather than changing that underlying design.
What makes the new increment path atomic?
It runs as a single Postgres function performing the read, stale-period reset, and increment as one `INSERT ... ON CONFLICT DO UPDATE` statement inside the database itself, rather than as two separate round-trips (a read, then a write) from application code.
Were decrement and reset affected by the same race?
The investigation found and corrected a related but distinct issue in those paths: their `upsert` conflict target did not match the table's actual composite unique constraint. The increment path's race was the more severe of the defects, and is the one moved to an atomic RPC.
What does Centriu Axiom cost?
It is sold by subscription with a published starting price — exact current values are on the central pricing page.
See how Centriu Axiom enforces usage quotas accurately, period by period
Reach our commercial team directly, or leave your details below — we'll follow up with guidance for your case.
