Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 5 OF 5 · 2 MIN READ

REST vs GraphQL vs gRPC vs WebSockets

REST, GraphQL, gRPC and WebSockets solve different problems. Interviewers want to hear trade-offs: when one shape of API fits better, not that one is always best.

What you will learn

  • Compare REST, GraphQL and gRPC on caching, typing and performance
  • Explain over-fetching and under-fetching
  • Choose between polling, Server-Sent Events and WebSockets for real-time features

Key concepts

GraphQL

One endpoint where the client sends a query describing exactly the fields it needs. Great for many different clients and nested data. Harder to cache over HTTP, and you must guard against expensive queries (depth and cost limits) and N+1 lookups (DataLoader).

gRPC

RPC over HTTP/2 with Protocol Buffers: a typed contract, small binary messages, streaming, and generated clients. Common between internal microservices; browsers need a proxy (gRPC-Web).

WebSocket

A full-duplex connection that starts as HTTP and upgrades. Both sides can send messages at any time. Used for chat, multiplayer and collaborative editing.

Server-Sent Events

A one-way stream from server to client over plain HTTP with automatic reconnect. Good for notifications, live scores and streaming AI responses.

Step by step

A quick decision guide

  • Public API, simple resources, HTTP caching matters → REST
  • Many screens or apps that need different shapes of the same data → GraphQL
  • Fast typed calls between internal services, streaming → gRPC
  • Server pushes updates only → SSE; both sides talk constantly → WebSocket

Scaling WebSockets

Connections are long-lived and stick to one server, so a message for user A may arrive on server 1 while A is connected to server 2. Use a pub/sub layer (Redis, NATS, Kafka) so every server can forward messages to its own connections.

Examples

Server-Sent Events in Express

app.get('/events', (req, res) => {
  res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' })
  res.flushHeaders()
  const send = (data) => res.write('data: ' + JSON.stringify(data) + '\n\n')
  const unsubscribe = bus.subscribe(req.user.id, send)
  req.on('close', unsubscribe)
})

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Pick an API style for: a mobile banking app, a stock ticker, a chat app, an internal pricing service.
  • Explain the N+1 problem in GraphQL resolvers and how DataLoader batches it.
  • Sketch how two Node.js servers deliver a chat message to users connected to different servers.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Why is GraphQL harder to cache than REST?

REST GETs have unique URLs that CDNs and browsers cache by URL. GraphQL usually sends POST to one URL with a query body, so HTTP caches cannot key on it. Teams use persisted queries over GET or caching in the client (Apollo, Relay) and in resolvers.

When would you choose long polling over WebSockets?

When updates are rare, the infrastructure does not support WebSockets well (some proxies, serverless) or you need the simplest possible fallback. Long polling is plain HTTP and works everywhere.

Summary

REST is simple and cacheable, GraphQL lets clients choose fields, gRPC is fast and typed between services, SSE streams server-to-client, and WebSockets carry two-way real-time traffic. Pick by the clients and traffic you have.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus