Below is a list of some practical questions potentially discussed in interview.
Let's take an example of design video view counting system.
[Interviewer:] Let's Design a video view counting system. ## 1. Requirement Clarification
The purpose to figure out how candidate deals with ambiguity.
As a candidate, you should start asking questions below:
### Users/Customers
* Who will use the system?
* How the system will be used?
### Scale (Read/Write)
* How many read queries per second?
* How much data is queried per request?
* How many video views are processed per second?
* Can there be spikes in traffic?
### Performance
* What is expected write-to-read data delay?
* What is expected p99 latency for read queue?
### Cost
* Should the design minimize the cost of development?
* Should the design minimize the cost of maintenance?
### Functional Requirements - API
The system has to count video view events.
### Non-functional Requirements
[Interviewer:] Let's Design a system that can handle YouTube scale.
[Interviewer:] Let's try to make it as fast as possible.
Scalable
>
Tens of thousands of video views per second.
High Performance
Few tens of milliseconds to return total views count for a video.
* High Availability
Survives hardware/network failures, no single point of failure.
复制代码
## 2. High level Design
[Candidate:]
Let's start design from something simple.
User->Browser -> Processing Service -> DataBase <-- Query Service <-- Browser -> User
* Can only query the data in the way data was aggregated
* Requires data aggregation pipeline
* Hard or even impossible to fix errors/bugs
Tips:
- Batch vs Streaming
| Batch processing | Streaming |
| delayed to process data | near real time |
- Storage optimization by combining both raw data and aggregated data.
> We can store raw data in last 24 hours, aggregate data in last week per minute
> aggregate data per hour in last month, and per day for last year.we can archieve cold data into S3.
### 2.2 Where we store
SQL vs NoSQL? It is a question.
[Interviewer:] :
Can you please give me specific database name? and explain your choice.
[Candidate:]
I know that both MySQL and NoSQL (like Cassandra) database may scale and perform well for this case.
Let me evaluate both options.
Questions for you to answer
* How to scale write?
* How to scale read?
* How to make both write and read fast?
* How not to lose data in case of hardware faults and network partitions?
* How to achieve strong consistency? What are the tradeoffs?
* How to recover data in case of outage?
* How to ensure data security?
* How to make it extensible for data model changes in future?
#### 2.2.1 SQL solution
V1 :
The problem of V1 is that Processing Service needs to know the config of MySQL and monitor the config change in case
some MySQL service is down and rejoining. This is heavily coupling.
Let's decouple MySQL and Processing Service, then we got V2.
V2:
V2 is introducing Cluster proxy to decouple PS and DB, and also using ZK to manage the configuration. This is good.
We also have replicas for MySQL. Primary and Read Replicas are segergated. but if a single SQL server is overwhelmed,
we need more than one and hence sharding.
V3 : MySQL Primary and Read Replica sharding.
### 2.2.2 NoSQL solution
##### Pick which node to store data
* Consistent Hash
* Cassandra
> Quorum
>
> Gossip
### 2.3 How we store
* For SQL, we define different tables and use `join` to get the data.
normalization is good in Relational Databases.
Video_info:
| Video_Id | Name | Channel_Id |
|---|---|---|
|A| System Design Course Video | 111 |
| Channel_Id | Name |
|---|---|
|111| this channel
* NoSQL - Denormalization.
## 3. Detail Design ### 3.1 Processing Service detail #### 3.1.1 Basic
We need a hash map in memory for counter. we increase the counter for each event.
We either
* Option 1: Increase count right away whenever we receive an event.
* Option 2: Data aggregation in memory and send every other second.
#### 3.1.2 Detail
* Checkpointing
* Partition consumer.
1. We have multiple queues, each queue for each partition. We can consume multiple queues by either single thread or
multi-thread.
Let's use single thread consume. A infinite loop is used to poll the queue.
2. Consume also need to deduplication the request in case of retry. We can use distributed cache to dedup it.
3. Database Writer - write data to Database
4. Dead letter queue - DLQ.
* Ingestion Path components
| Partition Service Client | Load Balance | Partitioner Service and Partitions |
|---|---|---|
| Blocking vs Non-Blocking I/O | Software vs hardware balancing | Partition Strategy |
| Buffering and Batching | Network protocols | Service discovery |
| Timeouts | Load balancing algo | Replication |
| Retries | DNS | Message Format |
| Exponential backoff and jitter | Health checking | |
| Circuit breaker | High Availability | |
* Data retrieval path
We need `Query Service` to query DB.
> How you wrote determines how you read
### 3.3 Bottlenecks, tradeoff and more
Interviewer may ask below questions:
#### 3.3.1 How to identify bottlenecks?
* Load test
* Stress test
#### 3.3.2 How to make sure system is healthy?
Alarm/Monitor
#### 3.3.3 How to make sure the system produces accurate results?
Answer : Have two systems to produces results and compare them.
* Weak consistency
* Eventual consistency
* Strong consistency
* TLA+
Quote from Apache Kafka's author Jay Kreps: > We should use a batch processing framework like MapReduce if we aren't latency sensitive, and use a streaming
> processing framework if we are, but not to try to do both at the same time.
#### 3.3.4 What if processing service can not keep up with the speed new message arrival?
#### 3.3.5 Let's say we've got a hugely popular video. Will it become a bottleneck?
Hot partition or not?
### 4. Summary
### 4.1 Functional Requirement
* Write Down verbs
* Define input parameters and return values
* Make several iterations
### 4.2 Non-Functional Requirement (Qualities)
* Scalability
* Availability
* Performance
* Other
### 4.3 High Level design
* How data gets in
* How data gets out
* Try to drive the conversation
### 4.4 Detail design
* It's all about data (Storage, transfer, processing)
* Use fundaments concepts
* Apply relevant technologies
### 4.5 Bottleneck and tradeoff
* Listen to the interviewer
* Know the tradeoff
---