Design throttling mechanisms and backpressure handling for protecting APIs from overload.
You are a reliability engineer specializing in traffic management and system protection.
## CONTEXT
I need to implement throttling and backpressure for my API.
**Requirements:**
- Traffic Patterns: {{TRAFFIC}}
- Protection Needs: {{PROTECTION}}
- Downstream Dependencies: {{DEPENDENCIES}}
- User Experience: {{UX_REQUIREMENTS}}
## TASK
Design throttling and backpressure system:
### 1. THROTTLING STRATEGIES
**Concurrent Request Limiting:**
```typescript
const semaphore = new Semaphore(MAX_CONCURRENT);
async function handleRequest(req) {
await semaphore.acquire();
try {
return await processRequest(req);
} finally {
semaphore.release();
}
}
```
**Queue-Based Throttling:**
```typescript
const queue = new RequestQueue({
maxSize: 1000,
processRate: 100 // per second
});
```
### 2. BACKPRESSURE SIGNALS
```
Response: 503 Service Unavailable
Retry-After: 30
X-RateLimit-Remaining: 0
```
### 3. LOAD SHEDDING
```typescript
if (systemLoad > 80%) {
// Reject lowest priority requests
if (req.priority < PRIORITY_HIGH) {
return res.status(503).json({ error: 'Service busy' });
}
}
```
### 4. ADAPTIVE THROTTLING
Adjust limits based on system health.
### 5. CLIENT GUIDANCE
- Retry-After headers
- Backoff recommendations
- Priority hints
### 6. MONITORING
- Queue depth metrics
- Rejection rates
- Latency percentilesOr press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[{TRAFFIC][{PROTECTION][{DEPENDENCIES][{UX_REQUIREMENTS]{
await semaphore.acquire();
try {
return await processRequest(req);
}{
semaphore.release();
}{
maxSize: 1000,
processRate: 100 // per second
}{
// Reject lowest priority requests
if (req.priority < PRIORITY_HIGH) {
return res.status(503).json({ error: 'Service busy' }