192

I learned REST and it feels a lot like CRUD (from what I have read about CRUD).

I know they are different, and I wonder if thinking they are similar means I don't understand them.

Is it that REST is a "superset" of CRUD? Does it do everything CRUD does and more?

1
  • 22
    Thinking they are similar means that you do understand them. In reading the answers, I see a surprising and what I consider to be incorrect level of not acknowledging the similarities between the concepts. I believe that the correct way to understand REST is to think of it as "CRUD for HTTP resources". If you understand what an HTTP resource is (its not the same as a database record obviously) and you know what CRUD is, then describing REST as "CRUD for HTTP resources" is a correct and succinct way to convey the essence of REST. May 7, 2014 at 8:30

6 Answers 6

235

Surprisingly, I don't see in the other answers what I consider the real difference between REST and CRUD: what each one manages.

CRUD means the basic operations to be done in a data repository. You directly handle records or data objects; apart from these operations, the records are passive entities. Typically it's just database tables and records.

REST, on the other hand, operates on resource representations, each one identified by an URL. These are typically not data objects, but complex object abstractions.

For example, a resource can be a user's comment. That means not only a record in a 'comment' table, but also its relationships with the 'user' resource, the post that comment is attached to, maybe another comment that it responds to.

Operating on the comment isn't a primitive database operation, it can have significant side effects, like firing an alert to the original poster, or recalculating some game-like 'points', or updating some 'followers stream'.

Also, a resource representation includes hypertext (check the HATEOAS principle), allowing the designer to express relationships between resources, or guiding the REST client in an operation's workflow.

In short, CRUD is a set primitive operations (mostly for databases and static data storages), while REST is a very high-level API style (mostly for web services and other 'live' systems).

The first one manipulates basic data, the other interacts with a complex system.

7
  • 3
    @Javier Thanks for setting them apart. I used REST learning Rails and I got the impression it was a replacement for CRUD (which I learned about since... the name that is, I was already using it, just didn't know what to call it)... You turned REST vs CRUD from comparing 2 apples to comparing apples and oranges. Thanks Nov 21, 2011 at 5:44
  • 2
    @Maudicus: i think it's very common, since RoR includes a CRUD layer (as most (every?) framework does), and it makes it easy (automatic?) to add a REST API on top of that, it's easy to think that's all what REST is. But then you can add functionality on top of CRUD but behind the REST API, making them more and more different.
    – Javier
    Nov 22, 2011 at 1:07
  • 1
    Your answer is correct, but the example isn't optimal: a comment can boil down to a single db row, and isn't it possible to implement dynamic changes to related objects with db triggers? I feel there's a little more than just crud operations in restful api though, and your answer clearly carry that feeling well.
    – didierc
    Jul 21, 2014 at 6:42
  • 2
    So...same thing, different layer :) Aug 8, 2014 at 12:08
  • Thank you so much for expressing this! I'd add that the limitation of HTTP verbs to CRUD operations result in implementing REST literally as CRUD, with lots of tools that generalize the CRUD boilerplate and miss a place for this custom "operating on a comment" logic.
    – sompylasar
    May 29, 2017 at 0:51
109

First of all, both are simply common initials; they're nothing to be afraid of.

Now, CRUD is a simple term that was abbreviated because it's a common feature in many applications, and it's easier to say CRUD. It describes the 4 basic operations you can perform on data (or a resource). Create, Read, Update, Delete.

REST however, is a named practice (just like AJAX), not a technology in itself. It encourages use of capabilities that have long been inherent in the HTTP protocol, but seldom used.

When you have a URL (Uniform Resource Locator) and you point your browser to it by the address line, you're sending an HTTP request. Each HTTP request contains information that the server can use to know which HTTP response to send back to the client that issued the request.

Each request contains a URL, so the server knows which resource you want to access, but it can also contain a method. A method describes what to do with that resource.

But this "method" concept wasn't used very often.

Usually, people would just link to pages via the GET method, and issue any type of updates (deletions, insertions, updates) via the POST method.

And because of that you couldn't treat one resource (URL) as a true resource in itself. You had to have separate URLs for deletion, insertion or update of the same resource. For example:

http://...com/posts/create- POST request  -> Goes to posts.create() method in the server
http://...com/posts/1/show- GET request  -> Goes to posts.show(1) method in the server
http://...com/posts/1/delete - POST request  -> Goes to posts.delete(1) method in the server
http://...com/posts/1/edit- POST request  -> Goes to posts.edit(1) method in the server

With REST, you create forms that are smarter because they use other HTTP methods aside of POST, and program your server to be able to distinguish between methods, not only URLS. So for example:

http://...com/posts - POST request  -> Goes to posts.create() method in the server
http://...com/posts/1 - GET request  -> Goes to posts.show(1) method in the server
http://...com/posts/1 - DELETE request  -> Goes to posts.delete(1) method in the server
http://...com/posts/1 - PUT request  -> Goes to posts.edit(1) method in the server

Remember, a single URL describes a single resource. A single post is a single resource. With REST you treat resources the way they were meant to be treated. You're telling the server which resource you want to handle, and how to handle it.

There are many other features to "RESTful architecture", which you can read about in Wikipedia, other articles or books, if you're interested. There isn't a whole lot more to CRUD itself, on the other hand.

