A flexible Cloudflare Worker for dynamically calculating system backpressure based on configurable queries.
💡 Backpressure is a flow control mechanism that prevents overload by regulating data production based on the system’s capacity to process it.
In distributed systems, excessive load can degrade performance or cause failures. To mitigate this, the Backpressure Worker implements:
- Rate limiting: Restricts incoming requests to prevent overload.
- Queueing & buffering: Temporarily stores excess data to smooth out spikes.
- Adaptive load shedding: Drops lower-priority requests when under stress.
Proper backpressure handling ensures system reliability, prevents resource exhaustion, and improves overall stability.
The Backpressure Worker enables you to:
- Define multiple query types for monitoring system load
- Calculate backpressure based on configurable thresholds
- Gracefully handle partial query failures
- Cache and track query results using Workers KV
- Visualize metrics via an interactive dashboard
npx wrangler kv:namespace create BACKPRESSURE_KV
{
"kv_namespaces": [
{
"binding": "BACKPRESSURE_KV",
"id": "your-production-namespace-id",
"preview_id": "your-preview-namespace-id"
}
]
}
{
"vars": {
"Queries": [
{
"name": "example-kv",
"type": "workers-kv",
"warn": 500,
"emergency": 900
}
]
}
}
npx wrangler kv:key put --binding=BACKPRESSURE_KV "backpressure_kv_queries/example-kv" "600"
npx wrangler deploy
The worker exposes the following endpoints:
GET /
- Returns the current backpressure valueGET /ui
- Real-time visualization dashboardGET /cache
- Retrieves the complete cache historyGET /queries
- Returns the configured queries
The built-in monitoring dashboard provides real-time visualization of system metrics, including:
- Line charts showing historical trends per metric
- Warning and emergency threshold indicators
- Auto-refresh every 60 seconds
- Time-based filtering via URL parameters (
start
andend
) - Responsive design with mobile support
The dashboard supports time-based filtering with the following parameters:
start
: Unix timestamp for the start of the time rangeend
: Unix timestamp for the end of the time range
Example: /ui?start=1706313600000&end=1706400000000
The worker caches query results using Workers KV:
- Updates cache every 60 seconds
- Maintains historical time-series data
- Falls back to cached values on query failures
name
: Unique identifier for the querytype
: Query type handler identifierwarn
: Warning threshold valueemergency
: Emergency threshold valuecurve
: Throttling curve exponential factor (default: 4)
- Normal Operation: Value ≤
warn
(0% throttling) - Degraded Performance:
warn
< value <emergency
(partial throttling) - Emergency State: Value ≥
emergency
(100% throttling)
throttlePercent = 1 - Math.exp(-curve * loadFactor);
loadFactor =
(currentValue - warningThreshold) / (emergencyThreshold - warningThreshold);
The final backpressure percentage is calculated as:
1 - Math.max(...throttlePercents);
- Graceful fallback to cached values on query failures
- JSON error responses with descriptive messages
- HTTP 500 status code for internal errors
- Console error logging for debugging
interface QueryHandler {
refresh(query: BackpressureQuery): Promise<number>;
}
class MyCustomQueryHandler implements QueryHandler {
async refresh(query: BackpressureQuery): Promise<number> {
// Implement query logic
return value;
}
}
export function getQueryHandler(type: string): QueryHandler {
// Add your handler here
}