Reliability Features
mik includes production-ready reliability features out of the box.
Circuit Breaker
Section titled “Circuit Breaker”Prevents cascading failures by stopping requests to failing handlers.
How It Works
Section titled “How It Works”- Closed - Normal operation, requests pass through
- Open - Handler is failing, requests rejected immediately
- Half-Open - Testing if handler recovered
Configuration
Section titled “Configuration”Circuit breaker is enabled by default with sensible defaults:
| Setting | Default | Description |
|---|---|---|
| Failure threshold | 5 | Failures before opening |
| Recovery timeout | 30s | Time before testing recovery |
| Half-open max requests | 3 | Test requests in half-open |
Response When Open
Section titled “Response When Open”HTTP/1.1 503 Service UnavailableRetry-After: 30Content-Type: application/json
{ "type": "urn:mik:error:circuit-open", "title": "Service Unavailable", "status": 503, "detail": "Circuit breaker is open for module: auth"}Per-Module Isolation
Section titled “Per-Module Isolation”Each WASM module has its own circuit breaker:
A failure in auth.wasm doesn’t affect orders.wasm.
Rate Limiting
Section titled “Rate Limiting”Prevents resource exhaustion from too many requests.
Two Levels
Section titled “Two Levels”- Global limit - Max concurrent requests across all handlers
- Per-module limit - Max concurrent requests per handler
Configuration
Section titled “Configuration”[server]max_concurrent_requests = 1000 # Global limitmax_per_module_requests = 10 # Per-handler limitResponse When Limited
Section titled “Response When Limited”HTTP/1.1 503 Service UnavailableRetry-After: 1Content-Type: application/json
{ "type": "urn:mik:error:rate-limited", "title": "Service Unavailable", "status": 503, "detail": "Module auth is overloaded"}Why Per-Module Limits?
Section titled “Why Per-Module Limits?”Prevents a slow handler from consuming all resources:
LRU Cache
Section titled “LRU Cache”Caches compiled WASM modules for faster cold starts.
Configuration
Section titled “Configuration”[server]cache_size = 100 # Max cached modulesmax_cache_mb = 256 # Max cache size in MBHow It Works
Section titled “How It Works”- First request compiles WASM → caches result
- Subsequent requests use cached module
- LRU eviction when cache is full
Cache Stats
Section titled “Cache Stats”Available via health endpoint:
curl http://localhost:3000/health{ "status": "ready", "cache_size": 5, "cache_capacity": 100, "cache_bytes": 1048576, "total_requests": 1000}Execution Timeout
Section titled “Execution Timeout”Prevents runaway handlers from blocking resources.
Configuration
Section titled “Configuration”[server]execution_timeout_secs = 30 # Max execution time per requestResponse When Timeout
Section titled “Response When Timeout”HTTP/1.1 504 Gateway TimeoutContent-Type: application/json
{ "type": "urn:mik:error:timeout", "title": "Gateway Timeout", "status": 504, "detail": "Handler execution timed out"}Body Size Limit
Section titled “Body Size Limit”Prevents memory exhaustion from large request bodies.
Configuration
Section titled “Configuration”[server]max_body_size_mb = 10 # Max body size in MBResponse When Exceeded
Section titled “Response When Exceeded”HTTP/1.1 413 Payload Too LargeContent-Type: application/json
{ "type": "urn:mik:error:payload-too-large", "title": "Payload Too Large", "status": 413, "detail": "Request body exceeds 10MB limit"}Graceful Shutdown
Section titled “Graceful Shutdown”Handles SIGTERM/SIGINT signals gracefully:
- Stops accepting new connections
- Waits for in-flight requests to complete
- Closes connections cleanly
- Exits with code 0
# Send shutdown signal (Ctrl+C or)kill -TERM $(pgrep mik)
# Logs:# Received shutdown signal, draining connections...# All connections drained, shutting downHealth Check
Section titled “Health Check”Built-in health endpoint for load balancers:
curl http://localhost:3000/health{ "status": "ready", "timestamp": "2025-01-01T00:00:00Z", "cache_size": 5, "cache_capacity": 100, "total_requests": 1000}Best Practices
Section titled “Best Practices”- Monitor circuit breaker state - Log when circuits open/close
- Tune per-module limits - Based on handler complexity
- Set appropriate timeouts - Balance user experience vs resource usage
- Use health checks - For load balancer integration
- Size cache appropriately - Based on module count and sizes