Enabling Metrics
Enable metrics collection on your workers by specifying how many data points to keep:Number of data points (minutes) to store. Each data point represents one minute of metrics.
MetricsTime Constants
BullMQ provides convenient time constants:Common Configurations
Memory Usage
Metrics are stored efficiently in Redis:- Data points are 1-minute intervals
- 2 weeks of data ≈ 120 KB per queue
- Older data is automatically removed
- Memory usage remains constant after reaching max data points
Querying Metrics
Retrieve metrics using thegetMetrics method on the Queue class:
Method Signature
The type of metrics to retrieve:
completed for successful jobs or failed for failed jobs.Starting index for pagination. Use
0 to start from the beginning.Ending index for pagination. Use
-1 to retrieve all data.Metrics Response Format
ThegetMetrics method returns a Metrics object:
Example Response
Understanding the Data
data array:
- Each element represents one minute
- Value is the number of jobs completed (or failed) in that minute
- Ordered from oldest to newest
count field:
- Total number of jobs completed (or failed) since the queue started
- Not limited to the queried time range
- Useful for long-term tracking
meta fields:
- Used internally by the metrics system
prevTSandprevCountshould not be used in application code
Practical Examples
Example 1: Basic Metrics Tracking
Example 2: Performance Dashboard
Example 3: Alerting on Failures
Example 4: Paginated Metrics
Example 5: Calculating Throughput
Prometheus Integration
For Prometheus metrics export, see Prometheus documentation.Best Practices
1
Use consistent settings across workers
All workers for the same queue must have identical metrics configuration.
2
Start with 2 weeks of data
This provides good balance between memory usage (~120 KB) and historical data.
3
Monitor both completed and failed metrics
Track success rate by comparing completed vs failed job counts.
4
Set up alerts for anomalies
Monitor failure rates, throughput drops, or unusual patterns.
5
Use pagination for large datasets
When querying long time ranges, use pagination to avoid loading too much data.
6
Combine with external monitoring
Integrate with Prometheus, Grafana, or other monitoring tools for visualization.
Limitations
Metrics data is stored in Redis and will be lost if Redis is flushed or data is not persisted.
Related Topics
Telemetry
OpenTelemetry integration for tracing
Queue Events
Listen to job events in real-time
Workers Overview
Configure and manage workers
Going to Production
Production deployment best practices
