|
| 1 | +# Container Policies |
| 2 | + |
| 3 | +This guide explains how to configure container policies for your services and understand the default failure handling behavior. |
| 4 | + |
| 5 | +## Default Failure Handling |
| 6 | + |
| 7 | +All services use {ruby Async::Service::Policy::DEFAULT} which monitors failure rates and stops the container when failures exceed a threshold. |
| 8 | + |
| 9 | +**Default threshold:** 6 failures in 60 seconds (0.1 failures per second). |
| 10 | + |
| 11 | +This means: |
| 12 | +- Services can tolerate occasional failures and transient issues. |
| 13 | +- More than 6 failures in any 60-second window stops the container. |
| 14 | +- Prevents services from restart-looping indefinitely when fundamentally broken. |
| 15 | + |
| 16 | +This fail-fast behavior is appropriate for orchestrated environments (Kubernetes, systemd) where the orchestrator will restart the entire service. |
| 17 | + |
| 18 | +### Why This Default? |
| 19 | + |
| 20 | +Without failure monitoring, a broken service with `restart: true` would restart indefinitely, wasting resources. The default policy: |
| 21 | + |
| 22 | +- **Catches problems quickly**: Broken services stop within 10-20 seconds. |
| 23 | +- **Prevents resource waste**: Doesn't keep trying to start services that will never succeed. |
| 24 | +- **Enables orchestrator recovery**: Systemd/Kubernetes can restart the whole process with a clean state. |
| 25 | +- **Detects environmental issues**: Bad hardware, corrupted pre-fork state, or system-level problems can't be fixed by restarting children - the entire service needs to be restarted (potentially on different hardware). |
| 26 | +- **Signals clear failure**: Exit code indicates the service couldn't maintain healthy operation. |
| 27 | + |
| 28 | +## Configuring Policies |
| 29 | + |
| 30 | +Use `container_policy` in your service configuration to customize failure handling: |
| 31 | + |
| 32 | +``` ruby |
| 33 | +# config/service.rb |
| 34 | + |
| 35 | +# More lenient: allow 5 failures per minute: |
| 36 | +container_policy Async::Service::Policy.new(maximum_failures: 5, window: 60) |
| 37 | + |
| 38 | +service "web" do |
| 39 | + # Your service configuration. |
| 40 | +end |
| 41 | + |
| 42 | +service "worker" do |
| 43 | + # Also uses the same policy. |
| 44 | +end |
| 45 | +``` |
| 46 | + |
| 47 | +The policy applies to **all services** in the configuration file. |
| 48 | + |
| 49 | +### Choosing a Threshold |
| 50 | + |
| 51 | +Consider your service characteristics: |
| 52 | + |
| 53 | +**Strict (catch problems immediately):** |
| 54 | +``` ruby |
| 55 | +container_policy Async::Service::Policy.new(maximum_failures: 1, window: 5) |
| 56 | +``` |
| 57 | + |
| 58 | +**Balanced (tolerate transient issues):** |
| 59 | +``` ruby |
| 60 | +container_policy Async::Service::Policy.new(maximum_failures: 5, window: 60) |
| 61 | +``` |
| 62 | + |
| 63 | +**Lenient (allow many retries):** |
| 64 | +``` ruby |
| 65 | +container_policy Async::Service::Policy.new(maximum_failures: 20, window: 60) |
| 66 | +``` |
| 67 | + |
| 68 | +Factors to consider: |
| 69 | +- **Traffic volume**: High-traffic services may have more absolute failures. |
| 70 | +- **Error types**: Some errors are transient (network timeouts, rate limits). |
| 71 | +- **Dependencies**: Upstream services may need time to recover. |
| 72 | +- **Deployment environment**: Kubernetes/systemd handle restarts, local dev doesn't. |
| 73 | + |
| 74 | +## Per-Container Policy Instances |
| 75 | + |
| 76 | +The `container_policy` method accepts a block that's evaluated **each time a container is created**: |
| 77 | + |
| 78 | +``` ruby |
| 79 | +# config/service.rb |
| 80 | +container_policy do |
| 81 | + # This block is called for EACH container created |
| 82 | + # Each container gets its own policy instance with fresh state |
| 83 | + Async::Service::Policy.new(maximum_failures: 5, window: 60) |
| 84 | +end |
| 85 | +``` |
| 86 | + |
| 87 | +If your policy is tracking per-container state, this will ensure each container has new policy with clean state. |
0 commit comments