Bird
Raised Fist0
Microservicessystem_design~10 mins

Fallback pattern in Microservices - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a fallback method for a microservice call.

Microservices
def get_user_data(user_id):
    try:
        return call_remote_service(user_id)
    except Exception:
        return [1]()
Drag options to blanks, or click blank then click option'
Alog_error
Bcall_remote_service
Cfallback_response
Draise_error
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original service call inside the except block
Raising an error instead of returning fallback
2fill in blank
medium

Complete the code to implement a fallback pattern using a circuit breaker.

Microservices
if circuit_breaker.is_open():
    response = [1]()
else:
    response = call_remote_service()
Drag options to blanks, or click blank then click option'
Afallback_response
Bcall_remote_service
Craise_exception
Dretry_call
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the remote service even when circuit is open
Raising exceptions instead of fallback
3fill in blank
hard

Fix the error in the fallback implementation to handle service failure gracefully.

Microservices
def fetch_data():
    try:
        data = call_service()
    except Exception:
        data = [1]
    return data
Drag options to blanks, or click blank then click option'
Alog_error()
Bcall_service()
Craise Exception
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the service again causing infinite loop
Raising exceptions instead of fallback
4fill in blank
hard

Fill both blanks to implement a fallback with a timeout and default response.

Microservices
try:
    response = call_service(timeout=[1])
except TimeoutError:
    response = [2]()
Drag options to blanks, or click blank then click option'
A5
B10
Cfallback_response
Dretry_call
Attempts:
3 left
💡 Hint
Common Mistakes
Using too large timeout
Retrying instead of fallback on timeout
5fill in blank
hard

Fill all three blanks to create a fallback pattern with logging and default data.

Microservices
def get_data():
    try:
        return call_service()
    except Exception as e:
        [1](f"Service failed: {e}")
        return [2]()

fallback_response = lambda: [3]
Drag options to blanks, or click blank then click option'
Alog_error
Bfallback_response
C{"data": "default"}
Draise_exception
Attempts:
3 left
💡 Hint
Common Mistakes
Not logging errors
Raising exceptions instead of fallback
Fallback not returning default data

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