REST stands for Representational State Transfer. REST works on a stateless client server
cacheable communication protocol which is basically the HTTP protocol.
If all this while, one was wondering if SOAP or RPC were some of the only ways to connect between machines, infact REST makes simple HTTP calls between machines for communication.
Rest is said to be an architectural style for designing networked application.
A very good example which i read about the difference between REST and SOAP is
the difference between mailing a letter using an envelope and a postcard.
Letter being the message of communication, envelope being SOAP and postcard being REST.
Postcards are easier to handle by the reciever, wastes less paper(takes
less bandwidth) and have a short content. If in between all this, if one
thinks that REST is not as secure as SOAP, then its wrong. REST and SOAP with
proper encryption are equally secure.
The differnce between SOAP request and REST style of request for getting all the details of an employee is as below.
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:body pb="http://www.acme.com/phonebook">
<pb:GetUserDetails>
<pb:UserID>12345</pb:UserID>
</pb:GetUserDetails>
</soap:Body>
</soap:Envelope>
With REST, its as simple as this,.This URL is sent to the server using a simpler GET request,
and the HTTP reply is the raw result data -- not embedded inside anything,just the data you need in a way you can directly use
Now to make this more interesting, lets see what stateless means and whats the advantage of having a stateless client server protocol
Stateless means the server will not keep the state information of the client.
Considering the example of FileOpen and FileRead operations,
File f = FileOpen("myfile.dat");
byte[] firstK = FileRead(f, 1024);
byte[] secondK = FileRead(f, 1024);
So what does this do.. firstK will hold teh first 1024 bytes and secondK will hold the next 1024 bytes.This happens because the
server remembers for each open file, where is the file read head which is infact the state.
The same thing can be achieved using the stateless API
firstK = FileRead("myfile.dat", 0, 1024);
secondK = FileRead("myfile.dat", 1024, 1024);
Here the FileRead funtion accepts the filename, from where to read and till what amount it has to read.. This stateless approach looks
better because, firstly it survives with a server restart... Secondly there is no client server binding... So each request can be handled by different
server but still yield the same result..
The problem with stateful api clearly is, that the exact same machine has to handle all the requests or
the state must be replicated to all the servers in the cluster
No comments:
Post a Comment