The Fallback Pattern
You'll learn how to maintain service availability by providing alternative responses when primary operations fail.
What Is the Fallback Pattern?
The Fallback Pattern is a resilience technique that provides a backup plan when your primary operation fails. Instead of letting errors bubble up and crash your application, you return an alternative response—a cached value, a default result, or degraded functionality that keeps your service running.
Think of it like a restaurant running out of fresh fish. Rather than closing the kitchen, they offer chicken instead. Customers still get fed, even if it's not their first choice.
How It Works
When a primary operation fails (a network call times out, a database is unreachable, a service returns an error), the fallback pattern automatically switches to a predetermined alternative:
- Try the primary operation (call an API, query a database)
- If it fails, execute the fallback logic
- Return the fallback result to the caller
Common Fallback Strategies
- Cached data: Return stale but useful information from a previous successful call
- Default values: Provide sensible defaults (empty list, generic recommendations)
- Static content: Serve pre-generated or simplified responses
- Alternative service: Try a backup data source or service
- Degraded functionality: Offer limited features instead of none
Combining with Other Patterns
Fallbacks work powerfully alongside circuit breakers, retries, and timeouts. The circuit breaker detects failures, retries give operations multiple chances, timeouts prevent hanging—and when all else fails, the fallback ensures your users still get something.
Key Takeaway: The Fallback Pattern maintains service availability by providing alternative responses when primary operations fail, ensuring users experience degraded functionality rather than complete failure.