DEV Community

Cover image for Chaos Middleware: Simulate disruptions in Django applications to improve resiliency
Bugra Derre for Proofdock

Posted on

Chaos Middleware: Simulate disruptions in Django applications 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 Django application ii) and the simulation of delays and application faults. Simulations are enabled by HTTP header requests or by the Chaos Platform control center.

Installation & configuration

  1. Install the Chaos Middleware for the Django application:

     cd /path/to/your/django-application  # Enter your Django application
     venv/bin/activate  # Active virtual environment
     pip install -U proofdock-chaos-middleware-django
    
  2. Configure the Django application:

     # Activate the ``Chaos Middleware`` component by adding 
     # it to the MIDDLEWARE list in your Django settings
     MIDDLEWARE = [
       '..'
       'pdchaos.middleware.contrib.django.django_middleware.DjangoMiddleware',
       '..'
     ]
    
     # Set the Chaos Middleware settings
     CHAOS_MIDDLEWARE = {
       'CHAOS_MIDDLEWARE_APPLICATION_NAME': 'example-application-name',
       'CHAOS_MIDDLEWARE_APPLICATION_ENV': 'example-environment'
     }
    
  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 center for Azure DevOps users

A more appealing way to control such attacks is the usage of the control center provided by Proofdock's Chaos Platform. The Chaos Platform extends the Azure DevOps with chaos engineering capabilities. The control center 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 center 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)