REST architectural constraints explained

Difficulty: Beginner

Question

What are the constraints that define REST, and why do statelessness and the uniform interface matter?

Answer

REST is often used loosely to mean any JSON over HTTP API, but it is actually an architectural style from Roy Fielding's dissertation. The value of knowing the constraints is that each one buys you a specific property, and interviewers want to hear you connect constraint to benefit.

The first constraint is client-server separation. The UI and the data storage concerns evolve independently. A mobile app, a web app and a partner can all consume the same backend as long as the contract holds.

The second is statelessness. Each request must carry everything the server needs to process it: credentials, parameters, context. The server does not remember your previous request in a session. Think of ordering at a busy counter where every order slip is complete by itself, versus a waiter who has to remember your table history. Stateless servers are trivial to scale horizontally because any instance can serve any request, and a crash loses no session data. The cost is slightly larger requests, usually a bearer token in the Authorization header. Note that stateless does not mean the application has no state; state lives in the database or on the client, not in server memory tied to a connection.

The third is cacheability. Responses must declare whether they can be cached, using Cache-Control, ETag and Last-Modified. This is how CDNs and browsers take load off your servers.

The fourth is the uniform interface, the constraint that most distinguishes REST. It has four parts. Resource identification: everything is a resource addressed by a URI, such as /orders/42. Manipulation through representations: the client works with a JSON or XML representation of the resource and sends it back to change it. Self-descriptive messages: each message says how to interpret it, through media types, methods and status codes. And hypermedia as the engine of application state, HATEOAS, where responses include links to the next possible actions. In practice most public APIs skip full HATEOAS, which is why Richardson's maturity model calls them level 2.

The fifth is the layered system. A client cannot tell whether it talks to the origin server or to a load balancer, gateway, or CDN in between. That enables API gateways, WAFs and caches without client changes.

The optional sixth is code on demand, where the server can send executable code such as JavaScript. Almost nobody discusses it in interviews.

Common edge case questions: Is a session cookie RESTful? Strictly, server-side session state violates statelessness, although a self-contained signed token is fine. Is RPC-style /createUser REST? No, because the verb is in the URI rather than in the method acting on a resource. Is a POST that searches with a big body REST? It is a pragmatic exception, since GET bodies are unreliable.

So when someone asks why REST scales, the crisp answer is: statelessness gives horizontal scaling, cacheability gives read offloading, the uniform interface gives decoupling and predictability, and layering gives infrastructure flexibility.

Code examples

Stateless request carrying all context

GET /orders?status=paid&limit=20 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOi...
Accept: application/json

Any server instance can handle this request because identity and parameters travel with it; no server-side session lookup is needed.

Key points

Common follow-up questions

Interview tips

Concepts covered

REST, statelessness, uniform interface, resources

More API Design questions