Notes, 大型网站技术架构

Contents
  1. 1. Overview
    1. 1.1. Evolution of Large Site Architecture
      1. 1.1.1. Characteristics
      2. 1.1.2. Development Progress
      3. 1.1.3. Values
      4. 1.1.4. Wrong Thinking
    2. 1.2. Models of Large Site Architecture
      1. 1.2.1. Layer (horizontal)
      2. 1.2.2. Split (vertical)
      3. 1.2.3. Distributed
      4. 1.2.4. Cluster
      5. 1.2.5. Cache
      6. 1.2.6. Asynchronization
      7. 1.2.7. Redundancy
      8. 1.2.8. Automation
        1. 1.2.8.1. Security
      9. 1.2.9. Architecture Model of Weibo
    3. 1.3. Keys of Large Site Architecture
      1. 1.3.1. Performance
      2. 1.3.2. Availability
      3. 1.3.3. Scalability
      4. 1.3.4. Extensibility
      5. 1.3.5. Security
  2. 2. Architecture
    1. 2.1. High-performance Architecture
      1. 2.1.1. Different Views of Website Performance
      2. 2.1.2. Metrics of Performance
      3. 2.1.3. Ways of Profiling
      4. 2.1.4. Strategy of Performance Optimization
      5. 2.1.5. Web Front Performance Optimization
      6. 2.1.6. Application Server Performance Optimization
    2. 2.2. High-availability Architecture
      1. 2.2.1. Layered Architecture
      2. 2.2.2. High-availability Application
      3. 2.2.3. High-availability Service
      4. 2.2.4. High-availability Data
      5. 2.2.5. CAP Principe
      6. 2.2.6. Data Backup
      7. 2.2.7. Failure Transferring
      8. 2.2.8. Quality Insurance
    3. 2.3. High-scalability Architecture
      1. 2.3.1. Physical Separation
      2. 2.3.2. Load Balancing
      3. 2.3.3. Distributed Cache
      4. 2.3.4. Distributed Database
    4. 2.4. High-extensibility Architecture
    5. 2.5. Security Architecture
      1. 2.5.1. Website attack and defense
      2. 2.5.2. Encryption and Key Security Management
      3. 2.5.3. Infomation filtering and Anti-spam
      4. 2.5.4. Risk Control
  3. 3. Cases
    1. 3.1. Taobao
    2. 3.2. Wikipedia
    3. 3.3. Doris (enormous distributed KV storage)
    4. 3.4. Seckilling System
      1. 3.4.1. chanllenge
      2. 3.4.2. solution
      3. 3.4.3. architecture
    5. 3.5. Failure Analysis
      1. 3.5.1. disk space increases surprisingly
      2. 3.5.2. high load of database
      3. 3.5.3. timeout failure in high concurrency
      4. 3.5.4. high load of database caused by cache
      5. 3.5.5. application start out of synchronization
      6. 3.5.6. big files occupy the disk IO
      7. 3.5.7. abuse of releasing environments
      8. 3.5.8. non-standard releasing procedure
      9. 3.5.9. bad programming habits
  4. 4. Postscript

Overview

Evolution of Large Site Architecture

Characteristics
  • high concurrency, huge traffic
  • high availability
  • vast data
  • users from everywhere, complex situation of network
  • bad environment of security
  • quick demands changing, frequently releasing
  • progessive development
Development Progress
  • begin
  • separate application and data service
  • use cache
  • use application cluster
  • separate read and write of database
  • use reverse proxy and CDN
  • use distributed file system and distributed database
  • use NoSQL and search engine
  • separate bussiness
  • use distributed services
Values
  • key is to be flexible to the bussiness
  • major pusher is bussiness developing
Wrong Thinking
  • follow solutions of the gaints blindly
  • be technical just for techniques
  • try to solve all problems with techniques

Models of Large Site Architecture

Layer (horizontal)
  • application layer: be responsible for specific bussiness and view showing (view, bussiness logic)
  • service layer: provide service for application layer (data interfaces, logic processing)
  • data layer: provide service data accessing and storage
Split (vertical)

split different functionalites and services into aggregated and decoupled modules.

Distributed

distributedly deploy the layered and splitted modules in different servers.

  • distributed applications and services
  • distributed static resources
  • distributed data and storage
  • distributed computing
  • distributed configuration
  • distributed lock
  • distributed file system
Cluster

cluster the independent deployed server, i.e., many servers deployed the same application consists of a cluster.

Cache
  • CDN
  • reverse proxy
  • local cache in application server
  • distributed cache
Asynchronization

distributed message queue, is a typical producer-consumer model

  • improve system availability
  • speed up the response of website
  • reduce the peek of concurrent accessing
