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 LOCKEDfor the outbox,LISTEN/NOTIFYfor live updates across replicas, andpg_trgmfor 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:
- The ledger where there is one. Stock never changes except through
moveStock, which updatesstock_levelsand appends tostock_movementswith the balance after the move. The ledger always explains the number on screen. - 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. - The audit log (
audit_log): every change, with actor (person or API token). - An event (
events.Emit): a row inoutboxfor webhooks and apg_notifythat 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=Laxcookies; requests that change data must also carryX-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_KEYthat 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 andHTTP_PROXYis ignored. - Background jobs (backups, polling, pruning) are claimed through the
jobstable 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
leastconnand 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_connsinSTATER_DATABASE_URLto 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_connsis the most Stater ever holds, plus 2 on the one server making a backup (see Database and backups). Ten default replicas fit PostgreSQL's defaultmax_connectionsof 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) andGET /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.