Overview
The OPFS (Origin Private File System) Worker is fundamental to GenosDB’s architecture, providing:- Non-blocking I/O: All persistence happens on a background thread
- Tiered storage: Automatic fallback from OPFS → IndexedDB
- Data integrity: Serialized access control prevents corruption
- Resource safety: Guaranteed cleanup even on errors
Core Architectural Pillars
1. Dynamic Storage Strategy with Tiered Fallback
The worker employs an intelligent, tiered approach to data storage, automatically selecting the most performant API available in the user’s browser at runtime.Synchronous OPFS (Preferred)
Leverages
createSyncAccessHandle API for lowest latency file I/O with direct, block-level accessAsynchronous OPFS (Fallback)
Uses
createWritable stream-based API for modern browsers without sync supportThis dynamic strategy allows GenosDB to capitalize on cutting-edge browser features while maintaining universal compatibility.
2. Serialized Access Control for Data Integrity
To prevent data corruption from concurrent write operations, the worker implements a sophisticated access control mechanism. Per-File Asynchronous Queue- Each file maintains a promise-based queue
- Every
saverequest is appended to the queue for its target file - Operations execute strictly one after another
3. Resilient and Leak-Proof Resource Management
Robustness is built into the worker’s design to handle unexpected failures gracefully. Guaranteed Resource Cleanup All interactions with OPFS file handles and writable streams are enclosed withintry...finally blocks:
- “File not found” errors for missing data
- I/O exceptions for system errors
- Permission errors for storage quota issues
4. True Asynchronous, Non-Blocking I/O
By operating entirely within a Web Worker, all file system interactions occur on a separate background thread.Performance Benefit
Intensive operations like serializing and persisting a large graph state will never block or freeze the main UI thread. Applications remain fluid and responsive at all times.
Worker Communication Protocol
The main thread and worker communicate via message passing:Message Format
Save OperationUsing Transferable Objects (ArrayBuffers) eliminates data copying overhead, dramatically improving performance for large databases.
Role within the GenosDB Ecosystem
The persistence worker is not just a technical convenience—it’s an enabler of GenosDB’s core capabilities:Performance at Scale
- Synchronous OPFS enables rapid persistence of compressed binary state
- Handles large datasets and high-frequency updates with minimal impact
- Tested with 100K+ nodes without performance degradation
Unyielding Data Consistency
- Serialized access control ensures on-disk state is never corrupted
- Provides reliable foundation for offline access
- Enables cross-tab synchronization via BroadcastChannel
Superior User Experience
- Offloading storage tasks prevents UI stutter
- Users experience no freezes while data saves in background
- Essential for collaborative and data-intensive applications
Developer-Friendly Abstraction
The worker exposes a simple, promise-based API:Cross-Tab Data Integrity
In version 0.11.8, a critical enhancement was made to prevent race conditions when multiple tabs write simultaneously.The Problem
Without coordination:- Tab A starts writing to
mydb.bin - Tab B starts writing to
mydb.binat the same time - File becomes corrupted with interleaved data
The Solution: Web Locks API
The Web Locks API ensures all write operations to a given file are serialized across tabs, guaranteeing data consistency.
Performance Characteristics
| Operation | Sync OPFS | Async OPFS | IndexedDB |
|---|---|---|---|
| Write (10KB) | ~2ms | ~5ms | ~10ms |
| Write (100KB) | ~5ms | ~15ms | ~30ms |
| Write (1MB) | ~20ms | ~50ms | ~100ms |
| Read (10KB) | ~1ms | ~3ms | ~8ms |
| Read (100KB) | ~3ms | ~10ms | ~25ms |
| Read (1MB) | ~15ms | ~40ms | ~90ms |
Performance varies based on hardware, browser, and system load. All operations are debounced (default 300ms) to batch rapid writes.
Configuration Options
You can configure the worker’s behavior during initialization:saveDelay: 0- Critical data that must persist immediatelysaveDelay: 300- Balanced performance (default)saveDelay: 1000+- High-throughput scenarios with acceptable data loss window
Related Pages
Architecture Overview
See how the worker fits into the overall system
Hybrid Delta Protocol
How synchronized data gets persisted