| Users / Operations | 100 | 10,000 | 1,000,000 | 100,000,000 |
|---|---|---|---|---|
| Number of saved states (mementos) | ~100-1,000 | ~10,000-100,000 | ~1M-10M | ~100M+ |
| Memory/storage usage | Low (MBs) | Moderate (GBs) | High (TBs) | Very High (PBs) |
| Access latency for restore | Instant | Milliseconds | Seconds | Minutes or more |
| System complexity | Simple | Moderate (needs indexing) | High (sharding, archiving) | Very High (distributed storage) |
| Backup and archival needs | Minimal | Required | Critical | Essential with tiered storage |
Memento pattern in LLD - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The first bottleneck is the storage and memory required to keep all mementos. Each saved state can be large, and as users or operations grow, storing all snapshots becomes expensive and slow. This affects both the system's memory and disk usage, and slows down retrieval times.
- Compression: Compress mementos to reduce storage size.
- Incremental snapshots: Save only changes (deltas) instead of full states.
- Archival storage: Move old mementos to slower, cheaper storage.
- Sharding: Distribute mementos across multiple storage nodes.
- Cache recent states: Keep only recent mementos in fast memory for quick access.
- Limit history depth: Restrict how many states are saved per user or object.
Assuming each memento is ~100 KB:
- At 10,000 mementos: ~1 GB storage needed.
- At 1,000,000 mementos: ~1000 GB (1 TB) storage needed.
- At 100,000,000 mementos: ~10 TB storage needed.
Requests per second depend on how often users save or restore states. For 1,000 concurrent users saving every 10 seconds, ~100 QPS write load.
Bandwidth depends on memento size and frequency. For 100 QPS at 100 KB each, ~10 MB/s bandwidth needed.
Start by explaining what the Memento pattern does: saving and restoring object states. Then discuss how storing many states can grow storage and memory needs. Identify storage as the first bottleneck. Suggest practical solutions like compression, incremental snapshots, and archival. Finally, mention trade-offs like limiting history depth to balance performance and resource use.
Your database handles 1000 QPS for saving mementos. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Implement caching and compression to reduce load, then add horizontal scaling with sharded storage to distribute the increased write traffic.
Practice
Memento pattern in system design?Solution
Step 1: Understand the role of Memento pattern
The Memento pattern is designed to capture and externalize an object's internal state so that it can be restored later without exposing the object's implementation details.Step 2: Compare with other design patterns
Other options describe different patterns: A is about object creation (Factory), C is about synchronization (Mutex), D is about interface compatibility (Adapter).Final Answer:
To save and restore an object's state without exposing its internal details -> Option DQuick Check:
Memento = Save & Restore State [OK]
- Confusing Memento with Factory or Adapter patterns
- Thinking it manages concurrency
- Assuming it changes object interfaces
Solution
Step 1: Identify components of Memento pattern
The Memento pattern consists of three main parts: Originator (the object whose state is saved), Memento (the object storing the state), and Caretaker (manages mementos).Step 2: Eliminate other patterns
Options B, C, and D correspond to Observer, Proxy, and Decorator patterns respectively, which are unrelated to Memento.Final Answer:
Originator, Memento, Caretaker -> Option BQuick Check:
Components = Originator + Memento + Caretaker [OK]
- Mixing Memento components with Observer or Proxy
- Forgetting the Caretaker role
- Confusing Memento with Decorator pattern
class Memento:
def __init__(self, state):
self._state = state
class Originator:
def __init__(self):
self._state = ""
def set_state(self, state):
self._state = state
def save(self):
return Memento(self._state)
def restore(self, memento):
self._state = memento._state
originator = Originator()
originator.set_state("State1")
memento = originator.save()
originator.set_state("State2")
originator.restore(memento)
print(originator._state)What will be printed?
Solution
Step 1: Trace state changes in Originator
Initially, Originator's state is set to "State1". Then a Memento is saved capturing "State1". Next, state changes to "State2".Step 2: Restore state from Memento
Calling restore with the saved Memento sets the state back to "State1". The print statement outputs the restored state.Final Answer:
State1 -> Option CQuick Check:
Restore resets state to saved value [OK]
- Assuming print shows latest state before restore
- Confusing save and restore methods
- Expecting error due to private variable access
class Originator:
def __init__(self):
self._state = ""
def set_state(self, state):
self._state = state
def save(self):
return self._state # returns state directly
def restore(self, memento):
self._state = memento
originator = Originator()
originator.set_state("State1")
memento = originator.save()
originator.set_state("State2")
originator.restore(memento)
print(originator._state)Solution
Step 1: Analyze the save method
The save method returns the internal state directly instead of encapsulating it in a Memento object, exposing internal details.Step 2: Understand Memento pattern principle
The pattern requires hiding the internal state inside a Memento object to prevent external access. Returning raw state breaks encapsulation.Final Answer:
The save method returns state directly, exposing internal details -> Option AQuick Check:
Save must hide state in Memento [OK]
- Thinking restore method is faulty
- Believing Memento class is mandatory in code
- Ignoring encapsulation principle
Solution
Step 1: Consider memory and undo tradeoff
Storing a Memento after every character change (Store a Memento after every single character change) uses excessive memory and is inefficient.Step 2: Evaluate checkpoint strategy
Storing Mementos after significant changes or checkpoints (Store a Memento only after significant changes or at checkpoints) reduces memory use while allowing meaningful undo steps.Step 3: Assess other options
Store all changes as raw text snapshots without Memento objects wastes memory by storing raw snapshots without encapsulation; Do not store any state; rely on user to retype removes undo capability.Final Answer:
Store a Memento only after significant changes or at checkpoints -> Option AQuick Check:
Checkpoint Mementos balance memory and undo [OK]
- Saving state too frequently causing memory bloat
- Ignoring encapsulation by storing raw snapshots
- Not implementing undo at all
