Reasons to Choose gRPC over REST and How to Adopt It into Your Python APIs | by Dimitris Poulopoulos | Towards Data Science

For a long time now, we’ve had REST (REpresentational State Transfer). REST APIs are cozy, warm, friendly, and very flexible. And if I’m permitted to be a bit abrupt here, thank God I don’t have to use SOAP anymore!
So why on earth do we want to change everything again? Why do we keep inventing new, shiny stuff to replace tools that work so well? What is this gRPC thing now?
Well, the simple answer is, “there is no one size that fits all.” If you were to deliver a letter reliably and as fast as possible, how would you do it? The first idea that pops into my mind is that I would do it myself. Hop into my car, drive to the destination and ring the bell with my own fingers.
But maybe I don’t have the time to do it myself. So, I would probably call an Uber and use the corresponding service. Wait now; this becomes way too expensive. Also, what if the destination is on the other side of the world? Express mail service is the way to go in the end. Different solutions to the same problem.
In any case, knowing the many benefits and downsides of using one form of message passing technique over the other is crucial when designing the communication APIs between microservices. In this story, we see when to choose gRPC over REST and implement a gRPC service in Python.
Learning Rate is a newsletter for those who are curious about the world of AI and MLOps. You’ll hear from me every Friday with updates and thoughts on the latest AI news and articles. Subscribe here!
As we’ve already seen, REST stands for “REpresentational State Transfer.” It provides a set of guidelines on how to create web APIs. But that’s it, a set of guidelines. It does not try to enforce anything.
To return to our mail delivery analogy, there are some guidelines for writing a letter. You may start with the date, the info and the recipient’s address, and then a salutation. In the end, you may close the letter with your personal signature.
However, is there someone or something to enforce those rules? Would the mailman deny the delivery of a letter that does not conform to these guidelines? No! You could even draw a picture inside if you wanted to. After all, they say that a picture is worth a thousand words.
So what exactly are the properties of REST?
Client-Server: A client makes a request and receives a response from the server. The client doesn’t need to know how the server is implemented. It also doesn't need to know if the server is sending back cached responses or if its request went through one hundred layers before getting a response.Statelessness: Each request is self-contained, and it includes all the information necessary to process it. The server does not keep track of any context.Uniform Interface: The API interface should be consistent, documenting the message format, the endpoints, and the message headers.
Client-Server: A client makes a request and receives a response from the server. The client doesn’t need to know how the server is implemented. It also doesn't need to know if the server is sending back cached responses or if its request went through one hundred layers before getting a response.
Statelessness: Each request is self-contained, and it includes all the information necessary to process it. The server does not keep track of any context.
Uniform Interface: The API interface should be consistent, documenting the message format, the endpoints, and the message headers.
Designing a REST API in Python is really straightforward but out of the scope of this story. If you want to see how it’s done, refer to Python’s Flask documentation.
gRPC is a message-passing technique developed by Google. But the “g” in gRPC does not mean “google”; it’s actually pretty random. For example, the “g” in gRPC 1.12 stands for “glorious.”
gRPC is programming-language agnostic, and it seems to be the new trend in the microservices communication world. Compared to REST, gRPC provides greater performance at the expense of less flexibility.
So, gRPC does not provide a set of guidelines on how to create web APIs; it enforces rules. This time, the mailman would be very angry if you didn’t start the letter with a proper salutation!
That is its main advantage over REST: gRPC, in most cases, is way faster and more robust, as it defines a specific set of rules each request and response should adhere to.
Let’s now build a gRPC server in Python. The first step we should take is to define our message structure as a protobuf file. In this example, we assume that we are a bookstore, and we would like to get the books we have in our catalog and add new ones.
This file (i.e., book.proto) defines a new message, BookMessage, that must contain an ISBN code, a title, an author, a description, a price and a condition, and in that order.
Then, it defines a new service (i.e., BookService), which can create a new book or get all the existing ones.
Next, implementing gRPC with Python involves two libraries:
grpcio to run client and server code
grpcio-tools to generate definition code
You can install both of them with the following command:
pip install grpcio grpcio-tools
After installing these libraries, use the grpcio-tools one to generate the code you’ll need to build your gRPC server and client. Run the following command:
python -m grpc_tools.protoc -I./ --python_out=./ --grpc_python_out=./ book.proto
This command should produce two files:
book_pb2.py
book_pb2_grpc.py
It’s not a good idea to edit those files directly. If you need to change anything, change the .proto file and rerun the command to re-generate the two Python files.
We can now use these files to create our gRPC service:
Finally, let’s create a client to test our service:
The client creates a new BookMessage object and submits it to the server. From there, we assume that the server stores this new book inside the store’s catalog.
gRPC seems to be the new, cool kid in town, but it is not here to replace REST. In general, REST has wider adoption. On the other hand, gRPC is more structured and usually faster out-of-the-box.
Suppose you need speed or structure, use gRPC. So, a good use case for gRPC would be the internal communications of your microservices. On the other hand, it’s better to provide a REST endpoint to the outside world, as REST is something everybody uses.
My name is Dimitris Poulopoulos, and I’m a machine learning engineer working for Arrikto. I have designed and implemented AI and software solutions for major clients such as the European Commission, Eurostat, IMF, the European Central Bank, OECD, and IKEA.
If you are interested in reading more posts about Machine Learning, Deep Learning, Data Science, and DataOps, follow me on Medium, LinkedIn, or @james2pl on Twitter.
Opinions expressed are solely my own and do not express the views or opinions of my employer.