Bird
0
0
LLDsystem_design~7 mins

Emergency handling in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When unexpected errors or failures occur during program execution, the system can crash or behave unpredictably, causing loss of data or poor user experience. Without a structured way to handle these emergencies, the entire application may stop working or produce incorrect results.
Solution
Emergency handling uses structured code blocks to catch and manage errors gracefully. It allows the program to detect problems, respond appropriately, and continue running or shut down safely. This mechanism separates normal logic from error management, improving reliability and user trust.
Architecture
Start
Try block
(normal code)
Success path
End
Except block
(handle error)
Recovery or
Cleanup
End

This diagram shows the flow of normal execution inside a try block and how errors divert control to an except block for handling before continuing or ending.

Trade-offs
✓ Pros
Prevents application crashes by catching runtime errors.
Separates error handling from main logic, improving code clarity.
Allows graceful recovery or cleanup after failures.
Improves user experience by providing meaningful error messages.
✗ Cons
Overusing emergency handling can hide bugs if errors are silently caught.
Handling all exceptions broadly may mask specific issues needing fixes.
Adds some performance overhead due to error checking.
Use emergency handling in any system where runtime errors can occur, especially in user input processing, file operations, network calls, or external API usage. Essential when system uptime and data integrity are critical.
Avoid using emergency handling to control normal program flow or replace proper validation. Do not catch exceptions without proper handling or logging, as this can hide real problems.
Real World Examples
Netflix
Netflix uses emergency handling in their streaming service to catch network errors and retry connections without interrupting the user experience.
Stripe
Stripe employs emergency handling to manage payment gateway failures gracefully, ensuring transactions are retried or users are informed without crashing the payment flow.
Uber
Uber uses emergency handling to catch GPS or API errors in their app, allowing fallback mechanisms to maintain service continuity.
Code Example
The before code crashes if the file does not exist because it does not handle exceptions. The after code uses a try-except block to catch the FileNotFoundError, prints a friendly message, and returns None to allow the program to continue safely.
LLD
### Before: No emergency handling

def read_file(filename):
    with open(filename, 'r') as f:
        data = f.read()
    return data

print(read_file('missing.txt'))  # This will crash if file not found


### After: With emergency handling

def read_file(filename):
    try:
        with open(filename, 'r') as f:
            data = f.read()
        return data
    except FileNotFoundError as e:
        print(f"Error: File {filename} not found.")
        return None

print(read_file('missing.txt'))  # Prints error message and returns None
OutputSuccess
Alternatives
Return codes
Instead of exceptions, functions return status codes indicating success or failure, requiring manual checks after each call.
Use when: Use when working in low-level or performance-critical systems where exceptions are too costly or unavailable.
Error callbacks
Errors are handled via callback functions passed to asynchronous operations rather than synchronous try-except blocks.
Use when: Use in asynchronous or event-driven systems where errors must be handled after operations complete.
Summary
Emergency handling prevents crashes by catching and managing runtime errors.
It separates error management from normal code, improving reliability and user experience.
Proper use requires catching specific exceptions and avoiding misuse for normal control flow.