[NineChap Sys] System Design Class 1 - Shuatiblog.com
the process of defining the architecture, components, modules, interfaces and data to satisfy specified requirements.
u – user
Similarity = # of same music for different users
Instead, we do micro design by starting at the interface.
This is roughly 0.2s per user. Thus Max QPS = 5.
Avg performance increase to ~ 20ns (with some optimization of MapReduce procedure, discuss later).
Max QPS increase to 50.
The answer is know. A machine with 8 (or 16) core CPU could be able to handle.
We can also have a hot-standby, to be safe.
Dataset can be put in different physical locations. Recommender don't really need, cuz it's only do calculation job.
Also, Big Brother is in charge of heart-beat. If not received, Big Brother have some double-check machanism.
It can be stateful or stateless.
Keep in mind the connection between Dataset and Recommender remains. It's slow going thru Dispatcher.
Notice the Web Service is connection heavy, which consume large CPU and RAM resource. It's better to seperate to one machine.
Big brother is read/write heavy, so it's OKAY to put on same machine with Dispatcher.
Since Dataset and Recommender have data exchange, it's a good idea to put on same machine.
https://en.wikipedia.org/wiki/Queries_per_second
the process of defining the architecture, components, modules, interfaces and data to satisfy specified requirements.
- conceptual design (macro)
- logical design
- physical design (micro)
Top-down design
Eg. MS Office, Huawei Security SystemBottom-up design
Most start-up use, MVP first using Medetor + MongoDb.5 steps (SNAKE Principle):
- Scenario: case/interface – input
- Necessary: constrain/hypothesis – input
- Application: service/algorithm – output
- Kilobit: data – output
- Evolve – solution
Step One, Scenario
- brain storm
- register/log in
- play music
- recommendation
- prioritize
- play music
- Get channel
- select a channel
- play
- play music
Step Two, Necessary
- ask
- total user – 100,000,000
- daily users – 1,000,000
- predict
- user analysis
- Traffic analysis
- Memory analysis
- QPS
- user analysis
Avg Concurrent users = daily users / 5 = 200,000
considering your product may grow in the next 3 month:
Peak Concurrent users = concurrent user * 3 = 600,000
Max Peak users in 3 month = Peak users * 10 = 6,000,000
- Traffic analysis
Request of new music per user: 1 music/min
Music size = 3MB
Max peak traffic (in 3 months) = 6,000,000 * 3MB / 60 = 300GB/s - Memory analysis
Memory per user (metadata) = 10KB
Max daily memory = 1,000,000 * 10 * 10 = 100 million KB = 100GB
(10 times of avg daily user)
Step Three, Application
- Replay the case, one service for each
- Merge the services
Step Four, Kilobit: data
- Append 1 dataset for each service
Eg. User service: stability, more addition, less modify and deletion.
Eg. Channel Service: high concurrency, MongoDB
Eg. Music Service: MP3 File Systems
Last Step, Evolve
- Better: constrains
eg. able to handle 300GB/s traffic? - Broader: new cases
share music? delete user account? - Deeper: details design
Exercise
design user recommendation module
Simple algo:
u1={m3,m5,m7,m11} u2={m1,m2,m3,m4,m5,m6,m7,m8,m9} Similarity( u1, u2 ) = 3
m – musicu – user
Similarity = # of same music for different users
Adv algo:
find his top-1 similar user. Stay tuned for future posts.Use the 5 Steps (SNAKE)
- Step One, Scenario
- Step Two, Necessary
- Step Three, Application
- Step Four, Kilobit: data
- Last Step, Evolve
Instead, we do micro design by starting at the interface.
Step One, Scenario
Interfaceclass Recommender { public int findSimilarUser(int userId) { // } }
Step Two, Necessary
- ask
- total users = 100,000,000
- total music = 10,000,000
- peak users in 3 month = 6,000,000
- participation percentage = 5%
- calculation frequency = 1 update/10min/user
- predict
- user analysis (skip)
- Traffic analysis (skip)
- Memory analysis (skip)
- QPS
Step Three, Application
The simpliest algorithm: BF compare. The complexity is O(m n) for each user, where m is # of music a person likes, and n is # of total users. For k users, it takes O(k m n) time (k can be = peak concurrent users).This is roughly 0.2s per user. Thus Max QPS = 5.
One word about complexity-to-seconds estimation.
O(n ^ 3) –> 1s
O(n ^ 2) –> 0.2s
O(n) –> 20ms
O(k) –> k ms
Step Four, Kilobit: data
Very simple:Last Step, Evolve
Read on.How to go from Level 0 to Level 1
Refer to the previous question. How can we improve???- performance
- scalability
- robustness
performance
A better algo: Inverted IndexAvg performance increase to ~ 20ns (with some optimization of MapReduce procedure, discuss later).
Max QPS increase to 50.
scalability
Use a dispatcher to re-direct the requests to multiple machines.How many machines do we need then?
Well we need 500 QPS. The algo above achieves ~ 50 QPS. Should we need 10 machines?The answer is know. A machine with 8 (or 16) core CPU could be able to handle.
We can also have a hot-standby, to be safe.
hot standby is used as a failover mechanism to provide reliability in system configurations.
When a key component fails, the hot spare is switched into operation.
robustness
Tips about system design for senior engineers:Draw 1 machine first. This machine can contains multiple datasets and run multiple processes.
On top of this machine, the interface layer is one single Manager process. The Manager is in charge of almost everything: handling data lost, handle high concurrency, copy multiple instance of itself…
Like this:
Back-end
Now we need a cluster of datasets (which has Manager on top of it), and a cluster of Recommenders. Manager is in charge of copying multiple instances.Dataset can be put in different physical locations. Recommender don't really need, cuz it's only do calculation job.
Receiving requests
Just now we used Receptionist (or Dispatcher) to handle request. Now we use a Web service (eg. Apache). It's not necessary to make it a cluster.Big Brother
We need a monitor system to oversee everything.Also, Big Brother is in charge of heart-beat. If not received, Big Brother have some double-check machanism.
Connecting the dots
Dispatcher is used to connect the 4 components. It's like a messaging queue that collects and distributes jobs among everybody (eg. control and distributed info).It can be stateful or stateless.
Keep in mind the connection between Dataset and Recommender remains. It's slow going thru Dispatcher.
Distribute it
During development, the 5 components can be put on same machine. When we deploy distributely, we use Socket connection (keep alive) to connect them.Notice the Web Service is connection heavy, which consume large CPU and RAM resource. It's better to seperate to one machine.
Big brother is read/write heavy, so it's OKAY to put on same machine with Dispatcher.
Since Dataset and Recommender have data exchange, it's a good idea to put on same machine.
Additional questions
Implement Dispatcher with consumer-producer model.https://en.wikipedia.org/wiki/Queries_per_second
Queries Per Second (QPS) is a common measure of the amount of search traffic an information retrieval system, such as a search engine or a database, receives during one second.[1]
High-traffic systems must watch their QPS in order to know when to scale the system to handle more load.
Read full article from [NineChap Sys] System Design Class 1 - Shuatiblog.com