System Design - web services

general approaches to design large web services

  • common
  • caching
  • scaling
  • service patterns
  • database patterns

bottleneck

  • database
    • optimize query
    • indexing
    • Data Sharding
    • separate db
  • cpu
    • async processing
    • optimize algorithm
    • distribute load(horizontal scaling)
  • storage
    • compress data
    • distributed File Storage
      • HDFS
      • AWS S3
      • Google Cloud Storage
  • network
    • cdn
    • use gRPC instead of RESTful api
    • compress data

caching

common caching systems:

  • memcached
  • redis (not support read though)
  • Hazelcast
  • Oracle Coherence
  • AWS DynamoDB Accelerator(DAX)

Cache Pattern

database as mastercache as master
asynccache aside, read thoughwrite back, write behind
syncwrite through

problems :

  • hot cache miss
    • sudden spike of requests for uncached data causes database overload
  • cache avalanche
    • simultaneous expiration of many cached items leads to database overload

solutions:

  • mutex lock : only one request can fetch data from database, others wait
  • prefetching : manually load data into cache before it is requested
  • staggered expiration : set add random to expiration times for each cached items

https://medium.com/@r06521601/%E5%88%86%E6%95%A3%E5%BC%8F%E5%BF%AB%E5%8F%96%E8%A8%AD%E8%A8%88-distributed-cache-design-f42b10e8af84

Scaling Approaches

  • vertical scaling
    • increase resources of a single server
  • horizontal scaling
    • load balancer
      • (round robin, least connections, ip hash)
      • nginx haproxy aws elb
    • database sharding(partitioning)

architecture patterns

  • Microservices Architecture
  • Event-Driven
  • Event Sourcing
  • CQRS
  • asynchronous processing(message queues, task queues)

database patterns

  • data sharding
    • Key Based Sharding
    • Range Based Sharding
    • vertical partitioning
    • horizontal partitioning
  • Replication
    • master-slave
    • master-master

fault tolerance

Disaster Recovery