Mehmet Ergülcü

From the register to head office: the architecture of abarow, in diagrams

· 12 min

architecturegonatspostgresqlpossystem-design

abarow is an open source, modular point of sale (POS) system for grocery shops and restaurants in Turkey, running on Linux. Its repository name, kasadan merkeze ("from the register to head office"), sums up the whole design: sales travel up, from the register to the store and from the store to head office, while catalog, prices and cashiers travel down.

This post walks through the architecture in diagrams, describing what is actually in the code. The version described is commit c83ab87 on main. The project is in its alpha stage: for now a simulator of the fiscal device (ÖKC, the cash register certified by Turkish tax law) prints the receipt, and the receipt says "MALİ DEĞERİ YOKTUR" (no fiscal value).

Every diagram opens at full size when you click it.

Who talks to whom?

A cashier or waiter uses the register. A store manager uses the register and the store server's screen, and head office users use head office. Next to the register are the peripherals (receipt printer, cash drawer, barcode scanner, scale) and the fiscal device. A business can plug in its own corporate identity provider (OIDC) if it wants.

abarow context diagram

Two things matter from the start:

  • Everything runs on the business's own hardware. There is no cloud service and no multi-tenant head office. Every business runs its own head office on its own server.
  • No customer data is kept. The only personal data is staff (name, role, a bcrypt hash of the PIN) and head office users.

Three tiers, one message bus

The system has three tiers:

  1. Register: the machine where the sale happens.
  2. Store server: a buffer that collects the events of the store's registers, and the place for store reports.
  3. Head office: where the data of all stores is collected and the catalog and prices are managed.

Every tier keeps its own data on its own disk, in its own PostgreSQL 18. Between tiers, data flows only as events, over NATS JetStream.

Topology of the three tiers

The lower tier always opens the connection: the register to the store server (7421), the store server to head office (7422), over TLS, with a NATS "leaf" connection. A practical consequence: no port has to be opened at the store; only head office needs a reachable address.

The core goal is this: the register keeps selling without internet and without the tiers above it. In short, when something breaks:

What breaks What happens
Internet (between store and head office) The store works as usual, events pile up at the store server, head office reports lag
The store server, or the link to it Grocery registers keep selling, events pile up at the register, store reports stop
One register The others are not affected
The message bus on a register Sales go on, events wait in the register's database; the end-of-day (Z) report is held back

Inside the register

On the register every service is its own container and its own systemd unit. The services are written in Go and talk to each other over gRPC. The screen is a Svelte application running full screen (kiosk) in Chromium, and it only talks to the gateway.

The services inside the register

A few details:

  • The barcode scanner is not read as a keyboard. The device service takes it straight from Linux (evdev) and passes the scans to the screen as an event stream (SSE). So "the focus was in another box and the barcode went to the wrong place" simply does not happen.
  • The gateway says who the cashier is. It passes the cashier's identity to the services behind it in a header (X-Abarow-Cashier-Id) and removes any header with the same name coming from the client, so the screen cannot pretend to be someone else. For sales requests it also checks that the cashier has an open shift.
  • No anonymous connections on the message bus. Every service on the register connects to NATS with its own key and can only see the subjects its job needs.
  • The register is light. The Go services run in minimal (scratch) images and every container has a memory cap. In the measured setup the register uses about 100 MiB at idle with 10 containers.

Grocery or restaurant?

The shared part of a sale (totals, VAT, payment, fiscal record) is in the core, in the sales service. The only thing that changes with the sector is how the lines are collected: a basket and barcodes in a grocery, tables, bills and kitchen tickets in a restaurant. That part is a separate "vertical module" that hands the lines to the core with sales.CreateSale.

Core and vertical module

As a result, the tiers above receive the same sale.completed.v1 event from both sectors; the store server and head office do not know which module is in use. Switching from grocery to restaurant means stopping one container and starting the other.

All services follow the same skeleton: the business rules (the domain) depend on no outside package, and the outside world, such as the database and the network, lives in "adapters". A change on the database or network side can therefore be made without touching the business code.

Never lose an event, never count one twice

The journey of a sale event from the register to head office is the heart of this architecture. Two things must not happen: an event getting lost (the sale is missing from the reports) and an event being counted twice (the revenue is inflated). Every flow uses the same five steps:

  1. Outbox: the service writes the sale and the event to send in the same database transaction. Either both exist or neither does.
  2. Relay: the events in the outbox are published to NATS in the order they were written. If publishing fails, the relay stops; the order is never broken.
  3. Sourcing: the upper tier's stream takes the lower tier's stream as a source over the leaf connection, and NATS remembers where it left off for each source.
  4. Durable consumer and de-duplication: the service that processes an event recognises it by its id. At head office the event id is a primary key; an event that arrives a second time is silently skipped.
  5. Delivery receipt: the interesting one, below.

NATS's own acknowledgement only says "the event was written to the other stream". It does not prove that the event made it into the upper tier's database. And that is exactly what the register wants to know before it deletes old events. So there is an application-level receipt: once the upper tier has written the event to its database, it publishes a receipt, and the receipt travels back down.

Delivery receipt between register and store server

