Database counters
pros
- No extra infrastructure
- Easy to query and audit
cons
- Race conditions under concurrency
- Slow — adds a DB round-trip to every request
- Window resets need cron jobs
solutions
Track and enforce per-customer usage limits, credits and quotas with a single API call.
Most SaaS products eventually need per-customer limits: a free tier capped at 1,000 requests/month, a team plan with a shared token budget, an API product billed by the call. The requirements sound simple.
The first version is usually a database counter. Add a row per user, increment on use, check before allowing. It works until traffic picks up and two requests race through the same limit simultaneously.
So you move to Redis. Then you need to reset counters at month-end, so you add a cron job. Then a customer asks for credits. Then you need idempotency so retries do not double-count. Then you need per-team and per-API-key scopes. Each requirement is solvable, but the cumulative complexity is substantial.
By the time the system is production-ready, you have built significant infrastructure that has nothing to do with your core product.
pros
cons
pros
cons
pros
cons
pros
cons
Limits need to reset on calendar boundaries, not rolling windows. Getting UTC midnight right across time zones is non-trivial.
Reading a counter and decrementing it are two operations. Without atomicity, concurrent requests can both pass a limit that only one should.
Credits are not plain rate limits. They accumulate, carry over, and have variable costs per operation. The data model is different.
Retried requests must not double-count. You need idempotency keys that are scoped, short-lived, and checked atomically with the quota.
Under load, multiple requests can race to consume the last units. A naive implementation lets all of them through.
Quotas need to be scoped per user, per team, per API key, and sometimes per feature — often all at once.
You need to know who is near their limit, who hit it, and why. Building dashboards and alerting on top of a home-grown system adds weeks.
counr exposes a single endpoint — POST /v1/check-consume — that checks a quota and consumes from it atomically. One round-trip. One decision. No race conditions.
remaining so your UI can show progressexample — enforce an AI token budget
import { CountrClient } from "countr-sdk";
const countr = new CountrClient({
apiKey: process.env.COUNTR_API_KEY,
failOpen: true, // allow if Countr is unreachable (default false); result.failedOpen marks it
});
// In your API handler — check and consume in one atomic call
const result = await countr.checkConsume({
subject: "user_abc123", // user, team, API key — any string
metric: "ai_tokens",
cost: 1500, // tokens used by this request
});
if (!result.allowed) {
return Response.json(
{ error: "Monthly token budget exhausted", remaining: result.remaining },
{ status: 429 }
);
}
// Request is allowed — proceedCap token spend per user or team per month. Cost per request varies — countr accepts any integer cost per call.
Enforce a monthly request cap before prompting users to upgrade. The response includes remaining units so you can show progress bars.
Set a shared quota across all members of a team. Use the team ID as the subject — individual members draw from the same pool.
Sell API access by the call. Each API key is its own subject. Quotas reset monthly or can be credit-style — your choice.
Give users N credits for a premium feature per billing cycle. Credits are consumed atomically; over-consumption is prevented.
You can. The components are well-understood: Redis, Lua scripts for atomicity, a cron for resets, a schema for credits. If you have the time and your requirements are stable, building in-house is a reasonable choice.
The part teams usually underestimate is maintenance. The initial implementation takes a week or two. Edge cases — idempotency, concurrent over-spend, multi-metric quotas, time zone handling — add another week. Then requirements change: a customer needs a custom window, another needs rollover credits. Every change touches infrastructure that affects every customer.
The deeper pattern: most product teams end up rebuilding the same quota infrastructure two or three times as the product grows. The first version handles the simple case. The second handles concurrency. The third handles credits and complex billing.
counr is the third version, already built.
One endpoint. No Redis to manage. Works with any backend.