-
Notifications
You must be signed in to change notification settings - Fork 1
01 Introduction
Murphy for Delphi is a comprehensive library that implements Chaos Engineering patterns for building resilient and fault-tolerant applications. Named after Murphy's Law ("Anything that can go wrong will go wrong"), this library helps you prepare for and handle failures gracefully in your Delphi applications.
In distributed systems and modern applications, failures are inevitable:
- Network requests can timeout
- External services can become unavailable
- Database connections can fail
- APIs can be throttled or return errors
- Resources can be exhausted
Murphy provides battle-tested patterns to handle these scenarios without writing complex error-handling code from scratch.
Murphy implements four essential resilience patterns:
- Retry - Automatically retry failed operations with configurable delays
- Circuit Breaker - Prevent cascading failures by temporarily blocking calls to failing services
- Fallback - Provide alternative results when operations fail
- Rate Limit - Control the rate of operations to prevent system overload
All patterns use a fluent, builder-based API that makes configuration intuitive and readable:
var
Policy: IRetryPolicy;
begin
Policy := TRetryBuilder.Create
.Handle(EIdHTTPProtocolException)
.Retry(3)
.Wait(TTimeSpan.FromSeconds(2))
.Build;
end;Murphy leverages Delphi's strong type system and generics for type-safe operations:
var
FallbackPolicy: IFallbackPolicy<string>;
Result: string;
begin
FallbackPolicy := TFallbackBuilder<string>.Create
.Handle(Exception)
.Fallback(function: string
begin
Result := 'Default Value';
end)
.Build;
Result := FallbackPolicy.Execute(function: string
begin
Result := GetDataFromService;
end);
end;Each policy can be configured to handle specific exception types:
RetryPolicy := TRetryBuilder.Create
.Handle(EIdHTTPProtocolException) // Only retry HTTP exceptions
.Handle(EIdSocketError) // Also retry socket errors
.Retry(3)
.Build;Murphy includes a test mode that allows you to unit test your code without actual delays:
uses Murphy.Globals;
procedure TMyTests.TestRetryPolicy;
begin
MurphyTestModeEnabled := True; // Disable actual waiting
// Your test code here - retries happen instantly
MurphyTestModeEnabled := False;
end;Patterns can be combined to create sophisticated resilience strategies (see Combining Patterns).
Murphy is ideal for:
- Microservices - Handle inter-service communication failures
- REST API Clients - Retry transient HTTP errors
- Database Operations - Handle connection timeouts and transient failures
- External Service Integration - Protect against third-party service outages
- Resource-Intensive Operations - Rate limit to prevent resource exhaustion
- Critical Systems - Improve overall system reliability
Murphy follows these principles:
- Simple by default, flexible when needed - Common scenarios require minimal configuration
- Explicit over implicit - Behavior should be clear and predictable
- Composable - Patterns work independently or together
- Testable - Test mode enables fast, deterministic testing
- Performant - Minimal overhead when patterns aren't triggered
Murphy is not:
- A complete application framework
- A replacement for proper error handling
- A solution for all reliability problems
- A logging or monitoring framework (though it works well with them)
Murphy complements your existing error handling and doesn't replace good design practices.
Murphy for Delphi is inspired by:
- Polly (.NET resilience library)
- Netflix's Hystrix (Circuit Breaker implementation)
- Chaos Engineering principles pioneered by Netflix
| Scenario | Without Murphy | With Murphy |
|---|---|---|
| HTTP request fails | Immediate error | Automatic retry with delay |
| Service constantly failing | Keep trying forever, wasting resources | Circuit breaker opens, fail fast |
| Database temporarily unavailable | Application crashes | Fallback to cached data |
| API rate limit exceeded | Errors for users | Rate limiter prevents over-calling |
- New to Chaos Engineering? Read Chaos Engineering Basics
- Ready to start? Check out Getting Started
- Want to dive deep? Explore the Architecture
- Need specific pattern info? See the Pattern Guides