DEV Community

Cover image for Introduce Chaos Middleware for Python Flask: Simulate disruptions to improve resiliency
Bugra Derre for Proofdock

Posted on

Introduce 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.

That is where the Chaos Middleware package goes into action. The Cloud Bug Study states, that neglected error handling - buggy code, timeouts, and others - leads to a great portion of a system's unavailability.

The Chaos Middleware allows you to throw 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 enabled by HTTP header requests or by the Chaos Platform control panel.

Installation & configuration

  1. 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 FlaskMiddleware
    
     app = Flask(__name__)
     app.config['CHAOS_MIDDLEWARE_APPLICATION_NAME'] = 'example-application-name'
     app.config['CHAOS_MIDDLEWARE_APPLICATION_ENV'] = 'example-environment'
     app.config['CHAOS_MIDDLEWARE_PROOFDOCK_API_TOKEN'] = 'eyJ0eXAi...05'
    
     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. The Chaos Middleware detects any value inside this header.

Set the header's value to {"actions":[{"name":"delay","value":"15"}]}. The value describes the simulation name and its value in seconds. The described simulation delays the response by 15 seconds.

For the simulation, we use the Postman client to visualize the HTTP requests.

Alt Text
A delay simulation

Simulate faults

Raise exceptions by setting the x-proofdock-attack request header.

Set the header's value to {"actions":[{"name":"fault","value":"Exception"}]}. The value describes the simulation name and its value as a fully qualified name of the exception to be raised. The simulation will raise the Exception.


Alt Text
A fault simulation

A control 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 the Azure DevOps with chaos engineering capabilities. The control panel allows you to control application attacks via a graphical user interface and lets your application cache the configured attack. Check out the docs when you are interested in the Chaos Platform.

Alt Text
The control panel in Azure DevOps

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

Top comments (0)