Circuit breaking is a traffic management strategy that protects a system from further damage during a failure or overload. In traditional Java services, you can use frameworks like Resilience4j to implement circuit breaking. However, Istio provides circuit breaking at the network level, which eliminates the need to integrate it into the application code of each service. You can configure a connection pool to implement circuit breaking, which improves system stability and reliability, and protects destination services from abnormal requests.
Prerequisites
Connection pool parameters
Before you enable circuit breaking, you must create a destination rule to configure circuit breaking for the destination service. For a description of the fields in a destination rule, see Destination Rule.
The connectionPool field defines parameters related to circuit breaking. The following table describes the common connection pool parameters.
|
Parameter |
Type |
Required |
Description |
Default |
|
|
int32 |
No |
The maximum number of HTTP/1.1 or TCP connections to a destination host. This limit applies independently to each sidecar proxy on the client and server. A single client pod cannot create more connections than this limit, and a single server pod cannot accept more. The total connections the destination service can accept is calculated as: min(number of client pods, number of server pods) * maxConnections. |
2³²-1 |
|
|
int32 |
No |
The maximum number of requests that can be queued while waiting for a ready connection from the connection pool. |
1024 |
|
|
int32 |
No |
The maximum number of active requests to a backend service. |
1024 |
In a simple scenario with one client and one destination service instance (in a Kubernetes environment, an instance is equivalent to a pod), the parameter configuration is straightforward. However, production environments often involve more complex scenarios:
-
One client instance and multiple destination service instances.
-
Multiple client instances and a single destination service instance.
-
Multiple client instances and multiple destination service instances.
You must adjust these parameters for different scenarios to ensure the connection pool performs reliably under high load. The following sections show how to configure the connection pool for these scenarios, helping you understand the client and server constraints and choose a suitable circuit breaking policy for your production environment.
Example
This topic uses two Python scripts: one represents the destination service (server) and the other represents the client that calls the service.
-
The server script uses the Flask framework to create an application and defines an API endpoint at the /hello route. When the endpoint is accessed, the server sleeps for 5 seconds and then returns a response containing the string
"Hello World!". -
The client script runs in an infinite loop, sending batches of 10 parallel requests to the server endpoint. To synchronize request batches across multiple client pods, the script uses the system clock to trigger sends at 0, 20, and 40 seconds past the minute.
Deploy the sample application
-
Create a YAML file with the following content and run the
kubectl apply -f ${YAML_FILE_NAME}.yamlcommand to deploy the sample application. -
Run the following command to view the client and server pods.
kubectl get po |grep circuitExpected output:
circuit-breaker-sample-client-d4f64d66d-fwrh4 2/2 Running 0 1m22s circuit-breaker-sample-server-6d6ddb4b-gcthv 2/2 Running 0 1m22s
Without an explicit destination rule to set limits, the server can handle all 10 concurrent client requests, and all requests receive a 200 status code. The following is an example of the client logs:
----------Info----------
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.016539812088013
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.012614488601685
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.015984535217285
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.015599012374878
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.012874364852905
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.018714904785156
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.010422468185425
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.012431621551514
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.011001348495483
Status: 200, Start: 02:39:20, End: 02:39:25, Elapsed Time: 5.01432466506958
Configure the connection pool
To enable a circuit breaking rule in the service mesh, define a destination rule for the destination service.
Create a destination rule with the following content. For more information, see Manage destination rules. This destination rule sets the maximum number of TCP connections to the destination service to 5.
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: circuit-breaker-sample-server
spec:
host: circuit-breaker-sample-server
trafficPolicy:
connectionPool:
tcp:
maxConnections: 5
Scenario 1: 1 client and 1 destination pod
-
Start the client pod and monitor its logs.
For a clearer result, restart the client. You will see logs similar to the following:
----------Info---------- Status: 200, Start: 02:49:40, End: 02:49:45, Elapsed Time: 5.0167787075042725 Status: 200, Start: 02:49:40, End: 02:49:45, Elapsed Time: 5.011920690536499 Status: 200, Start: 02:49:40, End: 02:49:45, Elapsed Time: 5.017078161239624 Status: 200, Start: 02:49:40, End: 02:49:45, Elapsed Time: 5.018405437469482 Status: 200, Start: 02:49:40, End: 02:49:45, Elapsed Time: 5.018689393997192 Status: 200, Start: 02:49:40, End: 02:49:50, Elapsed Time: 10.018936395645142 Status: 200, Start: 02:49:40, End: 02:49:50, Elapsed Time: 10.016417503356934 Status: 200, Start: 02:49:40, End: 02:49:50, Elapsed Time: 10.019930601119995 Status: 200, Start: 02:49:40, End: 02:49:50, Elapsed Time: 10.022735834121704 Status: 200, Start: 02:49:40, End: 02:49:55, Elapsed Time: 15.02303147315979All requests succeed. However, in each batch, only 5 requests have a response time of about 5 seconds. The other requests are slower, taking 10 seconds or more. This result shows that using only
tcp.maxConnectionsqueues excess requests. By default, the limit for this pending request queue is 1024. -
Update the destination rule to allow only one pending request. For more information, see Manage destination rules.
To achieve true circuit breaking (fail-fast behavior), you also need to set
http.http1MaxPendingRequeststo limit the number of queued requests. The default value for this parameter is 1024. If you set it to 0, it reverts to the default value. Therefore, you must set it to at least 1.apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: circuit-breaker-sample-server spec: host: circuit-breaker-sample-server trafficPolicy: connectionPool: tcp: maxConnections: 5 http: http1MaxPendingRequests: 1 -
Restart the client pod to avoid skewed results and continue to observe the logs.
The following is an example of the logs:
----------Info---------- Status: 503, Start: 02:56:40, End: 02:56:40, Elapsed Time: 0.005339622497558594 Status: 503, Start: 02:56:40, End: 02:56:40, Elapsed Time: 0.007254838943481445 Status: 503, Start: 02:56:40, End: 02:56:40, Elapsed Time: 0.0044133663177490234 Status: 503, Start: 02:56:40, End: 02:56:40, Elapsed Time: 0.008964776992797852 Status: 200, Start: 02:56:40, End: 02:56:45, Elapsed Time: 5.018309116363525 Status: 200, Start: 02:56:40, End: 02:56:45, Elapsed Time: 5.017424821853638 Status: 200, Start: 02:56:40, End: 02:56:45, Elapsed Time: 5.019804954528809 Status: 200, Start: 02:56:40, End: 02:56:45, Elapsed Time: 5.01643180847168 Status: 200, Start: 02:56:40, End: 02:56:45, Elapsed Time: 5.025975227355957 Status: 200, Start: 02:56:40, End: 02:56:50, Elapsed Time: 10.01716136932373As expected, 4 requests are immediately throttled, 5 requests are sent to the destination service, and 1 request is queued.
-
Run the following command to check the number of active connections established between the client's Istio proxy and the destination service pod.
kubectl exec $(kubectl get pod --selector app=circuit-breaker-sample-client --output jsonpath='{.items[0].metadata.name}') -c istio-proxy -- curl -X POST http://localhost:15000/clusters | grep circuit-breaker-sample-server | grep cx_activeExpected output:
outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local::172.20.192.124:9080::cx_active::5The output shows that 5 active connections are established between the client's Istio proxy and the destination service pod.
Scenario 2: 1 client and multiple destination pods
This section verifies whether the connection limit applies per-pod or per-service, using one client pod and three destination service pods.
-
If the limit applies per-pod, the total capacity would be 15 connections (3 pods × 5 connections/pod).
Because only 10 requests are sent, no throttling or queuing would occur, and all requests would succeed in about 5 seconds.
-
If the connection limit is applied at the service level, the total number of connections is limited to 5, regardless of the number of pods for the destination service.
In this case, we would expect to see 4 requests immediately throttled, 5 requests sent to the destination service, and 1 request queued.
-
Run the following command to scale the destination service Deployment to multiple replicas (for example, 3).
kubectl scale deployment/circuit-breaker-sample-server --replicas=3 -
Restart the client pod and monitor its logs.
The following is an example of the logs:
----------Info---------- Status: 503, Start: 03:06:20, End: 03:06:20, Elapsed Time: 0.011791706085205078 Status: 503, Start: 03:06:20, End: 03:06:20, Elapsed Time: 0.0032286643981933594 Status: 503, Start: 03:06:20, End: 03:06:20, Elapsed Time: 0.012153387069702148 Status: 503, Start: 03:06:20, End: 03:06:20, Elapsed Time: 0.011871814727783203 Status: 200, Start: 03:06:20, End: 03:06:25, Elapsed Time: 5.012892484664917 Status: 200, Start: 03:06:20, End: 03:06:25, Elapsed Time: 5.013102769851685 Status: 200, Start: 03:06:20, End: 03:06:25, Elapsed Time: 5.016939163208008 Status: 200, Start: 03:06:20, End: 03:06:25, Elapsed Time: 5.014261484146118 Status: 200, Start: 03:06:20, End: 03:06:25, Elapsed Time: 5.01246190071106 Status: 200, Start: 03:06:20, End: 03:06:30, Elapsed Time: 10.021712064743042The similar throttling and queuing indicate that the client's connection pool limit is applied to the service as a whole and is not increased by adding more destination service instances.
-
After running for a while, run the following command to check the number of active connections between the client's Istio proxy and the destination service pods.
kubectl exec $(kubectl get pod --selector app=circuit-breaker-sample-client --output jsonpath='{.items[0].metadata.name}') -c istio-proxy -- curl -X POST http://localhost:15000/clusters | grep circuit-breaker-sample-server | grep cx_activeExpected output:
outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local::172.20.192.124:9080::cx_active::2 outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local::172.20.192.158:9080::cx_active::2 outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local::172.20.192.26:9080::cx_active::2The output shows a total of 6 active connections, not 5, distributed across the destination service pods. This demonstrates that connection limits are not always strictly enforced, a known behavior noted in the Envoy and Istio documentation.
Scenario 3: Multiple clients and 1 destination pod
-
Adjust the replica counts for the client and destination service:
kubectl scale deployment/circuit-breaker-sample-server --replicas=1 kubectl scale deployment/circuit-breaker-sample-client --replicas=3 -
Restart the client pods and monitor their logs.
The number of 503 errors on each client increases. The destination service as a whole limits the total concurrent requests from all clients to 5.
-
View the client proxy logs.
You can see two different types of log entries for the throttled requests (503 errors). The
RESPONSE_FLAGSfield includes the valuesUOandURX.-
UO: Upstream overflow (circuit breaking). -
URX: The request was rejected because the upstream retry limit (HTTP) or maximum connection attempts (TCP) was reached.
Based on the values of other fields in the logs, such as
DURATION,UPSTREAM_HOST, andUPSTREAM_CLUSTER, we can draw a further conclusion:Requests with the
UOflag are throttled locally by the client proxy, while requests with theURXflag are rejected by the destination service proxy. -
-
Verify the conclusion from the previous step by checking the logs of the destination service proxy.
As expected, the logs of the destination service proxy show a 503 response code. This is why the client proxy logs contain
"response_code":"503"and"response_flags":"URX".
Each client proxy enforces its 5-connection limit, queuing or locally throttling excess requests (flagged UO). At the start of a batch, the three client proxies can attempt up to 15 concurrent requests. However, the single destination service proxy also enforces a 5-connection limit, accepting only 5 total requests and rejecting the rest. These rejections from the destination proxy appear in client proxy logs with a URX response flag.
The following diagram illustrates this scenario:
Scenario 4: Multiple clients and multiple destination pods
Increasing the number of destination service replicas increases the total request capacity, as each new replica's proxy can handle 5 concurrent requests. This allows you to observe the interplay between client-side and server-side limits.
-
Run the following commands to increase the number of destination service replicas to 2 and the number of client replicas to 3.
kubectl scale deployment/circuit-breaker-sample-server --replicas=2 kubectl scale deployment/circuit-breaker-sample-client --replicas=3You can see that 10 of the 30 requests generated by all three client proxies in one batch are successful.
-
Run the following command to increase the number of destination service replicas to 3.
kubectl scale deployment/circuit-breaker-sample-server --replicas=3You can see 15 successful requests.
-
Run the following command to increase the number of destination service replicas to 4.
kubectl scale deployment/circuit-breaker-sample-server --replicas=4Even when the destination service is scaled from 3 to 4 replicas, the number of successful requests remains 15. The client side has become the bottleneck: the three client proxies can only send a combined total of 15 concurrent requests (5 each).
Related operations
Metrics for connection pool circuit breaking
Connection pool circuit breaking is achieved by limiting the maximum number of TCP connections to a destination host. It produces a series of related metrics to help determine if a circuit breaker has been tripped. The following table describes some of these metrics:
|
Metric |
Type |
Description |
|
envoy_cluster_circuit_breakers_default_cx_open |
Gauge |
1 if the connection pool circuit breaker is tripped; 0 otherwise. |
|
envoy_cluster_circuit_breakers_default_rq_pending_open |
Gauge |
1 if the pending request queue is full; 0 otherwise. |
Configure the sidecar proxy's proxyStatsMatcher field to report these metrics, then use Prometheus to collect and view them.
-
Configure the sidecar proxy to report circuit breaking metrics by using
proxyStatsMatcher. When configuringproxyStatsMatcher, select Regular Expression Match and set the value to.*circuit_breaker.*. For more information, see Custom Envoy metrics (proxyStatsMatcher). -
Redeploy the circuit-breaker-sample-server and circuit-breaker-sample-client Deployments. For more information, see Redeploy workloads.
-
Follow the steps above to configure connection pool circuit breaking and send requests.
-
Run the following command to view the connection pool circuit breaking metrics for the circuit-breaker-sample-client service.
kubectl exec -it deploy/circuit-breaker-sample-client -c istio-proxy -- curl localhost:15090/stats/prometheus | grep circuit_breaker | grep circuit-breaker-sample-serverExpected output:
kubectl exec -it deploy/circuit-breaker-sample-client -c istio-proxy -- curl localhost:15090/stats/prometheus | grep circuit_breaker | grep circuit-breaker-sample-server envoy_cluster_circuit_breakers_default_cx_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 1 envoy_cluster_circuit_breakers_default_cx_pool_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_default_remaining_cx{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_default_remaining_cx_pools{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 18446744073709551613 envoy_cluster_circuit_breakers_default_remaining_pending{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 1 envoy_cluster_circuit_breakers_default_remaining_retries{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 4294967295 envoy_cluster_circuit_breakers_default_remaining_rq{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 4294967295 envoy_cluster_circuit_breakers_default_rq_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_default_rq_pending_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_default_rq_retry_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_high_cx_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_high_cx_pool_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_high_rq_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_high_rq_pending_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0 envoy_cluster_circuit_breakers_high_rq_retry_open{cluster_name="outbound|9080||circuit-breaker-sample-server.default.svc.cluster.local"} 0
Metric collection and alerting
After configuring the relevant metrics, you can configure Prometheus to collect them and set up alert rules based on key metrics to ensure timely notification when circuit breaking occurs. The following example shows how to configure metric collection and alerting for connection pool circuit breaking by using Managed Service for Prometheus.
-
In Managed Service for Prometheus, integrate or upgrade the Alibaba Cloud ASM component in your data plane cluster. This allows it to collect the exposed circuit breaking metrics. For more information about how to update integrated components, see Manage integrations. (Skip this step if you already use a self-managed Prometheus as described in Monitor ASM instances with a self-managed Prometheus instance.)
-
Create an alert rule for connection pool circuit breaking. For more information, see Create a rule with a custom PromQL statement. The following table provides an example of how to fill in the key parameters for the alert rule. You can configure the other parameters based on your needs.
|
Parameter |
Example |
Description |
|
Custom PromQL Statement |
(sum by(cluster_name, pod_name,namespace) (envoy_cluster_circuit_breakers_default_cx_open)) != 0 |
This query uses the |
|
Alert Message |
Connection pool circuit breaking has occurred. The number of TCP connections established by the proxy has reached the maximum limit! Namespace: {{$labels.namespace}}, Pod where circuit breaking was triggered: {{$labels.pod_name}}, Upstream service info: {{ $labels.cluster_name }} |
This example alert message shows the pod that triggered the circuit breaking, the upstream service it connects to, and the pod's namespace. |
Connection pool constraints
The following table describes the constraints that connection pool configurations place on clients and destination services.
|
Role |
Description |
|
Client |
Each client proxy enforces the limit independently. A limit of 100 means each proxy allows 100 concurrent requests before it begins local throttling. With N clients, the total concurrent requests can reach 100 * N. The client proxy limit applies to the entire destination service, not to individual replicas of the service. Even if the destination service has 200 active pods, the throttling limit per client proxy remains 100. |
|
Destination service |
Each destination service proxy also enforces the limit. If a service has 50 pods and a limit of 100, each pod can accept up to 100 concurrent requests before it throttles them and returns 503 errors. |