Overview
kSync follows a local-first, event-sourced architecture designed for modern applications that need real-time synchronization without sacrificing offline capabilities or performance.
Core Principles
Local-First
All operations happen locally first, ensuring your app works offline and provides instant feedback
Event-Sourced
State is derived from an immutable sequence of events, providing complete audit trails and time-travel debugging
Schema-Driven
All events are validated against Zod schemas, ensuring type safety at runtime
Real-Time Sync
Changes are synchronized across all clients in real-time via WebSocket connections
Architecture Components
1. Event Store
The event store is the heart of kSync’s architecture. It maintains an append-only log of all events.- Immutable: Events are never modified once created
- Ordered: Events have sequence numbers for consistent ordering
- Typed: Each event type has a corresponding Zod schema
- Traceable: Full audit trail of all changes
2. Storage Layer
kSync provides multiple storage implementations:- IndexedDB (Browser)
- Memory (Server/Testing)
- Custom Storage
- Persistent across browser sessions
- Efficient querying and indexing
- Automatic cleanup of old events
- Works offline
3. Synchronization Layer
The sync layer handles real-time communication between clients and servers. Sync Features:- Automatic Reconnection: Handles network interruptions gracefully
- Event Batching: Groups events for efficient network usage
- Conflict Resolution: Last-write-wins with timestamp ordering
- Heartbeat Monitoring: Detects and handles stale connections
4. Tab Coordination
For browser applications, kSync coordinates between multiple tabs using a leader election system.- Web Locks API (Modern Browsers)
- IndexedDB Fallback (Older Browsers)
- Single Connection: Only one tab per origin connects to the server
- Efficient: Reduces server load and network usage
- Reliable: Automatic failover if leader tab closes
5. State Materialization
Events are transformed into queryable state using materializer functions.Data Flow
The complete data flow in kSync follows this pattern:Step-by-Step Flow
- User Action: User performs an action (e.g., sends a message)
- Event Creation: kSync creates a typed event with schema validation
- Local Storage: Event is immediately stored locally
- Optimistic Update: UI updates immediately for responsive UX
- Network Sync: Event is sent to server via WebSocket
- Server Broadcast: Server broadcasts event to all connected clients
- Remote Application: Other clients receive and apply the event
- State Materialization: Events are transformed into queryable state
Performance Optimizations
Event Batching
kSync batches events to reduce network overhead:Memory Management
- Event Trimming: Automatically removes old events to prevent memory leaks
- Lazy Loading: Only loads events when needed
- Efficient Indexing: Uses optimized data structures for fast queries
Network Efficiency
- Connection Pooling: Reuses WebSocket connections
- Compression: Automatically compresses large payloads
- Heartbeat: Minimal ping/pong for connection health
Conflict Resolution
kSync uses a simple but effective conflict resolution strategy:Last-Write-Wins (LWW)
Custom Resolution
For complex scenarios, you can implement custom conflict resolution:Security Considerations
Schema Validation
Schema Validation
All events are validated against Zod schemas before processing:
Client Authentication
Client Authentication
Implement authentication at the WebSocket level:
Event Sanitization
Event Sanitization
Sanitize event data before storage:
Scalability
Horizontal Scaling
Vertical Scaling
- Event Partitioning: Split events by type or user
- Sharding: Distribute events across multiple storage instances
- Caching: Use Redis for frequently accessed events
Comparison with Other Architectures
- vs Traditional REST
- vs GraphQL Subscriptions
- vs Firebase/Supabase
| Feature | kSync | REST API |
|---|---|---|
| Offline Support | ✅ Full | ❌ None |
| Real-time Updates | ✅ Built-in | ❌ Requires polling |
| Optimistic Updates | ✅ Automatic | ❌ Manual |
| Type Safety | ✅ Runtime + Compile | ❌ Compile only |
| Audit Trail | ✅ Complete | ❌ Manual |

