Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

High-Performance Services with gRPC

Learn why gRPC is a solid framework that serves its purpose very well.


advertisement

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 author
message 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 grpc.tools.protoc -I./ --python_out=. --grpc_python_out=. quote_service.proto
```

This will generate two files: quote_service_pb2.py and quote_service_pb2_grpc.py. The server and the client use the quote_service_pb2.py 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 random
from concurrent import futures
import time

from collections import defaultdict

import grpc
import sys
from pathlib import Path

sys.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__).parent
lines = open(str(script_dir / '../quotes.txt')).read().split('\n')
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 request.author in all_authors:
            author = request.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 grpc
import sys
from 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, r.author))

if __name__ == '__main__':
  main()
```

Here is the output from a run:

```
I take my wife everywhere, but she keeps finding her way back. ~ Henny Youngman

The 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 Young

Courage 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.



   
Gigi Sayfan is the chief platform architect of VRVIU, a start-up developing cutting-edge hardware + software technology in the virtual reality space. Gigi has been developing software professionally for 21 years in domains as diverse as instant messaging, morphing, chip fabrication process control, embedded multi-media application for game consoles, brain-inspired machine learning, custom browser development, web services for 3D distributed game platform, IoT/sensors and most recently virtual reality. He has written production code every day in many programming languages such as C, C++, C#, Python, Java, Delphi, Javascript and even Cobol and PowerBuilder for operating systems such as Windows (3.11 through 7), Linux, Mac OSX, Lynx (embedded) and Sony Playstation. His technical expertise includes databases, low-level networking, distributed systems, unorthodox user interfaces and general software development life cycle.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date