High-Performance Services with gRPC

High-Performance Services with gRPC

Large-scale cloud native applications are often architected using tens, hundreds and even thousands of individual microservices. Public services typically expose a REST API to the outside world, but many microservices are used internally and talk to each other within the same network environment. While, it is possible to use REST APIs for such communication it is not the best choice. With REST APIs, you pay with performance overhead the use of inter-operable ubiquitous HTTP infrastructure such caching, proxies and global DNS resolution. In these cases, an RPC (remote procedure call) framework can be a better solution. In this article I’ll introduce the gRPC framework?and show a basic server and client implemented in Python that communicate via gRPC.

What’s gRPC?

gRPC is an open source framework for connecting services. It is very versatile and can be used inside a data center or across data centers. It was designed by Google as the next generation of Stubby – the internal RPC framework that powered every microservice in Google for over a decade. gRPC gives you a lot:

  • Cross-platform
  • Idiomatic Client libraries for all the popular languages
  • Super compact wire protocol
  • Strong typing for messages over the wire (Google protocol buffers)
  • Bi-directional streaming with http2 support
  • Extensible (you can plug in auth, load balancing and health checking)
  • Well-documented

gRPC is also used heavily by companies like Netflix, Square and Cisco.

The Quote Service

The sample application we will build today is a service that provides a random quote over gRPC. It manages a list of authors and for each author a list of quotes. The service exposes one operation: “GetQuote”. The client provides an author name and if the author is in the list then one of their quotes will be randomly selected and returned. If the author is not in the list, then a random quote from a random author is returned.

The . proto file

With gRPC you need to define the schema of your service using a. proto file. It is simple. You define a service with multiple operations with an input message and output message. Each message has a list of fields with a data type and an index number, which is important for packing. Fields are optional by default.

```syntax = "proto3";package quotes;// The quote service definition.service Quoter {  // Request a quote  rpc GetQuote (QuoteRequest) returns (QuoteReply) {}}// The request message containing the author's name.message QuoteRequest {  string author = 1;}// The response message containing the quote and authormessage QuoteReply {  string quote = 1;  string author = 2;}```

You can do much fancier stuff too like synchronous and asynchronous calls, uni-directional and/or bi-directional streaming.

Once your . proto file is ready you need to generate the wrappers using the grpc tools:

```python -m -I./ --python_out=. --grpc_python_out=. quote_service.proto```

This will generate two files: and The server and the client use the module.

The Server

The server first reads imports, a bunch of necessary dependencies including some classes and functions from the generated quote_service_pb2 module:

```import randomfrom concurrent import futuresimport timefrom collections import defaultdictimport grpcimport sysfrom pathlib import Pathsys.path.insert(0, '')from quote_service_pb2 import (QuoterServicer,                               QuoteReply,                               add_QuoterServicer_to_server)```

Then it reads a file that contains quotes (one line per quote is separated from the quote by a “~”). It stores the quotes in a Python dictionary keyed by author and also keeps all the authors in a tuple for easy reference instead of calling quotes.keys() every time.

``` script_dir = Path(__file__).parentlines = open(str(script_dir / '../quotes.txt')).read().split('
')quotes = defaultdict(list)for line in (x for x in lines if x):    q, a = line.split('~')    quotes[a.strip()].append(q.strip())all_authors = tuple(quotes.keys())```

Next is the service class itself that inherits from the QuoterServicer base class that was generated from .proto file. It implements the GetQuote() method by accepting a request object that contains an author name and follows the author selection logic. Once an author is selected it selects one of its quotes randomly and returns the selected quote and author in a QuoteReply message (also generated automatically from the .proto file)

```class Quoter(QuoterServicer):    def GetQuote(self, request, context):        # Choose random author if it requested author doesn't has quotes        if in all_authors:            author =        else:            author = random.choice(all_authors)            # Choose random quote from this author        quote = random.choice(quotes[author])        return QuoteReply(quote=quote, author=author)```

Finally, the serve() method runs the grpc server on port 5050 and adds the QuterServicer class, so it can serve requests. It’s interesting that the start() method doesn’t block, so it is necessary to run an infinite loop to keep the server running.

```def serve():    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))    add_QuoterServicer_to_server(Quoter(), server)    server.add_insecure_port('[::]:5050')    server.start()    print('Started...')    try:        while True:            time.sleep(9999)    except KeyboardInterrupt:        server.stop(0)if __name__ == '__main__':    serve()```

Overall, it is fairly compact code. Most of the nasty networking and marshaling boilerplate code is hidden.

The Client

The client is even simpler. It connects to the local server running on port 5050 and opens a channel. Then it creates a QuoterStub and initializes it with the channel. Once the stub is initialized it calls its GetQuote() method 3 times passing a QuoteRequest message with an author name of None, so a completely random quote is returned.

```import grpcimport sysfrom quote_service_pb2 import (QuoteRequest, QuoterStub)def main():  channel = grpc.insecure_channel('localhost:5050')  stub = QuoterStub(channel)  for i in range(3):      r = stub.GetQuote(QuoteRequest(author=None))      print('{} ~ {}'.format(r.quote, __name__ == '__main__':  main()```

Here is the output from a run:

```I take my wife everywhere, but she keeps finding her way back. ~ Henny YoungmanThe trouble with weather forecasting is that it's right too often for us to ignore it and wrong too often for us to rely on it. ~ Patrick YoungCourage is the art of being the only one who knows you're scared to death. ~ Harold Wilson```

gRPC is a solid framework that serves its purpose very well. If you need efficient communication between internal services it should definitely be on your short list.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist