Choosing the Right API Protocol: From RESTful to GraphQL, gRPC, WebSocket, and SSE
This article, based on the open‑source HiMarket project, systematically compares six mainstream API protocols—from classic RESTful and SOAP to modern GraphQL, microservice‑focused RPC frameworks, real‑time WebSocket, and streaming SSE—detailing their core concepts, technical traits, advantages, and ideal application scenarios.
The author recently contributed to the open‑source project HiMarket, an AI‑focused platform that manages private AI Open APIs and MCP services. Using this background, the article classifies and explains six major API protocols, helping developers understand each protocol’s positioning, technical characteristics, and suitable use cases.
1. Widely Used RESTful API
REST (Representational State Transfer) is the most common architecture style. It treats every resource as a URL and uses HTTP methods (GET, POST, PUT, DELETE) to operate on those resources. A typical REST request looks like:
GET /users/123 HTTP/1.1
Host: api.example.com
Accept: application/jsonResponse example:
HTTP/1.1 200 OK
Content-Type: application/json
{"id":123,"name":"Alice"}Intuitive: URL = resource, HTTP method = operation.
Stateless: Each request carries its own context, simplifying scaling.
Standardized: Leverages existing HTTP infrastructure (caching, load‑balancing).
Cross‑language: JSON/XML payloads are easy to parse in any language.
2. Front‑end Friendly GraphQL
GraphQL, open‑sourced by Facebook in 2015, lets the client specify exactly which fields it needs, avoiding over‑fetching and under‑fetching problems common with REST. All queries go through a single endpoint (usually /graphql).
Client request example:
POST /graphql HTTP/1.1
Host: api.example.com
Content-Type: application/json
{ "query": "{ user(id:123){ name avatar orders(limit:3){ id total } } }" }"Server response example:
{
"data": {
"user": {
"name": "Alice",
"avatar": "https://cdn.example.com/avatar/123.png",
"orders": [
{"id":"A001","total":99.9},
{"id":"A002","total":58.0},
{"id":"A003","total":199.5}
]
}
}
}On‑demand data: Eliminates both over‑fetching and under‑fetching.
Single entry point: All queries hit /graphql, simplifying routing.
Strongly‑typed schema: Guarantees consistent data shapes between client and server.
Self‑documenting: The schema serves as live documentation via introspection.
Front‑end friendly: Teams can define required fields without backend changes.
3. Microservice‑Oriented API: Apache Dubbo
Dubbo is an RPC framework originated by Alibaba. It supports multiple protocols (Dubbo, gRPC, REST), uses a registry (Zookeeper, Nacos) for service discovery, provides built‑in load balancing, fault tolerance, and integrates tightly with the Java/Spring ecosystem.
4. High‑Performance RPC: gRPC
gRPC, created by Google in 2015, runs over HTTP/2 and uses Protocol Buffers for binary serialization, offering lower latency and smaller payloads than JSON‑based REST. It supports unary calls as well as client, server, and bidirectional streaming.
Protocol: HTTP/2 for multiplexed streams.
Data format: Protobuf (binary) for compact messages.
API design: RPC‑style, feels like local function calls.
Streaming: Supports full‑duplex streaming, ideal for large‑scale inter‑service communication.
5. Real‑Time Communication: WebSocket
WebSocket upgrades an HTTP connection to a full‑duplex TCP channel, enabling low‑latency bidirectional messaging. It is suited for chat, collaborative editing, gaming, and market data push.
Typical client code:
const socket = new WebSocket("ws://example.com/chat");
socket.onopen = () => socket.send("Hello!");
socket.onmessage = (event) => console.log(event.data);Bidirectional: Both client and server can initiate messages.
Low latency: Reuses a single TCP connection, avoiding repeated handshakes.
High‑frequency use: Ideal when many messages are exchanged rapidly.
6. Stream‑Based API for Large‑Model Scenarios: Server‑Sent Events (SSE)
SSE delivers a unidirectional stream over a regular HTTP connection (Content‑Type: text/event-stream). It fits AI inference where the server progressively emits tokens.
Server‑side flow:
GET /events HTTP/1.1
Accept: text/event-stream
[data: token1]
[data: token2]
...Client usage example:
const source = new EventSource("/events");
source.onmessage = (e) => console.log("New data:", e.data);Native streaming: Sends data as it is generated, reducing perceived latency.
HTTP‑based: Works with existing proxies, firewalls, and load balancers.
Lightweight: Only server pushes data; no need for full duplex.
Reconnect support: Uses Last‑Event‑ID to resume after interruptions.
7. MCP‑Oriented API (Streamable HTTP)
Earlier MCP implementations combined HTTP requests with SSE for streaming, which caused long‑lived connections, extra complexity, and poor compatibility with enterprise network devices. The newer Streamable HTTP protocol unifies the endpoint, allows on‑demand streaming, and introduces session management for better stability and performance.
Conclusion
The evolution of APIs mirrors the shifting demands of software engineering: from heavyweight SOAP to lightweight REST, from static request/response to flexible GraphQL, from simple HTTP to high‑performance RPC, and from bidirectional WebSocket to unidirectional SSE for AI workloads. As AI‑native applications grow, new protocols will continue to emerge, and platforms like HiMarket aim to provide a unified management layer for RESTful, MCP, and future API styles.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
