Introduce a chaos middleware for Python Flask: Simulate disruptions to improve resiliency
Resiliency is key to software nowadays. It contributes to your application’s reliability and plays a major role in the competitive advantage of your business. Resilient applications smartly exploit the strengths of software patterns that handle timeouts, delays and faulty service responses.
Great authors and presenters like Uwe Friedrichsen and Donald Firesmith share their ideas and knowledge about resilient software design. Such articles describe software patterns that overcome disruptions and improve application resiliency in a handsome way.
However, deploying a resilience pattern requires you as a developer to verify your implementation. When touching the ground of verification, things appear to get difficult.
Chaos engineering tools advertise features like stopping a virtual machine, or to terminate pods in your Kubernetes cluster. The “blast radius” gets high here. Higher than a developer expects when exploring a resilience design pattern on Friday afternoon at 2PM.
The Cloud Bug Study states, that neglected error handling — buggy code, timeouts, and others — leads to a great portion of a system’s unavailability.
That is where the Chaos Middleware
package for Python Flask goes into action. It allows you to put your application at turbulent conditions. It lets you simulate delayed responses or raise errors. Activate disruptions when needed and narrow them down on specific URL paths. The Chaos Middleware
gives you the opportunity to minimize the “blast radius” and verify your application’s resilience, even on a Friday afternoon.
The next sections will guide you through i) the installation and configuration process in your Flask application ii) and the simulation of delays and application faults. Simulations are either enabled by HTTP header requests or with a graphical user interface provided by the Chaos Platform
.
Installation & configuration
- Install the
Chaos Middleware
for the Flask application:
cd /path/to/your/flask-application # Enter your Flask application
venv/bin/activate # Active virtual environment
pip install -U proofdock-chaos-middleware-flask
2. Configure the Flask application:
from flask import Flask, jsonify
from pdchaos.middleware.contrib.flask.flask_middleware import
FlaskMiddlewareapp = Flask(__name__)
app.config[‘CHAOS_MIDDLEWARE_APPLICATION_NAME’] = ‘webshop’
app.config[‘CHAOS_MIDDLEWARE_APPLICATION_ENV’] = ‘dev’
middleware = FlaskMiddleware(app)
# Your business logic here@app.route(“/products”)
def hello():
return “Hello, from the products”if __name__ == “__main__”:
app.run()
3. Start your application
Simulate delays
Trigger delays by setting the x-proofdock-attack
request header. Set the header’s value to {"actions:[{"name":"delay", "value":"4"}]"}
. The value describes the simulation name
and its value
in seconds. The described simulation delays the response by 4 seconds.
For the simulation, we use the Postman client to visualize the HTTP requests.
Simulate faults
Raise exceptions by setting the x-proofdock-header
request header. Set the header value to {"actions:[{"name":"fault", "value":"Exception"}]"}
.
The value describes the simulation name
and its value
as the fully qualified name of the exception that is going to be raised.
Chaos panel for Azure DevOps users
A more appealing way to control such attacks is the usage of the control panel provided by Proofdock’s Chaos Platform
. The Chaos Platform
extends your Azure DevOps space with chaos engineering capabilities. The chaos panel allows you to control application attacks via a graphical user interface. Check out the docs for more.
Thank you
We hope you had fun and learned some new aspects on resilience.
We are Proofdock, a software tech company located in Germany helping engineers build more resilient and reliable software products. Check out the Chaos Platform for Microsoft Azure and explore your system.
References
- Check out the source code at GitHub
- Read the docs
- Install the Chaos Platform