Redundancy
  • cold backup: storage archived in fixed period
  • hot backup: separate read and write of database, real-time synchronization
  • disaster recovery data center
Automation
  • automatic releasing

    • automatic source control
    • automatic testing
    • automatic security dection
    • automatic deployment
  • automatic monitoring

  • automatic alerting
  • automatic failure transferring
  • automatic failure recoverring
  • automatic downgrading
  • automatic resource allocating
Security
  • password
  • verification code
  • encryption
  • filtering
  • risk control
Architecture Model of Weibo

Keys of Large Site Architecture

Performance
  • response time
  • throughput
  • system performance monitor (top)

Availability
  • available time (99.99%)
  • redundancy
  • pre-released verification
  • gray releasing
Scalability

easy to add and remove servers in cluster

Extensibility
  • event driven architecture
  • distributed service
Security

Architecture

High-performance Architecture

Different Views of Website Performance
  • user

  • developer
    the performance of application itself and relevant subsystem.
    response latency, system throughput, concurrency, and system stability

  • maintainer
    infrastructure performance, resource utilization

Metrics of Performance
  • response time

  • number of concurrency
    number of total users >> number of online users >> number of concurrent users

  • throughput
    TPS(transaction per second), QPS(query per second), HPS(HTTP request per second)

  • system performance monitor (top)
    system load, number of objects and threads, memory and CPU used, disk and netword I/O
    top: there floating number, recent 1min, 10mins, 15mins average running processes

Ways of Profiling
  • performance testing
  • load testing
  • stress testing
  • stability testing

  • performance report
Strategy of Performance Optimization
  • performance analysis
  • performance optimization
Web Front Performance Optimization
  • browser

    • reduce the number of HTTP requests
    • browser cache
    • enable compression
    • put CSS at the front of page, and JS at the bottom
    • reduce the transferring of Cookie
  • CDN (Content Distribute Network)

  • reverse proxy

Application Server Performance Optimization

distributed cache

  • cache principle (80%-20% law)

  • use cache properly

    • infrequently modified (read:write ≥ 2:1)
    • hot piece
    • set expired time
  • cache availability

    • cache warm up
      preload the hot pieces
    • cache penetrating
      situation that requires to nonexistent data in high concurrency, one way is to cache it (nonexistent-null)

architecture of distributed cache

JBoss Cache: update synchronously (enterprise use)

Memcached: no communication between servers

  • communication protocol: TCP, UDP, HTTP
    communication serializating protocol: text(XML, JSON), binary(Google Protobuffer)

  • memcached use TCP for communication protocol, and it defines its own text serializating protocol.

  • memcached’s server communication module is based on Libevent.

  • memory management

    • chunk-based allocation
      find a minimal chunk that can save the data
    • LRU

asynchronizaton
use message queue to reduce the peek

cluster

code optimization

  • multi-thread
    number of threads = $\frac{task execution time}{task execution time-IO waiting time}\times CPU cores$
  • thread-safe
    stateless object, local object, lock
  • resource reusing
    singleton, object pool
  • data structure
    hashtable: originlal-MD5->info figureprint-HASH->hashcode
  • garbage collection
    object created in Eden-Young GC->From-Young GC->To-Young GC->From-…threshold times Young GC->Old->Full GC

storage performance optimization

  • mechanical hard disk vs. solid state hard drive
  • B+ tree vs. LSM tree
    N-branch search tree: at most 3 level, (maybe 5 disk IOs to update, 3 to get the index, 1 to read, 1 to write)

    N-level mergeable search tree: write operations do in memory, and create a new record in the $C_0$ tree

  • RAID vs. HDFS

High-availability Architecture

Layered Architecture

application layer <- service layer <- data layer
more complicated:

High-availability Application
  • failure transferring through load balancing
  • session managemant
    a session is a semi-permanent interactive information interchange, i.e., a dialogue
    • session copy
    • session binding
    • use cookie to record session
    • session server
High-availability Service
  • managed in priority
  • time-out setting
  • asynchronous call
  • downgrade-abled service
  • idempotent design
    i.e., repeated call can be handled properly.
High-availability Data
CAP Principe
  • consistency
  • availablity
  • partition tolerance

consistency

  • data strong consistency
    data is always consistent in all the physical copies
  • data user consistency
    data may be not consistent in all the physical copies, but it
    can be accessed a consistent and right data for user through
    error correction and verification.
  • data final consistency
    data may be not consistent in all the physical copies, and it
    may be not accessed consistently. but after some time, it can be
    corrected to user consistency.
Data Backup
Failure Transferring
  • failure confirmation
  • access transferring
  • data recovering
Quality Insurance

automatic releasing
(gray releasing)

automatic testing

pre-releasing verification

