One of the common questions asked during my #JavaEE7 presentations around the world is how do WebSockets compare with REST ?
First of all, REST is a style of architecture so what really people mean is RESTful HTTP. As an architecture cannot be compared with a technology. But the term is so loosely used that they are used in place of each other commonly.
Lets start with a one line definition for WebSocket …
Bi-directional and full-duplex communication channel over a single TCP connection.
WebSocket solves a few issues with REST, or HTTP in general:
- Bi-directional: HTTP is a uni-directional protocol where a request is always initiated by client, server processes and returns a response, and then the client consumes it. WebSocket is a bi-directional protocol where there are no pre-defined message patterns such as request/response. Either client or server can send a message to the other party.
- Full-duplex: HTTP allows the request message to go from client to server and then server sends a response message to the client. At a given time, either client is talking to server or server is talking to client. WebSocket allows client and server to talk independent of each other.
- Single TCP Connection: Typically a new TCP connection is initiated for a HTTP request and terminated after the response is received. A new TCP connection need to be established for another HTTP request/response. For WebSocket, the HTTP connection is upgraded using standard HTTP Upgrade mechanism and client and server communicate over that same TCP connection for the lifecycle of WebSocket connection.
- Lean protocol: HTTP is a chatty protocol. Here is the set of HTTP headers sent in request message by Advanced REST Client Chrome extension.
1234567891011POST /websocket-vs-rest-payload/webresources/rest HTTP/1.1\r\nHost: localhost:8080\r\nConnection: keep-alive\r\nContent-Length: 11\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36\r\nOrigin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo\r\nContent-Type: text/plain \r\nAccept: */*\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: en-US,en;q=0.8\r\n\r\n
And the response headers received from WildFly 8:
12345678HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nX-Powered-By: Undertow 1\r\nServer: Wildfly 8 \r\nContent-Type: text/plain\r\nContent-Length: 11 \r\nDate: Fri, 21 Feb 2014 21:27:53 GMT \r\n\r\n
These are 663 characters exchanged for a trivial “Hello World” echo. The source code for this simple application is here.
For WebSocket, after the initial HTTP handshake, the data is minimally framed with 2 bytes.
Lets take a look at a micro benchmark that shows the overhead caused by REST over a WebSocket echo endpoint. The payload is just a simple text array populated with ‘x’. The source code for the benchmark is available here.
The first graph shows the time (in milliseconds) taken to process N messages for a constant payload size.
Here is the raw data that feeds this graph:
This graph and the table shows that the REST overhead increases with the number of messages. This is true because that many TCP connections need to be initiated and terminated and that many HTTP headers need to be sent and received. The last column particularly shows the multiplication factor for the amount of time to fulfill a REST request.
The second graph shows the time taken to process a fixed number of messages by varying the payload size.
Here is the raw data that feeds this graph:
This graph shows that the incremental cost of processing the request/response for a REST endpoint is minimal and most of the time is spent in connection initiation/termination and honoring HTTP semantics.
Together the graph also shows that WebSocket is a more efficient protocol than RESTful HTTP. But does that mean it will replace RESTful HTTP ?
The answer to that, at least in the short term is, NO!
- WebSocket is a low-level protocol, think of it as a socket on the web. Every thing, including a simple request/response design pattern, how to create/update/delete resources need, status codes etc to be build on top of it. All of these are well defined for HTTP.
- WebSocket is a stateful protocol where as HTTP is a stateless protocol. WebSocket connections are know to scale vertically on a single server where as HTTP can scale horizontally. There are some proprietary solutions for WebSocket horizontal scaling, but they are not standards-based.
- HTTP comes with a lot of other goodies such as caching, routing, multiplexing, gzipping and lot more. All of these need to be defined on top of WebSocket.
- How will Search Engine Optimization (SEO) work with WebSocket ? Works very well for HTTP URLs.
- All proxy, DNS, firewalls are not yet fully aware of WebSocket traffic. They allow port 80 but might restrict traffic by snooping on it first.
- Security with WebSocket is all-or-nothing approach.
This blog does not provide any conclusion because its meant to trigger thoughts!
And if you want a complete introduction to JSR 356 WebSocket API in Java EE 7, then watch a recently concluded webinar at vJUG:
So, what do you think ?