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

System Architecture Overview

The billing platform is a Rust workspace composed of 18 crates arranged in a layered dependency graph.

Crate Dependency Graph

                      ┌─────────────┐
                      │   common    │  Money, IDs, BillingError, types, tax trait
                      └──────┬──────┘
              ┌──────────────┼──────────────┬──────────────┐
              ▼              ▼              ▼              ▼
        ┌─────────┐   ┌──────────┐   ┌──────────┐   ┌──────────┐
        │metering │   │ pricing  │   │customers │   │  ledger  │
        └────┬────┘   └────┬─────┘   └──────────┘   └──────────┘
             │             │
             ▼             ▼
        ┌─────────┐   ┌──────────┐
        │ rating  │◄──│ pricing  │
        └────┬────┘   └──────────┘
             │
             ▼
       ┌──────────────┐
       │  invoicing   │  + ledger, wallet
       └──────┬───────┘
              │
    ┌─────────┼──────────┐
    ▼         ▼          ▼
┌────────┐ ┌────────┐ ┌──────────┐
│dunning │ │payments│ │contracts │
└────────┘ └────────┘ └──────────┘
              │
    ┌─────────┼──────────┐
    ▼         ▼          ▼
┌────────┐ ┌────────┐ ┌───────────────┐
│ audit  │ │ wallet │ │spend-alerts   │
└────────┘ └────────┘ └───────────────┘
              │
              ▼
     ┌───────────────────┐
     │    api-gateway    │  Axum HTTP server
     └───────────────────┘

Technology Stack

ComponentTechnologyNotes
LanguageRust 2021Fearless concurrency, zero-cost abstractions
HTTP FrameworkAxum 0.7Tower middleware, typed extractors
LedgerTigerBeetleIn-memory shim in dev; TB client in prod
DatabaseCockroachDBREGIONAL BY ROW multi-region schema
AnalyticsClickHouseMetering aggregation, MV queries
Event streamingKafkabilling.<service>.<event> topic naming
CacheRedisRate limiting (token bucket), hot-path accumulators
PaymentsStripePaymentIntent API + webhook verification
AuthJWT HS256 + API keysConstant-time comparison
Serializationserde + serde_jsonAll monetary i128 as string

Deploy Architecture

Internet
   │
   ▼
Caddy (TLS termination, reverse proxy)
   │
   ├─► api.bill.sh → api-gateway (port 3000)
   ├─► docs.bill.sh → mdBook static site
   └─► ui.bill.sh → Askama/HTMX server-rendered UI

Infrastructure: Hetzner VPS (Ubuntu 24.04, Docker 29.x)

Key Design Principles

  1. Correctness over performance: i128 pico-units eliminate money rounding bugs
  2. Pure functions for business logic: rate_all(), prorate_days(), compute_true_up() have no I/O — deterministic and easily tested
  3. Idempotency everywhere: All mutating operations safe to retry
  4. In-memory for development: All repositories have InMemory* implementations — no database required for local dev or tests
  5. Swap-in production backends: Implement Repository traits with CockroachDB/ClickHouse/TigerBeetle for production

Service Crates

CrateResponsibility
commonShared types: Money, NanoMoney, typed IDs, Currency, BillingError, TaxAdapter
meteringUsageEvent ingestion, MeterDefinition, aggregation, EventStore
ratingPriceSheet, rate_all() pure engine, RatedLineItem
pricingCatalogService, PriceVersionService, plan/product/SKU CRUD
subscriptionsSubscriptionService, state machine, proration engine
invoicingInvoicingService, CalculateFeesService, FinalizeService, CreditNoteService
contractsContractService, amendment chain, coterm, commit draw-down
ledgerTigerBeetle client wrapper, double-entry transfers
dunningDunningSchedule, retry scheduler
paymentsStripeClient, PaymentIntent, webhook verification
notificationsOutbox pattern, webhook delivery, circuit breaker
auditAuditLog, 25+ action types, before/after state
reportingAR aging, MRR movements, deferred revenue (ASC 606)
walletCreditWalletService, auto-recharge
spend-alertsSpendAlertService, SoftLimit/HardLimit
customersCustomerService, hierarchy, entity tree
engineBillingEngine orchestrator (ties all services together)
demoSeed data for development