You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a MongoDBBackend implementation to support MongoDB as a storage backend for RunnerQ.
Background
With the pluggable storage abstraction layer (QueueBackend and InspectionBackend traits), RunnerQ can now support alternative backends beyond Redis. MongoDB is a natural fit for activity processing scenarios where:
Document Model - Rich, flexible schema for activity payloads and metadata
Atomic Operations - findOneAndUpdate for safe lease-based dequeuing at the document level
Change Streams - Real-time event streaming for observability (requires replica set) - polling can also be considered
Scalability - Replica sets and sharding for high availability
Enterprise Adoption - Many organizations already have MongoDB infrastructure
TTL Indexes - Native support for automatic expiration of results and idempotency keys
// Main activity queries{status: 1,priority: -1,created_at: 1}// Dequeue (pending by priority){status: 1,scheduled_at: 1}// Process scheduled{status: 1,lease_deadline: 1}// Requeue expired{idempotency_key: 1}// Idempotency lookups (sparse)// Results collection (TTL){created_at: 1},{expireAfterSeconds: 86400}// Idempotency collection (TTL) {created_at: 1},{expireAfterSeconds: 86400}// Events collection (TTL or capped){activity_id: 1,timestamp: -1}{timestamp: 1},{expireAfterSeconds: 604800}// 7 days
Important Caveats
Change Streams Requirement: MongoDB Change Streams require a replica set or sharded cluster. They will NOT work on a standalone MongoDB instance. This affects the event_stream() implementation.
TTL Index Precision: MongoDB's TTL background thread runs approximately every 60 seconds. Document deletion may be delayed by up to 60+ seconds after expiration. For time-critical expiration, consider application-level checks.
Priority Queue Efficiency: Unlike Redis ZSET which provides O(log N) operations, MongoDB's compound index approach requires a full query + sort. For very high-throughput queues, Redis may be more performant.
Multi-Document Transactions: Available since MongoDB 4.0+ but require replica set deployment. For single-document operations (our primary use case), this is not a concern.
Licensing: MongoDB uses the Server Side Public License (SSPL).
Tasks
Research MongoDB Rust client (mongodb crate)
Design collection schema and indexes
Create src/backend/mongodb/ module structure
Implement MongoDBBackend struct with builder pattern
Implement QueueBackend trait
Implement InspectionBackend trait
Handle priority queue semantics with compound indexes
Implement Change Streams for event_stream() (with replica set fallback/error)
Add TTL indexes for results and idempotency
Write integration tests (requires MongoDB replica set / Docker)
Summary
Add a
MongoDBBackendimplementation to support MongoDB as a storage backend for RunnerQ.Background
With the pluggable storage abstraction layer (
QueueBackendandInspectionBackendtraits), RunnerQ can now support alternative backends beyond Redis. MongoDB is a natural fit for activity processing scenarios where:findOneAndUpdatefor safe lease-based dequeuing at the document levelProposed Implementation
File Structure
API
Collection Structure
Design Considerations
QueueBackend Implementation
enqueue()status: "pending"orstatus: "scheduled"dequeue()findOneAndUpdatewithstatus: "pending"→status: "processing", setlease_deadlineack_success()status: "completed", clear lease fieldsack_failure()retry_count, setstatus: "scheduled"with backoff, or move to DLQprocess_scheduled()status: "scheduled"wherescheduled_at <= now, update tostatus: "pending"requeue_expired()status: "processing"wherelease_deadline < now, update tostatus: "pending"extend_lease()lease_deadlinewhere_idmatchesstore_result()get_result()_idin results collectioncheck_idempotency()$setOnInsert, check if insert or existingInspectionBackend Implementation
stats()$groupby status and prioritylist_pending()status: "pending", sort by priority desc, created_at asc, with skip/limitlist_processing()status: "processing"with skip/limitlist_scheduled()status: "scheduled", sort byscheduled_atlist_completed()status: "completed", sort bycompleted_atdesclist_dead_letter()get_activity()_idget_activity_events()activity_idin events collection, limitevent_stream()Activity Document Schema
Indexes
Important Caveats
Change Streams Requirement: MongoDB Change Streams require a replica set or sharded cluster. They will NOT work on a standalone MongoDB instance. This affects the
event_stream()implementation.TTL Index Precision: MongoDB's TTL background thread runs approximately every 60 seconds. Document deletion may be delayed by up to 60+ seconds after expiration. For time-critical expiration, consider application-level checks.
Priority Queue Efficiency: Unlike Redis ZSET which provides O(log N) operations, MongoDB's compound index approach requires a full query + sort. For very high-throughput queues, Redis may be more performant.
Multi-Document Transactions: Available since MongoDB 4.0+ but require replica set deployment. For single-document operations (our primary use case), this is not a concern.
Licensing: MongoDB uses the Server Side Public License (SSPL).
Tasks
mongodbcrate)src/backend/mongodb/module structureMongoDBBackendstruct with builder patternQueueBackendtraitInspectionBackendtraitevent_stream()(with replica set fallback/error)examples/mongodb_example.rsFeature Flags
Dependencies
References
Related
QueueBackendandInspectionBackendtraits insrc/backend/traits.rssrc/backend/redis/