Detailed explanation of the multi-connection pool health check mechanism
The manager owns health-check scheduling. A timed-out ping remains in flight until the underlying operation settles, preventing overlapping pings to the same client. Configured failure thresholds determine down status; public status reports degraded before the threshold and recovers on a completed successful check. Stop, removal and replacement fence late results. A custom ping that never settles prevents further checks for that client; shutdown revokes its update authority without waiting indefinitely.
Overview of health check mechanism
Working principle
The health checker (HealthChecker) regularly performs health checks on all connection pools. When problems are found, it will automatically mark the status of the connection pool and trigger failover.
Health status
Problem discovery mechanism
1. Health check automatic discovery
Checking method: Use MongoDB’s db.admin().ping() command
Trigger conditions:
- Periodic check (default every 5 seconds)
- Timeout (default 3 seconds)
- Continuous failure threshold (default 3 times)
Public API example:
2. Found when selectingPool
Trigger time: When selectPool tries to use a connection pool
Discovery:
3. Found during actual use
Trigger timing: Connection failed when executing query
Processing method:
Problem handling process
Automatic processing process
Downgrade strategy example
Automatic recovery mechanism
Operation and maintenance notification method
Method 1: Logging (Basic)
Automatic recording: monSQLize automatically records all health status changes
Operation and maintenance view log:
Method 2: Regular monitoring (recommended)
Check your health regularly:
Run monitoring script:
Method 3: Application-level status transitions
ConnectionPoolManager exposes health snapshots, not public health events. If your application needs notifications, compare consecutive snapshots:
Monitoring integration solution
Option 1: Integrate Prometheus + Grafana
Exposing Prometheus Metrics:
Grafana Alert Rules:
Solution 2: Integrate Enterprise WeChat/DingTalk
Send alert to corporate WeChat:
Option 3: Integrate email alerts
Alarm configuration example
Complete production environment alarm system
FAQ
Q1: Will health checks affect performance?
A: The impact is minimal.
- Only one lightweight
pingcommand is executed for each check - The check interval defaults to 5 seconds and can be adjusted to 10-30 seconds
- Asynchronous execution, does not block business requests
Performance overhead:
- CPU: < 0.1%
- Network: ~100 bytes per check
- Response time: 1-3ms
Q2: How to customize health check logic?
A: The public health-check contract exposes configuration for enabling checks, interval, timeout, and retry threshold. It does not expose a custom health-check callback.
Q3: How to ensure that no data is lost after the main database fails?
A: Use the correct failover strategy:
Q4: How to test the alarm system?
A: Manual trigger fault:
Summary:
- ✅ Automatic Discovery: Health check automatically detects issues every 5 seconds
- ✅ Automatic processing: selectPool automatically skips the fault pool
- ✅ Multiple notifications: logs, monitoring scripts, events, Prometheus, Enterprise WeChat/DingTalk, emails
- ✅ Automatic recovery: The connection pool is automatically marked as up after recovery.
Recommended plan:
- Basics: Regular monitoring script + Enterprise WeChat/DingTalk
- Advanced: Prometheus + Grafana + Alert Rules