4
  • 5
    sorry, but REST is a lot more than CRUD. mostly because resources embody a lot more than a single record each, and each operation does a lot more than updating a record.
    – Javier
    Nov 21, 2011 at 3:23
  • 13
    Ok. I agree. Why are you sorry? I didn't say that it wasn't a lot more than CRUD. I think that's exactly what I did say. Nov 21, 2011 at 17:36
  • 4
    This should be the right answer.
    – Brandon
    May 9, 2014 at 16:11
  • The HTML specification only allows the GET and POST methods for form submission, so the other methods were not used in services handling requests from webclients before AJAX became widespread. Some services use a hidden input field with the name "_method" as a workaround to specify a method other than POST while still submitting the form using the POST method. Sep 24, 2019 at 21:28
22

REST stands for "representational state transfer", which means it's all about communicating and modifying the state of some resource in a system.

REST gets quite involved, because the theory behind REST gets into leveraging media, hypermedia, and an underlying protocol to manage information on a remote system.

CRUD, on the other hand, is a mnemonic for the common operations you need for data in a database: Create Retrieve Update Delete. But it really doesn't get any deeper than that.

So that's the answer to your question, but I'll mention the common mistake I see when REST and CRUD are discussed together. A lot of developers want to map REST to CRUD directly, because REST over HTTP provides for GET PUT POST and DELETE, while CRUD provides for CREATE RETRIEVE UPDATE DELETE. It's natural to want to map the REST verbs directly to CRUD operations.

However, HTTP uses a "create or update" style, while CRUD separates create and update. That makes it impossible (!) to make a clean, general mapping between the two (!)

GET and DELETE are easy... GET === RETRIEVE, and DELETE === DELETE.

But per the HTTP spec, PUT is actually Create AND Update:

  • Use PUT to create a brand new object when you know everything about it, including its identifier

  • Use PUT to update an object (usually with a complete representation of the object)

POST is the "processing" verb, and is considered the "append" verb:

  • Use POST to append a new object to a collection -- that is, create a new object

  • POST is also used when none of the other verbs quite fit, as the HTTP spec defines it as the "data processing" verb

  • If your team is getting hung up on POST, remember that the entire WWW was built on GET and POST ;)

So while there is similarity between REST and CRUD, the mistake I see most teams make is to make an equivalence between the two. A team really needs to be careful when defining a REST API not to get too hung up on the CRUD mnemonic, because REST as a practice really has a lot of additional complexity that doesn't map cleanly to CRUD.

7

CRUD specifies a minimal set of basic storage verbs for data reading and writing: create, read, update and delete. Then, you can build other operations by aggregating these. These are usually considered database operations, but what is considered a database is arbitrary (e.g., could be a relational DBMS, but could also be YAML files).

REST is an "architectural style" that usually includes CRUD operations and other, higher level operations, all to be performed on some concept of "resources" (arbitrary, but these are entities in your application). REST has a bunch of constraints that make it interesting (and particularly well-paired with HTTP).

A REST interface can, but doesn't have to, expose all CRUD operations on a particular resource. What is available in a REST interface is arbitrary and may change due to system permissions, UI considerations, and how hot it was on the day the interface was designed and created. Hotter days lead to more minimalist interfaces, usually, though the opposite can be true.

4
  • Thanks Yar. It seems my "Does everything CRUD does and more?" is a yes, with a technicality of REST applies to more than just entries in a database. Nov 20, 2011 at 7:06
  • @Maudicus I updated the answer, but to be specific: it can but does not HAVE to. Nov 20, 2011 at 7:07
  • I wouldn't say that they're required for your application to be considered complete. Some applications don't need insertions, removals or updates, by nature. Nov 20, 2011 at 7:37
  • @Yam Marcovic, okay, adjusted Nov 20, 2011 at 8:39
7

CRUD

  • CRUD is four basic types of SQL commands: Create, Read, Update, and Delete
  • Most applications have some kind of CRUD functionality
  • A CRUD application is one that uses forms to get data into and out of a database

REST

  • REST stands for Representational State Transfer.(It is sometimes spelled "ReST")

  • It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used

  • REST is an architecture style for designing networked applications

2

REST is something like a webpage for machines, which they can browse, while CRUD is something like SOAP, which is strongly coupled to its clients. These are the main differences. Ofc. they are similar on the surface, but CRUD describes basic entity manipulation, while REST can describe the interface of any application. Another difference that REST can use more the 4 HTTP methods. It would be a very long answer if I'd want to collect all the differences, if you check the questions about REST vs SOAP, then you will find most of them.

I think confusing REST with CRUD is a very common mistake and the cause is that developers don't have the time to read about REST in depth. They just want to use the technology - without understanding it - based on limited CRUD style examples written by similar developers. The vast majority of the examples and tutorials are reflecting serious lack of knowledge. Mapping REST resources to entities and HTTP methods to CRUD operations of these entities and using REST without hyperlinks is just a symptom of that. By REST you map hyperlinks (including links with POST/PUT/DELETE/PATCH methods) to your operations and you identify the operation on the client side by checking the (usually API specific) link relation. If a REST client does not know what a link relation is, and knows only the HTTP methods and maybe some URI templates, then that is not a REST client, but a CRUD on HTTP client. Another common mistake that a REST client is a single page javascript application running in the browser. Of course you can implement such a client, but REST was meant mainly for automatic clients (server side applications written by developers you don't even know) and not for manual clients (user controlled browser applications written by you). Having only a single browser client might be a sign that you don't really need REST and you just overengineered the project. In these cases a CRUD API is a viable solution and developers are calling these CRUD APIs as REST, because they don't know the difference.

Not the answer you're looking for? Browse other questions tagged or ask your own question.