Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use TCP in a RESTful service?

Tags:

rest

c#

.net

wcf

REST is using current features of the Web and applying some principles on it to make it more efficient. It uses standard HTTP verbs for communication and take help of its stateless nature.

However, is it possible that a REST service use the TCP protocol for its communication? If yes, then will it violate its principles?

like image 911
funsukvangdu Avatar asked Dec 26 '12 09:12

funsukvangdu


People also ask

Does a REST API use TCP?

The TCP/IP Data REST API allows you to view and manipulate TCP connection data. Operations include: Create, update, or delete a TCP connection counter.

Does Web API support TCP protocol?

Supports HTTP, TCP, UDP and custom transport protocol. Uses attributes based programming model. Uses routing and controller concept similar to ASP.NET MVC. Uses Service, Operation and Data contracts.

What protocol is used in RESTful API?

When a request for data is sent to a REST API, it's usually done through hypertext transfer protocol (commonly referred to as HTTP). Once a request is received, APIs designed for REST (called RESTful APIs or RESTful web services) can return messages in a variety of formats: HTML, XML, plain text, and JSON.

Is API a TCP or UDP?

APIs are like the TCP/IP suite for applications. It specifies how applications interact and exchange data between each other.


5 Answers

HTTP is a TCP/IP based protocol. So when you use REST you are already using TCP for communication. But if you want to use REST over pure TCP socket, without HTTP, then no, this doesn't make sense because REST is based on HTTP verbs and headers. Those notions exist only in the HTTP protocol.

like image 142
Darin Dimitrov Avatar answered Oct 18 '22 22:10

Darin Dimitrov


REST is an architectural style (or set of constraints). It just so happens that HTTP can match all of those constraints easily. And on top of that a lot of HTTP/1.1 infrastructure out there is already supporting it: servers, proxies, caches, client libraries, parsers, etc. Something like this:

HTTP and REST relationship

Can systems be built from scratch be to RESTful and not rely on HTTP? Sure. Coming from the authoritative source on the topic Roy Fielding himself:

A REST API should not be dependent on any single communication protocol.

If you read the article or in fact Roy's dissertation you would realize that if you try to follow all of the constraints you would end up with something that looks and behaves pretty much like the modern HTTP though it would probably lack most of the infrastructure support that HTTP has. Hence the question: Is it worth it?

Also if you take a look at the majority of the RESTful services out there they are very rarely fully REST services. This is why they call themselves "RESTful services", and not "REST services". BTW This site's API comes very close to a full REST implementation.

like image 13
Георги Кременлиев Avatar answered Oct 18 '22 20:10

Георги Кременлиев


However, is it possible that a REST service use the TCP protocol for its communication? If yes, then will it violate its principles?

Short Answer

No it will not violate its principles if you write it according to REST principles. Yes it will violate its principles if you do not follow REST principles. Your code on the client and server side will have to obey REST rules. For example, if I send a "GET" to "...employee/22", it better send me a REST response e.g. 200, headers and content type etc. And doing all that will be basically doing what HTTP does.


Long Answer

The answer to your question is provided by @ГеоргиКременлиев. I actually think that might be the only correct answer.

It is true that REST is mostly tied to HTTP and when people speak about REST they're talking about HTTP. But, "mostly" does not mean "always" and even if it was "always" it still does not mean that REST is not possible without HTTP and that is pointed out in the links from @ГеоргиКременлиев answer. HTTP worked so well (Client and Server), the principles were taken to create any client/server application with those principles so they can enjoy the same benefits as HTTP. In other words,

  1. Any client can be designed to understand REST (For example, browsers understand REST)
  2. Any server can be designed to understand REST (For example, web servers understand REST)

Here is an important bit, REST principles were borrowed from HTTP. Therefore, if you are RESTful, it means you are following HTTP principles.

Having said that, if you do not want to use HTTP, then your client and/or server both have to understand the same language that HTTP speaks. For example, it uses verbs such as GET, POST, PUT etc. and response codes such as 200, 300, 400, and so on. Therefore, your server needs to understand how to respond to a request such as ...employee/22 and return:

  • 200 OK, 301 moved permanently, 500 etc...
  • Content-Type
  • other headers needed

Your client also needs to understand those headers to be able to send requests to the server and consume responses from the servers.

And if you do implement all this, and security (authentication and authorization etc.), and chaching and the list goes on, you will realize you are just trying to re-invent the wheel: HTTP.

Is that worth it? Are you sure the bottleneck regarding performance was the HTTP protocol or was it your backend code, the queries to the database etc?

like image 2
CodingYoshi Avatar answered Oct 18 '22 21:10

CodingYoshi


Slightly different angle:

1 . Don't use WCF to create REST based services. Use Asp.Net WebAPI.

2 . Just for fun: if you want to use WCF TCP bindings with 'REST' principles in mind, maybe you can create a stateless API based on 'resources' instead of a typical RPC like one.

[ServiceContract]
interface RestApi
{
    Result Get(string id);
    Result Post(string id, Resource resource);
    Result Put(string id, Resource resource);
    void Delete(string id);
}

That way you won't have to define different contract for every service and just adjust your resources for different interactions.

NOTE: I am not suggesting anyone to do this: it's reinventing the wheel. Use WebAPI if you want REST.

like image 1
ziya Avatar answered Oct 18 '22 21:10

ziya


As Darin already answered, HTTP is a TCP protocol with some overhead that is exactly what is used in the RESTful definition. So, no, you can't remove the HTTP overhead.

I believe that your question "Can I use TCP for a faster RESTful app?" is related with the question "Why so many websites are using REST if HTTP is slower than pure TCP?".

The truth is: HTTP is indeed slower than the pure binary TCP form, but in most applications, your users will not notice the difference because the overhead is really very small and usually the client will make just a few requests per minute.

For example: GET /posts?userId=5

If this request takes more than a few milliseconds to complete, then the problem is not in the HTTP protocol. The performance problem is related with the network latency, with your server-side code or how you are retrieving data from your database.

like image 1
Zanon Avatar answered Oct 18 '22 21:10

Zanon