Bird
0
0
LLDsystem_design~10 mins

Memento pattern in LLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Memento pattern
Growth Table: Memento Pattern Scaling
Users / Operations10010,0001,000,000100,000,000
Number of saved states (mementos)~100-1,000~10,000-100,000~1M-10M~100M+
Memory/storage usageLow (MBs)Moderate (GBs)High (TBs)Very High (PBs)
Access latency for restoreInstantMillisecondsSecondsMinutes or more
System complexitySimpleModerate (needs indexing)High (sharding, archiving)Very High (distributed storage)
Backup and archival needsMinimalRequiredCriticalEssential with tiered storage
First Bottleneck

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.

Scaling Solutions
  • 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.
Back-of-Envelope Cost Analysis

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.

Interview Tip

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.

Self Check

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.

Key Result
The Memento pattern scales well at small user counts but quickly hits storage and memory bottlenecks as saved states grow. Efficient storage techniques and distribution are key to scaling.