Stage 1 · Foundations · Easy · 15 min

API styles: REST, gRPC, WebSockets and SSE

Compare four ways for an app to talk to a server: REST, WebSockets, server-sent events (SSE) and gRPC. Learn what each one is for and when to pick it.

What each interview level is expected to know

Learn first: HTTP, TCP, UDP and connections

After this lesson you can:

  • Choose between REST, WebSockets, server-sent events and gRPC for a given feature.
  • Page through a long list with a cursor, so new items do not make a page repeat one.
  • Plan for reconnect storms and changes to an API that would break old clients.

Start here

See it in 60 seconds

How one screen asks for its data

In this REST API, each kind of data has its own address and a fixed reply, while GraphQL asks once for exactly the fields needed.

Scene 1 of 6 · One screen, four things

A profile screen needs a name, a photo, the 3 latest posts and a follower count.

gRPCPhone appPhone appprofile screenAPI serverAPI serverPosts servicePosts service
  • Phone app — healthy — profile screen
  • API server — healthy
  • Posts service — healthy
REST calls
not shown yet
REST data
not shown yet
GraphQL calls
not shown yet
GraphQL data
not shown yet
  1. One screen, four things. A profile screen needs a name, a photo, the 3 latest posts and a follower count.
  2. REST: one call each. With REST, the app calls one web address per kind of data: 3 calls for this screen.
  3. Whole objects come back. This API always sends the whole object, so the app gets 4.6 KB but shows much less.
  4. GraphQL: one shaped query. With GraphQL, one query names exactly the fields it needs: 1 call and 1.2 KB.
  5. gRPC between services. Behind the API, services often talk gRPC: small binary messages in a fixed, typed format.
  6. GraphQL has costs too. One GraphQL query can still need several back-end calls, and its many shapes are harder to cache and limit.
  7. Now you try. Pick the fields the screen should show, and compare the calls and data. For example, tapping "Name", "Photo link", "3 latest posts" and "Follower count" gives this: REST makes 3 calls and moves 4.6 KB; GraphQL makes 1 call and moves 1.2 KB. REST calls /users/42, /users/42/posts and /users/42/followers, and here each returns a whole object. GraphQL returns only the fields asked for.

Scene 1 of 6: One screen, four things. A profile screen needs a name, a photo, the 3 latest posts and a follower count.

Key points

  1. With REST, each kind of data has its own web address, so one screen can need several calls.
  2. This REST API returns whole objects. A REST API can let callers pick fields, if it is built to.
  3. One GraphQL query names the fields and gets only those, though the server may make several calls to fill them.
  4. Services inside the system often use gRPC: small, typed binary messages that are fast to send.

In plain words

A shopping app loads a product page: it asks once and gets one answer. A chat app is different: new messages must reach you without your app asking each time. An API style is the pattern an app and a server use to exchange messages. is ask-and-answer over . A is one open connection where both sides can send. Server-sent events (SSE) let the server push updates one way. sends small binary messages, mostly between a company's own services.

To choose, ask which way the data flows, and how often. A page that loads once fits ask-and-answer. A live score flows only from server to app, so a one-way push fits, and the browser reconnects by itself if the line drops. Chat flows both ways at any moment, so it needs a connection where either side can send. Calls between your own services, many each second, often use the compact binary style.

The problem it solves

With plain HTTP, only the client can start. To show a new chat message, the app must keep asking "anything new?". This is called . Ask every 2 seconds and each user sends 30 requests a minute, most of them answered "no". Ask less often and messages arrive late. The other styles remove that waste or that delay.

Why it matters

The API style decides three things. Can the server send without being asked? How long does a connection stay open? How much must each server remember? A product page and a live chat need different answers.

A worked example

A chat app polling a REST endpoint every 2 seconds sends 30 requests a minute to check for new messages, since 60 ÷ 2 = 30. A WebSocket instead keeps one connection open, so those 30 checks become zero. The server pushes each message as soon as it has it.

Think of it like…

REST is like posting a letter and getting one reply. A WebSocket is like a phone call: the line stays open and both sides can talk. Server-sent events are like a radio station: it sends, and you only listen.

Where this analogy stops working

Who may speak, and when, is the useful part. But a radio station sends the same thing to everyone. An event stream is one open connection for each listener. Each one uses memory on a server, and can drop without warning, so the client must reconnect.

Try it

See it working

Guided walkthroughOpen the full lab →

Lab · Foundations

WebSockets & heartbeats

A phone opens one connection that stays open, and messages travel both ways on it. Pick how the connection ends, and how often the gateway checks on it, then step forward and back.

Try it

