jiaqing-tee / starquest_jq

0 stars 0 forks source link

System design training #1

Closed jiaqing-tee closed 4 months ago

jiaqing-tee commented 5 months ago

System Design Training

Playlist: https://www.youtube.com/playlist?list=PLLTou95Km32QtvOHQpZqOggppmXN-qj2Z

jiaqing-tee commented 4 months ago

Latency Numbers

Title: Latency Numbers Programmer Should Know: Crash Course System Design #1 Link: https://youtu.be/FqR5vESuKe0

Takeaways:

Latency numbers for 2020s

jiaqing-tee commented 4 months ago

CI / CD

Title: CI/CD In 5 Minutes | Is It Worth The Hassle: Crash Course System Design #2 Link: https://youtu.be/42UP1fxi2SY

Takeaways:

Continuous Integration (CI)

Continuous Deployment (CD)

jiaqing-tee commented 4 months ago

REST API

Title: What Is REST API? Examples And How To Use It: Crash Course System Design #3 Link: https://youtu.be/-mN3VyJuCjM

Takeaways:

jiaqing-tee commented 4 months ago

DNS

Title: Everything You Need to Know About DNS: Crash Course System Design #4 Link: https://youtu.be/27r4Bzuj5NQ

Takeaways:

DNS query flow

DNS propagation

DNS propagation slow due to TTL on DNS record

Solution:
  1. Shorten TTL (to maybe 60s) --> Allow actual record update to take effect
  2. Leave server running on old IP address for a while. Decommission after traffic dies down.
jiaqing-tee commented 4 months ago

Memory & Storage Systems

Title: 10+ Key Memory & Storage Systems: Crash Course System Design #5 Link: https://youtu.be/lX4CrbXMsNQ

Takeaways

Memory
RAM
Storage
jiaqing-tee commented 4 months ago

Password Storage in Database

Title: System Design: How to store passwords in the database? Link: https://youtu.be/zt8Cocdy15c

Takeaways

image

jiaqing-tee commented 4 months ago

Full-Stack "Burger"

Title: The Most Beloved Burger for Developers Link: https://youtu.be/7swoLEqABhQ

Takeaways

image image image

Scenario: Early stage startup, limited resources, prefer maximum flexibility

jiaqing-tee commented 4 months ago

Open Systems Interconnect (OSI) Model

Title: What is OSI Model | Real World Examples Link: https://youtu.be/0y6FtKsg6J4

Takeaways

  1. Physical layer: Transmit raw bits of data across physical connection
  2. Data link layer: Organize raw bits into frames and ensure frames are delivered to correct destination
  3. Network layer: Route data frames across different networks
  4. Transport layer: Handles end-to-end communication between 2 nodes
  5. Session layer
  6. Presentation layer
  7. Application layer
jiaqing-tee commented 4 months ago

CAP Theorem

Title: CAP Theorem Simplified Link: https://youtu.be/BHqjEjzAicA

Takeaways

Concept in computer science explaining trade-offs between:

image

jiaqing-tee commented 4 months ago

Microservices

Title: What Are Microservices Really All About? (And When Not To Use It) Link: https://youtu.be/lTAcCNbJ7KE

Takeaways

API gateway

Communication

When to use microservices

jiaqing-tee commented 4 months ago

Proxy & Reverse Proxy

Title: Proxy vs Reverse Proxy (Real-world Examples) Link: https://youtu.be/4NB0NDtOwIQ

Takeaway

Forward proxy

Reverse proxy

jiaqing-tee commented 4 months ago

API Gateway

Title: What is API Gateway? Link: https://youtu.be/6ULyxuHKxg8

Takeaways

Functions

API gateway flow

  1. HTTP request: Client sends a request to API gateway
  2. Parameter validation: Validates HTTP request
  3. Allow-list / Deny-list: Checks caller's IP address and other HTTP headers, and perform basic rate limit checks against IP address and HTTP headers
  4. Authentication / Authorization: Passes request to identity provider for authentication and authorization
  5. Rate limit: Higher level rate-limit applied against authenticated session. Over limit, request rejected
  6. Dynamic routing
  7. Service discovery: Locates appropriate backend service and handle request path matching
  8. Protocol Conversion: Transform request into appropriate protocol and sends transformed request to backend service (eg protocol: gRPC)
