“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

There are three important elements in the routing policy of Nacos: weight, protection threshold and nearby access. Because these three elements are independent of each other, today we will discuss the “protection threshold” in detail.

Protect the threshold

ProtectThreshold: To prevent the failure of too many instances from causing all traffic to flow into the remaining healthy instances and thus causing the remaining healthy instances to be crushed by the traffic pressure, resulting in avalanche effect. The health protection threshold should be defined as a floating point number between 0 and 1. When the ratio of domain name healthy instances to total service instances is smaller than the value, the domain name is returned to the client regardless of whether the instance is healthy or not. Doing so loses some traffic, but keeps the remaining healthy instances in the cluster working.

That is, the protection threshold is the minimum allowed percentage of healthy instances in a cluster. It needs to set a floating point value of 0-1. The default value is 0. The protection threshold can be queried and set in service details, as shown in the following figure:

How to understand the protection threshold?

To understand the protection threshold, one prerequisite is that Nacos has a natural responsibility for the registry function of the service Consumer to forward requests to a healthy service Provider. However, in an extreme case where a service has 100 instances, 99 of which go down and only one healthy instance is left, forwarding all requests to that one healthy instance can cause an avalanche and ultimately cause the business system to crash. To prevent this extreme situation, a “protection threshold” is created. Once triggered, Nacos will forward requests to all service instances, namely healthy + unhealthy instances, which may lose some traffic but keep the remaining healthy instances in the cluster working.

Protection threshold trigger condition :(actual health instance/total service instance) ≤ protection threshold

Set a protection threshold

We can set the protection threshold by “Edit Service”, as shown below:

Trigger protection threshold

Next, we will create a service to test the protection threshold functionality by adding two instances to the created service, as shown in the figure below: By default, all service instances are healthy. Next, we set the protection threshold to 0.8, which means the minimum requirement for healthy instances is 80%. If the proportion of healthy instances is less than this value, the protection threshold will be triggered, as shown in the figure below:When all nodes are healthy, observe the service list page and you can see that the protection threshold function is not triggered, as shown in the following figure:At this point we manually stop a service instance as shown below:The percentage of healthy instances decreases from 100% to 50%, which is less than the set protection threshold 0.8 (80%). Then return to the service list page, you can see that the protection threshold function is triggered:If we access the service again, we will see that part of the request will be forwarded to the non-healthy instance, that is, the access will fail, as shown in the following figure:

The protection threshold is not triggered. Procedure

Next, we lower the protection threshold and set it to 0.3, that is, 30% is the minimum proportion of healthy instances, otherwise threshold protection will be triggered, as shown in the figure below:At this time, because the proportion of our health instances is 50%, which is greater than the set threshold protection 0.3, threshold protection will not be triggered, which can be observed in the service list:When the protection threshold is not triggered, Nacos will forward all requests to the healthy instance, so that the service can be accessed normally each time, as shown in the following figure:

conclusion

The protection threshold is to prevent the failure of too many instances from causing all the traffic to flow into the remaining healthy instances and then causing the flow pressure to crush the remaining healthy instances, resulting in an avalanche effect. Its default value is 0, and the value should range from 0 to 1 floating point. This value is to define the cluster instance allows health accounted for more than the minimum value, if the actual health services accounted for less than or equal to this value, can trigger protection threshold, Nacos will replace all instances: health instance + unhealthy instances all returned to the caller, and when not trigger protection threshold, Nacos only healthy instance is returned to the caller.

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public account: Java Chinese Community

Java Interview Collection: gitee.com/mydb/interv…