Architecture

How Stater is built and how it scales: one Go binary in front of PostgreSQL, stateless replicas, the write path and security.

On this page

Stater is one Go binary with the web app embedded, in front of PostgreSQL. The API is declared in code and is its own OpenAPI document, errors are RFC 9457 problem responses, the web app is React built with Vite, and the design system ships as tokens and CSS.

            browsers, tills, tablets          integrations (online store, accounting)
                     │  cookie session              │  Bearer stk_… token
                     ▼                              ▼
              ┌──────────────── HAProxy (leastconn, /health/ready) ────────────────┐
              ▼                          ▼                          ▼
        stater serve               stater serve               stater serve      (stateless replicas)
        API · SSE · web app        API · SSE · web app        API · SSE · web app
        webhook dispatcher         webhook dispatcher         webhook dispatcher
              │   SQL, LISTEN/NOTIFY    │                           │
              └──────────────────────── PostgreSQL 17 ──────────────────────────────┘
                 records · ledger · timeline · audit · outbox · sessions · licence

Why these choices#

  • PostgreSQL, not SQLite. Retail has many writers at once (every till, every receiving tablet) and several stores. PostgreSQL gives row locking, SKIP LOCKED for the outbox, LISTEN/NOTIFY for live updates across replicas, and pg_trgm for typo-tolerant search.
  • Stateless replicas. Sessions, the licence, live events and the outbox are in the database. Any replica can serve any request, so scaling is "add a replica", and failover is the load balancer's health check.
  • No message broker. A transactional outbox in PostgreSQL does the same job (an event is recorded in the same transaction as the change, then delivered at least once) with one less system for a small business to run.
  • One API. The web app has no private endpoints. Whatever a person can do, an API token with the same role can do.

The write path#

Every change runs in one transaction that also writes:

  1. The ledger where there is one. Stock never changes except through moveStock, which updates stock_levels and appends to stock_movements with the balance after the move. The ledger always explains the number on screen.
  2. The timeline of the records involved (timeline): what happened, who did it, with a reference. A sale writes to the order and to the customer.
  3. The audit log (audit_log): every change, with actor (person or API token).
  4. An event (events.Emit): a row in outbox for webhooks and a pg_notify that every replica relays to open browsers over server-sent events. Screens refresh the data they show; nobody presses refresh.

If any of these fails, nothing is saved.

Business rules#

Business rules are pure functions with tests: stock status words, reorder quantities in packs, order stage moves, count reconciliation (a count compares what was counted with the snapshot plus every movement since the count started, so sales during a count are not double counted), cash variance and tolerance, invoice status, loyalty tiers and points. Money is integer cents; prices include tax and tax is extracted, never added.

Security#

  • Passwords and PINs: argon2id. Sessions and API tokens are random 256-bit values; only their SHA-256 is stored.
  • Browser sessions are HttpOnly, SameSite=Lax cookies; requests that change data must also carry X-Stater-Client, which a cross-site form cannot send.
  • API tokens carry a role (never more than their creator's), read or write access, an optional store and an expiry. Read-only tokens are refused on anything but GET.
  • Permissions are checked on the server for every operation, and store scope is enforced: a person sees only their stores unless their role covers every store.
  • Saved integration passwords (email, directory, AI, SIEM, TAXII, S3, accounting software) are sealed with AES-256-GCM under a key from STATER_SECRET_KEY that is never in the database, each bound to its name; a canary row catches replicas with different keys. They are write-only over the API and never logged or audited. See Secrets.
  • Outbound connections that follow a setting someone typed go through one outbound connection guard: the dialled address is checked after DNS, link-local and cloud metadata are always refused, private and loopback only for a connector set to "On my own network" on a server that allows it (STATER_PRIVATE_CONNECTORS), redirects are not followed and HTTP_PROXY is ignored.
  • Background jobs (backups, polling, pruning) are claimed through the jobs table with a lease, so one replica runs each; a crashed replica's lease runs out and another takes over. "Run now" is owner-only and audited.
  • Wrong passwords and manager PINs are limited across every replica (sign-in per username and address, PINs to 5 wrong in 15 minutes per caller). Attempts from one caller are checked one at a time, so right answers sent in parallel never count against each other. CSV exports guard against formula injection.

Scaling notes#

  • Replicas: tested with two behind HAProxy. Live-update connections are long-lived; HAProxy uses leastconn and a 5-minute tunnel timeout with server keep-alives every 25 s.
  • Database connections: each server opens at most 8 in all by default. Set pool_max_conns in STATER_DATABASE_URL to change that total (5 or more): it covers everything the server opens, including the 2 kept apart for counting sign-in and PIN attempts, so replicas × pool_max_conns is the most Stater ever holds, plus 2 on the one server making a backup (see Database and backups). Ten default replicas fit PostgreSQL's default max_connections of 100; behind PgBouncer, size its pool the same way.
  • Database: indexes cover every list's default sort and filter; search uses trigram GIN indexes. For larger chains, put PostgreSQL on a managed service with a read replica for Insight.
  • Webhooks: each replica's dispatcher claims outbox rows with FOR UPDATE SKIP LOCKED, so they share the work without duplicates. Failed deliveries back off exponentially for about two hours.

Observability#

  • GET /health/live (process up) and GET /health/ready (database reachable) for load balancers and monitoring.
  • GET /metrics: Prometheus text format — requests by status class, live connections, database pool, outbox backlog, licence days left. Restrict it at the proxy if the server is reachable from outside.

Alerts, the other metrics and background jobs are in Monitoring.