System Design Flashcards
What is a semaphore
Non negative integer variable shared between various threads
It has 2 operations: Wait & Signal
Used to control access to a common resource by multiple processes or threads
What is a mutex
MUTUAL EXCLUSION
Specific kind of semaphore that is a locking mechanism
Used to ensure exclusive access to a single resource
What is a thread
Smallest sequence of a programmed instruction that can be managed by a single scheduler
It is an execution unit that contains a thread ID, program counter, register set and track
Types of load balancer
Hardware v software
Layer 4 v Layer 7
4: operates on transport layer, routes traffic based on IP address and port
7: operates on app layer and routes based on content such as URL paths or HTTP headers
Examples of load balancing algorithms
Round robin: distributed requests sequentially among servers
Least connections
IP hash: determines server based on IP address of client
Weighted algos: distributes to servers based on weights
What is a scheduler? Give examples of types?
System component responsible for managing the execution of processes and threads
Job scheduler: long term
CPU scheduler: short term
Medium term
Scheduling algos
First come first served
Shortest Job next
Prioritising
Round robin
Multilevel: split into sub queues with own algo
Multilevel feedback: same as multilevel but allows movement between sub queues based on some criteria
What is a RESTful API
Representational state transfer
Uses stateless communication
Client server architecture
Cacheable responses
Uniform interfaces
Layered system: client (usually) can’t tell if is communicating with end server
Alternatives to RESTful
SOAP (simple object access protocol)
GRAPH QL
gRPC Google Remote Procedure Calls
OData open data protocol
Why use RESTful
Simplicity: uses standard HTTP methods
Scalability: stateless nature allows
Flexibility: can handle many type of calls and return in many formats
Portability: can access from any environment that supports HTTP
Low bandwidth
Steps to make RESTful
- Define resources
- Define HTTP methods
POST GET DELETE - use status codes to represent outcome of posts
- Ensure statelessness
Each call should contain all necessary info without relying on stored user info - Implement error handling
- Documentation
- Versioning
- Security considerations
Implement rate limiting, input validation, maybe auth - Response formatting
Use JSON
Problems of SOAP
Simple object access protocol
Complexity, heavier than RESTful
Verbosity
Flexibility, often protocol dependent
Limitations of GraphQL
Client side caching can be difficult due to query complexity
File upload is more complex
More overheads (processing time)
Steeper learning curve
Limitations of gRPC
Google remote procedure calls
Its use of HTTP/2 is not natively support in all web browsers
Readability: uses binary format (which is efficient)
Complexity
Ecosystem: not as mature but is growing
Steps for designing a system
1) scope problem: determine parameters
2) determine assumptions: q&a
3) draw out simplified system
4) identify issues
5) redesign
Designing algorithms the scale
1) ask lots of Qs to determine limitations
2) ‘make believe’ begin at simple one machine
3) now determine difference between this and question
4) what are issues in reaching scaled system
5) adjust and iterate
Horizontal versus vertical scaling
Vertical: increasing resources to a specific node, eg; additional memory to server
Horizontal: increasing # of nodes
Vertical is generally easier but is limited, eg; you can only add so much memory to a server
Denormalization
Adding redundant information into a database to speed up reads
Alternative to joining tables using SQL
Sharding
Database partitioning
Vertical: effectively partitioning by feature, eg: for social network separate profiles out into tables that have image_url, friends, posts
Hash based: uses an ID to partition. Issue: relocation of all data may be required when scaling
Directory based: lookup table
Cache
In memory cache can deliver rapid results
Key value pairing
Sits between app layer and data source
When to use async processes
If a process is slow, you don’t want the whole system being held up by it
Networking metrics
Bandwidth: maximum amount of date per unit of time
Throughput: actual amount of data transferred
Latency