Skip to Content

Is REST API TCP or UDP?

REST (Representational State Transfer) API is a software architectural style for building web services. REST API relies on a stateless client-server protocol, almost always HTTP. HTTP can run on top of either TCP or UDP transport protocols. So which one does REST use – TCP or UDP?

REST API has become the standard for building web services and web APIs. Its simple architecture has made it popular for connecting applications over the internet. Understanding whether REST uses TCP or UDP helps clarify its design and performance.

This article will provide a definitive answer on whether REST APIs use TCP or UDP as their underlying transport protocol. We’ll cover:

  • An overview of REST API architecture
  • Key differences between TCP and UDP protocols
  • Why HTTP and REST APIs rely on TCP rather than UDP
  • Advantages of TCP for REST APIs
  • Examples of REST APIs using TCP

By the end, you’ll have a clear understanding of why REST APIs overwhelmingly use TCP as their transport protocol.

REST API Architecture Overview

First, let’s provide some background on REST API architecture. REST stands for Representational State Transfer. It’s an architectural style for building distributed, networked systems and web services.

Some key principles of REST include:

  • Client-server separation – REST systems have a client-server architecture. Clients initiate requests to servers, which process requests and return responses.
  • Statelessness – No client context is stored on the server between requests. Each request contains all necessary information.
  • Cacheable – REST responses should be cacheable to improve performance.
  • Uniform interface – All resources (data objects) are accessible through a uniform interface, typically HTTP.

REST APIs expose data resources through a uniform interface, which is almost always HTTP. Client applications access and manipulate those resources through standard HTTP methods like GET, POST, PUT, and DELETE.

Because REST is built on HTTP, it relies on the underlying HTTP transport protocol to send requests and responses. Most of the time, that transport is TCP.

Key Differences Between TCP vs UDP

To understand why REST favors TCP over UDP, we need to highlight some key differences between the two protocols:

TCP

  • Connection-oriented – A reliable connection is established between client and server before data transfer.
  • Guaranteed delivery – Packets are guaranteed to be delivered in order to the destintation.
  • Flow control – Data transfer rate is managed to prevent overwhelimg the receiver.
  • Congestion avoidance – TCP throttles data transfer to avoid congesting the network.
  • Error checking – Checksums validate packet integrity and request retransmissions if needed.

UDP

  • Connectionless – No connection is established beforehand between client and server.
  • Unreliable delivery – Packets may be lost or arrive out of order.
  • No flow control – Senders can overwhelm receivers by sending too much data.
  • No congestion avoidance – UDP does not throttle transfer rates, which can congest networks.
  • No error checking – Packets that are corrupted, lost, duplicated, or delayed won’t be detected.

In summary, TCP provides reliable, ordered delivery while UDP does not. For most types of data communication on the internet, especially web traffic, reliability is a key requirement. This is why TCP is the dominant protocol underlying HTTP and REST APIs.

Why HTTP and REST Use TCP

There are several key reasons why HTTP and REST APIs rely on TCP rather than UDP as their transport protocol:

Reliability

TCP guarantees reliable delivery of data packets. This ensures that clients receive the full responses sent by the server. Lost or corrupted packets are detected and retransmitted using TCP.

This reliability is essential for HTTP requests and responses. Losing part of a REST API response could break client applications that depend on receiving complete JSON or XML payloads.

Ordered Data Delivery

TCP ensures packets are delivered in the order they were sent. This preserves data sequences and maintains integrity. Out of order packets could break applications.

Again, for HTTP messages and API payloads, ordered delivery is necessary. REST responses contain structured data like JSON that depends on proper sequencing.

Congestion Avoidance

TCP implements congestion avoidance algorithms that prevent flooding the network with more data than it can handle. This maintains performance and fairness across the internet.

Without congestion control, HTTP-based traffic could overwhelm internet infrastructure. So TCP is crucial for preventing REST APIs and web services from destabilizing the internet.

Error Checking

TCP checksums allow detection of any corrupted data packets. Damaged packets are then retransmitted. This error checking ensures the integrity of data transfer.

For REST API communications, the ability to detect and correct errors is important. Checksums provide that validation mechanism when using TCP as the transport protocol.

Advantages of TCP for REST APIs

Given REST’s dependence on HTTP, TCP is the logical transport protocol choice. Specifically, TCP gives REST APIs the following advantages:

  • Reliable data transfer ensures clients receive complete API responses.
  • Ordered delivery maintains integrity of structured API payloads.
  • Flow control prevents API traffic from flooding the network.
  • Error checking rectifies problems with corrupted or missing packets.
  • HTTP compatibility leverages TCP’s benefits for web traffic.

In summary, TCP’s connection-oriented, reliable packet transmission aligns perfectly with the needs of HTTP and REST API communication across the internet.

Examples of REST APIs Using TCP

Most public REST APIs rely on TCP as their underlying transport protocol. For example:

Twitter API

The Twitter REST API enables developers to integrate Twitter data into applications. GET requests retrieve tweets, users, trends and other Twitter data as JSON over HTTP.

All communication between Twitter clients and servers occurs over TCP to provide reliable transport.

Stripe API

Stripe’s REST API allows accepting payments within other apps. The API uses HTTP requests/responses to create charges, refunds, customers, etc.

Stripe leverages TCP to reliably transmit payment data between its servers and client applications.

Twilio API

Twilio provides a REST API for its cloud communications platform. The API uses HTTP to make/receive calls, send messages, and get account data.

Under the hood, all Twilio API traffic rides on TCP for guaranteed, ordered delivery.

The overwhelming majority of public REST APIs use TCP in this fashion. It has proven the most effective transport protocol for enabling reliable HTTP communication.

Conclusion

In summary:

  • REST APIs rely on HTTP as their application layer protocol.
  • HTTP itself runs on top of a lower level transport protocol.
  • TCP is the predominant transport layer protocol used by HTTP and REST.
  • TCP provides reliable, ordered data delivery essential for web APIs.
  • UDP is unsuitable for REST due to lack of reliability mechanisms.
  • Leading REST APIs like Twitter, Stripe, and Twilio all use TCP.

So in answer to the original question – REST APIs use TCP as their transport protocol, not UDP. TCP guarantees reliable and ordered data transfer, which HTTP and REST require. UDP does not provide the error checking and congestion avoidance that’s crucial for web traffic. For these reasons, TCP is the near universal standard for underlying REST API communications.