System Design
Quick notes
User <--> DNS
User <--> Web server <--> Database
Vertical scaling
Horizontal scaling
Load balancers
Multiple servers
Database replication
Master/slave relationships
Sharding
Resharding data
Celebrity problem
Join and de-normalization
Performance improvements
CDN
Cache
Stateless web tier
Data centers
Message queue
Logging, metrics, automation
L1 and L2 cache
Mutex lock/unlock
Branch mispredict
Disk seek
Back of the envelope estimation
Cloud microservices
API gateway
rate limiting
SSL termination
authentication
IP whitelisting
servicing static content
etc.
Rate limiting algorithms
Token bucket
Leaking bucket
Fixed window counter
Sliding window log
Sliding window counter
Rate limiter in a distributed environment
Race condition
Synchronization issue
Sticky sessions
Redis
Performance optimization
Monitoring
Consistent hashing
The rehashing problem
Hotspot key problem
Virtual nodes
Key-value store
Single server key-value store
Distributed key-value store
CAP (Consistency, Availability, Partition)
CAP Theorem
CP, AP, CA
System components
Data paritition
Data replication
Consistency
Quorum consensus
N, W, R
Consistency models
Strong consistency
Weak consistency
Eventual consistency
Inconsistency resolution
Versioning
Detection and reconciliation
Vector clock
Handling failures
System architecture diagram
Write path
Read path
Gossip protocol
Hinted handoff
Anti-entropy protocol
Merkle tree
Unique ID generator
Multi master replication
UUID
Ticket server
Twitter snowflake approach
Applications
URL Shortener
Use cases
URL shortening
URL redirecting
High availability
Back of the envelope estimation
No. of urls to store: to decide hash function
Storage requirement
Design
301 redirect: permanently redirected
302 redirect: temporarily redirected
Hash functions
Hash + collisions resolution
Base-n conversion of unique ids
Web crawler
Use cases
Search engine indexing
Web archiving
Web mining
Web monitoring
Characteristics of a good web crawler
Scalability: efficiency, parallelization, etc.
Robustness: Handle edge cases like traps, bad HTML, unresponsive servers, etc.
Politeness: Do not send too many requests
Extensibility: Extend with minimal changes to crawl more stuff in future like images.
Queries per second
Peak QPS
Average web page size
Storage requirements
Design
Seed urls
URL Frontier
HTML downloader
DNS resolver
Content parser
Content seen?
Content storage
URL Extractor
URL Filter
URL seen?
URL storage
Web crawler workflow
Priority
Front queues: manage prioritization
Back queues: manage politeness
Freshness
Performance optimization
Distributed crawl
Cache DNS resolver: Keep mapping and update periodically using cron jobs.
Locality
Short timeout
Robustness
Consistent hashing: To distribute load among downloaders
Save crawl states and data
Exception handling
Data validation
Extensibility
Avoid problems
Redundant content
Spider traps
Data noise
Server side rendering
Filtering
Analytics
Notification system
Requirements
Types: Push notification, SMS, email
Real time?: Soft real time?
Supported devices: IOS, android, laptop/desktop
Trigger notifications?
opt-out?
how many?
High level: types, contact info gathering, sending/receiving flow
APNS, FCM, SMS, Email
Design
Notification servers
Cache
DB
Message queues
Workers
Third-party servers
IOS, Android, SMS, Email
News Feed
High level: Feed publishing, feed building
User <--> Load balancer <--> Web servers <--> [Post service, fanout service, notification service]
Post service <--> Post cache
News feed service <--> News feed cache
Post cache <--> Post DB
Fanout service
Fanout on write
Hotkey problem
Fanout on read
Cache architecture
New feed: news feed
Content: host cache, normal
Social Graph: follower, following
Action: liked, replied, others
Counters: like counters, reply counter, other counters
Chat system
Requirements
A one-on-one chat with low delivery latency
Small group chat
Online status
Multiple device support. The same account can be logged in to multiple devices at the same time.
Push notifications
Sender <--> Chat service [store message, relay message] <--> receiver
HTTP is client-initiated connection
Server-initiated connection
Polling
Long polling
web socket
Apache zookeeper
User online status
Search autocomplete
Back of the envelope estimation: Peak QPS, storage
High-level design: Data gathering service, query service
Trie
Stream processing: Apache Hadoop MapReduce, Apache Spark Streaming, Apache Storm, Apache Kafka
YouTube
Back of the envelope estimation: Average video size, daily storage space need, CDN cost, etc.
Transcoding
MPEG-DASH
Proximity Service
Two-dimensional search: Evenly divided grid, Geohash, Quadtree, Google S2 (Hilbert Curve)
Blue/green deployment
Quick terms
High availability
High scalability
Automatic scaling
Heterogeneity
Tunable consistency
Low latency
Asynchronous
High speed networks
Bloom filter
Dedupe mechanism
Questions
What DB to use relational/non-relational?
How non-relational databases differ from each other?
How load-balancers work?
Latency vs throughput?
ACID properties?
Memcached and Redis?
Anycast?
Why use vector clocks and not twitter snowflake approach for versioning
Quick links
Last updated