Home   dsa  

Design a system like netflix - System design interview

Designing a system like Netflix involves structuring a scalable architecture to handle video streaming, user management, content cataloging, and more. Below is a high-level system design using Prisma for database modeling, Express.js for API routes, and a recommended tech stack:

Tech Stack

System Components

  1. Client Applications (Web, Mobile, Smart TVs)

    • Various clients for accessing Netflix content.
  2. Express.js Backend

    • Prisma Models:

      // schema.prisma
      
      datasource db {
        provider = "postgresql"
        url      = env("DATABASE_URL")
      }
      
      generator client {
        provider = "prisma-client-js"
      }
      
      model User {
        id       Int      @id @default(autoincrement())
        username String   @unique
        email    String?  @unique
        password String
        profiles Profile[]
      }
      
      model Profile {
        id       Int      @id @default(autoincrement())
        userId   Int
        name     String
        age      Int?
        user     User     @relation(fields: [userId], references: [id])
      }
      
      model Movie {
        id          Int      @id @default(autoincrement())
        title       String
        description String
        duration    Int      // in seconds
        genres      String[]
        actors      String[]
        director    String
        releaseYear Int
      }
      
    • Express API Routes:

      // server.js
      
      const express = require('express');
      const { PrismaClient } = require('@prisma/client');
      const prisma = new PrismaClient();
      
      const app = express();
      app.use(express.json());
      
      // Endpoint for creating a user profile
      app.post('/users/:userId/profiles', async (req, res) => {
        const { userId } = req.params;
        const { name, age } = req.body;
        try {
          const profile = await prisma.profile.create({
            data: {
              userId: parseInt(userId),
              name,
              age,
            },
          });
          res.json(profile);
        } catch (error) {
          console.error(error);
          res.status(500).json({ error: 'Failed to create profile' });
        }
      });
      
      // Endpoint for retrieving movie details
      app.get('/movies/:id', async (req, res) => {
        const { id } = req.params;
        try {
          const movie = await prisma.movie.findUnique({
            where: { id: parseInt(id) },
          });
          res.json(movie);
        } catch (error) {
          console.error(error);
          res.status(500).json({ error: 'Failed to fetch movie' });
        }
      });
      
      const PORT = process.env.PORT || 3000;
      app.listen(PORT, () => {
        console.log(`Server is running on http://localhost:${PORT}`);
      });
      
  3. Database Layer

    • PostgreSQL for user data (authentication, profiles)
    • MongoDB for flexible schema data (movie metadata, genres, actors)
  4. Content Delivery

    • CDN: Delivers video content globally with edge caching for faster streaming.
  5. Caching

    • Redis: Caches frequently accessed data such as user profiles and movie metadata to improve performance.
  6. Video Storage and Streaming

    • Amazon S3: Stores video files securely with high availability and durability.
    • Streaming: Utilizes HLS for adaptive bitrate streaming to deliver high-quality video content to users.
  7. Monitoring and Analytics

    • Prometheus and Grafana: Monitors system health, performance metrics, and usage statistics to optimize performance and troubleshoot issues.

Scalability and Fault Tolerance

Security and Authentication

Published on: Jul 10, 2024, 01:05 AM  
 

Comments

Add your comment