Blog

WireMock.Net : Chaos Engineering with Fault Injections

This blogpost is part of a series where advanced and new features from WireMock.Net are explained.

In this blogpost : Chaos Engineering with Fault Injections.




1️⃣ Intro WireMock.Net

As author from WireMock.Net, I started this blog series where I’ll explain new features or advanced functionality / usage scenarios for WireMock.Net.

My previous post is about “Generate C# Code from Mapping(s)” and can be found here.

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. It’s based on the functionality from WireMock.org but extended with more functionality.

2️⃣ Chaos Engineering and Fault Injections

Chaos engineering has become a popular practice among software development teams. It involves injecting various types of failures into systems to identify potential weaknesses and improve resiliency. In this post, I’ll explain how WireMock.Net can be used for chaos engineering and to introduce faults using some examples.

3️⃣ Chaos Engineering: Embracing Failure

Chaos engineering is built on the premise that failures are inevitable, and achieving a highly resilient system requires proactive testing for these failures. By injecting faults into your applications and observing their behavior, you can identify problems and make improvements before these issues become critical.

WireMock.Net, typically used for stubbing HTTP requests and responses, can be used as a powerful chaos engineering tool. With its ability to manipulate HTTP responses or return different HTTP responses, WireMock.Net can simulate various failure scenarios and help you assess the robustness of your application.

3.1 Injecting Latency (delays)

One common issue that chaos engineering aims to address is how an application behaves under high latency conditions. Using WireMock.Net, you can simulate slow server responses, allowing you to observe how your application handles such situations.

Here’s an example of how to use WireMock.Net to inject latency (delay) into your API responses:

 

This code snippet sets up a WireMock server that responds to /api/data with a 10-second delay, simulating a slow server response.

3.2 Simulating Unreliable Networks

Another important aspect of chaos engineering is testing how your application performs under unreliable network conditions, such as broken connections or packet loss. WireMock.Net allows you to simulate these scenarios by randomly returning error responses.

Here’s an example of how to use WireMock.Net to simulate a flaky API:

 

In this example, we set up two mappings with different priorities. Both mappings match on a GET request to the /api/data path. But the first mapping has a 30% chance of being matched due to the defined probability. If that request is not matched, the second mapping will be used instead, simulating an internal server error (status code 500).

3.3 Simulating Faulty Data

Another aspect of chaos engineering is testing how your application handles unexpected or faulty data. WireMock.Net can be used to generate different types of erroneous data, allowing you to test the resilience of your application against various input scenarios.

These faults are currently supported:

  • EMPTY_RESPONSE: Return a completely empty response.
  • MALFORMED_RESPONSE_CHUNK: Send an OK status header, then garbage, then close the connection.

Here’s an example of how to use WireMock.Net to return a MALFORMED_RESPONSE_CHUNK:

 

In this example, we set up a mapping which has a 50% chance to return a normal valid JSON response and 50% chance to return a MALFORMED_RESPONSE_CHUNK response. This enables you to observe how your application under test handles this faulty data.

4️⃣ Controversial Chaos Engineering Techniques

While chaos engineering can help improve the resilience of your applications, it’s essential to strike a balance between injecting failures and maintaining a productive development environment. Some controversial chaos engineering techniques may involve injecting faults in production systems or applying a large number of random faults simultaneously, which can lead to unexpected downtime or degraded user experiences. Therefore, it’s advised to run WireMock.Net as unit-tests during CI/CD build scenarios or just on the test system.

5️⃣ Conclusion

To be successful in chaos engineering, you must adopt a methodical and controlled approach. Focus on injecting faults that are representative of real-world scenarios and ensure that your team is equipped to monitor, analyze, and learn from the results. WireMock.Net, when used thoughtfully, can be a powerful tool in your chaos engineering toolkit, enabling you to create more reliable and robust applications.

Remember, chaos engineering is not about causing chaos for the sake of it but rather about embracing failure and learning from it to build better systems. WireMock.Net can be an invaluable help in this journey.


❔ Feedback or Questions

  • Questions and issues can also be added on my -project.
  • Do you have any feedback or questions regarding this or one of my other blogs? Feel free to contact me!

📚 Additional resources


Stef Heyenrath

Stef Heyenrath is naast een zeer ervaren ontwikkelaar ook de auteur van meerdere succesvolle NuGet packages, waaronder WireMock.Net.

Written by: Stef Heyenrath

Stef started writing software for the Microsoft .NET framework in 2007. Over the years, he has developed into a Microsoft specialist with experience in: backend technologies such as .NET, NETStandard, ASP.NET, Ethereum, Azure, and other cloud providers. In addition he worked with several frontend technologies such as Blazor, React, Angular, Vue.js.

He is the author from WireMock.Net.

Mission: Writing quality and structured software with passion in a scrum team for technically challenging projects.

Want to know more about our experts? Contact us!