Everything you need to know about REST

2707 views Designing μ-services

Is REST all about just exposing an HTTP endpoint?

REST is an abbreviation that stands for Representational State Transfer. A lot of complex words but stick with me.

REST is a specification that suggests how a client should demand/request information from the server and how the server should respond; it does not enforce anything.

Everything is a Resource

In REST, everything is a resource. Any actionable entity in your application eg: book, student, customer, or order is a resource for REST. The client can demand action on a resource like get, create, delete, or update.

REST does not put any restriction on how the data is stored in your application, but it allows clients to specify how it wants the data from the server eg: XML, JSON, CSV, etc. So long as the server supports the representation the server would respond in the demanded format.

Underlying Protocol for REST

REST does not enforce any restriction on the underlying protocol to be used. All it cares about is that we have a defined way to act on a resource. So, we can implement REST over HTTP or even hardware like USB.

Why REST over HTTP?

The most common implementation of REST is over HTTP and it gels very well together. Why so?

HTTP has verbs like GET, POST, DELETE, and PUT and these become actions on our resource. The resource is specified by the URL of the HTTP request. For example, to get a student’s details whose id = 1 we fire

GET /students/1

We are representing the student with id = 1 using the URL /students/1 and are specifying action GET using the HTTP verb GET. Similarly to update the student with id = 1 we could

POST /students/1

Here we see how REST is resource-oriented and we have a way to specify the action to be taken on it.

Existing tooling

One very important reason why HTTP is so commonly used in implementation is the availability of existing tooling. We can reuse the existing set of tools to get the job done, which includes.

  • using existing HTTP Clients like Postman, cURL, Requests
  • use caches like Ngnix, HA Proxy, and Varnish to boost performance
  • use monitoring tools like Distributed Tracing
  • use load balancers to uniformly distribute the load
  • use SSL to get out-of-the-box security

Downsides of doing REST over HTTP

  • all clients would have to serialize and deserialize the HTTP body
  • all clients would have to repetitively handle failures and retries
  • some web servers might not support verbs like PUT, DELETE
  • HTTP payloads are JSON and hence huge
  • HTTP would not support switching underlying protocols

Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 17000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.

Other essays that you might like

BFF - Backend for Frontend - Pattern in Microservices

2225 views 114 likes 2022-07-04

As your application evolves, supporting multiple types of clients like Desktop, Mobile apps, etc becomes tricky. The bac...

Best practices that make microservices integration easy

850 views 50 likes 2022-06-27

Running microservices in isolation does not make any sense. To get something done, multiple microservices need to talk t...

Things to remember while building Microservices

856 views 35 likes 2022-06-20

An engineer working on Microservices should not only just focus on engineering; there are so many other aspects to look ...

Why should we have a standard way of building Microservices?

631 views 27 likes 2022-06-17

We all love creating microservices, but what if every team creates its own microservice uniquely and uses its own conven...

Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

Enrolled by 700+ learners

Details →

Designing Microservices

A free course to help you understand Microservices and their high-level patterns in depth.

Enrolled by 17+ learners

Details →

GitHub Outage Dissections

A free course to help you learn core engineering from outages that happened at GitHub.

Enrolled by 67+ learners

Details →

Hash Table Internals

A free course to help you learn core engineering from outages that happened at GitHub.

Enrolled by 25+ learners

Details →

BitTorrent Internals

A free course to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

Enrolled by 42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.

Arpit's Newsletter read by 17000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.

  • v12.4.4
  • © Arpit Bhayani, 2022

Powered by this tech stack.