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 | Queue-local temporal indexing, cleanup range queries | O(log q) |
| 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”The delayed-job heap uses a different lazy-removal check: a Map<jobId, runAt>
is the live source of truth. When no delayed jobs remain, the heap is cleared
immediately. Otherwise it is rebuilt in O(n) once there are at least 256 stale
entries and stale entries are at least as numerous as live entries. This bounds
retained heap memory after cancellation or promotion churn.
Skip List
Section titled “Skip List”Used for queue-local temporal indexes (jobs ordered by createdAt, then job ID) and efficient range queries during cleanup. Each queue owns a separate skip list, while a direct job-ID map points to the corresponding entries for logarithmic removal. 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 q + k), where q is the number of indexed jobs in that queue and k is the number returned. Removal uses the job-ID map plus a queue-local skip-list delete: O(log q).
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)”writer = true before resolving the oldest waiter; late arrivals cannot bargeComplexity 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 | Queue-local skip list | O(log q + k) |
| Remove temporal entry | Job-ID map + queue skip list | O(log q) |
| Lock acquire | RWLock | O(1) uncontested |