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

devx-admin

devx-admin

Share the Post:
India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1

Copilot Revolution

Microsoft Copilot: A Suit of AI Features

Microsoft’s latest offering, Microsoft Copilot, aims to revolutionize the way we interact with technology. By integrating various AI capabilities, this all-in-one tool provides users with an improved experience that not