jiaqing-tee commented 4 months ago

Cloud Native

Title: But What Is Cloud Native Really All About? Link: https://youtu.be/p-88GN1WVs8

Takeaways

Cloud computing

Cloud Native Pillars

  1. Microservices: Break functionalities of large application into smaller microservices
  2. Containers: Applications packaged in containers. Container orchestration platform: Kubernetes - Oversees and controls where container run, detect and repair failures, and balances load between microservices
  3. DevOps: Emphasizes collaboration, communication, and automation between development and operations team to deliver cloud-native applications quickly and reliably. Use CI/CD to automate software development and deployment process
  4. Cloud Native Open Standards: Critical components standardized and best practices are widely available.
jiaqing-tee commented 4 months ago

Content Delivery Network (CDN)

Title: What Is A CDN? How Does It Work? Link: https://youtu.be/RI9np1LWzqw

Takeaway

2 Common CDN technologies to direct request

  1. DNS-based routing
    • PoP has its own IP address
    • When user look up IP address for CDN, DNS returns closest PoP IP address
  2. Anycast
    • All PoPs share same IP address
    • Request comes into Anycast network, network sends request to PoP closest to requester

CDN Benefits

  1. Security
    • Provide effective DDoS protection
    • Effective with CDN built on Anycast network: Diffuse attack traffic over huge number of servers
  2. Availability
    • By nature, highly distributed
    • Able to withstand hardware failures
jiaqing-tee commented 4 months ago

Cache Systems

Title: Cache Systems Every Developer Should Know Link: https://youtu.be/dGAgxozNWFE

Takeaways

Computer hardware cache

  1. L1 cache
    • Smallest and fastest cache, typically integrated into CPU
    • Stores frequently accessed data and instructions
  2. L2 cache
    • Larger but slower than L1 cache
    • Typically located on CPU or separate chip
  3. L3 cache
    • Larger and slower than L2 cache
    • Often shared between multiple CPU cores
  4. Translation Lookaside Buffer (TLB)
    • Stores recently used virtual to physical address translations
    • Used by CPU to translate virtual memory addresses to physical memory address
    • Reduce time needed to access data from memory

Operating System

  1. Page cache
    • Managed by OS and resides in main memory
    • Store recently used disk blocks in memory
  2. Inode cache
    • Used to speed up file system operations by reducing disk accesses required to access files and directories
  3. File system cache

image

jiaqing-tee commented 4 months ago

Redis

Title: Top 5 Redis Use Cases Link: https://youtu.be/a4yX7RUgTxI

Takeaway

Use Cases for Redis

  1. Cache

    • Cache object to speed up web applications
    • Stores frequently request data in memory
    • Allow web servers to return frequently accessed data quickly
    • Reduce load on database and improves response time for application
    • Cache is distributed among cluster of Redis servers
    • Sharding: Common technique to distribute caching load evenly across cluster
    • When using Redis as distributed cache, to consider
      • Setting correct TTL
      • Handling thundering herd on cold start
  2. Session

    • Share session data among stateless servers
    • Session data stored in Redis with unique session ID returned to client as cookie
    • When request is made, session ID is included in the request
    • Stateless web server retrieves session data from Redis using session ID
    • Session data stored will be lost if Redis server restarts!
    • Redis provides persistence options like snapshots and Append-Only File (AOF) which allow session data to be saved to disk and reloaded into memory in the event of restart
    • Such options takes too long to load on restart to be practical
    • As such, replication is used instead
    • Data replicated to a backup instance
    • Event of crash of main instance, backup is used to take over traffic
  3. Distributed Lock

    • Used when multiple nodes in application need to coordinate access to shared resource
    • Used with its atomic commands like SET if Not eXists (SETNX): allow caller to set key if not exist
    • Simple implementation but not completely fault tolerant
    • Many production libraries providing high quality distributed lock implementation
    • Eg
      • Client 1 tries to acquire lock by setting key with unique value and time out using SETNX command
      • If key not set
        • SETNX command returns 1: indicates lock acquired by Client 1
        • Client 1 finishes work and release lock by deleting key
      • If key set
        • SETNX command returns 0: indicates lock is held by another client
        • Client 1 waits and retries the SETNX operations until lock is released by other client
  4. Rate Limiter

    • By using increment command on some counters and setting expiration times on those counters
    • Basic rate limiting algorithm
      • Request IP or user ID used as key
      • Number of request for key incremented using INCR command in Redis
      • Current count compared to allowed rate limit
        • Count within rate limit: Request processed
        • Count not within rate limit: Request rejected
      • Keys set to expire after specific time window which resets the counts for next time window
      • Sophisticated rate limiters like leaky bucket algorithm can be implemented
  5. Rank / Leaderboard

    • Fundamental data structure: Sorted sets - collection of unique elements with score associated and elements are sorted by score
    • Allows for quick retrieval of elements by score in logarithmic time
