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
- Backend: Node.js with Express.js
- Database: PostgreSQL for relational data (user profiles, metadata), MongoDB for flexible data (catalog metadata)
- ORM: Prisma for database interactions
- Caching: Redis for caching frequently accessed data
- Storage: Amazon S3 for video storage
- Streaming: HLS (HTTP Live Streaming) for adaptive bitrate streaming
- Monitoring: Prometheus and Grafana for metrics monitoring
System Components
-
Client Applications (Web, Mobile, Smart TVs)
- Various clients for accessing Netflix content.
-
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}`); });
-
-
Database Layer
- PostgreSQL for user data (authentication, profiles)
- MongoDB for flexible schema data (movie metadata, genres, actors)
-
Content Delivery
- CDN: Delivers video content globally with edge caching for faster streaming.
-
Caching
- Redis: Caches frequently accessed data such as user profiles and movie metadata to improve performance.
-
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.
-
Monitoring and Analytics
- Prometheus and Grafana: Monitors system health, performance metrics, and usage statistics to optimize performance and troubleshoot issues.
Scalability and Fault Tolerance
- Horizontal Scaling: Deploy multiple instances of microservices and use load balancers to handle increased traffic.
- Database Sharding: Partition databases to distribute load and improve scalability.
- Redundancy and Backup: Deploy services across multiple availability zones (AZs) for fault tolerance and ensure regular backups for data integrity and disaster recovery.
Security and Authentication
- OAuth: Used for authentication and authorization of users.
- SSL/TLS: Encrypts data in transit to ensure secure communication between clients and servers.
Published on: Jul 10, 2024, 01:05 AM