The problem
A counter cannot stop because a fiscal service is unreachable. Sales must queue safely and sync later without duplicates.
POS SaaS is a multi-tenant point of sale for pharmacies and restaurants. It is early-stage, and this case study leads with the design. The design documents are a PRD and decision records for RLS tenancy, the fiscal invoice outbox and offline sync, plus a failure analysis.
The constraint
My design notes record three facts that conflict. A sales-tax-registered business has to send each sale to a tax authority. For goods that is FBR, and for services it is the provincial authority. The cashier cannot wait on a foreign network, and a queue of customers cannot wait for a retry. And the rules as I documented them allow offline issuance, if the invoice is uploaded within 24 hours of connectivity returning.
I assembled those rules from secondary sources. The field-level details are not confirmed against the official documentation, and this is not legal advice.
On top of that, a sale must never be duplicated, and an unsent sale must never be lost. A second fiscal invoice for one real sale is a tax problem, not a bug report.
Decisions
Shared database with row-level security
Many businesses share one schema, with a tenant column on every table and isolation enforced by Postgres. I rejected a project per tenant, which means one migration run and one set of credentials per customer. I also rejected a schema per tenant, and application-level filtering alone. The cost is permanent. Every table needs a policy, and a policy bug is a cross-tenant leak. My rule is that a table does not ship without a cross-tenant test.
A transactional outbox for fiscal invoices
The sale and its intent to transmit commit in one transaction. A separate worker drains the outbox with backoff, and the result is written back later. The receipt prints at once and says fiscal confirmation is pending.
I rejected sending inside the sale, because it ties the counter's availability to the authority's. I rejected fire-and-forget after commit, because a restart loses invoices silently. The costs are two receipt states that staff must understand, a worker to operate and monitor, and reconciliation for an invoice the authority accepted but I did not record.
An idempotency key on every retry
If the authority accepts an invoice and the reply is lost, the retry carries the same key and returns the original answer, so there is no second invoice. If a provider has no idempotency support, the adapter must query before it posts again. That is still an open item.
A small middle and defended edges
I walked the sale path step by step and asked what breaks. The middle is nearly idle, and the failures are at the edges: the printer, the shop's internet and the tax API. So the middle stays small and the effort goes to the edges. Printing and fiscal submission are consequences of a sale, not conditions for it.
I also record that the database is a single point of failure. I accepted that, with managed failover and point-in-time recovery, and I wrote down when to revisit it.
Rotating refresh tokens with reuse detection
Sessions use rotating refresh tokens with reuse detection. I have not written a trade-off for this one, so I will not invent it.
Result
There is a PRD, decision records for tenancy, the fiscal outbox and offline sync, and a failure analysis. The stack is Next.js 16, Drizzle and Postgres. I make no production claim and I have no metrics to show. What I can show is the reasoning, written before the code.
What I would change
This section is opinion. If I rebuilt the plan, I would test the outbox against a real provider early. The idempotency question will only be settled by what that provider actually does.
Version one is online-only: when the shop's internet drops, the counter blocks with a banner and keeps the cart. Offline sync is a phase-two engine choice. I would pull the decision forward, because the design notes name the shop's internet as an edge failure.
I would also put the age of the oldest pending invoice on a screen from the first day. The design says queue depth can be large and healthy, but age cannot.