Skip to content
16px
Why YouTube Uses Server-Sent Events (SSE) Over WebSockets: A Cost-Effective Architecture Decision
ArchitectureScalabilitySystem DesignYouTubeBackendSSEWebSocketsCost Optimization

Why YouTube Uses Server-Sent Events (SSE) Over WebSockets: A Cost-Effective Architecture Decision

When serving 2 billion logged-in users per month, every architectural decision has massive cost implications. Discover why YouTube chose SSE over WebSockets and how this choice saves millions of dollars annually.

January 29, 20266 min read

The Billion-Dollar Question

When you're serving 2 billion logged-in users per month like YouTube, every architectural decision has massive implications on infrastructure costs. Let's break down why YouTube chose Server-Sent Events (SSE) over WebSockets, and how this "simple" choice saves millions of dollars annually.

Understanding the Use Case

YouTube needs to push real-time updates to users:

  • πŸ“Ή New video notifications from subscribed channels
  • πŸ’¬ Live chat messages during streams
  • πŸ‘ Like count updates
  • πŸ”” Community post notifications
  • πŸ“Š Live streaming viewer counts

Key insight: Notice something? All these are one-way communications - server to client. Users don't need to send continuous data back to the server.

The Cost Breakdown: SSE vs WebSockets

Let's examine the infrastructure requirements and costs for each approach.

WebSockets: The Heavyweight Champion

Connection Type: Full-Duplex (Two-way) Protocol: Custom WebSocket protocol Connection Management: Complex sticky sessions required

  • Load balancers need sticky sessions (session affinity)
  • Connections must stay on the same server
  • Complex horizontal scaling
  • Higher memory footprint per connection (~8KB per connection)
  • Custom protocols require special proxy/CDN handling

Cost Impact at YouTube Scale

WebSocket Infrastructure:

