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 master | cache as master | |
|---|---|---|
| async | cache aside, read though | write back, write behind |
| sync | write 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
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)
- load balancer

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