System Design: How to scale a server| 青训营

87 阅读1分钟

Single server setup

Request Flow

image.png

  1. Users access websites through domain names, such as api.mysite.com. Usually, the Domain Name System (DNS) is a paid service provided by 3rd parties and not hosted by our servers.
  2. Internet Protocol (IP) address is returned to the browser or mobile app. In the example, IP address 15.125.23.214 is returned.
  3. Once the IP address is obtained, Hypertext Transfer Protocol (HTTP) [1] requests are sent directly to your web server.
  4. The web server returns HTML pages or JSON response for rendering.

Trafic source

  • Web
  • Mobile

Database

RDBMS: Relational Database Management System
  • Relational
  • Non-Relational

Scaling

Vertical Scaling

Adding more CPU, RAM, etc.

#horizontal-scale

add servers

Load Balancing

In previous step 3, might routed to different private IP address.

Database Replication

A master database generally only supports write operations.

  • All the data-modifying commands like insert, delete, or update must be sent to the master database. A slave database gets copies of the data from the master database and only supports read operations.

image.png

Cache

Cache used to store result of existing responses. Use Cache when:

  • data frequently read but not modify
  • expiration policy is set
  • data is consistent
  • try to avoid #SPOF A single point of failure (SPOF) is a part of a system that, if it fails, will stop the entire system from working”

CDN

Content Delivery Network

def

Dispersed servers used to deliver static content image.png

Considerations

  • Cost
  • Cache Expire
  • What to do when CDN outage
  • Invalidating files

Update

image.png

Stateless web tier

#horizontal-scale A stateful server remembers client data (state) from one request to the next. A stateless server keeps no state information

image.png

Message Queue

A message queue is a durable component, stored in memory, that supports asynchronous communication

Architecture

Producers/Publishers

Create messages and publish them to a queue.

Consumers/Subscribers

connect to the queue and perform actions defined by messages. image.png

Database Scaling

image.png

#horizontal-scale Sharding

Def

Sharding separates large databases into smaller, more easily managed parts called shards. Each shard shares the same schema, though the actual data on each shard is unique to the shard.

Factors

Most important: the choice of the sharding key.

Sharding key (known as a partition key) consists of one or more columns that determine how data is distributed.

Challenges

  1. Resharding: when data is exceed shard length
  2. Celebrity Problem: some data would be read overwhelmed times than normal data
  3. Hard to perform join operations across database shards. image.png