Architecture

Designing an Offline-First Sync Architecture for Specialty Retail: Conflict-Free Replicated Data Types (CRDTs) and Monotonically Increasing Ledgers

A deep-dive into how we maintain ledger convergence and zero-trust double-spend immunity when both the POS register and the customer smartphone are completely disconnected.

Enes Demirel
Enes DemirelHead of Customer Success, Australia
May 27, 2026
14 min read

The Disconnected Edge: Solving Loyalty Convergence Without the Cloud

In special retail and hospitality—particularly busy high-volume specialty coffee roasteries—network connectivity is highly volatile. An subterranean train station kiosk or a rural espresso bar frequently drops internet access for minutes or hours.

Despite zero network access, checkout counters must continue scanning loyalty cards, accumulating stamps, and processing rewards at 120 FPS UI response rates. Stalling the queue due to a cloud API timeout is unacceptable.

However, letting disconnected terminals record transactions independently introduces classic distributed systems issues:

  • Out-of-Order Conflicts: A customer spends a voucher offline on Register A, while Register B registers additional stamps that theoretically earned that voucher.
  • Double-Spend Vector: A customer scans their offline PWA at two disconnected registers within seconds to claim two rewards.
  • State Drifts: Reconnecting registers overwrite each other’s stamp balances, corrupting customer logs.

At Loyalisa, we solved this by moving away from traditional master-slave database replication. Instead, we utilize a custom implementation of Conflict-Free Replicated Data Types (CRDTs) paired with Monotonically Increasing Ledgers. Here is the deep engineering blueprint of how we guarantee state convergence.


1. The Algebra of Convergence: Conflict-Free Replicated Data Types (CRDTs)

A traditional database transaction updates a column value directly (e.g. SET stamps = stamps + 1). If Register A and Register B execute this concurrently while disconnected, merging their states on reconnection is mathematically ambiguous.

Loyalisa replaces mutable values with a State-Based Grow-Only Counter (G-Counter), a state-based CRDT that converges deterministically without a central coordinator.

G-Counter Mathematical Definition:

A G-Counter for a customer's loyalty balance across N register nodes is represented as a vector V of size N, where each index i corresponds to the stamp transactions registered by node i:

V = [v_1, v_2, …, v_n]

  • State Increment: When Register i adds 1 stamp, it increments its own vector index:

v_i ← v_i + 1

  • Value Query: The total stamp balance is the algebraic sum of all vector components:

Value(V) = ∑ v_k

  • Merge Operator (Join): When Register A and Register B reconnect, they merge their state vectors by taking the maximum value of each index component. This operation is Commutative, Associative, and Idempotent:

V_C = V_A ⊔ V_B = [max(v_{A,1}, v_{B,1}), …, max(v_{A,n}, v_{B,n})]

The G-Counter Merge in TypeScript:

Here is the core logical block of the Loyalisa reconciliation engine:

typescript
interface GCounter {
  [nodeId: string]: number;
}

function mergeCounters(a: GCounter, b: GCounter): GCounter {
  const merged: GCounter = { ...a };
  
  for (const [nodeId, value] of Object.entries(b)) {
    merged[nodeId] = Math.max(merged[nodeId] || 0, value);
  }
  
  return merged;
}

function queryCounterValue(counter: GCounter): number {
  return Object.values(counter).reduce((sum, val) => sum + val, 0);
}

By storing the transaction logs as a G-Counter vector inside the local client Isar NoSQL store, we ensure that no matter how many times or in what order offline sync packets arrive, the merged stamp balance is mathematically identical.


2. Double-Spend Immunity via Monotonically Increasing Ledgers

While CRDTs guarantee that stamp additions converge, reward redemptions represent decrements, which standard G-Counters cannot handle securely in a partition-tolerant environment (as POS nodes are prone to double-spending when network split-brain occurs).

To secure reward redemptions offline, we implement a Monotonically Increasing Ledger (MIL).

  • The Stamp Log: Every stamp is written as an immutable ledger record containing a unique transaction hash, client signature, sequence ID, and node identifier.
  • The Spent Ledger: Vouchers and rewards are issued with unique, cryptographically signed serial numbers.
  • Serial Consumption Lock: When a reward is redeemed offline, the terminal writes a record to the Spent Ledger:
json
  {
    "voucherId": "vch_8819a",
    "redeemedAt": "2026-05-29T03:30:00Z",
    "nodeId": "pos_terminal_03",
    "signature": "3a0bfa9d..."
  }
  • Reconciliation Priority: When sync states merge, the Spent Ledger is processed first. If a voucher ID appears in any node's Spent Ledger, it is instantly marked globally spent.
  • The Convergence Audit: If the server detects that the same voucher ID was processed at two different terminals during an offline partition, it triggers our automatic recovery path. The ledger recalculates the user’s true balance. It automatically flags the anomalous terminal and applies a corrective debit ledger entry, maintaining absolute ledger integrity.

3. Edge Sync Protocol Efficiency

To prevent network bloat, our sync workers do not transmit full transaction histories. Instead, they exchange state vectors during the reconnect handshake:

Sync PhaseVector ExchangedPayload SizeLatencyNetwork Bandwidth
1. HandshakeRegional Sync Vector (CSV)< 250 bytes12msVery Low
2. Delta CalculationMerkle Tree Roots< 1 KB8msLow
3. Merge SweepDiff Bytes PayloadVariable (10-150 Scans)22msMedium (Gzipped)
4. Final ConvergenceEmpty JSON { "status": "SYNCHRONIZED" }24 bytes4msMinimal

By combining G-Counter vector addition with a strict monotonically increasing Spent Ledger, Loyalisa achieves a partition-tolerant, zero-trust loyalty system. The checkout counter keeps humming at 120 FPS, double-spend vector exploits are cryptographically mitigated, and business margins remain completely secure.

Want to launch a loyalty system like this?

Create custom stamp cards, add passes dynamically inside mobile wallets, and start Retargeting regulars automatically.