jiaqing-tee commented 4 months ago

Kafka

Title: System Design: Why is Kafka fast? Link: https://youtu.be/UNUz1-msbOM

Takeaways

Design Decision for Kafka Performance

  1. Sequential I/O
    • Misconception: Disk access is slow compared to memory access. Its dependent on data access patterns
    • Access patterns
      1. Random
        • HDD takes time to physically move arm to different location of magnetic disk - makes access slow
      2. Sequential
        • SSD doesn't need arm to jump - much faster to read and write blocks of data
    • Uses sequential access pattern: Append-Only Log as primary data structure
    • Append-only log adds new data to end of file
  2. Zero copy principle
    • Move lots of data, important to eliminate excess copy when moving pages of data between disk and network
    • Uses zero copy principle
    • Modern unix OS are optimized to transfer data from disk to network without copying data excessively
    • Zero copy not used
      1. Data loaded from disk to OS cache
      2. Data copied from OS cache into Kafka application
      3. Data copied from Kafka to socket buffer
      4. Data copied from socket buffer to Network Interface Card (NIC) buffer
      5. Data is sent over network to consumer
        • Uses 4 copies and 2 system calls
    • Zero copy used
      1. Data loaded from disk to OS cache
      2. Kafka use system call sendfile() to tell OS to directly copy data from OS cache to NIC buffer
        • Only copy from OS cache into NIC buffer
        • Modern NIC copy with Direct Memory Access (DMA)
        • When DMA used, CPU not involved --> more efficient
jiaqing-tee commented 4 months ago

GraphQL

Title: What Is GraphQL? REST vs. GraphQL Link: https://youtu.be/yWzKJPw_VzM

Takeaways

GraphQL vs REST

GraphQL Drawbacks

jiaqing-tee commented 4 months ago

Single Sign-on (SSO)

Title: What Is Single Sign-on (SSO)? How It Works Link: https://youtu.be/O1cRJWYF-g4?list=PLLTou95Km32QtvOHQpZqOggppmXN-qj2Z

Takeaway

2 Common Protocols

  1. Security Assertion Markup Language (SAML)
    • XML based open stand for exchanging identity information between services
  2. OpenID Connect
    • Uses JSON Web Token (JWT) to share identity information between services

SSO Login Flow (SAML)

  1. Service provider server detects user is from work domain, returns a SAML authentication request to the browser
  2. Browser redirects user to Identity Provider specified in SAML authentication request
  3. Identity provider shows login page for users to enter login credentials
  4. After authentication, identity provider generates a SAML response to return to browser AKA SAML assertion - cryptograhically signed XML document containing information about user and what user can access
  5. Browser forwards signed SAML assertion to service provider
  6. Service provider validates assertion is signed by identity provider (done using public key cryptography)
  7. Service provider returns the protected resource to browser based on what user is allowed to access specified in SAML assertion

Attempts to access another service provider

  1. New service provider server detects user is from work domain, returns a SAML authentication request to the browser
  2. Browser redirects user to Identity Provider specified in SAML authentication request
  3. Since user already logged in with Identity Provider, skips login process
  4. Generate SAML assertion for new service provider indicating what user can access
  5. SAML assertion returned to browser and forwarded to new service provider
  6. New service provider validates the signed assertion and grants user access accordingly

OpenID Flow

Which Protocols to use?

jiaqing-tee commented 4 months ago

SQL Queries Optimization

Title: Secret To Optimizing SQL Queries - Understand The SQL Execution Order Link: https://youtu.be/BHwzDmr6d7s

Takeaways