source control

  • master developing, branch releasing
  • master releasing, branch developing

website monitoring

  • user behavior log collection
    tools based on Storm (real-time computing framework)
  • server performance monitoring
  • running data report

website monitoring control

  • system alerting
  • failure transferring
  • automatic downgrading

High-scalability Architecture

Physical Separation

(clustering)

Load Balancing
  • HTTP redirection protocol
  • DNS (domain name resolution)
  • reverse proxy
  • IP
  • data link layer
    LVS (Linux Virtual Server)

load balancing algorithms

  • round robin
  • weighted round robin
  • random
  • least connections
  • source hashing
Distributed Cache

memchached access model

consistent hashing

to solve the influence of cache load => virtual nodes (a server to 150 nodes)

Distributed Database

spread tables into different database servers
put table into slices, then spread into different database servers

database products of data slices: Amoeba, Cobar


NoSQL

  • abandon 2 basis of relation database:
    SQL based on relation algebra,
    and transaction consistency guarantee [atomicity, consistency, isolation, durability] (ACID)

  • strengthen characteristics that large site concerned:
    high-availability, high-scalability

Apache HBase

High-extensibility Architecture

  • event driven architecture
  • distributed message queue
    Apache ActiveMQ
  • distributed services

  • web service and enterprise distributed service

  • distributed service framework
    large site need simple and efficient distributed service
    framework to build its service oriented architecture (SOA)
    it is said that Facebook manages its distributed service based on
    Thrift (an opensource remote service call framework)
    Alibaba-Dubbo

  • extensible data structure
    NoSQL ColumnFamily (first in Google Bigtable)

  • open platform to build website ecosystem

Security Architecture

Website attack and defense

XSS(Cross Site Script) attack

  • reflective type

  • persistent type

  • solution: filter, HttpOnly

Injection attack
SQL injection, OS injection

SQL injection: open source(table name is public), error echoed, blind injection

solution: filter, parameter binding

CSRF(Cross Site Request Forgety) attack

solution: form token, verification code, referer check

other attack

  • error echoed
  • HTML comment
  • file uploading
  • path traversal

web application firewall
ModSecurity

website security scanning

Encryption and Key Security Management
  • one-way hashing encryption
    MD5, SHA
    Rainbow Table to try to decrypt MD5

  • symmetric encryption
    DES, RC

  • asymmetric encryption
    RSA
    information security transmission, digital signature

  • key security management

Infomation filtering and Anti-spam
  • text matching
    double array trie, multi-level hashtable (simpler)

  • classification algorithm
    Native Bayes, TAN, Association Rule Clustering System (ARCS)

  • blacklist
    hashtable, bloomfilter

Risk Control
  • rule engine
  • statistics model

Cases

Taobao

At first, Ma Yun bought a C2C website, then LAMP:

MVC: decouple view and bussiness logic
ORM (Object-relational mapping): decouple objects and relational database

Taobao didn’t use the hot Struts and Hibernate,
but choose to develop its own MVC frameword Webx, and to use IBatis for ORM.
Taobao also used Weblogic for application server, Oracle for database. They are commercial softwares.

Then, to use Spring instead of EJB, free JBoss instead of Weblogic

At last, abandon Oracle, IBM, EMC, and back to open source MySQL and NoSQL

Wikipedia

based on LAMP

Wikipedia’s web front
the key architecture of is Squid cluster:

Wikipedia’s backend

  • cache the format that application can be used directly
  • cache servers store the session objects
  • memcached’s connection is cheap, and create one when needed
  • increase memory to improve MySQL
  • use RAIO0 to speed up disk accessing
  • set ACID of database at a some low level
  • if Master database sever crashed, switch to Slave,
    the close the write service, i.e., close the edition of users.

Doris (enormous distributed KV storage)









Seckilling System

chanllenge
  • strike for the current bussiness
  • high load of high concurrency
  • increasing bandwidth
  • the URL to place an order
solution
  • independently deploy seckilling system
  • static page for seckilling product
  • rent netword bandwidth for seckilling
  • dynamically generate random URL for placing order
architecture



Failure Analysis

disk space increases surprisingly

set log level to DEBUG by mistake.

high load of database

a SQL executes in the index page.

timeout failure in high concurrency

a singleton object need the unique lock to execute for a long time.

high load of database caused by cache

close the cache servers when releasing.

application start out of synchronization

Apache and JBoss start at the same time.
JBoss first, and curl to validate, then Apache.

big files occupy the disk IO

separate different sizes or types of files.

abuse of releasing environments

someone did performance testing in releasing environments.

non-standard releasing procedure

forget to uncomment some codes.
commit after diff checking.

bad programming habits

NullPointerException throws.
forget to check whether the object is null.

Postscript


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到