Core Mechanics
When a customer consumes a metered resource, you create a usage event that captures:- Who consumed it (customer ID)
- What was consumed (metric ID and quantity)
- When it occurred (timestamp)
- How to prevent duplicates (idempotency key)
Event Processing Models
Direct Account Processing
Standard flow for real-time billing:- Event arrives with customer and metric identifiers
- System resolves active subscription and pricing rules
- Cost calculation based on quantity × unit price (or dynamic/percentage models)
- Immediate account debit from consumer
- Instant credit to merchant (minus platform fees)
- Transaction recording and analytics update
Entitlement-Based Processing
Pre-authorized payment flow:- Event includes entitlement ID linking to reserved funds
- Same pricing calculation occurs
- Deduction from pre-reserved entitlement balance
- Merchant credit remains immediate
- Entitlement balance decrements
Event Structure
Essential fields for every usage event: Identity & DeduplicationidempotencyKey- Unique identifier preventing duplicate processingcustomerId- Links consumption to specific customer relationshipmerchantId- Identifies the service provider
timestamp- Exact moment of resource consumption (ISO 8601)properties- Array of metric consumption records
entitlementId- References pre-authorized payment reservationmetadata- Arbitrary key-value pairs for internal tracking
Consumption Properties
Each element in the properties array represents one metric’s consumption:price field serves different purposes based on the pricing model:
- Dynamic pricing: Total cost for this specific consumption
- Percentage pricing: Base amount for percentage calculation
- Standard pricing: Not required (uses plan’s fixed rate)
Pricing Model Interactions
Standard Pricing
Fixed rate multiplication. Quantity × configured unit price.Dynamic Pricing
Runtime price determination. The event carries the actual price within bounds set by the plan. Useful for spot pricing, market rates, or time-based variations.Percentage Pricing
Transaction-based fees. The event includes the transaction amount; the system calculates the percentage cut with min/max bounds applied.Idempotency & Reliability
The idempotency key ensures exactly-once processing semantics:- Same key within time window → subsequent attempts ignored
- Failed requests can be safely retried with identical payload
- Generate deterministic keys from your internal identifiers
Timing Constraints
Events must align with subscription billing periods:- Timestamp within active subscription range
- Events outside current period may be rejected
- Historical backfill requires special handling
Batch Processing
For high-volume scenarios, batch multiple events in a single request:- Reduces network overhead
- Atomic processing (all succeed or all fail)
- Same validation rules per event
- Ideal for periodic bulk reporting
Regional Optimization
Leverage edge infrastructure for reduced latency:- Regional endpoints process events locally
- Entitlements enable ultra-low latency processing
- Critical for real-time applications
Error Handling
Common failure scenarios and recovery: Validation Failures- Invalid customer/metric IDs → verify references exist
- Timestamp outside billing period → check subscription status
- Price outside dynamic bounds → validate against plan limits
- Insufficient account balance → requires top-up or entitlement
- Entitlement exhausted → create new entitlement
- Network timeouts → retry with same idempotency key
Design Patterns
High-Frequency Reporting
For systems generating thousands of events per second:- Buffer and batch events locally
- Use entitlements to guarantee payment
- Leverage regional endpoints
- Implement exponential backoff for retries
Guaranteed Delivery
For critical billing accuracy:- Local event queue with persistence
- Idempotency keys derived from queue position
- Dead letter queue for failed events
- Periodic reconciliation jobs
Cost Attribution
For detailed cost tracking:- Rich metadata on each event
- Hierarchical customer IDs for sub-accounts
- Temporal bucketing for period analysis
- Tag-based grouping in metadata
Next Steps
- Customer Lifecycle - Understand billing flows
- Entitlements - Pre-authorize payments
- Accounts - Manage payment instruments