SOAP and gRPC — Contract-First Calls
BookNook has to talk to two outsiders. A national library network, built in the 2000s, only speaks SOAP. Inside BookNook, the search service must answer the loans service thousands of times a second. The first is heavy and old, the second fast and modern, yet both share one idea: the contract is written before the code, and the call looks like calling a function on another machine.
The rule
Write the contract first; generate the code; call remote functions.
Both styles are forms of RPC (remote procedure call): instead of acting on resources with HTTP verbs, you call named operations like GetBook or ReserveTitle.
SOAP sends XML messages wrapped in an envelope, almost always as POST. Its contract is a WSDL file, an XML description of every operation and type. Tools read the WSDL and generate client code.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<ReserveTitle><isbn>9780261103344</isbn></ReserveTitle>
</soap:Body>
</soap:Envelope>SOAP brings formal standards for security and reliable messaging, which is why banks and governments adopted it. It is also verbose and awkward from a browser.
gRPC writes its contract in a .proto file using Protocol Buffers, a compact binary format, and runs over HTTP/2:
service Search {
rpc FindBooks (FindRequest) returns (FindReply);
rpc WatchAvailability (BookId) returns (stream Availability);
}
message FindRequest { string text = 1; int32 limit = 2; }- Code for many languages is generated from the
.proto, so client and server cannot disagree on field names or types. - Messages are small and quick to parse.
- HTTP/2 allows streaming: the server, the client, or both can send a stream of messages, as
WatchAvailabilitydoes. - Browsers cannot call gRPC directly; they need a translating proxy such as gRPC-Web.