How the connection ends

The phone goes into a tunnel and sends nothing.

Heartbeats from the gateway

With these settings: Found after 15 s. Heartbeats every 10 s found the dead connection at second 30. Before that, 1 message went into it and was lost.

one open connectionPhone, user 42Phoneuser 42asking to upgradeWebSocket gateway, gateway 1, NOWWebSocket gatewaygateway 1NOWSession registry, who is whereSession registrywho is whereChat service, sends messagesChat servicesends messages
  • Phone — user 42 — healthy — asking to upgrade
  • WebSocket gateway — gateway 1 — healthy — NOW
  • Session registry — who is where — healthy
  • Chat service — sends messages — healthy
  • Request
  • Answer
  • Record
  • Message
  • Look up
  • Ping
  • Pong
  • Lost
  • Remove
Step 1 of 17

Step 1 of 17 · WebSocket gateway

Done: The phone asks to upgrade to a WebSocket

What happens
The chat app sends an ordinary HTTP request with the header Upgrade: websocket.
Why
Starting as HTTP lets the connection use the same ports and proxies as web pages.
What changes
The gateway supports WebSockets, so it can agree.
Next
The gateway answers 101 Switching Protocols

Event log

16 more to go. Press a line to go back to that step.

Clock
0s
Connection
not open
Registry says user 42 is on
nobody
Heartbeats missed
0 of 2
Messages lost
0

Step 1 of 17: WebSocket gateway. The phone asks to upgrade to a WebSocket. The chat app sends an ordinary HTTP request with the header Upgrade: websocket.

What this model simplifies
  • This is one phone, one gateway and times in whole seconds. A real gateway holds many thousands of connections, and each uses memory until it is closed.
  • Here the gateway sends the pings. In many apps the phone sends them, which works the same way.
  • Real apps usually number their messages, so a phone that reconnects can ask for the ones it missed.

Mechanics

How it works

Each API style decides who speaks first and how much is sent at once. The pictures follow a sports app's phone: a live score, then a long feed of posts.

  1. REST

    REST uses plain HTTP: a URL names a thing, and the method says what to do. GET /orders/42 reads order 42. Each request carries all it needs, so any server can answer it. That also lets a or browser the answer to a GET.

  2. WebSockets

    A WebSocket is one connection that stays open, and both sides can send at any time. It suits chat, games and shared editing. The cost is that each server must hold many open connections.

  3. Server-sent events (SSE)

    Pick a case to follow below

    With this case: Asking every 5 s took 3 requests, and the fan saw the goal 3 s late.

    Phone app, shows the scorePhone appshows the scoreAPI server, knows the score, NOWAPI serverknows the scorestill 0–1NOWMatch feed, sends each goalMatch feedsends each goal
    • Phone app — shows the score — healthy
    • API server — knows the score — healthy — NOW — still 0–1
    • Match feed — sends each goal — healthy
    • Request
    • New score
    • Response
    Step 1 of 5

    Step 1 of 5 · API server

    Done: At 5 s the phone asks again

    What happens
    The phone sends a new request and gets the same score back.
    Why
    The phone cannot know when the score will change, so it keeps asking.
    What changes
    Nothing new: the request was wasted.
    Next
    At 10 s the phone asks again
    Phone shows
    0–1
    Requests sent
    1

    Step 1 of 5: API server. At 5 s the phone asks again. The phone sends a new request and gets the same score back.

    With SSE the server keeps one HTTP response open and pushes updates down it. Data flows one way only, from server to client. It suits live scores, and answers from an AI model that arrive word by word.

    In the picture, a goal makes the score 1–1 at 12 seconds. With plain REST, the phone must keep asking, here every 5 seconds. It sends three requests, and it finds the goal only at 15 s.

    With SSE, the phone sends one request and the server keeps the response open. The goal arrives as it happens. Asking more often would cut the wait, but most of those requests would bring nothing new.

  4. gRPC

    gRPC is mostly used for calls between your own services. Both sides share a file that lists each call and its fields. Messages travel over HTTP/2 in a compact binary format called Protobuf, so they are usually smaller than the same data in . Browsers cannot call it directly. A web page needs a version called gRPC-Web and a proxy in between.

  5. Long lists: send a page at a time

    Pick a case to follow below

    With this case: The new post moved every place down by one, so "skip 20" started at post 381 again and it showed twice.

    Phone app, shows the feed, NOWPhone appshows the feed400 to 381NOWAPI server, reads the postsAPI serverreads the postsDatabase, posts, newest firstDatabaseposts, newest first
    • Phone app — shows the feed — healthy — NOW — 400 to 381
    • API server — reads the posts — healthy
    • Database — posts, newest first — healthy
    • Response
    • Request
    • Read
    • Repeated post
    Step 1 of 5

    Step 1 of 5 · Phone app

    Done: The phone shows page 1

    What happens
    The server sent the 20 newest posts, 400 down to 381.
    Why
    Sending 20 posts is quick. Sending all of them would be slow.
    What changes
    The user scrolls down to the end of the page.
    Next
    Post 401 arrives
    Page 1 showed
    400 to 381
    Newest post
    400
    Repeated posts
    0
    none

    Step 1 of 5: Phone app. The phone shows page 1. The server sent the 20 newest posts, 400 down to 381.

    What these pictures simplify. These pictures use round example times and one phone. A real app with many fans holds one open stream per fan, and it may fall back to polling when a network drops long-lived connections.

    Sending a feed of 10,000 posts whole would be slow and wasteful. So the API sends a page, such as 20 posts, and the app asks for the next one. This is called . An page says "skip 20, take 20". A page says "the 20 posts after post 381". If a new post arrives between two requests, the offset page repeats a post and the cursor page does not.

    In the picture, page 1 showed posts 400 down to 381. Then post 401 arrives, and every older post moves down one place. Skipping 20 now starts at post 381, so the user sees post 381 twice.

    The cursor names post 381 itself, so new posts above it do not move it. A cursor is also faster on a long list. The database jumps to the post with an , instead of counting past every skipped row.

Use it when

  • REST: public APIs and most app screens, where the client asks once and gets one answer. Nearly every language and tool can call it.
  • WebSockets: chat, multiplayer games and shared editing, where both sides send often and a delay is noticed.
  • Server-sent events: live scores, price tickers and AI answers shown word by word, where only the server sends.
  • gRPC: calls between your own services at a high rate, where small messages and a shared, typed contract pay off.
  • : a screen that needs several kinds of data at once. One query names the exact fields, in place of several REST calls.

Skip it when

  • WebSockets for pages that rarely change, such as a blog: a CDN can cache plain HTTP answers. An open connection for each reader is wasted.
  • gRPC for an API that browsers call directly: they cannot speak it, so you would need a proxy in between.
  • Polling for chat: most requests return nothing, and each new message waits for the next poll.

Check yourself

A feed is sent 20 posts at a time. A new post arrives before the app asks for page 2. Which way of asking shows no post twice?

Also

More in 60 seconds

Polling or webhooks

With polling, your app keeps asking a service for news; with a webhook, the service calls your app the moment news happens.

Scene 1 of 7 · Waiting for news

Two payments finish this minute, at 14 s and 41 s, and only the payment service knows.

PollingWebhookaskscallsYour shop appYour shop appPayment servicePayment serviceYour shop appYour shop appPayment servicePayment service
  • Your shop app — healthy
  • Payment service — healthy
  • Your shop app — healthy
  • Payment service — healthy
One minute of payments

Payments finish

  1. payment finished at 14 s
  2. payment finished at 41 s
Polling calls
not shown yet
Polls with no news
not shown yet
Typical polling delay (half the gap)
not shown yet
Webhook calls
not shown yet
Webhook calls with no news
not shown yet
Webhook delay
not shown yet
  1. Waiting for news. Two payments finish this minute, at 14 s and 41 s, and only the payment service knows.
  2. Polling: keep asking. With polling, your app asks for news every 10 seconds; at 10 s there is none.
  3. News waits for a poll. The first payment finishes at 14 s, but your app only hears at the 20 s poll.
  4. Most polls find nothing. Over the minute that is 6 calls, and 4 of them find nothing new.
  5. Webhook: they call you. With a webhook, the payment service calls your app's web address the moment a payment finishes.
  6. Two calls, no waiting. Over the same minute that is 2 calls, none wasted, and almost no delay.
  7. The catch. Your app needs a public web address, must check who sent each call, and can miss calls while down.
  8. Now you try. Pick how often your app polls, from every 1 to every 30 seconds. For example: Polling every 10 s makes 6 calls, 4 with no news, and hears this minute's two payments 6 s late and 9 s late. On average, news waits about half the gap, 5 s; a webhook needs only 2 calls and hears at once.

Scene 1 of 7: Waiting for news. Two payments finish this minute, at 14 s and 41 s, and only the payment service knows.

Key points

  1. Polling means asking again and again, and most answers are "nothing new yet".
  2. A webhook reverses the direction: the service calls your app when something happens.
  3. Shorter polling gaps cut the delay but waste more calls; webhooks avoid both.
  4. Services retry failed webhook calls, so one can arrive twice; apps skip repeats and poll now and then as backup.

Polling or a WebSocket

For a live chat, polling asks the server again and again, while a WebSocket stays open so the server sends each message at once.

Scene 1 of 7 · A chat needs news

Ana sends Ben a message, and Ben's phone should show it within a second.

PollingWebSocketasksstays openBen's phoneBen's phoneChat serverChat server1 new messageBen's phoneBen's phoneChat serverChat server1 new message
  • Ben's phone — healthy
  • Chat server — healthy — 1 new message
  • Ben's phone — healthy
  • Chat server — healthy — 1 new message
Polls a minute, one person
not shown yet
Polls with nothing new
not shown yet
Typical wait for the next poll
not shown yet
Wait for a poll, with a WebSocket
not shown yet
Polls a second, everyone
not shown yet
Open WebSockets, everyone
not shown yet
  1. A chat needs news. Ana sends Ben a message, and Ben's phone should show it within a second.
  2. Polling: ask every 5 s. Polling every 5 seconds, a message waits 2.5 seconds on average: too slow for this chat.
  3. Most polls find nothing. With one message a minute, 11 of Ben's 12 polls each minute come back empty.
  4. WebSocket: open once. A WebSocket starts as one web request asking to upgrade, and then the connection stays open.
  5. The server sends at once. When Ana's message arrives, the server pushes it down the open connection with no waiting.
  6. Heartbeats keep it alive. Every 30 seconds, a small ping and pong check that the connection is still there.
  7. The catch: held connections. Polling a million people costs 200,000 requests a second; WebSockets instead hold a million connections, each using server memory.
  8. Now you try. Pick how many people are online at once, from 1 thousand to 2 million. For example: 200,000 people online: polling every 5 s makes 40,000 requests a second, while WebSockets hold 200,000 open connections. In this example one server holds up to 100,000 connections, so you need 2, and each message must find the right server.

Scene 1 of 7: A chat needs news. Ana sends Ben a message, and Ben's phone should show it within a second.

Key points

  1. Polling sends a new request every few seconds, and most answers say nothing new.
  2. A WebSocket starts as one web request, then stays open, so both sides can send at any time.
  3. Messages arrive at once, but every open connection uses server memory, and messages must find the right server.
  4. Heartbeats keep quiet connections alive and show which ones dropped without saying goodbye.

Go deeper in the WebSockets & heartbeats lab →

Changing a record without breaking readers

During a deploy, old and new code read each other's records, so each change to a record must keep both directions working.

Scene 1 of 6 · Two versions at once

During a rolling deploy, some servers run the old app and some the new one, sharing one table.

Old app (v1)Old app (v1)knows id, name, emailNew app (v2)New app (v2)adds phoneUsers tableUsers table
  • Old app (v1) — healthy — knows id, name, email
  • New app (v2) — healthy — adds phone
  • Users table — healthy
Fields of a user record
FieldOld app (v1)New app (v2)
idneeds itneeds it
nameneeds itneeds it
emailneeds itneeds it
phone—has a default
  1. Two versions at once. During a rolling deploy, some servers run the old app and some the new one, sharing one table.
  2. New app adds phone. The new app saves user:42 with a new phone field, next to its name and email.
  3. Old app reads it. The old app reads user:42 and skips phone, a field it does not know: that is forward compatibility.
  4. New app reads old data. The new app reads user:7, saved before the deploy, and uses a default phone: that is backward compatibility.
  5. A rename breaks both. Rename name to fullName, and each version looks for a field the other one never wrote.
  6. Safe changes only. So add fields only with a default, never rename a field matched by name, and remove only fields with defaults.
  7. Now you try. Pick the change the new app makes, and see which reads still work. For example: Old app reading new records: breaks, as there is no name. New app reading old records: breaks, as there is no fullName. Fields are matched by name here. Formats that tag fields with numbers, like Protocol Buffers, can rename safely.

Scene 1 of 6: Two versions at once. During a rolling deploy, some servers run the old app and some the new one, sharing one table.

Key points

  1. Forward compatible: old code can read records that new code wrote.
  2. Backward compatible: new code can read records that old code wrote.
  3. Add a field only with a default, so old records still read.
  4. Never rename a field matched by name, and remove only fields with a default.

Page 2 of a list that keeps changing

A long list is sent a page at a time, and the way the app asks for page 2 decides what repeats.

Scene 1 of 6 · A page at a time

A feed holds 10 posts, and the app asks for them 4 at a time, newest first.

Phone appPhone appshows nothing yetFeed APIFeed APIwaitsPostsPosts10 posts
  • Phone app — healthy — shows nothing yet
  • Feed API — healthy — waits
  • Posts — healthy — 10 posts
The feed, newest first
PostSent to the app on
Post 10not yet
Post 9not yet
Post 8not yet
Post 7not yet
Post 6not yet
Post 5not yet
Post 4not yet
Post 3not yet
Post 2not yet
Post 1not yet
Posts shown twice
0
Posts never shown
0
Posts the database read for page 2
not shown yet
  1. A page at a time. A feed holds 10 posts, and the app asks for them 4 at a time, newest first.
  2. Page 1. The app asks for the first 4 and gets posts 10 to 7.
  3. A new post arrives. Before the next request, post 11 is added on top, and every older post moves down one place.
  4. Page 2 by offset. "Skip 4, take 4" counts from the new top, so post 7 comes back a second time.
  5. Page 2 by cursor. "The 4 posts after post 7" starts from a post, not from a count, so nothing repeats.
  6. Fewer rows read. The cursor read 4 posts where the offset read 8, and that gap grows with every page.
  7. Now you try. The app has page 1. What happens to the feed before it asks for page 2? For example: By offset, page 2 is posts 5 to 2, so post 6 is never shown. By cursor, it is posts 6 to 3. An offset counts places from the top, and the places moved. A cursor names the last post seen, and that is still the same post.

Scene 1 of 6: A page at a time. A feed holds 10 posts, and the app asks for them 4 at a time, newest first.

Key points

  1. An offset page counts places from the top: "skip 4, take 4".
  2. A cursor page names the last item seen: "the 4 after post 7".
  3. When the list changes between requests, an offset page repeats or skips an item.
  4. A cursor page also reads fewer rows, however deep the page is.

Judgement

Trade-offs and how it fails

What you give up

Performance
Protobuf messages are usually smaller than JSON and faster to parse, which matters at high request rates. A WebSocket adds only a few bytes to each message, where each HTTP request carries its own headers.
Complexity
An open connection ties each user to one server. To deliver a message, the system must find the server that holds that user's connection. A restart drops every connection on that server, and those clients all reconnect.

Failure modes

  • Reconnect storm: a server restarts, and every client it dropped reconnects at the same moment. The rush can overload the servers that are left. Clients should wait a random, growing time before each retry.
  • A connection that looks alive but is dead: a phone loses signal without closing it. The server keeps it open until a goes unanswered.
  • A breaking change to the API: old versions of the app still send the old shape, and fail until users update.

In the real world

  • Discord's engineering blog (2017) describes holding millions of open WebSocket connections on servers written in Elixir. Source: Discord blog: How Discord scaled Elixir (checked 14 October 2026)
  • A 2018 case study by the Cloud Native Computing Foundation describes Netflix moving calls between its services from its own HTTP/1.1 stack to gRPC. Source: CNCF case study: Netflix (checked 14 October 2026)

Trade-offs to be ready to discuss: Rest vs grpc · Websockets vs polling

Check yourself

Why might two backend services talk over gRPC instead of REST?

Interview

What interviewers look for

  • Name a style for each link and give the reason: "REST for the app's screens, because each one asks once. A WebSocket for chat, because the server must push."
  • Ask which way the data flows before you pick. If only the server sends, SSE is enough.
  • For a list endpoint, say how it is paged. Prefer a cursor when new items arrive often.

Say it out loud: When are server-sent events (SSE) a better choice than a WebSocket?

A dashboard shows a live price that the server updates every second. The browser never sends data back. Which fits best?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorCore
Design clean REST endpoints for the main operations.
MidCore
Choose REST, gRPC or GraphQL for the case; pagination and versioning.
SeniorExpected
Idempotent APIs and backward compatibility.
StaffExpected
API contracts as boundaries between teams.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

On a shared whiteboard, each user draws, and every stroke must reach the others within a moment. Which API style fits best?

Apply it

Which of these requests can a CDN keep and serve again to other users, if the server allows it?

Judge a design

A chat server holding 50,000 WebSocket connections restarts. The app reconnects the moment it is dropped. What should change?

Next: scaling, how to handle more traffic with a bigger server or with more servers.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Official docsIntroduction to gRPCgrpc.ioServices defined in a .proto file, binary messages, and the four kinds of call.
  • StandardRFC 6455: The WebSocket ProtocolIETFHow an HTTP request is upgraded to a two-way connection, and how messages are framed.
  • StandardServer-sent eventsWHATWG HTML StandardThe one-way stream from server to browser, including how the browser reconnects.
  • StandardGraphQL SpecificationGraphQL FoundationThe query language and how a server must run a query against its schema.