What is CRUD?
CRUD is an acronym for Create, Read, Update, and Delete, the four fundamental operations of data storage. Computer scientists use CRUD as a measure of an application's completion state. If an application can't perform CRUD operations, it is incomplete.
CRUD requires that four fundamental operations can be performed by an application. They are:
- Create - the operation creates a new record in the database.
- Read - the operation reads information from a record in the database.
- Update - the operation changes a record's information in the database.
- Delete - the operation removes a record from the database.
What is REST?
A RESTful API is an acronym for Representational State Transfer. REST is an architectural style which defines a set of architectural constraints for stateless communication between Application Programming Interfaces, or APIs. It is not a standard, so it allows developers some flexibility, but it acts as a mediator between users, clients, and resources. REST has become ubiquitous in API programming because it emphasizes scalability and greater interoperability.
A REST API sends HTTP requests to endpoints, and receives JSON files in return. The most common requests (or HTTP verbs) are:
- GET request - This request reads information from a record in the database.
- PUT request - This request changes a record's information in the database.
- POST request - This request creates a new record in the database.
- DELETE request - This request removes a record from the database.
You can see how the GET, PUT, POST, and DELETE requests are roughly equivalent to CRUD, but let's use an example to examine their differences.
REST vs CRUD
Let's say we're building a web business that sells shoes. When a customer visits our website, we want to provide up-to-date inventory information on shoes. This application must include CRUD to be considered complete, but will use the REST architectural style in negotiating HTTP methods, URIs (Universal Resource Identifiers), and database records.
What do those operations look like in CRUD and a RESTful architecture?
- Create- We're adding green shoes to our inventory, so we need a function that creates a new record. The new record contains values for shoe size and the number of shoes in inventory. In REST terms, this is a POST method to add a new resource to our relational databases on the server side.
- Read- When a customer views our new green shoes, a function reads the shoe record, so the customer knows if it's available. In REST terms, this is a GET request.
- Update- The customer loves the green shoes. Great! When they buy the shoes, a function updates the shoe record. We might also use this event to trigger some webhooks. In REST terms, this is a PUT request.
- Delete- Green dye has gotten expensive, so we're no longer selling green shoes. Delete removes the record from the database. In REST terms, this is a DELETE request.
So, what's the difference?
REST refers to manipulating data through HTTP commands, and an ideology for how to streamline the relationship between the data that a user is manipulating on screen and the information that is saved on the server. A programmer can create a REST API that handles basic CRUD functions, but building a CRUD application in no way insinuates the use of RESTful programming.
CRUD describes an application's functions, not the relationship between web services, web applications, or APIs. In this way, CRUD is a relic of the monolithic era, when applications lived in one place, while APIs are better at describing client-server applications in the microservices era, where programs live across servers and clusters.
It's easy to confuse CRUD and REST operations because their operations are roughly analogous to each other, but if you remember that REST is a set of standards, and CRUD is a set of functions, you'll be on your way to a deeper understanding.