Usage-Based Revenue Controls: Thresholds and Entitlements Design
As Sherlock, I'll design a robust, scalable system for usage-based revenue controls centered on thresholds (limits that trigger billing tiers, warnings, or blocks) and entitlements (customer allocations defining what's allowed). This is ideal for SaaS products with metered billing (e.g., API calls, storage GB, active users).
The design assumes:
Usage is tracked in real-time (e.g., via event streaming like Kafka).
Billing integrates with systems like Stripe Metered Billing or Chargebee.
Enforcement is API-gateway enforced (e.g., at ingress/egress).
Principles : Progressive billing (free → pay-as-you-go), overage protection, multi-tenancy, and auditability.
1. Core Concepts
Entitlement : Customer's purchased allocation (quota per feature). E.g., "1000 API calls/month free, unlimited paid."
Threshold : Metered limits per tier. Crossing one triggers revenue events:
Type Behavior Revenue Impact Soft Warning/email, continue usage Bill overage at tier rate Hard Block further usage No overage, force upgrade Billing Usage window (e.g., monthly reset) Accumulate for invoice
Revenue Flow : Usage → Check Entitlement → Apply Threshold → Accumulate Billable Units → Generate Invoice.
2. Data Models (Schema)
Use a relational DB (e.g., PostgreSQL) for entitlements/thresholds + time-series DB (e.g., ClickHouse) for usage.
Entitlements Table
sql Copy code
CREATE TABLE entitlements (
id UUID PRIMARY KEY,
customer_id UUID NOT NULL, -- Links to billing customer
feature_key VARCHAR(50) NOT NULL, -- e.g., 'api_calls', 'storage_gb'
plan_tier VARCHAR(20) NOT NULL, -- e.g., 'free', 'pro', 'enterprise'
limit_amount DECIMAL(20,6), -- e.g., 1000.0 (NULL = unlimited)
limit_unit VARCHAR(10) DEFAULT 'month', -- 'month', 'day', 'total_lifetime'
reset_schedule VARCHAR(10) DEFAULT 'monthly', -- 'monthly', 'annually'
allow_overage BOOLEAN DEFAULT TRUE, -- Soft threshold
created_at TIMESTAMP DEFAULT NOW(),
UNIQUE(customer_id, feature_key)
);
Thresholds Table (Tiered Pricing)
sql Copy code
CREATE TABLE pricing_thresholds (
id UUID PRIMARY KEY,
feature_key VARCHAR(50) NOT NULL,
plan_tier VARCHAR(20) NOT NULL,
tier_order INT NOT NULL, -- 1=base, 2=first overage, etc.
up_to_amount DECIMAL(20,6) NOT NULL, -- Cumulative: tier 1: up_to=1000, tier2: up_to=10000
price_per_unit DECIMAL(10,6) NOT NULL, -- e.g., 0.00 (free), 0.01 ($/unit)
enforcement_type ENUM('soft', 'hard', 'billing') DEFAULT 'billing',
active BOOLEAN DEFAULT TRUE
);
-- Example data:
-- INSERT: feature='api_calls', tier='pro', order=1, up_to=5000, price=0.0 (free tier)
-- order=2, up_to=50000, price=0.005
Usage Table (Aggregated)
sql Copy code
CREATE TABLE usage (
id UUID PRIMARY KEY,
customer_id UUID NOT NULL,
feature_key VARCHAR(50) NOT NULL,
period_start DATE NOT NULL, -- e.g., 2024-10-01
period_end DATE NOT NULL, -- e.g., 2024-10-31
consumed_amount DECIMAL(20,6) NOT NULL,
billable_amount DECIMAL(20,6), -- Computed post-thresholds
reported_to_billing BOOLEAN DEFAULT FALSE,
updated_at TIMESTAMP DEFAULT NOW()
);
3. System Architecture
code Copy code
[Customer Request] → [API Gateway] → [Entitlement Service] → [Usage Service] → [Core App]
↓
[Threshold Evaluator] → [Billing Queue]
Services :
Service Responsibility Entitlement API CRUD entitlements, check allowance. Usage Tracker Ingest events, aggregate by period. Threshold Engine Evaluate usage vs. thresholds, compute billable units. Billing Sync Export to Stripe/Chargebee.
4. Key APIs & Logic
Check Entitlement (Pre-Usage Gate)
python Copy code
# Pseudo-code (Python/FastAPI)
@app.post("/entitlements/check")
def check_allowance(customer_id: str, feature: str, delta: float):
entitlement = get_entitlement(customer_id, feature)
current_usage = get_usage_this_period(customer_id, feature)
if entitlement.limit_amount and (current_usage + delta > entitlement.limit_amount):
if not entitlement.allow_overage:
raise HTTPException(429, "Hard limit exceeded")
# Soft: allow, but flag for billing
# Apply thresholds for projected cost (optional preview)
projected_billable = evaluate_thresholds(feature, current_usage + delta, entitlement.plan_tier)
return {"allowed": True, "projected_cost": projected_billable * price_per_unit}
Threshold Evaluation (Compute Billable Units)
Tiered progressive billing: Bill only excess at higher rates.
python Copy code
def evaluate_thresholds(feature: str, total_usage: float, plan_tier: str) -> float:
thresholds = get_sorted_thresholds(feature, plan_tier) # tier_order ASC
billable = 0
prev_up_to = 0
for t in thresholds:
if total_usage <= prev_up_to:
break
excess = min(total_usage, t.up_to_amount) - prev_up_to
billable += excess * t.price_per_unit
prev_up_to = t.up_to_amount
# Unlimited tier? billable remains from last finite threshold
return billable
Example :
Usage: 15,000 API calls.
Thresholds (pro): [0-5k: 0 , 5 k − 50 k : 0, 5k-50k: 0 , 5 k − 50 k : 0.005, 50k+: $0.01]
Billable: (5k * 0) + (10k * 0.005) = $50.
Record Usage (Post-Action)
python Copy code
@app.post("/usage/record")
def record_usage(customer_id: str, feature: str, amount: float):
# Async: publish to Kafka for aggregation
aggregate_usage(customer_id, feature, amount)
# Enqueue billing preview if soft threshold crossed
Monthly Reset & Billing
Cron job: Reset usage at period end.
Compute final billable → POST to billing provider.
Audit log: All threshold crosses.
5. Enforcement & Controls
Real-time : API Gateway calls /entitlements/check → 403/429 on hard limits.
Overage Handling :
Scenario Action Under limit Free/allow. Soft over Allow + email + bill overage. Hard over Block + notify upgrade. Unlimited Bill at top tier rate.
Grace Periods : Add grace_days to entitlements for late payments.
Multi-Feature : Bundle entitlements (e.g., API + Storage combo limits).
6. Revenue Optimization Features
Usage Forecasting : API to predict next bill: sum(evaluate_thresholds(...)).
Auto-Upgrade : If 80% of hard limit → suggest plan bump.
Credits : entitlements supports negative limit_amount for promo credits.
Analytics Dashboard : Query usage vs. thresholds for churn prediction.
7. Implementation Roadmap
MVP : Models + Check/Record APIs (1 week).
Threshold Engine : Tiered calc + tests (1 week).
Integrations : Stripe sync, Kafka for usage (2 weeks).
Scale : Sharding by customer_id, caching entitlements (Redis).
8. Example Customer Journey
Free Tier : Entitlement: api_calls limit=1000/month (hard).
Usage hits 1000 → Block, upsell to Pro.
Pro : Thresholds as above → Usage 15k → Invoice $50.
This design ensures predictable revenue (thresholds drive upsells), customer trust (transparent entitlements), and fraud-proofing (hard gates). Questions or refinements?