Overview
Database synchronization in LiveSync ensures that changes in your backend database are reliably propagated to all connected clients in realtime. This page covers synchronization patterns, the Models SDK, and best practices for maintaining consistent state across distributed systems.Synchronization Patterns
Outbox Pattern (Postgres)
The outbox pattern provides transactional guarantees when publishing database changes:- Transactional consistency between database writes and message publishing
- Exactly-once delivery guarantees
- Automatic retry on failures
- Message ordering preserved per channel
Change Streams (MongoDB)
MongoDB uses Change Streams to watch for document modifications:- Native MongoDB integration
- Real-time change detection
- Full document access with pre/post images
- Flexible filtering via aggregation pipelines
Frontend Data Models
The Models SDK provides a high-level abstraction for managing synchronized state in frontend applications.Installing the Models SDK
Creating a Model
A model represents a synchronized data structure in your application:The Sync Function
The sync function fetches the current state from your backend:The Merge Function
The merge function combines incoming changes with existing state:- Pure: Same input always produces same output
- Deterministic: No randomness or external state
- Side-effect free: No API calls, mutations, or logging
Subscribing to Changes
Subscribe to model updates to react to state changes:Optimistic Updates
Optimistic updates provide instant feedback by applying changes locally before server confirmation.Implementing Optimistic Updates
Backend Confirmation
Synchronization Strategies
Full State Synchronization
Send complete object state with each update:- Simple to implement
- No merge logic needed
- Always consistent state
- Higher bandwidth usage
- May exceed message size limits
- Less efficient for large objects
Delta Synchronization
Send only the changed fields:- Efficient bandwidth usage
- Works with large objects
- Clear change intent
- Requires merge logic
- More complex implementation
JSON Patch Synchronization
Use standardized JSON Patch format:- Standardized format
- Automatic merge with libraries
- Precise change tracking
- More complex to generate
- Requires JSON Patch library
Implementation Example
Handling History and Replay
Message History
Retrieve historical changes when a client reconnects:Automatic History Replay
The Models SDK automatically handles history replay:Performance Optimization
Channel Design
Design channels for optimal performance:Message Batching
Batch multiple changes into single messages:Event Buffering
Buffer events to reduce merge operations:Error Handling
Connection Errors
Sync Errors
Optimistic Update Failures
Best Practices
1. Design Clear Channel Hierarchies
2. Use Consistent Event Names
3. Implement Idempotent Operations
4. Handle Concurrent Updates
5. Monitor and Log
Next Steps
Conflict Resolution
Learn strategies for resolving conflicts in distributed systems
Postgres Models
Explore the Models SDK for Postgres in depth
Quickstart
Build a complete LiveSync application from scratch
