Skip to main content
reactive microservices architecture transactional outbox and event sourcing with java 21

Verification and Reliability Engineering

3 min read Chapter 9 of 10
Summary

ArchUnit, Testcontainers, and Toxiproxy are used to verify...

ArchUnit, Testcontainers, and Toxiproxy are used to verify a system's reliability against network partitions and race conditions.

Verification and Reliability Engineering

Introduction to Verification and Reliability Engineering

Verification and reliability engineering are crucial aspects of ensuring that a system functions as expected, even in the presence of failures or unexpected events. In the context of distributed systems, such as those utilizing Kafka and PostgreSQL, it is essential to verify that the system can withstand network partitions and race conditions. This section will delve into the tools and techniques used to achieve this goal, including ArchUnit, Testcontainers, and Toxiproxy.

Architectural Constraints and Verification

ArchUnit is a free, simple Java library for checking the architecture of Java code using any plain Java unit test framework. It can be used to enforce layered architectures by defining ‘onion’ or ‘hexagonal’ rules. For instance, the following ArchUnit rule enforces domain isolation:

ArchRule domainIsolation = classes().that().resideInAPackage("..domain..").should().onlyBeAccessedByClassesThat().resideInAPackage("..application..", "..domain..");

This rule ensures that logic within a specific domain or aggregate does not leak into or depend on foreign domain layers, thus maintaining domain isolation.

Infrastructure Verification with Testcontainers

Testcontainers is an open-source framework for providing throwaway, lightweight instances of databases, message brokers, or anything that can run in a Docker container. It can be used to spin up Kafka and Postgres instances on a per-test-suite basis, allowing for more comprehensive testing of the system. The following example demonstrates how to declare Testcontainers for Kafka and Postgres integration:

@Container
static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0"));
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine");

This approach enables the verification of the system’s behavior under various infrastructure conditions, such as database or broker availability.

Simulating Network Conditions with Toxiproxy

Toxiproxy is a TCP proxy to simulate network and system conditions for chaotic and resiliency testing. It exposes an API to manipulate latency, jitter, bandwidth, and connection termination in real-time, allowing developers to prove that an application has no single point of failure. By simulating network latency and partitions, Toxiproxy helps identify timeouts and retry-storm vulnerabilities in the system.

Verification Tooling Matrix

The following table summarizes the verification tooling matrix:

ToolTargetSimulation Metric
ToxiproxyNetworkLatency (ms), Jitter, Partition
ArchUnitCodeLayer violations, cyclic deps
TestcontainersInfrastructureDatabase/Broker availability
This matrix highlights the different tools used to verify various aspects of the system, from code architecture to infrastructure and network conditions.

Performance Impact and Reliability Engineering

Reliability engineering is a sub-discipline of systems engineering that emphasizes the ability of equipment and systems to function without failure for a specified duration. By using tools like ArchUnit, Testcontainers, and Toxiproxy, developers can ensure that their system is reliable and can withstand various types of failures. However, it is essential to consider the performance impact of these tools and techniques, as they may introduce additional latency or overhead. Therefore, it is crucial to carefully evaluate the trade-offs between reliability and performance when designing and implementing a system.

Sources

[1] ArchUnit User Guide. https://www.archunit.org/userguide/html/000_Index.html [2] Testcontainers Kafka Module. https://www.testcontainers.org/modules/kafka/ [3] Toxiproxy GitHub Documentation. https://github.com/Shopify/toxiproxy