Skip to main content

πŸ”„ Hybrid Sync Strategy: Polling + Delta Sync via MQTT

πŸ“‹ Overview

MStore Mobile menggunakan Hybrid Sync Strategy yang menggabungkan:
  1. Polling-based Sync (Existing) - Full refresh setiap 5 menit sebagai fallback
  2. Delta Sync via MQTT (New) - Real-time incremental updates untuk perubahan data

🎯 Benefits


πŸ“š Documentation

For Mobile Developers:

For Backend Developers:


πŸ—οΈ Architecture Components

Mobile (Flutter):

Backend:


πŸ”„ How It Works

1. Initial Load (App Start)

2. Real-time Update (Delta Sync)

3. Fallback (Polling)


πŸ“‘ MQTT Topics & Payloads

Product Stock Update

Topic: {env}/{merchant}/{branch}/product/updates Payload:

Inventory Quantity Update

Topic: {env}/{merchant}/{branch}/inventory/updates Payload:

Transaction Notification

Topic: {env}/{merchant}/{branch}/transaction/updates Payload:

πŸš€ Getting Started

Mobile Setup (Already Done βœ…)

  1. DeltaSyncService sudah diimplementasikan di lib/core/sync/delta_sync_service.dart
  2. MqttBloc sudah updated untuk subscribe delta topics
  3. Dependency Injection sudah configured via @LazySingleton()
  4. Build runner sudah di-run

Backend Setup (TODO)

  1. Install MQTT Client Library
  2. Implement EventPublisher
  3. Integrate dengan Services
    • ProductService: Call PublishProductStockUpdate() after stock change
    • InventoryService: Call PublishInventoryUpdate() after quantity change
    • TransactionService: Call PublishTransactionCreated() after create
  4. Configure MQTT Broker ACL
    • Allow backend to publish to update topics
    • Deny devices from publishing to update topics
    • Allow devices to subscribe only to their branch

πŸ§ͺ Testing

Manual Test

  1. Subscribe ke topic menggunakan MQTT client:
  2. Update stock via backend API atau database
  3. Verify message diterima di mosquitto_sub
  4. Check mobile app - stock harus update real-time

Integration Test


πŸ“Š Monitoring

Logs to Watch

Mobile:
Backend:

Metrics

  • MQTT Publish Success Rate: Target > 99%
  • Delta Sync Latency: Target < 100ms
  • Fallback Trigger Rate: Target < 1% (most updates via MQTT)

πŸ” Security

MQTT Authentication

  • Each device uses unique credentials
  • Username: deviceId
  • Password: Secret token from backend

Topic ACL

  • Devices can only subscribe to their branch topics
  • Only backend can publish to update topics
  • Wildcard subscriptions restricted

SSL/TLS

  • Production: MQTT over TLS (port 8883)
  • Development: Plain MQTT (port 1883)

πŸ› Troubleshooting

Issue: Stock tidak update real-time

Check:
  1. MQTT connection status: AppLog tag mqtt
  2. Subscription success: Look for β€œsubscribe delta sync topics”
  3. Message received: Look for β€œMQTT message received”
  4. Handler executed: Look for β€œProduct stock updated”
Solution:
  • Verify MQTT broker running
  • Check ACL rules
  • Verify topic format matches

Issue: Duplicate updates

Cause: Both polling and delta sync triggered Solution: Normal behavior - delta sync updates immediately, polling is fallback

πŸ“ž Support

  • Mobile Issues: Check lib/core/sync/delta_sync_service.dart
  • Backend Issues: Check internal/service/event_publisher.go
  • MQTT Issues: Check broker logs & ACL config
  • Documentation: See delta-sync-architecture.md

πŸŽ‰ Summary

βœ… Hybrid sync strategy implemented
βœ… Real-time updates via MQTT delta sync
βœ… Polling fallback untuk reliability
βœ… Documentation lengkap untuk mobile & backend
βœ… Security via ACL & authentication
βœ… Monitoring via structured logging
Next Steps:
  1. Backend team implement EventPublisher
  2. Configure MQTT broker ACL
  3. Deploy & test in staging
  4. Monitor metrics & optimize

Last Updated: 2025-10-14
Version: 1.0.0
Author: MStore Development Team