Chapter 1: Basics of System Design Flashcards
How to give a SD interview
- Comprehend scope of the question.
- Never assume things that are not explicitly stated
- Gather all the requirements
- You are required to scale breadth and depth of a blurred problem.
- The solution is not the goal of the interview, it is the conversation, which should cover breadth as well as depth.
- Break problem into modules and tackle modules individually.
- There are no solutions to many system design problems, its about making progress.
- Summary
a. Scoping the problem.
b. Sketching up and abstract design
c. Identify and address bottlenecks.
System Design basics
- What are different architectural pieces that can be used
- How do these pieces work with each other?
- How can we best utilize these pieces and what are the tradeoffs?
Load balancing
Can be added at three layers
- Between user and webserver
- Between webserver and appserver
- Between appserver and database.
- Smart client
- Hardware load balancer.
- Software load balancer
Caching
- Appserver cache
- Distributed cache (consistent hashing)
- Global cache
- Content distribution network
- Cache invalidation
a. Write through
b. Write around
c. Write back - Eviction policies
a. FIFO
b. LIFO
c. LRU
d. LFU
e. Random
Sharding or Data Partitioning
Methods:
1. Horizontal - different rows on different machines. Also called range based partitioning. Unbalanced - side effect.
2. Vertical partitioning
3. Directory based partitioning - There will be a directory server which will handle redirecting queries to the server.
Criteria:
1. Key or hash based partitioning.
2. List partitioning - based on group like region
3. Round robin.
4. Composite of above
Common problems of sharding
- Joins and Denormalization
- Referential integrity.
- Rebalancing
Indexes
Nothing much
Proxy
Proxy sits between user and app server and can cache data.
Queues
For large scale processing
Redundancy and Replication
Shared nothing architecture
SQL vs NoSQL
SQL:
Relational databases, mysql, oracle etc.
NoSQL:
Key - Value stores, document databases, Wide-Column databases, graph databases
CAP Theorem
Consistency, Availability and partition all three cannot be achieved.
Consistent Hashing
Having n hash servers and dividing data with hash % n will not scale. Some servers become hot and others cold depending on data.
ConsistentHashing:
/Users/manish/Downloads/687474703a2f2f626c6f672e636f64696e676c6162732e6f72672f75706c6f6164732f70696374757265732f636f6e73697374656e742d68617368696e672f362e706e67.png
Long-Polling Vs WebSockets vs Server sent events
- Ajax
- Long polling:
Server may not immediately respond.
Client may immediately re request, so that server will be able to push as soon as it has data. This is sometimes called hanging get.
Websockets
Provides full duplex communication over TCP.
Server side events
Only server to client communication.