You have applied the Microservice architecture. Services sometimes collaborate when handling requests. When one service synchronously invokes another there is always the possibility that the other service is unavailable or is exhibiting such high latency it is essentially unusable. Precious resources such as threads might be consumed in the caller while waiting for the other service to respond. This might lead to resource exhaustion, which would make the calling service unable to handle other requests. The failure of one service can potentially cascade to other services throughout the application.
Trích https://microservices.io/patterns/reliability/circuit-breaker.html
Let’s forget about the rest of the article, Scala is hard to understand anw, let’s see an implementation from an open-source package: https://github.com/sony/gobreaker
Don’t understand tho? me too, let’s see another library that wrap around this package: https://github.dev/typesense/typesense-go
They have made a doc.go
file to document the package. +1 👍 for this practice
// Package circuit implements the Circuit Breaker pattern for http client.
// It will wrap a http request and monitor for
// failures and/or time outs. When a threshold of failures or time outs has been
// reached, future requests will not run. During this state, the
// breaker will allow limited number of http requests to run and, if they are successful,
// will start performing all http requests again.
package circuit
Ok, Circuit breaker là 1 cái công tắc được implement ở phía caller (trong trường hợp này là http client, dùng để make request đến other services). Công tắc này được configured để nếu http client gặp quá nhiều lỗi (vượt quá threshold) hoặc tốn quá nhiều thời gian để đợi phản hồi khi gọi request, nó sẽ tự động ngắt (trip).
https://resilience4j.readme.io/docs/circuitbreaker
Trạng thái ban đầu của CB là CLOSED, các request có thể được thực thi, khi số lượng request fail vượt quá threshold cho phép, CB chuyển trạng thái OPEN, ở trạng thái này, các request sẽ không được gửi đi và lỗi sẽ được trả về ngay lập tức. Sau một khoảng thời gian chờ, CB sẽ chuyển về HALF_OPEN.