Problem Statement
When a microservice or its dependencies fail or become slow, the entire system can become unresponsive or crash, causing a poor user experience and potential data loss.
Jump into concepts and practice - no test required
This diagram shows a client request flowing through an API Gateway to a microservice. If the microservice fails or is slow, the fallback mechanism activates, possibly using cache or circuit breaker to provide degraded but available responses.
### Before: No graceful degradation def get_user_profile(user_id): # Direct call to microservice response = call_profile_service(user_id) return response.data ### After: With graceful degradation from functools import lru_cache @lru_cache(maxsize=1000) def get_cached_profile(user_id): # Cached fallback response = call_profile_service(user_id) return response.data def get_user_profile(user_id): try: response = call_profile_service(user_id) return response.data except ServiceUnavailableError: # Fallback to cached data return get_cached_profile(user_id)
graceful degradation in microservices?response = callService()
if response == null:
response = getCachedData()
return responsecallService() fails?try {
data = fetchFromService()
} catch (Exception e) {
data = null
}
return data.toString()