Chaos Engineering in a Spring Boot Microservice
Is your microservice ready for production ? Is your microservice fault-tolerant ? To answer those questions, all you have to do is break things on purpose and see how your microservice will react to it. Those experiments are what chaos engineering really is.
As defined by wikipedia :
Chaos Engineering is the discipline of experimenting on a system in order to build confidence in the system’s capability to withstand turbulent conditions in production.
Those planned experiments are designed to help us figure out the weakness of the system we are building/already built.
Benefits of chaos engineering
- Controlled and planned chaos experiments
- Preparing for unpredictable failure
- Preparing engineers for failure
- Improving SLA
- Fortifying resilience of microservice architecture
Integrating chaos engineering into the DevOps toolchain contributes to the goal of continuous testing. Many companies such as Netflix, Amazon, Dropbox, Uber, Slack, … follow that discipline.
One of the tools that can help you to implement chaos engineering in a spring boot application is ChaosMonkey.
Chaos Monkey
Chaos monkey, a tool created by Netflix internally in 2010 and published in 2012, aims to help applications tolerate random instance failures. This tool has introduced some principles of chaos engineering into spring boot application.
Functionalities
- Latency assault : adding random latency to REST endpoints
- Exception assault : throwing random runtime exceptions
- AppKiller assault : killing the app
How to configure Chaos Monkey in a spring boot application ?
Add the following dependency in your sprint boot application
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>chaos-monkey-spring-boot</artifactId>
<version>2.1.1</version>
</dependency>
Customise the chaos-monkey properties inside your application.properties
file, inside your application.yml
file. The following configuration will enable chaos monkey and will generate one of the assault with a frequency of 5 (1 out 5 calls) on each rest call.
chaos:
monkey:
enabled: true
assaults:
level: 5
watcher:
rest-controller: true
Start your application by specifying the spring profile as chaos-monkey
--spring.profiles.active=chaos-monkey
Once you run the microservice, you can check in the console to make sure that chaos monkey is ready to create chaos.
Adding random latency to the REST endpoints
With the following configuration, you enable latency to REST endpoint. In this case, each latency will vary from 1 to 5 seconds.
chaos:
monkey:
assaults:
latency-active: true
latency-range-end: 1000
latency-range-start: 5000
Throwing random exceptions
With the following configurations, random runtime exceptions will be thrown.
chaos:
monkey:
assaults:
exceptions-active: true
Killing the application
With the following configurations, the microservice will be destroyed.
chaos:
monkey:
assaults:
kill-application-active: true
You can check the full documentation of the properties on the following link.
Thank you for reading my first post, hopefully it was helpful and gave you an idea on what chaos engineering is and how to implement it using chaos monkey in a spring boot microservice.
Stay tuned for my next posts. Feel free to comment down, if you have any question or If I missed something 😬.