AiTechWorlds
AiTechWorlds
The year is 2000. Roy Fielding, a principal author of the HTTP specification, submits his PhD dissertation at UC Irvine. In Chapter 5, he describes an architectural style he calls Representational State Transfer — REST. It is not a protocol or a standard; it is a set of constraints for designing networked applications. Within a decade, virtually every public API on the internet would claim to follow it.
Fifteen years later, Facebook engineers are building the first version of their mobile app. They face a concrete problem: the REST API designed for the website returns far too much data for a slow mobile network, while simultaneously forcing the mobile app to make five separate API calls to render a single screen. In 2015, they open-source their solution: GraphQL. A year after that, Google open-sources gRPC, designed for the high-performance, strongly-typed communication between their internal services.
Three different problems. Three different solutions. All three are production-critical tools that every engineer must understand.
REST is built on six constraints that, when followed, give you a scalable, simple, and cacheable API.
The six REST constraints:
HTTP Methods and What They Mean:
| Method | Meaning | Idempotent? | Safe? |
|---|---|---|---|
| GET | Retrieve a resource | Yes | Yes |
| POST | Create a new resource | No | No |
| PUT | Replace a resource entirely | Yes | No |
| PATCH | Partially update a resource | No | No |
| DELETE | Remove a resource | Yes | No |
Idempotency is critical: making the same PUT or DELETE request 10 times has the same effect as making it once. POST is not idempotent — posting the same order 10 times creates 10 orders.
Essential HTTP Status Codes:
| Code | Meaning | When to Use |
|---|---|---|
| 200 | OK | Successful GET or PATCH |
| 201 | Created | Successful POST |
| 204 | No Content | Successful DELETE |
| 400 | Bad Request | Invalid input data |
| 401 | Unauthorised | Authentication required |
| 403 | Forbidden | Authenticated but lacks permission |
| 404 | Not Found | Resource does not exist |
| 409 | Conflict | Duplicate resource, version conflict |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Internal Server Error | Unexpected server failure |
| 503 | Service Unavailable | Server temporarily down |
REST API Best Practices:
/v1/users, /v2/users/users/42 not /getUser?id=42Facebook's mobile app in 2012 had a concrete problem with REST:
/users/42 returns 30 fields (address, preferences, billing info, etc.) but the mobile screen only needs 3 (name, avatar, bio). All that extra data wastes bandwidth on a slow mobile connection.GraphQL solves both problems with a single endpoint. The client describes exactly what data it needs in a query, and the server returns exactly that — nothing more, nothing less.
GraphQL concepts:
The N+1 Problem: If you query 100 users and their posts, a naive GraphQL implementation runs 1 query for users and then 100 separate queries for their posts — 101 queries total. The DataLoader library, also developed by Facebook, solves this by batching and caching per-request database calls.
GraphQL is used by: GitHub (their public v4 API is GraphQL), Shopify, Twitter, Airbnb.
In 2016, Google open-sourced gRPC — the protocol they use internally across their data centres. It is built on two technologies:
.proto schema file.A REST+JSON response for a user object might be 240 bytes of human-readable text. The same data as Protobuf binary is about 30 bytes — 8× smaller. Smaller payloads mean faster transmission and less CPU spent on serialisation/deserialisation.
From a .proto file, gRPC auto-generates client and server code in Go, Java, Python, C++, Node.js, and other languages. Type errors are caught at compile time, not at runtime.
gRPC streaming modes:
gRPC is used by: Google internally, Netflix, Dropbox, Square. Kubernetes uses gRPC for all its internal API communication.
In a real system, different services may use different API styles. The API gateway provides a single entry point and handles cross-cutting concerns for all of them.
JWT (JSON Web Tokens): After login, the server issues a signed token containing the user's ID and permissions. The client sends this token with every request. The server verifies the signature without a database lookup. JWT tokens must have an expiration time (exp claim) — they are bearer tokens and can be stolen.
OAuth 2.0: The standard for delegated authorisation. When you log into a third-party app with "Login with Google," you are using OAuth. The third-party app gets a token scoped to specific permissions (read contacts, for example) without ever seeing your Google password.
API Keys: Simple secret strings sent in headers. Easy to implement, hard to rotate. Suitable for server-to-server communication where the client is a trusted service.
Rate Limiting: Every public API must limit how many requests a client can make. Common algorithms: Token Bucket (smooth rate limiting), Leaky Bucket (constant output rate), Sliding Window (accurate count over rolling time window). GitHub's API allows 5,000 requests per hour per authenticated user.
| Aspect | REST | GraphQL | gRPC |
|---|---|---|---|
| Protocol | HTTP/1.1 or HTTP/2 | HTTP/1.1 or HTTP/2 | HTTP/2 only |
| Data format | JSON (or XML) | JSON | Protocol Buffers (binary) |
| Schema | OpenAPI (optional) | Strongly typed (required) | .proto files (required) |
| Over-fetching | Common problem | Eliminated | Not applicable |
| Under-fetching | Common problem | Eliminated | Not applicable |
| Browser support | Native | Yes | Requires proxy (grpc-web) |
| Streaming | Limited | Subscriptions | Full bidirectional |
| Code generation | Optional | Optional | Core feature |
| Best for | Public APIs, web services | Complex data, mobile apps | Internal service-to-service |
| Used by | Almost everyone | GitHub, Shopify | Google, Netflix, Kubernetes |
Roy Fielding's 2000 dissertation did not invent the web — it described why the web worked so well and gave engineers a vocabulary for replicating that success. Facebook's GraphQL in 2015 did not invalidate REST — it solved a specific problem (mobile bandwidth and round trips) that REST was not designed to address. Google's gRPC solved a different problem entirely: making cross-service communication inside a data centre as fast and safe as a function call.
The professional answer to "which API technology should we use?" is never a single technology. It is: REST for public-facing APIs, GraphQL where flexible queries are needed, and gRPC where raw performance matters inside your infrastructure.
Get this course's notes on Telegram!
Free cheat sheets, summaries & practice exercises