Chaos Middleware: where Spring Boot meets Chaos Engineering
Tags: chaosengineering, sre, springboot, java
Published: 2020-10-12 05:55:28

Chaos engineering tools activate features like stopping virtual machines or terminating pods in your Kubernetes cluster. The Cloud Bug Study states that neglected error handling - buggy code, timeouts, etc. - accounts for the vast majority of system malfunction.

**Chaos Middleware** allows you to inject turbulent conditions into your application. It lets you delay or raise errors when handling requests, and by applying additional conditions, you can  decrease the blast radius of your attack.

The following sections will guide you through: i) the installation and configuration process of **Chaos Middleware** in your Spring Boot application and ii) simulation of delays and application faults.

## Installation & configuration

1\. Install the Chaos Middleware by including following dependency in your spring boot application `pom.xml`.
```xml
<dependency>
  <groupId>io.proofdock</groupId>
  <artifactId>spring-boot-starter-chaos-middleware</artifactId>
  <version>0.1.4</version>
</dependency>
```
2\. Add an **application name** to the `application.properties`.
    
```ini
chaos.middleware.application-name=webshop-order
```
3\. Start your application.


## Simulate delays
The **delay** attack introduces delays to the request/response cycle. This type of the attack enables you to verify the behavior of your system in case of timeouts or slow responses.

Trigger delays by setting the **x-proofdock-attack** request header. The Chaos Middleware detects any value inside this header and injects appropriate turbulent conditions.

To simulate delay, set the x-proofdock-attack header's value to ```json
{"actions":[{"name":"delay", "value":"5"}], "target":{  "application":"webshop-order"}}
```
- **name** - set delay attack
- **value** - delay response by 5 seconds
- **target** - target application name


Let's attack an application using Postman.
<br/>
![chaos_panel_delay](https://dev-to-uploads.s3.amazonaws.com/i/mdcimff0ug86xgz94yqy.gif)
 
 

## Simulate faults
The **fault** attack injects an **exception** into your code.  This type of the attack enables you to verify the behavior of your application in case of an unexpected event.

Trigger faults by setting the **x-proofdock-attack** request header. 

```json
{"actions":[{"name":"fault", "value":"java.lang.Exception"}], "target":{  "application":"webshop-order"}}
```
- **name** - set fault attack
- **value** - thow exception of the type ``java.lang.Excpetion``
- **target** - the targeted application name


Let's attack an application using Postman.
<br/>
![chaos_panel_fault](https://dev-to-uploads.s3.amazonaws.com/i/pmzcxcxbwspt10w3y93z.gif)
 

## Chaos Panel for Azure DevOps users
A more convenient way to control such attacks is the usage of the **Chaos Panel** provided by Proofdock's Chaos Platform. The Chaos Platform extends the Azure DevOps with chaos engineering capabilities. The Chaos Panel allows you to control application attacks via a graphical user interface.

![4xs9vhcsdtmqp83tffan](https://dev-to-uploads.s3.amazonaws.com/i/3q8afxx8d2tydmmwqj0s.png)
_The Chaos Panel in Azure DevOps_

Check out the [docs](https://docs.proofdock.io/chaos/guide/), if you are interested in the Chaos Platform.


## Thank you
We hope you enjoyed reading this article and got a new perspective on resilience.

We are [Proofdock](https://proofdock.io), a software tech company located in Germany, created to help engineers build more resilient and reliable software products. Check out the [Chaos Platform for Microsoft Azure](https://marketplace.visualstudio.com/items?itemName=proofdock.chaos-platform) and explore your system.

<span>Article photo by <a href="https://unsplash.com/@raychelsnr?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Raychel Sanner</a> on <a href="https://unsplash.com/s/photos/spring-weather?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>