System Design
Notes
System Design Interview: An Insider's Guide - Alex Xu
Chapter 1 - Scale from zero to millions of users
Start with a single server setup
Move the database to a separate server (consider carefully SQL vs. NoSQL)
Add a load balancer to enable horizontal scaling
Set up database replication (with master write / slave read)
Caching to improve performance (memory vs. disk trade-off)
Content Delivery Network for static assets and/or geographical latency
Stateless web tier to avoid sticky sessions
Data centers for geographical latency / redundancy
Message queues for asynchronous processing
Logging / metrics for identifying and resolving problems
Sharding for database horizontal scaling (celebrity problem)
Chapter 2 - Back-of-the-envelope estimation
Read from memory: 250 µs
RTT in the same data center: 500 µs
Read from network: 10 ms
Read from disk: 30 ms
RTT internationally: 150 ms
Use rounding / approximation to make calculations easiers
Write down the assumptions you're making
Always label the units
Chapter 3 - A framework for system design interviews
Understand the problem and establish the design scope
What features?
How many users?
Anticipation of scale?
Technology stack?
Existing services?
Propose high level design and get buy-in
Initial blueprint
Ask for feedback
Draw boxes diagrams with components
Back-of-the-envelope calculations to check scale
Design deep dive
Wrap up
Bottlenecks
Potential Improvements
Recap
Next scale curve
Chapter 4 - Design a rate limiter
Client-side, server-side, or middleware
Send back 429 Too Many Requests when throttled
Algorithms for rate limiting
Token bucket
Leaky bucket
Fixed window counter
Sliding window log / counter
In-memory cache supports time-based expiration
Rules for limiting in cache / disk
Race conditions / synchronization
Chapter 5 - Design consistent hashing
hash(key) % N
good for fixed-size pools
bad when servers are added / removed
Consistent Hashing: only K / N keys need to be remapped
Introduce a hash ring and hash server / nodes onto it
Server lookup is done clockwise on the ring
Problems with partition size and non-uniform distribution
Solution: virtual nodes (more nodes, better balance)
Chapter 6 - Design a key-value store
Basic approach: in-memory hash table -> does not scale
CAP theorem: pick two among consistency, availability and partition tolerance
CP -> blocking write for accuracy
AP -> eventual consistency
Data replication on K server
Resolve incosistencies using vector clocks
Gossip protocol to handle failure
Merkle trees to detect inconsistencies and calculating smallest deltas
Coordinator node as proxy between client and store
Chapter 7 - Design a unique ID generator in distributed systems
Auto increment in relational database -> does not scale
Multi-master replication (auto_increment with K) -> problem when adding / removing nodes
UUID -> not numeric, don't go up with time
Centralised ticket server -> single point of failure
Snowflake approach: f(timestamp, data center ID, machine ID, sequence number)
Clock synchronisation?
Section length tuning?
Chapter 8 - Design a URL shortener
REST APIs with URL redirecting (Location header, temporal vs. permanent redirect)
Intuitive approach: hash table (URL ->
tinyurl.com/${hash(URL)}
) -> does not scale<shortURL, longURL>
tuples in RDBMSHash + collision resolution vs. base62 conversion
Rate limiting
Analytics
Availability vs. consistency
Chapter 9 - Design a web crawler
Parallelisation to achieve scalability
Robustness for bad HTML, malicious links, unresponsive servers
Politeness: do not overload servers while indexing
Start from seed URL (divide URLs by country / topic)
URL frontier to keep track of websites to visit
Content parser to filter out malformed pages
Content deduplicator to avoid visiting mirrors / duplications
URL extractor to progress in the crawl
URL deduplicator to avoid visiting the same URL multiple times
BFS vs. DFS: depth can grow pretty quickly
Different queues per domain for politeness
Priority queues for proritising important content
Chapter 10 - Design a notification system
On app install or sign up, collect mobile token, phone number and email address
Decouple notification systems from actuators (iOS, Android, SMS, email servers) through message queues
Prevent data loss through at-least once delivery (with retries)
Templates to avoid building content from scratch
Respect user preferences with respect to communication channels
Rate limiting
Event tracking / analytics
Chapter 11 - Design a news feed system
Feed publishing -> fanout on write (good for regular people)
Feed retrieval -> fanout on read (good for celebrities with caching)
Use graph DB to store / retrieve user connections
Use a CDN for media files (photos / videos)
Chapter 12 - Design a chat system
Clients connect to chat servers via WebSockets for two-ways communication (send/receive messages)
Other operations (login, group management, user profile) are stateless and can be done over HTTP
Notification service for newer message
Chat data is very large, read-to-write ratio is 1:1 -> prefer key-value store over SQL
Service discovery to find out chat server to connect to
One message sync queue per user -> either delivery the message or store them when offline
Online presence through heartbeat messages
End-to-end encryption of messages
Caching messages on the client side
Chapter 13 - Design a search autocomplete system
One request per input character, with low latency
Data gathering system, takes input queries and aggregates them (real-time or batch)
Query service, given a prefix return the most 5 searched items
Using prefix trees / tries is crucial for scalability
Limiting max length for query makes it a O(1) operation
Cache top queries at each node to avoid full traversal
Tries are not suited for SQL, better to use document store or key-value store (where the key is the prefix and the value is the trie node)
AJAX requests to save a full page re-render
Browser caching for data changing infrequently
Store tries in CDNs for local queries
Desing 14 - Design YouTube
Use CDN (expensive!) for streaming videos, API servers for other operations
Video uploading: original storage, transcoding servers (multiple formats and bit rates), transcoded storage, CDN, metadata servers
Video streaming: access metadata servers for search, then CDN for playback
Pre-signed uploads and DRM/encryption for security
Send only popular videos to CDN to save costs
CDN also for geographical content (popular only in one country)
Capitolo 15 - Design Google Drive
Reliability is extremely important (data loss in unacceptable)
Bandwidth usage needs to be contained
Web server to handle upload/download
Database to keep track of metadata
Storage system for actual files (e.g., S3) with cold storage for inactive data
The block server analyses deltas between versions and only sends changed blocks (saves bandwidth)
Needs strong consistency by default (different clients must see the same file)
Notification service via WebSockets for updates
De-duplicate blocks to save on storage data
Resources
Articles
5 Caching Mechanisms to Speed Up Your Application - Pragati Verma
Consistent hashing algorithm - High Scalability
Consistent Hashing Explained - SystemDesign
Database Sharding: Concepts and Examples - MongoDB
Hashing - Sam Rose
Load Balancing - Sam Rose
SQL vs NoSQL: The Differences - Craig Buckler, SitePoint
Visualizing algorithms for rate limiting - smudge.ai
What is a content delivery network (CDN)? - CloudFlare
Books
Designing Data-Intensive Applications - Martin Kleppmann (website)
Courses
System Design - Karan Pratap Singh
GitHub repositories
Byte-sized System Design Concepts - ByteByteGo
CDN Up and Running - Leandro Moreira
Images
Videos
Scalability - David Malan, Harvard University
Websites
YouTube playlists
System Design - Gaurav Sen
Last updated