August 27th, 2024

Reliable Web App – Reliability Patterns

As applications migrate to the cloud, they must be designed to handle inevitable failures gracefully. Network issues, service outages, and other transient faults are common in distributed systems. Without proper handling mechanisms, these failures can lead to system instability and poor user experiences. This is why Retry and Circuit Breaker patterns are essential. The Retry pattern allows applications to recover from temporary failures by reattempting failed operations, while the Circuit Breaker pattern helps prevent cascading failures by stopping attempts to perform an action that is likely to fail, thereby maintaining the overall health of the system.

You can think of the Reliable Web Application (RWA) pattern as a pattern of patterns. Each pattern provides prescriptive guidance on how to build a specific aspect of a reliable web application. The patterns are derived from both the Azure Well-Architected Framework and the 12-factor app methodology.

In this blog, we’ll demo both the Retry and the Circuit Breaker patterns. The retry pattern involves making repeated attempts to execute a task until successful, while the circuit-breaker pattern prevents a system from executing a task that’s likely to fail, to avoid further system degradation.

Installation

To run the simple demo, you’ll need:

  • Java Development Kit (JDK) 17: Essential for developing Java applications. Make sure to install JDK 17 as specified in your pom.xml file. Download JDK 17
  • Apache Maven: A build automation tool used primarily for Java projects. It helps manage project dependencies and streamline the build process. Download Maven
  • Visual Studio Code: A lightweight but powerful source code editor that runs on your desktop. It’s available for Windows, macOS, and Linux. Download Visual Studio Code
  • Java Extension Pack for Visual Studio Code: This extension pack includes essential Java tools such as Maven support and Java 17 compatibility. Download Java Extension Pack

Clone the sample repo

git clone https://github.com/Azure/reliable-web-app-pattern-java.git

Navigate to the project directory:

cd reliable-web-app-pattern-java/workshop/Part0-Basic-App/src

Install dependencies:

mvn clean install

Start the application using Maven:

mvn spring-boot:run

You can then access the API at http://localhost:8080/product/1.

Example API Call

To retrieve a product by its ID, you can use the following curl command:

curl http://localhost:8080/product/1

Running the Tests

To run the automated tests for this system, use the following command:

mvn test

These tests verify the functionality of all components, ensuring that the application behaves as expected.

Failure Mode

One of the most essential principles of the Reliable Web App pattern is the Retry Pattern. It helps your application deal with situations where a service might be temporarily down, a ‘transient fault’. The Retry pattern resends failed requests to the service until it’s working again.

But the Retry Pattern alone is not enough. Sometimes, a service might be unavailable for a long time, or it might even be gone forever. It would be useless to keep trying to call such a service. That’s why we need the Circuit Breaker Pattern.

The application includes a feature to simulate failures, useful for testing its resilience capabilities such as the circuit breaker and retry mechanisms. This simulation can be controlled directly from a web browser, making it easy to demonstrate or test the effects of these patterns.

Enabling Failures

To simulate failures in the system, which will trigger the circuit breaker or retry logic, you can activate failure mode using the following URL:

http://localhost:8080/configure/failure?fail=true

You can then trigger the failure at http://localhost:8080/product/1.

Image failure web

View the logs in your browser and terminal:

Image failure

Disabling Failures

To return the system to normal operation, disable the failure mode using this URL:

http://localhost:8080/configure/failure?fail=false

Test the application again by accessinghttp://localhost:8080/product/1

Remember to close your terminal by pressing Ctrl+C to stop the application.

Circuit Breaker in Testing

The circuit breaker pattern is crucial for managing failures in distributed systems. It prevents a network or service failure from causing your application to become unstable. This pattern temporarily halts operations when a particular service or operation fails repeatedly, which is vital for maintaining overall system health.

The ProductServiceTest class in thesrc/test/java/com/example/demo/service/ProductServiceTest.java tests this behavior by simulating failures to trigger the circuit breaker. Here’s an explanation of the test and the method it tests:

Code Snippets and Explanation

getProductById Method in ProductService

This method includes a circuit breaker and retry mechanism, which are critical for handling failures in production environments. Here’s how it works:

    @CircuitBreaker(name = "default", fallbackMethod = "fallback")
    @Retry(name = "default")
    public Product getProductById(Long id) {
        if (failForCircuitBreakerTest || Math.random() > 0.7) {
            throw new RuntimeException("Service failure - Circuit breaker activated");
        }
        return new Product(id, "Product Name", "Product Description");
    }

The line if (failForCircuitBreakerTest || Math.random() > 0.7) in the getProductById method is crucial for testing and demonstrating the resilience of the application. It simulates failures by combining a manual flag (failForCircuitBreakerTest) and a random factor (Math.random() > 0.7). This setup allows developers to trigger failure scenarios and introduces unpredictability, mimicking real-world conditions. By validating the system’s fault tolerance mechanisms, it ensures reliable service even under adverse conditions.

ProductServiceTest Class

The test method simulates a scenario where the getProductById method is called and a failure is expected to trigger the circuit breaker:

    @Test
    public void whenGetProductByIdCalledMultipleTimes_thenCircuitBreakerShouldBeTriggered() {
        boolean exceptionThrown = false;
        for (int i = 0; i < 20; i++) {
            try {
                productService.getProductById((long) i);
            } catch (RuntimeException e) {
                exceptionThrown = true;
            }
        }
        assertTrue(exceptionThrown);
    }

This test ensures that the circuit breaker is functioning as expected by verifying that an exception is thrown when the method is repeatedly called under failure conditions.

Conclusion

This project provides a simple demonstration of two Reliable Web App patterns in action. By exploring the code and running the application, you can gain a better understanding of how these patterns can enhance the reliability and performance of web applications.

What’s Next?

If you found this exercise valuable, the RWA workshop offers a wide range of other features and best practices that you can explore. For example, you can delve into security best practices, learn how to deploy applications across multiple regions for greater resilience, or optimize your application’s performance and operational efficiency. Each of these topics is crucial for building a robust, cloud-native application that can scale and perform reliably in production environments.

Links

Get started with the remainder of the RWA workshop –> Part 1 – Tooling

Learn more about the Retry pattern

Learn more about the Circuit Breaker pattern

Category
Java

Author

0 comments

Discussion are closed.