Introduction
I'll be documenting all the most relevant system design concepts I've come across, especially useful in interviews.
Typing google.com into my browser?
What actually happens under the hood?
1. The browser takes the url (google.com) and sends it to the DNS server. The DNS server will do a look up and return the server's IP address.
2. The browser establishes a TCP connection with the server through a three-way handshake (SYN, SYN-ACK, ACK).
3. The browser sends an HTTP request to the server.
4. The server sends an HTTP response back to the browser (HTML, CSS, JavaScript).
5. The browser renders the page using the response!
Databases
There are two types of databases: relational and non-relational.
Relational databases (SQL databases) are used to store data in tables and rows, which you can perform JOIN operations on via SQL. Common ones include MySQL, PostgreSQL, and SQLite.
Non-relational databases (NoSQL databases) will store data in a key-value pair, column storage, graph storage, or document storage. Common ones include MongoDB, Cassandra, and Redis.
When do I use which?
Non-relational databases are better when
- Dealing with massive amounts of data
- Low-latency is required
- No relational data is required
- You only need to serialize and deserialize data
Otherwise use a relational database.
Scaling
There are two main types of scaling, vertical and horizontal.
Vertical scaling ("scale up") means adding more power (CPU, RAM) to your server.
Horizontal scaling ("scale out") means adding more servers to your server pool.
When do I use which?
Vertical scaling is better with low traffic, since it's simple. But the cons are that there are limits to how much power you can add. And if the server crashes, everything crashes (violates SPOF).
Horizontal scaling is better with high traffic, and large scale applications.
Consider your black-friday app running on 1 server. On black friday, you'll receive so much traffic that it would reach the web server's load limit. Users will start to experience slower responsiveness and eventually, the server will crash.
A load balancer is used for this reason, to distribute traffic evenly across multiple servers.
Load Balancer
The app will connect to a load balancer via the server's IP address. Then the load balancer will connect to each server via a private IP address.
This allows the load balancer to identify and redirect traffic to the appropriate server.
Database Replication
In the same way having multiple servers prevents SPOF, we use database replication (multiple databases) to prevent data loss.
This is modelled via the master-slave relationship. The master database (only writes) and the slave database (only reads) replicate off the master database.
Apps typically have more reads than writes, hence # of slaves > # of masters.
Having slave databases also allows for parallel reads, leading to better performance.
Sharding is another popular concept which partitions a large database into smaller independent pieces called shards, which allows you to spread traffic across multiple shards so the database can handle more users / read+writes without one server being bottlenecked.
Cache
Cache is a temporary storage area to store the result of expensive responses or frequently accessed data in-memory.
When the browser requests a resource, the web server checks if the cache has the resource. If it does, return it instantly. If not, the server will query the database for the response, add it to the cache, and return it.
Something to note is databases and caches aren't always in sync, i.e. if the database undergoes an UPDATE SQL query. One strategy is setting an expiration policy (TTL) on the cache, and resetting it.
If you reset it too often, you'll need to query the database more often. If you reset it too rarely, you'll risk serving stale data.
Sometimes, a cache can become full. If it does, there are a few strategies to evict and make space. Some include LFU (Least Frequently Used), and FIFO (First In First Out).
LRU is typically used by default. Temporal locality suggests that recently used items are likely to be used again soon.
LFU is better when some items are consistently popular over time.
CDN
CDN (Content Delivery Network) are geographically disposed servers used to deliver static content (images, videos, CSS, JavaScript) to users.
They're like a cache, but for static content.
Stateful vs Stateless
Stateful servers remember client data from 1 request to the next.
Stateless servers keep no information about the client between requests.
The industry is moving towards stateless servers, as they are more scalable and easier to manage.
Message Queue
Message queues are durable components stored in memory that support async communication (think buffer for async requests).
The advantage comes in the decoupling. If the provider is unavailable, the consumer can still function and vice versa.
Metrics
1KB -> 1MB -> 1GB -> 1TB -> 1PB
- L1 cache: 0.5ns
- L2 cache: 5ns
- Mutex lock: 100ns
- Main memory reference: 100ns
- Disk seek: 10ms
Takeaways
- Memory is fast, but disk is slow
- Perform lossless compression on data before sending it over the internet
- Data centers are usually in different regions, so it takes time to send data between them
Availability
High availability means for the system to be continuously operational for a long period of time.
Service level agreement (SLA) is an agreement between service provider and customer, defining the level of uptime your service will deliver.
Twitter Example
Consider twitter. 300 million monthly users. 50% use Twitter daily. Users post 2 posts / day. 10% of tweets contain media. Data stored for 5 years.
Query per second (QPS)
- Daily active users (DAU) = 300 million x 0.5 = 150 million
- Tweets (QPS) = 150 million x 2 tweets / 24h / 3600s = ~3500
- Peak QPS = 3500 x 2 = 7000
Average tweet size
- tweet_id: 64 bytes
- text: 140 bytes
- media: 1MB
Media storage: 150 million * 2 * 10% * 1MB = 30TB/day
5-year media storage = 30TB/day * 365 days * 5 = 55PB
System Design Interview
The general flow of a system design interview should follow this format:
1. Clarify the problem at hand
- What is the feature set of this application?
- What is the scope of the problem we're most interested in?
- Is this a mobile app or web app?
2. List functional requirements (things users should be able to do)
- Users should be able to post tweets
- Users should be able to follow other users
- Users should be able to see tweets from users they follow
3. List non-functional requirements (what your system should support)
- System should scale to support 100M DAU's
- System should be highly available, prioritizing availability over consistency (CAP theorem mention)
- System should be low latency, and should render <100ms
- System should be read heavy
4. Design your database
- We'll need a user, tweets, and follower table
- What are the fields each table will store?
- What are the primary and foreign keys?
5. Design your API endpoints
Will you use REST (most common), GraphQL (allows clients to query for exact data, used if you have diverse clients with different needs), or RPC (when performance is critical)?
Define your endpoint names, request body, and response body (these should cover your functional requirements).
6. Architecture diagrams
Drawing boxes and arrows to represent the different components of the system and how they interact (client, server, databases, CDN, caching, load balancer).
Below is an example of an architecture diagram for designing twitter.

7. Deep dive
- Ensure your solution satisfies your non-functional requirements (if 100M DAU, discuss horizontal scaling solutions)
- Make those improvements suggested by the interviewer
Core Concepts
Networking is how services talk to each other, and what happens when they fail or get slow.
In system design interviews, the typical usage is HTTP over TCP.
Websockets and server-side events (SSE, uni-directional) come up when dealing with real-time updates.
However, typically, HTTP with polling could work as an alternative to websockets in most cases.