The same happens one level up, between the store and head office. At head office the event record, the tables read by the reports and the receipt are written in the same database transaction: if the event cannot be written, there is no receipt either.

Delivery receipt between store server and head office

One more detail: head office knows which store an event came from from its own configuration, not from what the event says. When it sources a store's stream, it adds that store's id to the front of the subject itself. A misconfigured register cannot write in another store's name, and registers with the same name (register-1) in two stores never get mixed up.

The receipt proves "processed". "Never arrived" is caught by something else: the register gives its events consecutive sequence numbers, the upper tiers keep them as ranges, and a gap shows up on the screen as a "missing record".

This is the life of an event on the register:

The life cycle of an event on the register

The rule is simple: an event is not deleted until it is proven to be in the upper tier's database. In a setup without an upper tier the events therefore stay on disk; there, keeping an eye on disk usage is enough.

An in-between state for money: charged

A sale's states are open, charged, paid and voided. The interesting one is charged: the payment was taken but the fiscal record (the receipt) could not be made yet.

The sale's state machine

This state comes from a real problem: the card was charged, then the printer stopped answering. Three rules work together here:

  • The payment is not taken twice. For the same sale, the payment service does not charge again; it returns the first result. Even if an answer is lost and the call is retried, the customer is not charged twice. Retrying the receipt continues with the payment already recorded.
  • The receipt number is assigned only after a successful fiscal record, in the same transaction as the sale and its event. Failed attempts leave no gaps in the numbering, so the "missing receipt" warning raises no false alarms.
  • It stays visible. While a charged sale exists, the register shows a warning, and it also appears on that register's card on the store server's screen. The way out is either to retry the receipt or, with a manager's approval, to refund the payment.

The end-of-day (Z) gate

The end-of-day report (Z) is not taken until the day's sale events have been processed. Otherwise, with the message bus down, the day would close on an incomplete report and sales would slide into the next day.

The end-of-day consistency gate

The order matters: first come the events the sales service has not published yet, then the events the shift service has not processed yet. In the opposite order, an event published in between could slip through. If a short wait runs out, the register says "events are waiting, try again when the message bus is back" and does not take the Z.

Three machines for head office

By default head office is a single machine. The reasoning: even when head office stops, no sale is lost; the events wait at the stores and head office picks up where it left off when it comes back. For businesses that want higher availability, there is a three-machine head office setup.

The three-machine head office

Every machine runs the same parts: a NATS node (streams with three copies), PostgreSQL managed by Patroni, etcd, and an HAProxy on each machine. The primary is not fixed; on failover Patroni promotes another machine, and the HAProxies send the connections there.

Replication is synchronous and strict. The reason ties straight back to the receipt: once head office has issued a receipt, the store may delete its own copy. If data with a receipt were lost in a failover, it would no longer exist anywhere.

Some head office jobs must run in only one copy at a time (processing events in order, for example). A PostgreSQL advisory lock takes care of that: the copy holding the lock works, the others wait. If the machine holding the lock silently disappears, its session is closed after 10 seconds, the lock is released and another copy takes over.

In the documented Docker test, when the primary is killed, writes pause for about 30–32 seconds and none of the acknowledged writes (2,499 and 2,509 in two runs) are lost. A trial on three real machines is next.

Updates: not automatic, approved

Updating is an architecture of its own. Images come down from the publisher to head office, from head office to the store, from the store to the register through a chain of mirrors, and every step is checked against a chain of trust.

The update trust and mirror chain

  • Channels: test, pilot, prod. Promoting a release does not mean rebuilding it; it means adding a second signature with that channel's key to the same image. prod machines only accept the prod signature.
  • No automatic updates. The agent downloads a new release in advance but does not apply it; it shows "new version ready" on the screen. Applying it needs an authorised person's approval, a manager's PIN on the register. It is never applied while a shift is open on the register.
  • Rolled back if unhealthy. If the new version does not become healthy within a set time, the agent goes back to the previous image.
  • The register does not know head office's address. A proxy on the store server forwards its registration and permission requests; the proxy decides nothing, it only carries them.

From a corner shop to a chain

The same images run in every tier; what makes one setup different from another is which containers are switched on. That is why the same software can grow from a single-register shop to a chain of stores.

Deployment options

In a small store, the store server can run on one of the registers; that register then becomes critical. If the store is linked to head office, the catalog is managed there and is read-only at the store; if not, the same catalog is managed at the store.

What comes next

Part of the architecture is designed and waiting its turn:

  • A real fiscal device. For now a simulator prints the receipt. The adapter for the Beko X30TR is ready (a Go service and a thin C++ bridge around the manufacturer's library) and tested against a fake library; the trial with a real device is next.
  • A single register connecting straight to head office. Today there is always a store server between the register and head office.
  • A multi-terminal restaurant. The restaurant module runs on a single register today; the API is prepared for the version where tables live on the store server and waiters take orders on a phone.
  • Real-world trials. The three-machine head office, and the part of the update agent that manages the services on the machine, run in a test environment; trying them in the field is next.

The project is licensed under Apache 2.0. Code, documentation and decision records: github.com/abarow/kasadan-merkeze-sistem-abarow

© 2026 Mehmet Ergülcü SvelteKit · Tailwind · <1 KB JS