2 billion users Γ— 8KB memory per WebSocket = 16 TB RAM

  • Dedicated WebSocket servers (can't easily share with HTTP)
  • Special load balancer configurations
  • CDN complexity/bypass costs = Massive infrastructure costs

SSE: The Efficient Contender

Connection Type: Half-Duplex (One-way) Protocol: Standard HTTP/HTTPS Connection Management: Standard HTTP load balancing

  • Works with standard HTTP load balancers (no sticky sessions!)
  • Easy horizontal scaling
  • Lower memory footprint (~2KB per connection)
  • HTTP/2 multiplexing support
  • Works through standard CDNs and proxies

SSE Cost Impact at Scale

SSE Infrastructure:

2 billion users Γ— 2KB memory per SSE = 4 TB RAM

  • Can share existing HTTP infrastructure
  • Standard load balancing (no special config)
  • CDN friendly (works like regular HTTP) = 75% less cost than WebSockets!

Real-World Cost Savings

Let's do some back-of-the-envelope math for YouTube's scale:

Scenario: 100 Million Concurrent Users

WebSocket Approach:

Server Requirements:

  • 10,000 dedicated WebSocket servers (10K connections each)
  • Each server: $200/month
  • Special load balancers: $5,000/month each Γ— 100
  • Monthly cost: $2,500,000

Memory: 100M Γ— 8KB = 800 GB Bandwidth overhead: ~20% higher (WebSocket frame overhead)

SSE Approach:

Server Requirements:

  • Can use existing HTTP infrastructure
  • 3,000 servers handling multiple duties (33K connections each)
  • Each server: $200/month
  • Standard load balancers: Already in place
  • Monthly cost: $600,000

Memory: 100M Γ— 2KB = 200 GB Bandwidth overhead: Minimal (standard HTTP)

Annual Savings: ~$22.8 million πŸŽ‰

Technical Advantages Beyond Cost

SSE provides several technical advantages that go beyond just cost savings.

1. HTTP/2 Multiplexing Magic

One TCP connection can handle multiple SSE streams:

javascript
1// One TCP connection, multiple SSE streams
2GET /notifications β†’ Stream 1
3GET /live-updates β†’ Stream 2
4GET /chat β†’ Stream 3
5// All over ONE TCP connection!

WebSockets require separate connections for each stream.

2. Automatic Reconnection

SSE handles reconnection automatically:

javascript
1// SSE handles this automatically
2const eventSource = new EventSource('/events');
3// If connection drops, browser auto-reconnects
4// and sends Last-Event-ID to resume from where it dropped

With WebSockets, you must implement this yourself with manual retry logic and state tracking.

3. CDN & Proxy Friendly

SSE uses standard HTTP, so it works seamlessly with:

  • Cloudflare
  • Akamai
  • AWS CloudFront
  • Corporate proxies
  • Firewalls

WebSockets often get blocked or require special configuration.

4. Infrastructure Reuse

YouTube's existing infrastructure:

  • HTTP Load Balancers βœ… (SSE works here)
  • Nginx/Apache servers βœ… (SSE works here)
  • CDN layer βœ… (SSE works here)
  • DDoS protection βœ… (SSE works here)
  • Rate limiting βœ… (SSE works here)
  • Monitoring tools βœ… (SSE works here)

No need for separate WebSocket infrastructure!

When WebSockets Would Cost More

Let's examine a bidirectional chat application at scale:

SSE Architecture for Chat Application - Showing HTTP/2 Multiplexing, Auto Reconnect, No Sticky Sessions, and 75% Less Memory

WebSockets for Chat

Advantages:

  • βœ… Native bidirectional support
  • βœ… Lower latency for client β†’ server

Disadvantages:

  • ❌ More complex infrastructure
  • ❌ Higher memory per connection

SSE + HTTP POST for Chat

Approach:

  • βœ… SSE for server β†’ client messages
  • βœ… Regular HTTP POST for client β†’ server messages
  • βœ… Simpler infrastructure
  • βœ… Lower memory usage
  • ⚠️ Slightly higher latency for client messages (negligible for chat)

Verdict: For most chat applications, SSE + POST is still more cost-effective!

The Hidden Costs of WebSockets

WebSockets introduce several hidden operational costs.

1. Sticky Sessions Nightmare

WebSocket Problem:

User connects to Server A Server A goes down User gets routed to Server B WebSocket connection lost β†’ Must reconnect

SSE Solution:

User connects to any server Server goes down β†’ Auto-reconnects to any available server No session stickiness needed!

2. Scaling Complexity

Adding new WebSocket servers:

  1. Update load balancer config
  2. Ensure session persistence
  3. Drain existing connections
  4. Test sticky session routing
  5. Monitor connection distribution

Adding new SSE servers:

  1. Add server to pool
  2. Done! βœ…

3. Monitoring & Debugging

WebSocket debugging:

  • Custom tools needed
  • Binary frames harder to inspect
  • Connection state tracking complex

SSE debugging:

  • Standard HTTP tools work
  • Plain text events
  • curl works out of the box!

YouTube's Architecture Wins

By choosing SSE, YouTube gets:

  • πŸ’° Lower Infrastructure Costs - 75% less memory per connection, no special WebSocket servers, standard load balancing
  • πŸ“ˆ Better Scalability - Easy horizontal scaling, no sticky sessions, HTTP/2 multiplexing
  • πŸ› οΈ Simpler Operations - Use existing HTTP stack, standard monitoring tools, easier debugging
  • 🌍 Better Global Performance - Works through CDNs, no proxy issues, firewall friendly
  • ⚑ Built-in Resilience - Automatic reconnection, event ID tracking, browser-native support

Code Comparison: The Simplicity Factor

Let's compare the implementation complexity:

WebSocket Server (Complex)

javascript
1// Requires special WebSocket library
2const WebSocket = require('ws');
3const wss = new WebSocket.Server({ port: 8080 });
4
5wss.on('connection', (ws) => {
6    // Manual ping/pong for health check
7    ws.isAlive = true;
8    ws.on('pong', () => ws.isAlive = true);
9    
10    // Send message
11    ws.send(JSON.stringify({ type: 'notification', data: '...' }));
12});
13
14// Manual cleanup of dead connections
15setInterval(() => {
16    wss.clients.forEach((ws) => {
17        if (!ws.isAlive) return ws.terminate();
18        ws.isAlive = false;
19        ws.ping();
20    });
21}, 30000);

SSE Server (Simple)

javascript
1// Uses standard HTTP
2app.get('/events', (req, res) => {
3    res.setHeader('Content-Type', 'text/event-stream');
4    res.write('data: notification\n\n');
5    // Connection cleanup handled by HTTP layer
6});

SSE is literally 4 lines vs 20+ lines for the same functionality!

The Bottom Line

For YouTube's use case:

  • βœ… Only need server β†’ client communication
  • βœ… Millions of concurrent connections
  • βœ… Standard HTTP infrastructure available
  • βœ… Global CDN distribution needed
  • βœ… Cost optimization critical

SSE is the obvious choice.

WebSockets are great for:

  • Real-time multiplayer games (constant bidirectional data)
  • Collaborative editing (like Google Docs)
  • Trading platforms (microsecond latency matters)

But for notifications, updates, and feeds? SSE saves millions while being simpler.

Takeaway for Your Projects

Ask yourself:

  • Do I need bidirectional communication?
    • No β†’ Use SSE
    • Yes β†’ Still consider SSE + HTTP POST
  • Do I need ultra-low latency (<100ms)?
    • No β†’ Use SSE
    • Yes β†’ Consider WebSockets
  • Am I optimizing for scale and cost?
    • Yes β†’ Use SSE
    • No β†’ Either works

Rule of thumb: Start with SSE. Only upgrade to WebSockets when you have a proven need for bidirectional real-time communication.

Conclusion

YouTube didn't choose SSE because it's "good enough" - they chose it because it's the optimal solution for their specific needs. When serving billions of users, every architectural decision matters, and SSE provides the perfect balance of simplicity, cost-effectiveness, and scalability.

For most applications requiring real-time server-to-client updates, SSE is not just a viable optionβ€”it's often the best choice. The cost savings, operational simplicity, and infrastructure reuse make it a compelling solution for any application at scale.

Bhupesh Kumar

Bhupesh Kumar

Backend engineer building scalable APIs and distributed systems with Node.js, TypeScript, and Go.