What if your system never crashed, even when parts failed?
Why Fallback pattern in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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 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.
response = callService() if response == null: showError('Service down')
response = callService() or fallbackResponse()
show(response)This pattern makes your system resilient, so it keeps working smoothly even when parts fail unexpectedly.
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.
Manual handling of failures is slow and frustrating.
Fallback pattern provides quick backup responses automatically.
It improves user experience and system reliability.
Practice
fallback pattern in microservices?Solution
Step 1: Understand the fallback pattern role
The fallback pattern is designed to handle failures gracefully by providing an alternative response.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.Final Answer:
To provide a backup response when a service call fails -> Option AQuick Check:
Fallback pattern = backup response [OK]
- Thinking fallback increases service calls
- Confusing fallback with permanent service replacement
- Assuming fallback is for logging only
Solution
Step 1: Review fallback implementation options
Fallback should provide a quick alternative response like cached data or default messages.Step 2: Eliminate incorrect options
Repeated calls can cause delays, ignoring failure hurts user experience, and restarting service is costly and slow.Final Answer:
Return cached data or a default message when the main service fails -> Option CQuick Check:
Fallback = cached or default response [OK]
- Retrying endlessly instead of fallback
- Returning errors instead of fallback data
- Restarting services unnecessarily
response = callMainService()
if response.failed:
response = fallbackResponse()
print(response)
What will be printed if callMainService() fails?Solution
Step 1: Analyze the failure condition
IfcallMainService()fails, the code assignsfallbackResponse()toresponse.Step 2: Determine printed output
The printed output will be the fallback response, not the failed original response or an error.Final Answer:
The fallback response -> Option AQuick Check:
Failed main call triggers fallback output [OK]
- Assuming failed response is printed
- Expecting program crash on failure
- Confusing fallback with error message
def get_data():
try:
return call_service()
except:
call_fallback()
What is the bug here?Solution
Step 1: Check try-except behavior
The try block returns the service call result, but except calls fallback without returning it.Step 2: Identify missing return
Without returning fallback's result, the function returns None on failure instead of fallback data.Final Answer:
The fallback function is not returned -> Option DQuick Check:
Missing return in except causes None [OK]
- Forgetting to return fallback data
- Misunderstanding try-except flow
- Assuming fallback raises error
Solution
Step 1: Understand fallback goals for payment service
Fallback should keep system responsive and avoid blocking user with delays.Step 2: Evaluate options for responsiveness and safety
Returning cached success immediately and updating asynchronously balances responsiveness and eventual consistency.Step 3: Eliminate risky or slow options
Retries cause delays, generic errors hurt UX, restarting service is costly and slow.Final Answer:
Return a cached success response immediately and update later asynchronously -> Option BQuick Check:
Cached immediate fallback with async update = best practice [OK]
- Excessive retries causing delays
- No fallback causing poor user experience
- Restarting services on failure
