Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Hystrix series semaphores and thread pools
Hystrix series semaphores and thread pools
Hystrix internally provides two execution logic modes: semaphore and thread pool.

By default, Hystrix uses thread pool mode.

But what is the difference between the two, and how to choose in the actual scene?

If you want to use semaphore mode, you need to configure the parameter execution. Isolated. Strategy = executionisolationstrategy. Flag language

In this mode, receiving requests and executing downstream dependencies are completed in the same thread, and there is no performance overhead caused by thread context switching, so semaphore mode should be selected in most scenarios, but it is not a good choice in the following situations.

For example, an interface depends on three downstream services: serviceA, serviceB and serviceC, and the data returned by these three services are independent of each other. In this case, if the fuse degradation of A, B and C uses semaphore mode, then the time consumption of the interface is equal to the sum of the time consumption of requesting services of A, B and C, which is undoubtedly not a good solution.

In addition, in order to limit the number of concurrent calls that depend on the downstream, Hystrix is executed. Isolated. Semantics You can configure the maximum number of concurrent requests. When the number of concurrent requests reaches the threshold, the requesting thread may fail quickly and perform demotion.

The implementation is also very simple. A simple counter, when the request enters fuse, tryAcquire () is executed, and 1 is added to the counter. If the result is greater than the threshold, it will return false, a semaphore rejection event will occur, and the degradation logic will be executed. When leaving the fuse is requested, release () is executed and the counter is decremented by 1.

In this mode, users' requests will be submitted to their respective thread pools for execution, and the threads that execute the downstream services will be separated, thus achieving the effect of resource isolation. When the thread pool is too late to process and the request queue is full, new incoming requests will fail quickly, which can avoid the spread of dependency problems.

The problems mentioned in semaphore mode can effectively improve the interface performance through asynchronous execution of multiple downstream services by thread pool.

superiority

disadvantaged

Because Hystrix uses thread pool mode by default, a corresponding thread pool is created at initialization for each command. If there are many interfaces to be downgraded in the project, such as hundreds, students who don't know much about Hystrix's internal mechanism may use them directly according to the default configuration, which may cause a lot of thread resources waste.