Kanngongkanngoen

Go Circuit Breakers with Hystrix: Protecting Services from Failure

💡 Key takeaways

A practical introduction to the Hystrix circuit-breaker pattern in Go, including its states, configuration options, and service-monitoring considerations.

Modern software systems often depend on third-party services. As those dependencies grow, handling service failures becomes essential. This guide introduces the Hystrix circuit-breaker pattern in Go, from its basic behavior to an example configuration.

What is a Hystrix circuit breaker?

A circuit breaker is a design pattern that controls calls to an external service. Like an electrical breaker, it can stop requests temporarily when failures or abnormal behavior cross a configured threshold. This helps prevent one failing dependency from disrupting the wider system.

Where Hystrix came from

Netflix developed Hystrix to manage failures in services on which its systems depended. It was designed for complex environments with many service calls and was intended to help maintain system stability when some services fail.

What Hystrix does

Hystrix is a library for handling service failures. Its settings can include retries, timeouts, and thresholds for deciding when a service is failing. It also provides monitoring features for observing circuit-breaker state and service performance.

The three circuit-breaker states

Why use Hystrix?

Hystrix is intended to help limit the effects of dependency failures, improve resilience, and make circuit-breaker state and service behavior easier to monitor. These benefits depend on choosing settings that fit the system.

Using a circuit breaker in Go

Using Hystrix in Go requires installing the library and configuring the command that represents the service call.

Install the Hystrix package

Install the package with this Go command:

go get github.com/afex/hystrix-go/hystrix

Separate the server and client

The example uses two parts: a server that provides the service to protect, and a client that calls that service through Hystrix.

Configure a Hystrix command

The configuration example sets a timeout, a concurrency limit, a minimum request volume, a sleep window, and an error-percentage threshold:

hystrix.ConfigureCommand("my_service", hystrix.CommandConfig{
    Timeout:                1000, // milliseconds
    MaxConcurrentRequests:  10,
    RequestVolumeThreshold: 10,
    SleepWindow:            5000, // milliseconds
    ErrorPercentThreshold:  50,
})

Run and test a request

After configuration, call the server from the client and inspect the result. If the server fails, Hystrix can open the circuit and pause calls temporarily.

err := hystrix.Do("my_service", func() error {
    _, err := http.Get("http://localhost:8080/api/data")
    if err != nil { return err }
    return nil
}, nil)
if err != nil { fmt.Println("Error:", err) }

The example endpoint is http://localhost:8080/api/data.

Monitor the circuit breaker

The Hystrix Dashboard can display circuit-breaker state, request volume, and error frequency. This can help teams investigate service behavior and find problems sooner.

Adjust settings for the system

Summary and practical considerations

Hystrix monitors service calls and can open a circuit when failures exceed a threshold, then use a half-open state to test whether the service has recovered. This can help a Go application contain dependency failures, but configuration and monitoring need to match the application’s needs.

For a free consultation, contact the team on LINE, or scan the QR code below.

QR code to contact the team on LINE

KGK

Kanngongkanngoen Editorial Team

We create clear educational content about personal finance, investing, and debt management, with an emphasis on accuracy and transparency.