bunqueue Data Structures: MinHeap, Skip List & LRU
Heaps, skip lists, LRUs.
bunqueue uses specialized data structures optimized for job queue operations: a 4-ary MinHeap, skip lists, LRU caches, FNV-1a hashing, and read-write locks.
Overview
Section titled “Overview”| Structure | Use Case | Complexity |
|---|---|---|
| 4-ary MinHeap | Priority queue, cron scheduling, delayed-job tracking | O(log₄ n) |
| Skip List | Temporal indexing, cleanup range queries | O(log n) |
| LRU Cache | Job results, custom IDs | O(1) |
| Hash (FNV-1a) | Sharding, distribution | O(len) |
4-ary MinHeap
Section titled “4-ary MinHeap”Used for priority queues, cron scheduling, and delayed-job tracking (TemporalManager keeps delayed jobs in a MinHeap ordered by runAt for O(k) refresh).
Trade-off: 4 comparisons per level vs 2. Win: better cache locality outweighs extra comparisons.
Heap with Lazy Deletion
Section titled “Heap with Lazy Deletion”Skip List
Section titled “Skip List”Used for the temporal index (jobs ordered by createdAt) and efficient range queries during cleanup. Delayed jobs are tracked separately in a MinHeap, not here.
Properties: probabilistic level assignment (p=0.5), expected height O(log n), simpler than balanced trees, good cache locality (sequential links).
Range Queries
Section titled “Range Queries”Total: O(log n + k) where k = results. Use case: find jobs older than X for cleanup.
LRU Cache
Section titled “LRU Cache”Used for job results, custom ID mapping, and logs.
All operations: O(1).
Memory Bounds
Section titled “Memory Bounds”BoundedSet (FIFO): no recency tracking (faster), batch eviction removes 10% when full, amortized cost across many operations.
Hash Function (FNV-1a)
Section titled “Hash Function (FNV-1a)”Used for sharding and distribution.
Sharding
Section titled “Sharding”shardIndex = fnv1a(queueName) & SHARD_MASKWhy bitwise AND? 3-5x faster than modulo, requires power-of-2 shard count, hash & SHARD_MASK is equivalent to hash % SHARD_COUNT.
Lock Structures
Section titled “Lock Structures”RWLock (Read-Write Lock)
Section titled “RWLock (Read-Write Lock)”if (!writer && readers === 0) { writer = true; return guard; }, synchronous, no PromiseComplexity Summary
Section titled “Complexity Summary”| Operation | Structure | Time |
|---|---|---|
| Push job | 4-ary heap | O(log₄ n) |
| Pop job | 4-ary heap | O(log₄ n) |
| Find job | Index map | O(1) |
| Remove job | Lazy deletion | O(1) |
| Get result | LRU map | O(1) |
| Shard lookup | Hash + AND | O(len) |
| Range query | Skip list | O(log n + k) |
| Lock acquire | RWLock | O(1) uncontested |