CHAPTER 04
Beginner
REST Principles and Constraints
Updated: May 14, 2026
25 min read
# CHAPTER 4
REST Principles and Constraints
1. Introduction
"REST" is not a programming language, a package you install, or a strict protocol like SOAP. REST (Representational State Transfer) is an architectural style defined by Dr. Roy Fielding in 2000. It is a set of six guiding constraints. If an API adheres to these constraints, it earns the title "RESTful". In this chapter, we will dive into the theory behind these constraints, understanding *why* they exist and how they force developers to build highly scalable, predictable web services.2. Learning Objectives
By the end of this chapter, you will be able to:- Identify the six architectural constraints of REST.
- Understand the implications of the Uniform Interface.
- Explain how Cacheability improves network performance.
- Comprehend the concept of a Layered System architecture.
- Distinguish between an API that is "RESTful" versus simply an "HTTP API".
3. Beginner-Friendly Explanation
Imagine building a worldwide franchise of fast-food restaurants. To ensure the franchise is wildly successful and can serve millions of people efficiently, corporate headquarters institutes six strict rules:- 1. The kitchen and the dining room must be separate. (Client-Server)
- 2. Cashiers cannot remember returning customers; every order must be stated completely. (Statelessness)
- 3. Burgers must be wrapped in standard paper, not custom boxes. (Uniform Interface)
- 4. Bottled water can be grabbed from a cooler without bothering the kitchen. (Cacheability)
- 5. The customer doesn't need to know if the cashier is talking to a cook in the back, or a centralized mega-kitchen down the street. (Layered System)
- 6. Sometimes the cashier hands the customer a coupon that changes their behavior. (Code on Demand).
By enforcing these constraints, the restaurant guarantees speed, predictability, and infinite scalability.
4. Constraint 1: Client-Server Architecture
As discussed in Chapter 2, there must be a strict separation of concerns. The Client manages the UI and user state. The Server manages data and security. They interact exclusively through the API. This allows both components to evolve independently.5. Constraint 2: Statelessness
Every request from the Client to the Server must contain all the information necessary to understand and process the request. The Server cannot rely on a "Session" stored in its RAM from a previous request. This constraint ensures that any server in a massive cluster can process any request, enabling infinite horizontal scaling.6. Constraint 3: Uniform Interface
This is the central feature that distinguishes REST from other network styles. It requires that communication between the Client and Server follows a strict, standardized format. It consists of 4 sub-rules:-
Identification of Resources: Everything is a "Resource" (a noun) identified by a stable URL (e.g.,
api.com/users/1).
- Manipulation through Representations: When the client wants to update a user, they send a JSON "representation" of the user to the server.
-
Self-descriptive Messages: The request must explicitly state what it is doing (using HTTP Methods like GET/POST) and what format the data is in (using Headers like
Content-Type: application/json).
- HATEOAS: (Advanced) The API response should include hyperlinks telling the client what other actions are possible (like navigating a website).
7. Constraint 4: Cacheability
Because networks are slow, REST requires that responses explicitly label themselves as "Cacheable" or "Non-Cacheable". If a Client requestsGET /api/logo.png, the server can attach a header saying Cache-Control: max-age=3600. The client's browser saves the image. For the next hour, if the client requests the logo again, the browser intercepts the request and loads the local copy instantly, saving the server from processing the request.
8. Constraint 5: Layered System
A Client usually assumes it is connecting directly to the API Server. However, REST architecture allows you to insert intermediary layers between the Client and Server without the Client knowing.- Layer 1: A Load Balancer routing traffic.
- Layer 2: A Web Application Firewall blocking hackers.
- Layer 3: The actual API Server.
- Layer 4: The Database Server.
9. Constraint 6: Code on Demand (Optional)
This is the only optional constraint. It allows the server to temporarily extend the functionality of a client by sending executable code (like a JavaScript widget) instead of raw JSON data. This is rarely used in modern REST APIs, which strictly favor JSON.10. Best Practices
-
Resource Over Action: The Uniform Interface dictates we interact with resources. Do not create endpoints like
POST /api/createUser. Instead, usePOST /api/users. The action is defined by the HTTP Method (POST), and the resource is defined by the URL (users).
11. Common Mistakes
-
Ignoring the Uniform Interface: Many developers claim to build "REST APIs" but ignore the rules. If your API relies entirely on POST requests (e.g.,
POST /api/getUsers,POST /api/deleteUser), you are NOT building a REST API. You are building an RPC (Remote Procedure Call) API disguised as REST. A true REST API uses GET, PUT, and DELETE appropriately.
12. Exercises
- 1. Review the six constraints of REST. Which constraint is explicitly responsible for allowing applications to scale horizontally across multiple servers behind a load balancer?
13. Coding Challenges
- Challenge: Look at the following two API endpoints. Identify which one violates the "Uniform Interface" constraint of REST by mixing verbs and nouns in the URL, and identify the correct RESTful version.
-
Endpoint A:
POST /api/update-user-password/42
-
Endpoint B:
PUT /api/users/42/password
14. MCQs with Answers
Question 1
Which of the six REST architectural constraints is the ONLY one considered optional?
Question 2
The "Layered System" constraint of REST ensures that:
15. Interview Questions
- Q: Name the six architectural constraints that define a RESTful system. Explain how the "Uniform Interface" constraint differs from traditional RPC (Remote Procedure Call) architectures.
- Q: Explain the concept of HATEOAS (Hypermedia As The Engine Of Application State). How does implementing HATEOAS make a REST API more discoverable for client applications?