Skip to main content
The latency proactive chaos strategy is designed to introduce controlled delays into system operations, simulating network latency or slow processing times. This strategy helps in assessing and improving the resilience of applications against increased response times.

Configuration

  • Options: ChaosLatencyStrategyOptions
  • Extensions: AddChaosLatency

Basic usage

Here are several ways to configure the latency chaos strategy:

Complete example

Here’s a complete example showing latency injection with timeout and retry:
Chaos strategies should be placed last in the resilience pipeline. This ensures that the latency is injected at the last minute, allowing your other resilience strategies (timeout, retry, etc.) to handle the delayed operations.

Strategy options

If both Latency and LatencyGenerator are specified then Latency will be ignored.
If the calculated latency is negative (regardless if it’s fixed or dynamic) then it will not be injected.

Use cases

Latency injection is useful for:
  • Testing timeout handling: Verify that your timeout and retry strategies work correctly when operations are slow
  • Simulating network delays: Test how your application behaves with high network latency
  • Validating user experience: Ensure your UI remains responsive and provides appropriate feedback during slow operations
  • Performance testing: Identify bottlenecks and areas where caching or optimization would help
  • Testing distributed systems: Simulate slow microservices or database queries

Dynamic latency injection

You can use LatencyGenerator to inject different latencies based on context:
Returning TimeSpan.Zero from the LatencyGenerator delegate means no latency will be injected for that particular execution.

Telemetry

The latency chaos strategy reports the following telemetry events: Here are some sample events:
The Chaos.OnLatency telemetry event will be reported only if the latency chaos strategy injects a positive delay. If the latency is not injected, or if injected but the latency is negative, or if the LatencyGenerator throws an exception, then there will be no telemetry emitted. Also, the Result will be always empty for the Chaos.OnLatency telemetry event.

How it works

Normal execution (no chaos)

Chaos execution (latency injected)

Unlike fault injection, latency injection does not prevent the user’s callback from being invoked. The delay is injected before the callback is executed.

Best practices

When using latency injection in production environments, always:
  • Start with a very low injection rate (e.g., 0.01 or 1%)
  • Use realistic latency values (e.g., 100-500ms for network delays, not minutes)
  • Target only specific test users or tenants
  • Monitor the impact on overall system performance
  • Ensure you have proper timeout strategies in place
  • Have a quick way to disable chaos injection if needed
Consider combining latency injection with timeout strategies to test how your application handles slow operations. This helps ensure your timeouts are configured appropriately and that your retry logic handles timeout exceptions correctly.