Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Changelog

Phase 5 — Production Hardening (Feb 28, 2026)

  • Idempotency: IdempotencyStore (DashMap, 24h TTL, method+path+key scoped) + Axum middleware + X-Idempotency-Replayed response header
  • Spend Alerts: New spend-alerts crate — SoftLimit/HardLimit alert types, SpendAlertService (create/check/reset), GET/POST /admin/v1/customers/:id/alerts + reset endpoint
  • Usage Summary: metering::summary::{UsageSummary, MeterSummary} + GET /v1/subscriptions/:id/usage + MeteringService in AppState
  • Tax Engine: TaxAdapter trait + PassThroughTaxAdapter + FlatRateTaxAdapter (bps, B2B exempt) + InvoiceLineItem.is_tax + InvoicingService::apply_tax() + POST /admin/v1/invoices/:id/calculate-tax
  • Developer Docs: mdBook docs site deployed to docs.bill.sh + OpenAPI 3.0 spec + Redoc UI at https://api.bill.sh/docs/api

Phase 4 — Scale & High Availability (Weeks 7-8)

  • ClickHouse Metering: DDL, materialized views, aggregation query builder, batch/top/summary helpers (11 tests)
  • Kafka Integration: EventPublisher trait, StubEventPublisher, RdkafkaProducer (feature-gated)
  • CockroachDB Schema: Multi-region REGIONAL BY ROW schema in infra/crdb-schema/ (6 SQL files)
  • Auth Middleware: JWT HS256 + constant-time API key validation (auth.rs)
  • Rate Limiting: Redis token bucket + InMemoryRateLimiter + Lua script for atomic Redis operations
  • Webhook Delivery: Retry engine + circuit breaker + HMAC-SHA256 signatures (notifications/delivery.rs)
  • Docker Compose: Full local dev environment (139 lines) — CockroachDB, ClickHouse, Kafka, Redis, TigerBeetle
  • CI Pipeline: GitHub Actions clippy + test on every push

Phase 3 — Enterprise Features (Weeks 5-6)

  • Contracts: Full contract CRUD — Draft→Active→Amended/Expired/Terminated state machine
  • Ramp Contracts: ContractTerm phases with per-phase pricing, discounts, and committed amounts
  • ContractSubscription: Join table with PLG/Coterm/Recontract adoption types
  • Proration Engine: Pure functions — prorate_days(), prorate_to_contract_end(), prorate_overlap_credit(), cancellation_credit()
  • Commit Draw-Down: apply_commit_drawdown() pure fn — priority-ordered, tag-scoped
  • True-Up: compute_true_up() + compute_phase_true_ups() pure fns
  • Ledger Provisioning: TigerBeetle two-phase transfers — pending/post/void engine, customer account provisioning
  • Audit Log: billing-audit crate — 25+ action types, ActorRef/ActorRole, InMemoryAuditLog, idempotency
  • Financial Reporting: billing-reporting — AR aging, MRR report, MRR movements, deferred revenue schedule (ASC 606 compliant)
  • Credit Notes: CreditNoteService — partial/full credits, issue_wallet_credit() (TigerBeetle transfer, idempotent)
  • PLG Adoption: PlgAdoptionService — in-place contract linking for product-led growth companies
  • Admin API: Customer 360, invoice void, credit note, pause/resume, catalog management endpoints

Phase 2 — Core Billing Loop (Weeks 3-4)

  • Rating Engine: PriceConfig, PriceSheet, TierOverride, RatedLineItem — flat/per_unit/graduated/volume/package/committed models
  • rate_all(): Pure rating function with contract discount (basis points) and tier override support
  • Invoice Calculation: CalculateFeesService — converts RatedLineItemInvoiceLineItem
  • Invoice Finalization: FinalizeServicefinalize_auto() with Arc<AtomicU64> sequential counter (INV-XXXXXX)
  • Progressive Billing: ProgressiveBillingService — threshold-triggered finalize + new draft
  • Period Advance: advance_period() on subscriptions — moves charged_through_date
  • Stripe Integration: StripeClientcreate_payment_intent(), capture(), refund(), webhook verification
  • Dunning: 4-attempt retry schedule (+3d/+7d/+14d/+21d)
  • Notifications: OutboxEntry + OutboxRepository — transactional outbox pattern
  • API Gateway: Axum router with subscriptions, invoices, events, webhooks routes

Phase 1 — Foundation (Weeks 1-2)

  • Workspace Scaffold: 10 service crates in Cargo workspace
  • common crate: Money, NanoMoney, typed IDs (UUIDv7), domain enums, BillingEvent, BillingError (87 unit tests)
  • Metering: MeterDefinition, MeterFilter, FilterOperator (7 operators), UsageEvent (CloudEvents-compatible), aggregation engine (COUNT/SUM/MAX/UNIQUE_COUNT/LATEST/WEIGHTED_SUM), InMemoryUsageEventStore
  • Redis Accumulator: Real-time spend controls — InMemoryAccumulator + Redis Lua script
  • Subscriptions: CRUD with state machine enforcement, InMemorySubscriptionRepository
  • Invoicing: Invoice + InvoiceLineItem + finalization state machine
  • Ledger: TigerBeetle client wrapper — InMemoryLedger (account create, transfer, balance query)
  • API Gateway: Axum router scaffold with health check