Skip to main content

Offline-First POS System

Dokumentasi lengkap untuk implementasi offline-first architecture di MStore Backend, memungkinkan POS client beroperasi tanpa koneksi internet dan sync otomatis saat online.

🎯 Overview

Offline-first architecture memungkinkan:
  • 100% operasional tanpa internet untuk transaksi CASH
  • Auto-sync background saat koneksi tersedia
  • Conflict resolution otomatis untuk data collision
  • Batch sync untuk efisiensi bandwidth
  • Idempotent operations untuk retry safety
  • Flutter + Isar DB untuk mobile client
  • IndexedDB untuk web client

🏗️ Architecture Flow


📊 Database Schema

Offline Fields in transactions Table

Conflict Tracking Table


📡 API Endpoints

1. Batch Sync

Endpoint untuk sync batch transactions, payments, dan voids dari offline client.
Response:

2. Resolve Conflict

Response:

📱 Flutter + Isar DB Implementation

1. Setup Dependencies


2. Define Isar Collections


3. Initialize Isar


4. Create Transaction (Offline-First)


5. Background Sync Worker


6. Conflict Resolution UI


🧪 Testing Scenarios

Scenario 1: Offline Transaction (CASH)


📊 Implementation Status

✅ Backend (100% Complete)

✅ Documentation (100% Complete)


💡 Best Practices

DO ✅

  • Always save to local DB first before API call
  • Use unique offline_reference for idempotency
  • Implement retry logic with exponential backoff
  • Show sync status to users
  • Handle conflicts gracefully
  • Encrypt sensitive data in local DB
  • Monitor sync metrics

DON’T ❌

  • Don’t block UI waiting for API response
  • Don’t sync on every transaction (batch instead)
  • Don’t ignore conflicts (must resolve)
  • Don’t store unencrypted payment data
  • Don’t retry infinitely (max 3-5 attempts)
  • Don’t sync when battery is low

🆘 Troubleshooting

Problem: Sync Queue Growing

Symptoms: Sync queue has 100+ pending items Solution:

Problem: Isar Database Locked

Symptoms: “Database is locked” error Solution:

Transaction Flow

Complete transaction flow & state machine

Payment Gateway

QRIS and payment integration

Inventory Flow

Inventory management & stock movement

Need Help? Contact backend team atau check GitHub Issues