Bird
Raised Fist0
Microservicessystem_design~7 mins

Fallback pattern in Microservices - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of the fallback pattern in microservices?
easy
A. To provide a backup response when a service call fails
B. To increase the number of service calls
C. To replace the main service permanently
D. To log all service requests for auditing

Solution

  1. Step 1: Understand the fallback pattern role

    The fallback pattern is designed to handle failures gracefully by providing an alternative response.
  2. Step 2: Identify the main goal

    Its main goal is to keep the system responsive and avoid cascading failures by returning backup data or default messages.
  3. Final Answer:

    To provide a backup response when a service call fails -> Option A
  4. Quick Check:

    Fallback pattern = backup response [OK]
Hint: Fallback means backup response on failure [OK]
Common Mistakes:
  • Thinking fallback increases service calls
  • Confusing fallback with permanent service replacement
  • Assuming fallback is for logging only
2. Which of the following is a correct way to implement a fallback method in a microservice?
easy
A. Ignore the failure and return an error to the user
B. Call the main service repeatedly until it succeeds
C. Return cached data or a default message when the main service fails
D. Restart the entire microservice on failure

Solution

  1. Step 1: Review fallback implementation options

    Fallback should provide a quick alternative response like cached data or default messages.
  2. Step 2: Eliminate incorrect options

    Repeated calls can cause delays, ignoring failure hurts user experience, and restarting service is costly and slow.
  3. Final Answer:

    Return cached data or a default message when the main service fails -> Option C
  4. Quick Check:

    Fallback = cached or default response [OK]
Hint: Fallback returns cached or default data on failure [OK]
Common Mistakes:
  • Retrying endlessly instead of fallback
  • Returning errors instead of fallback data
  • Restarting services unnecessarily
3. Consider this pseudocode for a microservice call with fallback:
response = callMainService()
if response.failed:
    response = fallbackResponse()
print(response)
What will be printed if callMainService() fails?
medium
A. The fallback response
B. The original failed response
C. An error message and no response
D. Nothing, the program crashes

Solution

  1. Step 1: Analyze the failure condition

    If callMainService() fails, the code assigns fallbackResponse() to response.
  2. Step 2: Determine printed output

    The printed output will be the fallback response, not the failed original response or an error.
  3. Final Answer:

    The fallback response -> Option A
  4. Quick Check:

    Failed main call triggers fallback output [OK]
Hint: Failed call triggers fallback print [OK]
Common Mistakes:
  • Assuming failed response is printed
  • Expecting program crash on failure
  • Confusing fallback with error message
4. This code snippet tries to implement a fallback but has a bug:
def get_data():
    try:
        return call_service()
    except:
        call_fallback()
What is the bug here?
medium
A. The code does not catch exceptions
B. The try block does not call the service
C. The except block should raise an error
D. The fallback function is not returned

Solution

  1. Step 1: Check try-except behavior

    The try block returns the service call result, but except calls fallback without returning it.
  2. Step 2: Identify missing return

    Without returning fallback's result, the function returns None on failure instead of fallback data.
  3. Final Answer:

    The fallback function is not returned -> Option D
  4. Quick Check:

    Missing return in except causes None [OK]
Hint: Always return fallback result in except block [OK]
Common Mistakes:
  • Forgetting to return fallback data
  • Misunderstanding try-except flow
  • Assuming fallback raises error
5. You design a microservice that calls a payment gateway. To avoid delays, you want to use the fallback pattern. Which fallback strategy is best to keep the system responsive and safe?
hard
A. Return a generic error message without fallback
B. Return a cached success response immediately and update later asynchronously
C. Retry the payment gateway call 10 times before fallback
D. Restart the payment microservice on failure

Solution

  1. Step 1: Understand fallback goals for payment service

    Fallback should keep system responsive and avoid blocking user with delays.
  2. Step 2: Evaluate options for responsiveness and safety

    Returning cached success immediately and updating asynchronously balances responsiveness and eventual consistency.
  3. Step 3: Eliminate risky or slow options

    Retries cause delays, generic errors hurt UX, restarting service is costly and slow.
  4. Final Answer:

    Return a cached success response immediately and update later asynchronously -> Option B
  5. Quick Check:

    Cached immediate fallback with async update = best practice [OK]
Hint: Use cached immediate fallback plus async update [OK]
Common Mistakes:
  • Excessive retries causing delays
  • No fallback causing poor user experience
  • Restarting services on failure