Comprehensive Guide to System Design

Jul 31, 2024

System Design Tutorial Notes

Overview

  • Key topics: scalability, reliability, data handling, high-level architecture
  • Focus: system design interviews, core concepts, practical strategies
  • Importance: not about coding but about integrating an entire system

Computer Architecture Basics

  • Binary & Data Units: Bits (0 or 1), Bytes (8 bits), KB, MB, GB, TB
  • Disk Storage: HDD vs SSD, non-volatile, stores OS, applications, user files
    • HDD: 80-160 MBps
    • SSD: 500-3500 MBps
  • RAM: Volatile, stores active data, faster than SSD
    • Speed: >5000 MBps
    • Size: GB in consumer devices, hundreds of GB in servers
  • Cache: Smaller, faster access than RAM, used for frequently accessed data
    • L1, L2, L3 cache levels
    • Access time in nanoseconds
  • CPU: Processes code, fetches/decodes/executes instructions, interacts with RAM, disk, cache
  • Motherboard: Connects all components, allows data flow

High-Level Architecture of Production-Ready App

  • CI/CD Pipeline: Automates deployment, tools like Jenkins, GitHub Actions
  • Load Balancers & Reverse Proxies: Manage user requests, ensure even distribution, e.g., NGINX
  • External Storage Server: Keeps data separate from production server
  • Logging & Monitoring: Tools like PM2 (backend), Sentry (frontend)
  • Alerting Services: Detect anomalies, send notifications, e.g., via Slack
  • Debugging: Use logs, replicate issue in test environment, hotfixes

System Design Principles

  • Good Design Principles: Scalability, Maintainability, Efficiency
  • Robust & Resilient: Plan for failures, ensure performance even during issues
  • Key Elements: Moving data, storing data, transforming data

CAP Theorem (Brewer's Theorem)

  • Consistency: All nodes have same data
  • Availability: System is always operational
  • Partition Tolerance: System functions despite network partitions
  • Trade-offs: Only 2 of 3 properties can be achieved simultaneously

Measuring System Performance

  • Availability: Measured in uptime/downtime, aim for 99.999%
  • Reliability & Fault Tolerance: Ensure system works correctly and handles failures
  • Throughput & Latency: Measures data handling over time vs. time to complete single request

Networking Basics

  • IP Addressing: IPv4 (32-bit), IPv6 (128-bit)
  • Data Packets: Include IP headers, application layer data
  • Transport Layer Protocols: TCP (reliable), UDP (faster but less reliable)
  • DNS (Domain Name System): Translates domain names to IP addresses
  • Networking Infrastructure: Public vs. private IPs, static vs. dynamic IPs
  • Ports: Identify specific processes/services

Application Layer Protocols

  • HTTP: Stateless, request-response, status codes (200, 300, 400, 500)
  • WebSockets: Two-way communication
  • SMTP, IMAP, POP3: Email transmission and retrieval
  • FTP, SSH: File transfer, secure remote operations
  • WebRTC, MQTT, AMQP: Real-time communication, messaging
  • RPC: Remote procedure calls for code execution on remote machines

API Design

  • CRUD Operations: Create (POST), Read (GET), Update (PUT/PATCH), Delete (DELETE)
  • Protocols: HTTP, WebSockets, JSON, XML, Protocol Buffers
  • APIs Paradigms: REST, GraphQL, gRPC
  • Versioning, Rate Limiting, CORS: Maintain backward compatibility, prevent misuse

Caching & CDNs

  • Browser Caching: Stores website resources locally
  • Server Caching: Reduces database queries
  • Database Caching: Improves read-heavy applications
  • CDNs: Serve static content from geographically distributed servers

Proxy Servers

  • Types: Forward, Reverse, Open, Transparent, Anonymous, Distorting, High Anonymity
  • Use Cases: Caching, load balancing, security, anonymizing
  • Load Balancing Strategies: Round-robin, least connections, weighted, geographical, consistent hashing

Database Essentials

  • Types: Relational (SQL), NoSQL, In-Memory
  • Scaling: Vertical (scale-up), Horizontal (sharding, replication)
  • Performance Techniques: Caching, indexing, query optimization
  • CAP Theorem: Prioritize two out of consistency, availability, partition tolerance