3 min read

How Ordering Food Explains REST

Woman pointing at a menu while placing an order with a server at a restaurant.
A woman ordering from a menu Photo by Fraser Cottrell / Unsplash

Imagine you’re ordering food from a restaurant. You, as the client, walk up to the counter (the server) and ask for a specific dish. The restaurant (server) then prepares the dish and gives it back to you. You don’t need to know the details of how the food is made; you just need to ask for what you want and receive it. This is the core idea behind REST—an architectural style for designing web services.

In this post, I’m going to break down how REST works and how it can be thought of as the system behind the interaction between clients (users) and servers (systems), much like how a restaurant works with its customers.

What Are Resources?

In the REST world, resources are like the items on a restaurant menu. They are specific things (like blog posts, images, or user data) that the server will give you when you ask for them. Each resource has a unique name, which is like its URI (Uniform Resource Identifier). For example, if you want to access a specific blog post, the URI might look like this: www.blog.com/post/1. It's like ordering a specific dish from the menu!

Uniform Interface: The Standard Way to Order

Just like you would order food using standard words like “Please give me the spaghetti,” REST uses standard actions to interact with resources. These actions are the HTTP methods:

  • GET: Get data (like ordering a dish)
  • POST: Create new data (like asking for a custom dish)
  • PUT: Update data (like asking for a dish with extra sauce)
  • DELETE: Remove data (like canceling an order)

Each action is like a way to communicate with the server to either retrieve, send, modify, or delete data. The key here is that these actions are consistent and predictable, just like how we all know how to order food in a restaurant.

Stateless: Every Order is Independent

Imagine every time you order food, you have to repeat your entire order each time. The server doesn’t remember your last meal or what you ordered previously. This is how REST works—stateless means that each request from the client to the server must include all the information necessary to complete that request. The server doesn’t store any previous context.

So, just like a restaurant doesn’t remember what you ate last time, the server doesn’t remember anything from your previous requests.

Client-Server: Separate Roles, Clear Responsibilities

In REST, just like in a restaurant, the client (you) and the server (restaurant) have different responsibilities. The client asks for a specific dish (or data), and the server processes that request and delivers it. The client is responsible for ordering (requesting), and the server is responsible for preparing and delivering (providing the data).

By separating these responsibilities, we improve the system’s scalability and flexibility—just like how the restaurant can focus on cooking while you enjoy ordering from a menu.

Cacheable: Save for Later

When you order your favorite dish, imagine the server telling you, “Hey, you can keep this order for later use, so you don’t need to reorder every time.” This is what cacheable means in REST. If the data doesn’t change, it can be saved (cached) on the client side, so the client doesn’t have to request the same data again and again, making things faster and more efficient.

Layered System: Behind-the-Scenes

The restaurant operates in layers. You, as the customer, only interact with the counter staff. You don’t need to know what happens in the kitchen or how orders are processed in the back. This separation helps with scalability and maintainability. In a RESTful system, we use a similar layered approach where each component (client, server, database, etc.) interacts only with the next layer. This keeps things clean and organized.

While REST is often used with HTTP (the protocol of the web), they are not the same. REST is a set of rules, and HTTP is a protocol used to implement those rules. Think of it like how a restaurant follows specific rules for ordering food, but the actual process may happen differently depending on the restaurant.

In Summary

In the RESTful world, everything revolves around resources—data or functionality that can be named and accessed via URIs. Clients interact with servers using standard HTTP methods (GET, POST, PUT, DELETE), and the system follows principles like statelessness and cacheability to improve efficiency.

Just like ordering food at a restaurant, REST ensures that data can be requested, modified, and removed in a consistent, simple way. And much like a restaurant system, REST ensures that clients and servers can evolve independently, keeping everything scalable and efficient.