System Design
System Design interviews are less about getting lucky and more about actually doing the hard work of attaining knowledge. Performance in these interviews depends on the following 2 factors—
- Your knowledge — gained either through studying or practical experience.
- Your ability to articulate your thoughts.
- When companies ask design questions, they want to evaluate your design skills and experience in designing large scale distributed systems
- How well you do in such interviews often dictates hiring level/ salary.
Note: Most of the contents in this port are a compilation of the references.
Steps of system design—
- get a functional system first
- Requirement gathering, (Design twitter)
- Who can post a tweet? (answer: any user)
- Who can read the tweet? (answer: any user — as all tweets are public)
- Will a tweet contain photos or videos (answer: for now, just photos)
- Can a user follow another user? (answer: yes).
- Can a user ‘like’ a tweet? (answer: yes).
- What gets included in the user feed (answer: tweets from everyone whom you are following).
- Is feed a list of tweets in chronological order? (answer: for now, yes).
- Can a user search for tweets (answer: yes).
- Are we designing the client/server interaction or backend architecture or both (answer: we want to understand the interaction between client/server but we will focus on how to scale the backend).
- How many total users are there (answer: we expect to reach 200 Million users in the first year).
- How many daily active users are there (100 million users sign-in everyday)
- System can be hypothetical, may consider basic functionalities. Say twitter dont need to support videos at this point.
- Requirement gathering, (Design twitter)
- API/Functionality definition
- postTweet(uid, time, location, tweet…)
- celebTweet(uid, time, location, tweet…)
- generateTimeline(uid, time)
- recordLove(uid, tweet_id)
- Hardware/Resource requirement: Show ur interviewer you understand the scale
- Number of tweets/updates.
- Storage requirement
- Caching requirement
- Latency requirement, say we want the timeline to generate fast
- BW requirement
- Network IO
- Firewall
- Data Model
- User
- Tweet
- userFollows
- FavoriteTweets
- High level design: Generally we think this is the only thing that matters, unfortunately at comes at 5th position here.
- Draw high level system diagram
- Detailed component design
- Bottleneck identification and resolve
- Reduce Latency
- Queuing System, service discipline to select server
- Failure tolerance
- Bring in server on demand for scalability and increased load 
Scalability
- Read is cheap write is expensive. 10ms for a disk seek.
- Batch writes, for faster writing.
- Writes –> disk seek. only 100seeks /second or 10ms per seek
- Reads–> 250ns to read 1 MB
- To learn more on these numbers Click Here
Design Patterns with examples
- Java design Patterns
- A great resource on adapter design pattern
A simple design pattern example-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
MediaPlayer.java
public interface MediaPlayer {
void play(String filename);
}
MediaPackage.java
public interface MediaPackage {
void playFile(String filename);
}
Now, you can create the implementations classes :
MP3.java
public class MP3 implements MediaPlayer {
@Override
public void play(String filename) {
System.out.println("Playing MP3 File " + filename);
}
}
VLC.java
public class VLC implements MediaPackage {
@Override
public void playFile(String filename) {
System.out.println("Playing VLC File " + filename);
}
}
Funny guy TechLead on Design Patterns
Simple twitter design
https://www.hiredintech.com/lecture_materials/twitter_problem_system_design.png