1. Home
  2. Explore
  3. Engineering
  4. System Design Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

System Design Course

System Design: Comprehensive Study Notes

Introduction to System Design

System design involves creating reliable, scalable, and maintainable systems that handle large-scale distributed requirements. This course introduces basic engineering design patterns used to build large-scale distributed systems.

  • A large-scale distributed system handles massive data volumes, high user counts, frequent updates, and strict performance expectations. Examples include Google Maps, YouTube, and Netflix.

  • Distributed means the system's components (servers, databases) are spread across multiple geographical locations to provide fault tolerance and performance.

  • Design patterns are reusable solutions to common engineering problems. For instance, the publisher-subscriber model solves the problem of one celebrity's post needing to be distributed to millions of followers without overwhelming the server.

"Design patterns are particular practices, principles, or processes which are used by engineers to build these systems."

Engineers convert business requirements into technical solutions using these patterns, ensuring the system is reliable, scalable, and maintainable.


The System Design Process

Requirements Definition

The first step is defining requirements from the user's perspective. Engineers often work from a Product Requirement Document (PRD). The most important features are identified first. For a live streaming system, core requirements include:

  • Streaming video to millions of users without failure.

  • Supporting comments, reactions, and advertisements.

  • Handling variable bandwidth and multiple device types.

Data Definitions

Abstract features are reduced to concrete data definitions. For example, a "like" is represented as an object with fields: like_id, user_id, timestamp, and comment_id. These definitions map to database tables.

API Endpoints

Once data is defined, APIs (Application Programming Interfaces) are specified to allow clients to query and manipulate the data. APIs have well-defined signatures and are accessed via network protocols. Example API for video streaming:

  • getVideoFrame(videoId, deviceType, offset) → returns a frame (10 seconds of video).

  • postComment(videoId, userId, text) → adds a comment.

Engineering Requirements

  • Fault tolerance: Multiple servers across regions prevent single points of failure. Data duplication or partitioning ensures continuity.

  • Extensibility: The system should be easy to modify as requirements change. Loosely coupled code avoids major redesigns.

  • Testing: The design must be tested with common and edge cases before implementation. Load testing and capacity estimation validate feasibility.


High-Level Design of a Live Streaming System

Client-Server Interaction

Clients (mobile, laptop, TV) query the server in real time. The server exposes APIs, and the client handles UI. The backend is the core of system design.

  • Stateless protocol (e.g., HTTP): Every request contains all necessary information. The server does not store client context, making it easy to add new servers and recover from crashes.

  • Stateful protocol: The server maintains context (e.g., the next video segment to send). This simplifies client code but makes the server complex and prone to state loss.

For comments, HTTP (stateless) is sufficient. For video streaming, protocols like WebRTC (peer-to-peer, low latency) are more suitable, but considerations of reliability vs. speed lead to choices like TCP (reliable) or UDP (fast, loss-tolerant). In live streaming, MPEG-DASH and HLS (HTTP Live Streaming) are adaptive protocols that adjust video quality based on bandwidth.

Database Considerations

  • Video data (large files) is best stored in file systems like HDFS or cloud storage (Amazon S3) due to low cost and high scalability.

  • Comment and user data (structured, relational) can use SQL databases (MySQL, PostgreSQL) for complex queries, but for high scalability and flexible schema, NoSQL databases (key-value stores) may be preferred.

Video Transformation

Raw video from the source (e.g., 8K camera) must be converted to multiple resolutions (4K, 1080p, 720p, 480p, 144p) and formats (H.264, proprietary). This is achieved using a map-reduce pattern:

  1. Raw video is split into 10-second segments.

  2. Each segment is sent to a pool of servers that transform it into various resolution/format combinations.

  3. The output is stored in the database/file system for distribution.

Caching and CDNs

  • Caching: Frequently accessed video segments (e.g., last 10 minutes) are stored on the server to reduce database load and latency.

  • Content Delivery Networks (CDNs) distribute static content (webpages, video) across geographically distributed nodes. Clients fetch data from the nearest CDN edge, improving performance. Authentication challenges arise; the CDN may handle auth or custom code must be deployed.


Low-Level Design

Low-level design focuses on specific functions of the system, going into depth on code and interactions while losing breadth. For the live streaming system, the core functionality is user video consumption.

Use Case Diagrams

Identify actors and use cases. For a video streaming service:

  • Actor: Customer

  • Use cases:

    • Play video from timestamp

    • Resume from last watched position

    • Automatically select best quality based on network/device

    • Nonstop play (buffering)

Other actors (admin, videographer) are omitted for focus.

Class Diagrams

Classes represent objects with state (data) and behavior (methods). Key classes:

  • Video: id, frames (ordered array), metadata. Behavior: getFrame(timestamp) returns the frame for that time.

  • User: id, name, email. Behavior: getId().

  • WatchedVideo: id, videoId, userId, seekTimestamp. Behavior: getSeekTime().

  • VideoConsumingService: exposes APIs getSeekTime(userId, videoId) and getFrame(videoId, timestamp) using a file system or database.

Sequence Diagrams

Diagrams show the order of interactions. For playing a video:

  1. User sends getSeekTime(userId, videoId) to the service.

  2. Service returns the timestamp where the user left off.

  3. User then sends getFrame(videoId, timestamp) (or a play request).

  4. Service fetches the frame from the file system/video object and returns it.

  5. This repeats continuously to stream the video.

Optimization: Place authentication directly in the video service to reduce network calls.

Coding Implementation

Using the diagrams as blueprints, code is written in an object-oriented language (e.g., Java). The Video class iterates over frames to find the correct one based on timestamp. Magic constants (like frame duration) should be avoided; instead, make frame duration a property of each Frame object. This flexibility sets apart good engineering.

  • WatchVideo class stores seek timestamp and provides methods to retrieve it.

  • VideoConsumingService uses a file system (e.g., S3) to load videos and call getFrame on the Video object.

Code is simplified and reusable because behaviors are abstracted into objects.


Recap and Key Takeaways

  • System design starts with requirements that are converted into data definitions, APIs, and engineering considerations (fault tolerance, extensibility, testing).

  • High-level design defines the system's components (server, databases, protocols, CDNs) and their interactions.

  • Low-level design drills into specific features using use case diagrams, class diagrams, sequence diagrams, and code.

  • Choosing the right network protocol and database depends on the trade-offs between reliability, latency, cost, and scalability.

  • Design patterns like map-reduce and publisher-subscriber provide proven solutions to common problems.

  • Reusing well-tested tools (AWS, Redis, CDNs) is often better than building custom solutions unless significant performance or cost benefits exist.

These principles enable engineers to build systems that are reliable, scalable, and maintainable for large-scale distributed environments.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent