0
0
Microservicessystem_design~7 mins

Fallback pattern in Microservices - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a microservice call fails due to network issues, timeouts, or downstream service errors, the entire user request can fail, causing poor user experience and cascading failures across the system.
Solution
The fallback pattern provides an alternative response or behavior when a service call fails. Instead of propagating the failure, the system returns a default value, cached data, or a simplified response to keep the system responsive and stable.
Architecture
Client
Service A

This diagram shows a client calling Service A, which calls Service B. If Service B fails, the fallback handler provides an alternative response to Service A, preventing failure propagation.

Trade-offs
✓ Pros
Improves system resilience by preventing failures from cascading.
Enhances user experience by providing graceful degradation.
Reduces load on failing services by avoiding repeated retries.
✗ Cons
Fallback responses may be stale or less accurate than live data.
Adds complexity to service logic and testing.
Improper fallback can mask real issues, delaying fixes.
Use when downstream services are unreliable or have intermittent failures, especially in systems with high availability requirements and user-facing APIs.
Avoid when data consistency is critical and stale or default responses can cause harm, such as financial transactions or critical control systems.
Real World Examples
Netflix
Netflix uses fallback patterns in its microservices to serve cached or default content when personalized recommendation services are down, ensuring uninterrupted streaming experience.
Amazon
Amazon applies fallback logic in its checkout process to show cached product availability when inventory services are temporarily unreachable, preventing cart failures.
Uber
Uber uses fallback mechanisms to provide estimated fares from cached data when real-time pricing services fail, maintaining user trust during outages.
Code Example
The before code calls the profile service directly and fails if the service is down. The after code wraps the call in a try-except block and returns cached data if the call fails or times out, implementing the fallback pattern.
Microservices
### Before applying fallback pattern

def get_user_profile(user_id):
    # Direct call without fallback
    response = call_profile_service(user_id)
    return response


### After applying fallback pattern

class FallbackException(Exception):
    pass

def get_user_profile(user_id):
    try:
        response = call_profile_service(user_id)
        if response.status_code != 200:
            raise FallbackException()
        return response
    except (TimeoutError, FallbackException):
        # Return cached or default profile as fallback
        return get_cached_profile(user_id)
OutputSuccess
Alternatives
Retry pattern
Retries the failed request multiple times before failing, instead of immediately providing an alternative response.
Use when: Use when failures are transient and likely to succeed on retry without impacting user experience.
Circuit breaker pattern
Stops calls to a failing service after a threshold, then optionally uses fallback; focuses on preventing overload rather than providing alternative data.
Use when: Use when you want to prevent system overload and quickly detect failing services.
Summary
Fallback pattern prevents system failures from cascading by providing alternative responses when service calls fail.
It improves user experience by enabling graceful degradation during outages or errors.
Fallback should be used carefully when stale or default data is acceptable and avoided when data accuracy is critical.