Bird
Raised Fist0
Microservicessystem_design~3 mins

Why Fallback pattern in Microservices? - Purpose & Use Cases

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
The Big Idea

What if your system never crashed, even when parts failed?

The Scenario

Imagine you run a busy online store with many small services talking to each other. When one service goes down, your whole site freezes or shows errors to customers.

The Problem

Manually checking each service and restarting them takes too long. Users get frustrated waiting or see broken pages. It's hard to keep the system running smoothly without automatic help.

The Solution

The fallback pattern lets your system quickly switch to a backup plan when a service fails. Instead of crashing, it shows cached data or a simple message, keeping users happy and your site stable.

Before vs After
Before
response = callService()
if response == null:
    showError('Service down')
After
response = callService() or fallbackResponse()
show(response)
What It Enables

This pattern makes your system resilient, so it keeps working smoothly even when parts fail unexpectedly.

Real Life Example

When a payment service is slow or down, the fallback pattern can show a "Try again later" message or let users save their cart instead of losing their order.

Key Takeaways

Manual handling of failures is slow and frustrating.

Fallback pattern provides quick backup responses automatically.

It improves user experience and system reliability.

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