System Design Interview Guide: Volume 1
System Design Interview Guide: Volume 1
OAuth provides user authentication and delegated access via tokens, ideal for scenarios involving authorization with third parties . JWT provides stateless, compact tokens that ensure quick authentication and are beneficial for high-traffic applications due to reduced server load. While OAuth excels in user consent scenarios, JWT's simplicity benefits high throughput environments, each offering unique security advantages based on application requirements .
CAP theorem states that a distributed database can only provide two out of the three properties: Consistency, Availability, and Partition tolerance. Practically, designers must prioritize based on application needs: choosing consistency and partition tolerance may cause downtime during network failures (common in bank systems), whereas availability and partition tolerance prioritize service continuity with eventual consistency, suitable for social media platforms .
Challenges include handling massive user volume, real-time updates, and personalized content delivery. Strategies include deploying efficient caching to reduce database load, using machine learning for personalized content ranking, and ensuring horizontal scalability through sharding and distributed storage systems . Edge computing can minimize latency for geographically dispersed users .
Horizontal scaling involves adding more machines or servers to the pool of resources, which can distribute the load and provide high availability. It is beneficial for systems expecting rapid growth in traffic or requiring redundancy. Vertical scaling involves adding more power (CPU, RAM) to an existing machine and is useful when increasing performance for a particular server without configurational changes .
SQL databases are preferable for applications requiring structured data and ACID compliance, ensuring reliable transactions and consistency . NoSQL databases are suited for unstructured data, large scale, and scenarios prioritizing availability over strict consistency (BASE properties). System design choices are affected as SQL requires careful schema design, while NoSQL offers flexibility in data models, impacting how data is accessed and stored .
GraphQL offers flexibility by allowing clients to specify exactly what data they need, reducing over-fetching . It is beneficial in applications requiring multiple versions of APIs or personalized data fetching. GraphQL's ability to aggregate data from several sources in a single request is advantageous in complex data scenarios or microservices environments .
Sharding involves splitting a database table into smaller, more manageable pieces called shards, each stored on a different database server. It enhances scalability by distributing data and load across multiple nodes . Partitioning divides data within a single database server based on a certain criteria. It allows efficient query handling within one server but does not distribute load across servers, limiting its scalability benefits compared to sharding .
Leader election protocols such as Raft and Paxos ensure consistency in distributed systems by designating a leader node to manage the state updates. Raft simplifies consensus by using a leader for client interaction to ensure a consistent state across nodes . Paxos allows nodes to propose values with a consensus on one value adding robustness but complexity in implementation . Consistency is maintained as all updates are sequential and agreed upon by nodes .
A CDN improves web application performance by caching content at edge locations closer to users, reducing latency and server load . The trade-offs include increased costs and complexity in managing multiple caching nodes. Additionally, CDNs can introduce caching issues such as stale data if not properly configured .
Caching reduces load times and server requests by storing copies of frequently accessed data. Write-through caching synchronously writes data to the cache and database, ensuring consistency but higher latency . Write-back caching improves performance with quicker writes to cache, but risks data loss on failures since the database can lag. Write-around caching avoids caching write operations, which can reduce unnecessary cache fills but lead to cache misses for newly written data .