Change Streams
MongoDB Change Streams provide real-time notifications when jobs are created or updated, eliminating polling latency for instant job processing.
What Are Change Streams?
Section titled “What Are Change Streams?”Change Streams allow applications to subscribe to real-time data changes in MongoDB collections. Instead of polling (“Are there new jobs?”), Monque receives push notifications (“A new job was just added!”).
How Monque Uses Change Streams
Section titled “How Monque Uses Change Streams”Default Behavior
Section titled “Default Behavior”When you call start(), Monque:
- Opens a Change Stream on the jobs collection
- Listens for events: new job insertions and status changes
- Triggers immediate processing when relevant changes occur
- Maintains polling as backup in case Change Streams fail
Event Types Monitored
Section titled “Event Types Monitored”Benefits of Change Streams
Section titled “Benefits of Change Streams”| Without Change Streams | With Change Streams |
|---|---|
| Reacts on poll cycle | Reacts on change events |
Latency: up to pollInterval | Latency: typically within the debounce window + processing time |
| Poll queries run while idle | Polling still runs, but change events reduce time-to-start |
| More idle work at scale | Fewer “wait for poll” delays at scale |
Requirements
Section titled “Requirements”Change Streams require:
- MongoDB 4.0+
- Replica Set or Sharded Cluster (not standalone)
Event Lifecycle
Section titled “Event Lifecycle”1. Change Stream Connected
Section titled “1. Change Stream Connected”When Monque successfully opens a Change Stream:
2. Processing Events
Section titled “2. Processing Events”When a job is inserted or becomes ready:
3. Error Handling
Section titled “3. Error Handling”If the Change Stream encounters an error:
4. Fallback to Polling
Section titled “4. Fallback to Polling”After 3 failed reconnection attempts:
5. Change Stream Closed
Section titled “5. Change Stream Closed”On graceful shutdown:
Configuration
Section titled “Configuration”Polling and Change Streams
Section titled “Polling and Change Streams”pollInterval always controls how often the scheduler polls the database:
Reconnection Strategy
Section titled “Reconnection Strategy”Monque implements exponential backoff for reconnection:
Monitoring Change Streams
Section titled “Monitoring Change Streams”Health Check Integration
Section titled “Health Check Integration”Detailed Monitoring
Section titled “Detailed Monitoring”Debouncing
Section titled “Debouncing”To prevent “claim storms” when many jobs arrive simultaneously:
Troubleshooting
Section titled “Troubleshooting”Change Stream Not Working
Section titled “Change Stream Not Working”-
Check MongoDB version: Must be 4.0+
-
Verify replica set: Change Streams require replica set
-
Check events: Listen for error events
High Latency Despite Change Streams
Section titled “High Latency Despite Change Streams”- Check if fallback occurred: Listen for
changestream:fallback - Verify network latency: High latency to MongoDB affects notifications
- Monitor debounce timing: Very high job volume may show 100ms debounce delay
Reconnection Loops
Section titled “Reconnection Loops”If you see repeated connection/disconnection:
Performance Comparison
Section titled “Performance Comparison”Monque does not publish benchmark numbers for Change Streams.
In general:
- Polling-only pickup latency is bounded by
pollInterval. - With Change Streams enabled, pickup latency is typically bounded by the Change Stream notification delay plus Monque’s 100ms debounce window and processing time.
Measure In Your Environment
Section titled “Measure In Your Environment”The exact numbers depend on MongoDB deployment (local vs Atlas), network latency, job volume, and worker load. If you want hard data, measure pickup latency in your application:
To compare polling-only vs Change Streams:
- Run against a standalone MongoDB (no replica set) to force polling-only mode.
- Run against a replica set/sharded cluster (e.g., Atlas) to enable Change Streams.
Resource Impact
Section titled “Resource Impact”Enabling Change Streams maintains an open cursor to the MongoDB server. While typically lightweight, the actual memory footprint depends on your specific workload:
- Driver Buffering: The MongoDB Node.js driver buffers events internally.
- Event Volume: High update rates will increase memory pressure if consumers lag.
- Document Size: Large
updateDescriptionfields in change events increase payload size.
If memory usage is a critical concern, we recommend profiling your application under load, as the overhead is generally negligible for most use cases but can vary.
Best Practices
Section titled “Best Practices”1. Always Listen for Events
Section titled “1. Always Listen for Events”2. Handle Graceful Degradation
Section titled “2. Handle Graceful Degradation”3. Monitor Latency
Section titled “3. Monitor Latency”The snippet above shows one simple way to capture pickup latency.
Next Steps
Section titled “Next Steps”- Atomic Claim Pattern - How job claiming works
- Heartbeat Mechanism - Detect stale jobs