Wednesday, September 2, 2015

[NineChap System Design] Class 3.2: Web Service - Shuatiblog.com



[NineChap System Design] Class 3.2: Web Service - Shuatiblog.com
fix MP3 problem
The process of fetching a MP3 (from CDN):

  1. acquire MP3 link, and send request
  2. send request to CDN
  3. CDN receive request, find MP3
  4. response to client
  5. play the music

Question: in step 2, there's more Network error, but in step 4, there's no Network error, but Timeout. Why?

Fix step 2, Network error

Problem is: MP3 url invalid. It actually comes from a failed CDN sever.
Solution: fix the server.

Fix step 3, CDN can't find MP3

Problem associated with Anti-Leech.
a leech) is one who benefits, usually deliberately, from others' information or effort but does not offer anything in return.
Example: Wi-Fi leeches, Direct linking (or hot-linking) and In most P2P-networks, leeching is whose who download too much.
Anti-Leech specializes in protecting file downloads and stopping bandwidth leeching.
See that some P2P and leeching software will steal your url links, so the MP3 url expiration time is 5 minutes.
So when CDN server's clock and web server's clock are not synchronized well, MP3 url can expire.
Solution: every 10 minutes sync CDN clock with web server clock.

Fix step 4, Timeout error

Some MP3 are relatively large. Thus timeout.
MP3 performs better at higher bps, and aac(Advanced Audio Coding) works better at lower bps.
Solution:
  1. compress MP3 to 48bps, or use aac format. So, play lower-rate music first, then switch automatically.
  2. pre-load a music while previous is playing.
  3. optimize CDN
CDN content delivery network is a large distributed system of servers deployed in multiple data centers across the Internet.
The goal of a CDN is to serve content to end-users with high availability and high performance.

Which CDN should client choose?
Not DNS, but web server calculates which to choose. It can be calculated using IP distance, or ISP provider, but not accurate.
We can also use local desktop apps (in different locations) to ping CDN servers. This may violate user privacy, though.

Fix step 5, Fail to play

Problem: some files got wrong decoding.

Fix step 6, unkown error

Problem: some users close the page while MP3 loading.

Question 5

fix player problem
Problem: iOS device can never play Flash.
Solution: develop HTML5 player.

5.2 how to evaluate that you solved the problem

  1. user complains
  2. important: daily retention rate!
We can't use daily active user, cuz it depends on marketing, competitors, and infrastructure changes.
One day retention rate:

Today's visitor = {U1, U3, U7, U9, U10}
Tomorrow's visitor = {U2, U3, U9,}
Today's one day retention rate = 2/5

Question 6 秒杀

Design

Queue A and Queue B

Queue A

Many queues, each one locates on a individual web server or reverse proxy. It is mainly used to accept large amount of requests coming from the clients.
Each machine may takes 10,000 or more requests per second.
Queue A will redirect most requests to a static page (cached).

Queue B

Queue B is a single machine, to avoid distributed problems. It takes in small amount of requests and decides results (eg. redirect to payment page).
Now, why do we need 2 queues, not 1?
Think about a hospital. Queue A is the hospital lobby and security guard, and Queue B is the queue of patience.

How to reduce traffic

  1. no image
  2. cache more static pages
  3. reverse proxy: batch sending the requests
Also, can use front-end javascript to prevent clicking. There are method to bypass, so we need to check IP or do some filtering.

How to keep it simple?

  1. no DB: basic logic. But rmb to use a log file
  2. no lock

How to improve stability

  1. use new server to do Miao Sha, in case of crash
  2. asyc prcossing everything! Don't let other people wait, in case of crash.

How to defend hackers

  1. IP address (to defend auto softwares), but it's easy for hackers to fake IP address
  2. CAPTCHA
CAPTCHA (an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart") is a type of challenge-response test used in computing to determine whether or not the user is human.

follow-up

How to design 12306 (support several million QPS)

Read full article from [NineChap System Design] Class 3.2: Web Service - Shuatiblog.com

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts