SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • What is an API?
  • API Styles
  • Idempotency
  • Data Formats
  • REST, GraphQL, gRPC
  • Pagination, Filtering and Sorting
  • API Versioning
  • Error Handling and Status Codes
  • OpenAPI / Swagger
  • Bulk APIs and File Upload
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/API Design

API Styles

Request-response, streaming and event-driven: the main ways APIs talk.

⏱ 12 min readBeginnerMust KnowHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

An API style is the pattern of conversation between two programs. The main patterns are request-response, streaming and event-driven.

  • Request-response: ask once, get one answer. The most common.
  • Streaming: keep a line open and keep sending.
  • Event-driven: “call me when something happens”.
  • Pick the style from how the data flows, not from what is fashionable.

02Concept: What is it?

There are two different ideas, and people mix them up. Keep them apart:

Conversation pattern
This topic
  • Who talks first, and how often?
  • One answer, many answers, or a call-back?
  • Request-response, streaming, event-driven
API technology
Next topic
  • How are the messages written and sent?
  • REST, GraphQL, gRPC, SOAP
  • See “REST, GraphQL, gRPC”
🧠Same technology, different pattern
gRPC can do request-response and streaming. HTTP can do request-response, and with SSE it can stream. So the pattern and the technology are different choices.

The three main patterns

🙋Request-response
The client asks. The server answers once.
🌊Streaming
One open connection carries many messages.
🔔Event-driven
The server notifies the client when something happens.

03Real-Life Example

🍵Three ways to follow a cricket match
  • Request-response: you phone a friend every few minutes: “What is the score?” Each time you ask, they answer.
  • Streaming: you stay on a live call, and your friend tells you every ball as it happens.
  • Event-driven: you tell your friend: “Message me only when a wicket falls.”
Three conversations
📱You
→↓score?
←120/3
🧑Friend
Request-response
📱You
←ball 1 · ball 2 · ball 3 …
🧑Friend
Streaming
📱You
←WICKET!
🧑Friend
Event-driven
Cricket match
API
Phoning every few minutes
Polling a request-response API
Staying on the live call
A stream (SSE, WebSocket, gRPC stream)
“Message me on a wicket”
A webhook or a message from a broker
Wasted calls when nothing changed
Empty polls

04Why Do We Need It?

Choosing the wrong pattern costs speed, money and happiness.

⚡Speed
Streaming and events deliver news at once. Polling can be late.
💰Cost
Asking again and again when nothing changed wastes servers.
🧠Simplicity
Request-response is the easiest to build, test and debug.
📈Scale
Millions of open streams need special care. Events can use queues.
🔌Fit
A chat app and a bank statement need very different patterns.
🛡️Reliability
Events may be lost or repeated; request-response is easier to retry.
Match the pattern to how often the data changes and who needs to know first.

05Where Do We Use It?

PatternUse it forExamples
Request-responseReading and changing data on demandOpen a profile, place an order, search products
StreamingLive data that keeps comingChat, live scores, stock prices, video upload, logs
Event-drivenTell others when something happenedPayment done, order shipped, code pushed
🛒Shop checkout
Request-response to place the order.
💬Chat apps
Streaming (WebSockets) for instant messages.
💳Payment status
Event-driven: the gateway calls your webhook.

06Types / Parts / How It Works

1. Request-response

The client sends a request and waits. The server sends one response. Then it is finished. This is how most web pages and REST APIs work.

📱Client
→↓1 request
🖥️Server
←2 response
📱Clientdone

Sync vs async: a call can be synchronous (the client waits) or asynchronous (the server says “accepted, check later”, usually with status 202 and a job id).

2. Streaming

One connection stays open and carries many messages. There are three shapes:

Server streaming
📱
←many
🖥️
Live scores, SSE
Client streaming
📱
→↓many
🖥️
Upload a big file in pieces
Two-way (bidirectional)
📱
↔both
🖥️
Chat, games (WebSockets)

3. Event-driven

Instead of asking, the client subscribes or registers an address. When an event happens, the server sends a message.

💳Payment serviceevent: paid
→↓POST to my webhook
🛒Shop servermarks order paid

A fourth idea: polling

If you cannot get a push, you can poll: ask again on a timer. It is simple but wasteful. Long polling improves it: the server holds the request open until it has news. See the Real-Time topics later.

A word on SOAP

SOAP is an older request-response style that sends XML messages with strict rules. You still see it in banks, insurance and old enterprise systems. New public APIs rarely choose it.

VisualizerWatch three conversation patternsPick a style, then press Next message or Play. Compare who sends first.
Pick an API style
📱Client
→↓asks every time
←answers once
🖥️Server
Press “Next message” or “Play”.
Client sent
0
last 8 messages
Server sent
0
last 8 messages
Who starts?
Client
each new message
Request-response. The client asks. The server answers once. Then the talk is over. Every new answer needs a new question. Used for: Loading a page, reading a profile, placing an order (REST, GraphQL, gRPC unary, SOAP).

07How to Use It / Steps / Implementation

How to choose a pattern

  1. 1
    Does the client need the data right now, on demand?
    Yes → request-response.
  2. 2
    Does the data change often and users need it live?
    Yes → streaming (SSE for one-way, WebSockets for two-way).
  3. 3
    Does another system need to know when something happens?
    Yes → event-driven (webhook, or a message queue between your own services).
  4. 4
    Can the work take a long time?
    Return 202 Accepted with a job id. Let the client check the status or get a webhook.
  5. 5
    Is the network weak or are clients behind firewalls?
    Prefer simple request-response or long polling. They pass through almost everything.
QuestionRequest-responseStreamingEvent-driven
Who starts each message?ClientServer (after open)Server
ConnectionShortLong, stays openShort call-back
Latency for newsAs late as the next pollInstantInstant
Server cost per clientLowHigher (open connections)Low
Ease to buildEasiestMediumMedium

08Key Terms to Know

TermMeaning in easy words
Request-responseOne request gets one response, then the exchange is over
SynchronousThe caller waits for the answer before doing anything else
AsynchronousThe caller does not wait; the answer comes later or through another channel
StreamingMany messages travel over one open connection
PollingAsking again and again on a timer to check for news
PushThe server sends data without being asked each time
EventA fact that happened, like “order paid”
WebhookAn address you give so the other system can call you when an event happens
BidirectionalBoth sides can send messages at any time
202 AcceptedA status code meaning “I got your request and will work on it”

09Pros, Cons and Trade-offs

PatternProsCons
Request-responseSimple, easy to cache, test and retry, works everywhereSlow for live data (needs polling), one answer per call
StreamingInstant updates, fewer wasted callsOpen connections cost memory, harder to scale, reconnect logic needed
Event-drivenSystems are loosely joined, instant news, scales well with queuesHarder to debug, events can be lost, repeated or arrive out of order
✅ Pros
  • You can mix patterns in one system
  • Each pattern is the best tool for some job
  • Event-driven parts can be built and scaled on their own
⚠️ Cons
  • More patterns means more things to learn and run
  • Mixing without a plan makes systems confusing
  • Tests and monitoring differ for each pattern
⚠️Do not stream everything
Streaming is exciting. But most screens only need a normal request. Choose it only for data that really changes while the user is watching.

10Common Problems and Mistakes

Common mistakes

  1. Polling very fast. A request every second from a million users is a million requests per second of mostly “nothing new”.
  2. WebSockets for simple data. A normal request would be easier and cacheable.
  3. Webhooks without a retry plan. If your server is down, the event is missed unless the sender retries.
  4. Not handling duplicates. Events and retries can arrive twice. Make handlers idempotent.
  5. Blocking on slow work. Make the call async: return 202 and a job id.
  6. Forgetting reconnects. Open streams break. Clients must reconnect and catch up.
🔁Duplicates
Retries and at-least-once delivery repeat events.
🔀Out of order
Event 2 can arrive before event 1.
🔌Connection limits
A server can hold only so many open streams.
🕳️Lost events
A call-back to a down server is lost without retries.

11Real-World Examples

🛒Online shopping
Browsing and checkout are request-response.
💬WhatsApp-style chat
A long-lived connection brings new messages at once (general idea).
📈Stock tickers
Prices stream to your screen as they change.
💳Payment gateways
They send a webhook when a payment succeeds or fails.
🧑‍💻GitHub / CI tools
A push to a repo triggers a webhook that starts a build.
🍕Food delivery
Order is request-response; live rider position is streaming.
Mini case: food delivery app
📱App
→↓place order
←live rider position
←“order delivered” push
🖥️Backend
Request-response Streaming Event-driven
One app uses all three patterns.

12Related Topics to Read Next

What is an API?→

Start here if the word API is new.

REST, GraphQL, gRPC→

The technologies that carry these patterns.

Long Polling→

A smarter way to poll.

Server-Sent Events (SSE)→

One-way streaming from the server.

WebSockets→

Two-way streaming.

Webhooks→

Event-driven call-backs between systems.

Idempotency→

Needed because events and retries can repeat.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔Three patterns: request-response, streaming, event-driven.
  • ✔Request-response: default choice. Simple, cacheable, easy to retry.
  • ✔Streaming: for live data. Costs open connections.
  • ✔Event-driven: tell me when it happens. Plan for retries, duplicates and order.
  • ✔Long jobs: return 202 + a job id.
  • ✔Do not: poll fast or stream what a normal request can serve.
  • ✔One line: “Pick the conversation pattern from how the data flows.”
← Previous
What is an API?
Next →
Idempotency