FileStorage is a file-based storage backend that provides persistence without external dependencies like Redis. It’s ideal for single-node deployments that need to survive process restarts.
Constructor
Configuration
Base directory for storing all queue data. The directory will be created if it doesn’t exist.FileStorage creates the following subdirectories:
queue/- Pending jobsprocessing/- Jobs being processed by workersjobs/- Job state filesresults/- Job resultserrors/- Job errorsworkers/- Worker registrationsnotify/- Notification files
When to Use
FileStorage is suitable for:
- Single-node deployments: Applications running on one machine
- Persistence without Redis: No external dependencies required
- Development with persistence: Local development that survives restarts
- Low-throughput queues: Background jobs that don’t require high concurrency
- Embedded systems: Resource-constrained environments
Limitations
- Single machine only: Cannot scale horizontally
- Filesystem performance: Limited by disk I/O and filesystem characteristics
- Network storage risks: Using NFS/CIFS may cause race conditions
- No cross-platform sync: Cannot share between Windows and Unix systems
Features
Atomic Writes
All writes usefast-write-atomic for crash safety:
- Crash resistant: Partial writes never corrupt existing files
- Race condition safe: Multiple processes can write safely
- Atomic operations: Rename is atomic on most filesystems
Filesystem Watching
Uses Node.jsfs.watch() for efficient blocking dequeue:
- No polling: Efficiently waits for filesystem events
- Low CPU usage: OS-level notifications via inotify/FSEvents
- Automatic retry: Watcher restarts on errors
Leader Election
Implements file-based leader election for cleanup coordination:TTL Management
Results and errors are stored with separate.ttl files:
Example
Basic Usage
Multiple Workers on Same Machine
Temporary Queue for Testing
File Structure
WithbasePath: '/var/lib/queue', FileStorage creates:
File Naming Conventions
- Queue files:
{sequence}-{jobId}.msg(sequence ensures FIFO order) - State files:
{jobId}.state(contains state string) - Result files:
{jobId}.result+{jobId}.ttl - Error files:
{jobId}.error+{jobId}.ttl - Worker files:
{workerId}.worker(contains expiry timestamp) - Notify files:
{jobId}-{timestamp}.notify - Lock files:
{lockKey}.lock(JSON with ownerId and expiresAt)
Implementation Details
Dequeue Race Conditions
Multiple workers can safely dequeue from the same filesystem:- Worker lists queue files sorted by sequence
- Worker attempts atomic
rename()to claim file - Only one worker succeeds (OS guarantees atomicity)
- Failed workers try the next file
Notification System
FileStorage uses a hybrid notification approach:- In-process: EventEmitter for same-process subscribers
- Cross-process: File watcher on
notify/directory
Cleanup Leadership
Only one FileStorage instance performs TTL cleanup:- Leader: Acquired lock, runs cleanup interval
- Follower: No lock, tries to acquire every 5 seconds
- Lock renewal: Leader renews every 3 seconds (TTL is 10 seconds)
- Automatic failover: If leader crashes, lock expires and follower takes over
Performance Considerations
Filesystem Recommendations
- Local SSD: Best performance (low latency, high IOPS)
- Local HDD: Acceptable for low-throughput queues
- Network storage: Avoid if possible (NFS/CIFS have race condition risks)
- Tmpfs/RAM disk: Fast but not persistent (use MemoryStorage instead)
Sequence Number Overflow
Sequence numbers are incremented per enqueue. With 12-digit zero-padding:- Max sequence: 999,999,999,999 (1 trillion)
- At 1000 jobs/sec: Overflows after ~31 years
- Overflow behavior: Filenames still sort correctly (lexicographic)
Directory Listing Performance
For large queues, consider:- Periodic cleanup: Use reaper to remove old job states
- Partition by date: Implement custom storage with date-based subdirectories
- Monitor inode usage: Large queues can exhaust filesystem inodes
Testing
FileStorage provides aclear() method for testing:
clear() method:
- Removes all queue files
- Removes all job states
- Removes all results and errors
- Recreates empty directory structure
Related
- MemoryStorage - In-memory storage for testing
- RedisStorage - Distributed production storage
- Queue API - Queue setup with storage backends