SlideShare a Scribd company logo
Building your First gRPC Service
Why gRPC?
Why gRPC?
From gRPC’s website:
“gRPC is a modern open source high performance RPC framework that can run in
any environment. It can efficiently connect services in and across data centers with
pluggable support for load balancing, tracing, health checking and authentication. It
is also applicable in last mile of distributed computing to connect devices, mobile
applications and browsers to back end services.”
Benefits of gRPC
● Low latency
○ Lack of parsing call parameters from paths and query strings allows for faster services
● Full-duplex streaming
○ Both requests and responses can be optionally streamed - gRPC uses HTTP/2 by default
● Supports multiple data formats
○ Protobuf, JSON, XML, FlatBuffers, and Thrift (varying levels of support)
● Static Types & Versioned Service Interface
○ Alleviates headaches where fields may be an object or an array/usually is an int, but under
some circumstance is a string
○ Fields can be depreciated without negative downstream consequences
(when handled properly)
Supported Languages
● C/C++
● C#
● Dart
● Go
● Java
● Node.js
● PHP
● Python
● Ruby
● Web (with Envoy)
gRPC vs REST
gRPC
● Supports streaming APIs over HTTP/2
● Uses Messages
● Strong Typing
● Not as straightforward to call from the the
browser as REST
● Supports many types of encoding
(protocol buffers by default)
● Fields can be deprecated without causing
breaking changes
REST
● Request/Response model over HTTP/1.1
● Utilizes resources and verbs
● Serialization
● Easy to call from the browser
● Supports limited number of encoding
(JSON by default)
● Deprecating fields is a breaking change
that requires versioning
Protocol Buffers
Building a gRPC Service: Part One
What is a protocol buffer?
From Google:
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be
structured once, then you can use special generated source code to easily write and read your structured
data to and from a variety of data streams and using a variety of languages.
In plain speak:
Protocol buffers are essentially a way to serialize a binary wire format message, where fields are
identified by order.
Protocol Buffers
gRPC uses Protocol Buffers as its default data format. To get started with gRPC,
you will need to create a .proto file that defines:
1. What services you will implement
2. The message definition for requests
3. The message definition for responses
Service Definition
Let’s create a service for running a pet store
(Lovingly borrowed from Swagger’s example
docs).
We’ll create endpoints for:
● Retrieving all pets
● Retrieving a pet by ID
● Creating a new pet
● Updating an existing pet
● Deleting a pet
We’ll define a pet as an entity with the following:
● id: unique identifier for the pet
● name: the pet’s name
● status: one of available, pending, or sold
The .proto file
Before we can write any code for our services,
we’ll need to define them in a .proto file.
The file will need to first define the syntax and
package name.
After that we can define what messages we’ll
send/receive and what services are available.
syntax = "proto3";
package petstore;
Creating the Pet Message
We’ve already determined a Pet is an entity with
the following:
● id: unique identifier for the pet
● name: the pet’s name
● status: one of available, pending, or sold
We’ll create a message that has three string
fields to represent this data.
By default, all fields are optionally. This will
allow us to use the same message for the
request and response for retrieving a pet.
message Pet {
string id = 1;
string name = 2;
string status = 3;
}
Defining Messages
Messages can be composed of a mixture of
scalar and custom types.
Scalar types include:
double, float, int32, int64, uint32, uint64, sint32,
sint64, fixed32, fixed64, sfixed32, sfixed64, bool,
string, bytes
Numeric values default to zero, booleans to
false, and strings to an empty string.
message Pet {
string id = 1;
string name = 2;
string status = 3;
}
Assigning Field Numbers
You’ll notice that each field in the message definition has a
unique number.
These fields numbers are used to identify your fields for the
binary parser and should not be changed once your
message is in use.
Fields 1-15 take one byte to encode, and should be reserved
for frequently occurring message elements.
Fields 16-2047 take two bytes to encode.
Deprecated fields should still be defined in your message
definition with their original field number.
message Pet {
string id = 1;
string name = 2;
string status = 3;
}
Enums
We wanted to limit status to be one of available,
pending, or sold. We can do this by defining an
enum.
In order to future proof our service, we’ll define
the default status as unknown in case we
decide to deprecate the field in the future.
enum Status {
UNKNOWN = 0;
AVAILABLE = 1;
PENDING = 2;
SOLD = 3;
}
message Pet {
string id = 1;
string name = 2;
Status status = 3;
}
Repeated Fields and Empty Messages
What if we want to return multiple pets? We can
do this by using the repeated key.
How about instances where we want to send
empty messages? We will still need to define a
message for those instances, but we will not
give it any fields.
message Pet {
string id = 1;
string name = 2;
Status status = 3;
}
message Pets {
repeated Pet pets = 1;
}
message Empty {}
Defining Services
Now that we’ve defined our messages, we’ll
need to define what services we’ll make
available.
We’ll create services for:
● Retrieving all pets
● Retrieving a pet by ID
● Creating a new pet
● Updating an existing pet
● Deleting a pet
service PetStore {
rpc GetAll(Empty) returns (Pets) {}
rpc GetPet(Pet) returns (Pet) {}
rpc CreatePet(Pet) returns (Pet) {}
rpc UpdatePet(Pet) returns (Pet) {}
rpc DeletePet(Pet) returns (Empty) {}
}
Putting it all Together
syntax = "proto3";
package petstore;
service PetStore {
rpc GetAll(Empty) returns (Pets) {}
rpc GetPet(Pet) returns (Pet) {}
rpc CreatePet(Pet) returns (Pet) {}
rpc UpdatePet(Pet) returns (Pet) {}
rpc DeletePet(Pet) returns (Empty) {}
}
message Empty {}
enum Status {
UNKNOWN = 0;
AVAILABLE = 1;
PENDING = 2;
SOLD = 3;
}
message Pet {
string id = 1;
string name = 2;
Status status = 3;
}
message Pets {
repeated Pet pets = 1;
}
Loading the Protobuf in Node
Building a gRPC Service: Part Two
Using Protobufs in Node
In Node, there are two options for using Protocol Buffers. Static generation or
dynamic loading.
With static generation, you’ll use the proto tool to load your protobuf definition and
create static JavaScript files that you’ll use for creating your server and client. This
is how protobufs are handled in most other supported languages.
With Node you also have the option to dynamically load your proto definition,
making it function similar to any other dependency you might have.
For our example, we’ll use dynamic loading.
Loading the Protobuf
You’ll need two modules to get started
● grpc
● @grpc/proto-loader
When loading the protocol definition, you’ll have a couple of options.
● keepCase: keep field casing instead of converting to camelcase
● longs: Should long numbers be treated as strings or numbers
● enums: Should enums use the string or numeric value
● defaults: Should default values be set
Loading the .proto file
const { resolve } = require('path')
const { loadPackageDefinition } = require('grpc')
const { loadSync } = require('@grpc/proto-loader')
const PETSTORE_PROTO_PATH = resolve(__dirname, 'petstore.proto')
const petstorePackageDefinition = loadSync(
PETSTORE_PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
)
const { petstore } = loadPackageDefinition(petstorePackageDefinition)
module.exports = { petstore }
Creating the gRPC Server
Building a gRPC Service: Part Three
Creating the gRPC Server
To run a gRPC server, you will need to create one in Node.
const grpc = require('grpc')
const server = new grpc.Server()
Additionally, you must tell it what security to use and what port to run on. For our
example, we’ll use the simplest type of security: insecure.
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
Adding the Service Definition
We now have a running server, but it doesn’t actually do anything. Let’s assume
we have a local package that already contains all of the service logic. We’ll also
need to load the proto definition from before.
const services = require('./services')
const proto = require('../proto')
For our server to register as a valid petstore server for our protobuf definition, it
must implement all of the services we defined.
Full Server File
const grpc = require('grpc')
const proto = require('../proto')
const services = require('./services')
const server = new grpc.Server()
server.addService(proto.petstore.PetStore.service, {
getAll: services.getPets,
getPet: services.getPet,
createPet: services.createPet,
updatePet: services.updatePet,
deletePet: services.deletePet
})
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
Writing Services
A service will receive two arguments, a call and a callback. The call contains the
request along with some other metadata. The callback has a signature of (error,
message).
const getPet = (call, callback) => {
const pet = db.getPet(call.request)
callback(null, pet)
}
Error Handling
We have a service that looks up a pet by ID, but what if we don’t have a record for that pet? Should we
send back a pet message with defaulted fields? While that is an option, gRPC has standardized status
codes, much like REST. We can build standardized status messages using the @grpc/grpc-js package.
const { status } = require('grpc')
const grpc = require('@grpc/grpc-js')
const getPet = (call, callback) => {
const pet = db.getPet(call.request)
if (!pet) {
const err = new grpc.StatusBuilder().withCode(status.NOT_FOUND).withDetails('Pet Not Found').build()
callback(err)
return
}
callback(null, pet)
}
Unimplemented Methods
As stated earlier, for our server to be of type petstore, it must implement all five
services, but what if we didn’t want to implement one of them? gRPC provides a
status code for unimplemented services.
const { status } = require('grpc')
const grpc = require('@grpc/grpc-js')
const deletePet = (call, callback) => {
const err = new grpc.StatusBuilder().withCode(status.UNIMPLEMENTED).withDetails('Service Not Implemented').build()
callback(err)
}
Available Status Messages
● Status (Closest HTTP Equivalent)
● OK (200)
● CANCELLED (499)
● UNKNOWN (500)
● INVALID_ARGUMENT (400)
● DEADLINE_EXCEEDED (504)
● NOT_FOUND (404)
● ALREADY_EXISTS (409)
● PERMISSION_DENIED (403)
● UNAUTHENTICATED (401)
● RESOURCE_EXHAUSTED (429)
● FAILED_PRECONDITION (400)
● ABORTED (409)
● OUT_OF_RANGE (400)
● UNIMPLEMENTED (501)
● INTERNAL (500)
● UNAVAILABLE (503)
● DATA_LOSS (500)
Creating the gRPC Client
Building a gRPC Service: Part Four
Creating the gRPC Client
Creating a gRPC client is fairly straightforward. You will just need to load a
protocol definition and know where the gRPC server is running.
const { credentials } = require('grpc')
const proto = require('../proto')
const client = new proto.petstore.PetStore('localhost:50051', credentials.createInsecure())
From there, you can make calls to your server and handle any errors.
client.getPet({ id:'23' }, (err, pet) => {
if (err) {
console.log(err.details) // Prints the error message, in this case “Pet Not Found”
}
console.log(pet)
})
Advanced Topics
Health Checks
● gRPC provides a standard protobuf definition for health checks.
● This defines two methods, Check and Watch.
○ Check is standard call. Watch is a streaming call.
● Returns one of three statuses: UNKNOWN, SERVING, NOT_SERVING
● The client can decide how to handle these statuses
○ For instance, if the health check returns NOT_SERVING you can fail the request, or queue it
and try again with an exponential backoff
Implementing Multiple Server Types
A single server can implement multiple server types. For instance, you can
implement both petstore and a health check server.
const server = new grpc.Server()
server.addService(proto.petstore.PetStore.service, {
// service functions
})
server.addService(proto.health.Health.service, {
// service functions
})
Importing Proto Definitions
You can reuse definitions from one proto file by importing them in another.
import "myproject/other_protos.proto";
For well known types, you can use the “Any” type which takes a definition URL for
deserializing arbitrary JSON and the value
{
"@type": "type.googleapis.com/google.protobuf.Duration",
"value": "1.212s"
}
Security
gRPC supports 3 security schemes:
1. Unsecure
2. SSL/TLS
3. Token-based authentication with Google (should only be used with Google
services)
Questions?

More Related Content

What's hot

Building microservices with grpc
Building microservices with grpcBuilding microservices with grpc
Building microservices with grpc
Sathiyaseelan Muthu kumar
 
GRPC.pptx
GRPC.pptxGRPC.pptx
GRPC.pptx
Afzal Juneja
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Codemotion
 
gRPC with java
gRPC with javagRPC with java
gRPC with java
Knoldus Inc.
 
REST vs gRPC: Battle of API's
REST vs gRPC: Battle of API'sREST vs gRPC: Battle of API's
REST vs gRPC: Battle of API's
Luram Archanjo
 
Power-up services with gRPC
Power-up services with gRPCPower-up services with gRPC
Power-up services with gRPC
The Software House
 
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
 Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t... Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
AboutYouGmbH
 
gRPC and Microservices
gRPC and MicroservicesgRPC and Microservices
gRPC and Microservices
Jonathan Gomez
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at Square
Apigee | Google Cloud
 
What is gRPC introduction gRPC Explained
What is gRPC introduction gRPC ExplainedWhat is gRPC introduction gRPC Explained
What is gRPC introduction gRPC Explained
jeetendra mandal
 
Inter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPCInter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPC
Shiju Varghese
 
Building High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersBuilding High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol Buffers
Shiju Varghese
 
GitHub Actions in action
GitHub Actions in actionGitHub Actions in action
GitHub Actions in action
Oleksii Holub
 
Logs/Metrics Gathering With OpenShift EFK Stack
Logs/Metrics Gathering With OpenShift EFK StackLogs/Metrics Gathering With OpenShift EFK Stack
Logs/Metrics Gathering With OpenShift EFK Stack
Josef Karásek
 
Designing a complete ci cd pipeline using argo events, workflow and cd products
Designing a complete ci cd pipeline using argo events, workflow and cd productsDesigning a complete ci cd pipeline using argo events, workflow and cd products
Designing a complete ci cd pipeline using argo events, workflow and cd products
Julian Mazzitelli
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and Tuning
NGINX, Inc.
 
Prometheus - basics
Prometheus - basicsPrometheus - basics
Prometheus - basics
Juraj Hantak
 
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse ProxyNginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
Amit Aggarwal
 
Nginx
NginxNginx

What's hot (20)

Building microservices with grpc
Building microservices with grpcBuilding microservices with grpc
Building microservices with grpc
 
GRPC.pptx
GRPC.pptxGRPC.pptx
GRPC.pptx
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
 
gRPC with java
gRPC with javagRPC with java
gRPC with java
 
REST vs gRPC: Battle of API's
REST vs gRPC: Battle of API'sREST vs gRPC: Battle of API's
REST vs gRPC: Battle of API's
 
Power-up services with gRPC
Power-up services with gRPCPower-up services with gRPC
Power-up services with gRPC
 
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
 Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t... Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
 
gRPC and Microservices
gRPC and MicroservicesgRPC and Microservices
gRPC and Microservices
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at Square
 
gRPC
gRPC gRPC
gRPC
 
What is gRPC introduction gRPC Explained
What is gRPC introduction gRPC ExplainedWhat is gRPC introduction gRPC Explained
What is gRPC introduction gRPC Explained
 
Inter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPCInter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPC
 
Building High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersBuilding High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol Buffers
 
GitHub Actions in action
GitHub Actions in actionGitHub Actions in action
GitHub Actions in action
 
Logs/Metrics Gathering With OpenShift EFK Stack
Logs/Metrics Gathering With OpenShift EFK StackLogs/Metrics Gathering With OpenShift EFK Stack
Logs/Metrics Gathering With OpenShift EFK Stack
 
Designing a complete ci cd pipeline using argo events, workflow and cd products
Designing a complete ci cd pipeline using argo events, workflow and cd productsDesigning a complete ci cd pipeline using argo events, workflow and cd products
Designing a complete ci cd pipeline using argo events, workflow and cd products
 
NGINX Installation and Tuning
NGINX Installation and TuningNGINX Installation and Tuning
NGINX Installation and Tuning
 
Prometheus - basics
Prometheus - basicsPrometheus - basics
Prometheus - basics
 
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse ProxyNginx A High Performance Load Balancer, Web Server & Reverse Proxy
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
 
Nginx
NginxNginx
Nginx
 

Similar to Building your First gRPC Service

CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIs
Tim Burks
 
Microservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPCMicroservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPC
WSO2
 
Building Services With gRPC, Docker and Go
Building Services With gRPC, Docker and GoBuilding Services With gRPC, Docker and Go
Building Services With gRPC, Docker and Go
Martin Kess
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to ThriftDvir Volk
 
Scaling application with RabbitMQ
Scaling application with RabbitMQScaling application with RabbitMQ
Scaling application with RabbitMQ
Nahidul Kibria
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at Google
Tim Burks
 
Rockin' Protobuf in Ruby
Rockin' Protobuf in RubyRockin' Protobuf in Ruby
Rockin' Protobuf in RubyBJ Neilsen
 
Managing gRPC Services using Kong KONNECT and the KONG API Gateway
Managing gRPC Services using Kong KONNECT and the KONG API GatewayManaging gRPC Services using Kong KONNECT and the KONG API Gateway
Managing gRPC Services using Kong KONNECT and the KONG API Gateway
João Esperancinha
 
Golang proto buff_ixxo
Golang proto buff_ixxoGolang proto buff_ixxo
Golang proto buff_ixxo
www.ixxo.io
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
Ovidiu Farauanu
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通
炜龙 何
 
Use perl creating web services with xml rpc
Use perl creating web services with xml rpcUse perl creating web services with xml rpc
Use perl creating web services with xml rpcJohnny Pork
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
support58
 
Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)
Tim Burks
 
Intake 37 12
Intake 37 12Intake 37 12
Intake 37 12
Mahmoud Ouf
 
Connect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyConnect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low Energy
Gabor Paller
 
CRUD Operation With Dgraph
CRUD Operation With DgraphCRUD Operation With Dgraph
CRUD Operation With Dgraph
Knoldus Inc.
 

Similar to Building your First gRPC Service (20)

CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIs
 
Microservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPCMicroservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPC
 
Building Services With gRPC, Docker and Go
Building Services With gRPC, Docker and GoBuilding Services With gRPC, Docker and Go
Building Services With gRPC, Docker and Go
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to Thrift
 
Scaling application with RabbitMQ
Scaling application with RabbitMQScaling application with RabbitMQ
Scaling application with RabbitMQ
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at Google
 
Rockin' Protobuf in Ruby
Rockin' Protobuf in RubyRockin' Protobuf in Ruby
Rockin' Protobuf in Ruby
 
Managing gRPC Services using Kong KONNECT and the KONG API Gateway
Managing gRPC Services using Kong KONNECT and the KONG API GatewayManaging gRPC Services using Kong KONNECT and the KONG API Gateway
Managing gRPC Services using Kong KONNECT and the KONG API Gateway
 
Golang proto buff_ixxo
Golang proto buff_ixxoGolang proto buff_ixxo
Golang proto buff_ixxo
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通
 
Rpc mechanism
Rpc mechanismRpc mechanism
Rpc mechanism
 
project_docs
project_docsproject_docs
project_docs
 
Use perl creating web services with xml rpc
Use perl creating web services with xml rpcUse perl creating web services with xml rpc
Use perl creating web services with xml rpc
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
 
Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)
 
Ice
IceIce
Ice
 
Intake 37 12
Intake 37 12Intake 37 12
Intake 37 12
 
Connect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyConnect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low Energy
 
CRUD Operation With Dgraph
CRUD Operation With DgraphCRUD Operation With Dgraph
CRUD Operation With Dgraph
 

Recently uploaded

Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 

Recently uploaded (20)

Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 

Building your First gRPC Service

  • 1. Building your First gRPC Service
  • 3. Why gRPC? From gRPC’s website: “gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to back end services.”
  • 4. Benefits of gRPC ● Low latency ○ Lack of parsing call parameters from paths and query strings allows for faster services ● Full-duplex streaming ○ Both requests and responses can be optionally streamed - gRPC uses HTTP/2 by default ● Supports multiple data formats ○ Protobuf, JSON, XML, FlatBuffers, and Thrift (varying levels of support) ● Static Types & Versioned Service Interface ○ Alleviates headaches where fields may be an object or an array/usually is an int, but under some circumstance is a string ○ Fields can be depreciated without negative downstream consequences (when handled properly)
  • 5. Supported Languages ● C/C++ ● C# ● Dart ● Go ● Java ● Node.js ● PHP ● Python ● Ruby ● Web (with Envoy)
  • 6. gRPC vs REST gRPC ● Supports streaming APIs over HTTP/2 ● Uses Messages ● Strong Typing ● Not as straightforward to call from the the browser as REST ● Supports many types of encoding (protocol buffers by default) ● Fields can be deprecated without causing breaking changes REST ● Request/Response model over HTTP/1.1 ● Utilizes resources and verbs ● Serialization ● Easy to call from the browser ● Supports limited number of encoding (JSON by default) ● Deprecating fields is a breaking change that requires versioning
  • 7. Protocol Buffers Building a gRPC Service: Part One
  • 8. What is a protocol buffer? From Google: Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. In plain speak: Protocol buffers are essentially a way to serialize a binary wire format message, where fields are identified by order.
  • 9. Protocol Buffers gRPC uses Protocol Buffers as its default data format. To get started with gRPC, you will need to create a .proto file that defines: 1. What services you will implement 2. The message definition for requests 3. The message definition for responses
  • 10. Service Definition Let’s create a service for running a pet store (Lovingly borrowed from Swagger’s example docs). We’ll create endpoints for: ● Retrieving all pets ● Retrieving a pet by ID ● Creating a new pet ● Updating an existing pet ● Deleting a pet We’ll define a pet as an entity with the following: ● id: unique identifier for the pet ● name: the pet’s name ● status: one of available, pending, or sold
  • 11. The .proto file Before we can write any code for our services, we’ll need to define them in a .proto file. The file will need to first define the syntax and package name. After that we can define what messages we’ll send/receive and what services are available. syntax = "proto3"; package petstore;
  • 12. Creating the Pet Message We’ve already determined a Pet is an entity with the following: ● id: unique identifier for the pet ● name: the pet’s name ● status: one of available, pending, or sold We’ll create a message that has three string fields to represent this data. By default, all fields are optionally. This will allow us to use the same message for the request and response for retrieving a pet. message Pet { string id = 1; string name = 2; string status = 3; }
  • 13. Defining Messages Messages can be composed of a mixture of scalar and custom types. Scalar types include: double, float, int32, int64, uint32, uint64, sint32, sint64, fixed32, fixed64, sfixed32, sfixed64, bool, string, bytes Numeric values default to zero, booleans to false, and strings to an empty string. message Pet { string id = 1; string name = 2; string status = 3; }
  • 14. Assigning Field Numbers You’ll notice that each field in the message definition has a unique number. These fields numbers are used to identify your fields for the binary parser and should not be changed once your message is in use. Fields 1-15 take one byte to encode, and should be reserved for frequently occurring message elements. Fields 16-2047 take two bytes to encode. Deprecated fields should still be defined in your message definition with their original field number. message Pet { string id = 1; string name = 2; string status = 3; }
  • 15. Enums We wanted to limit status to be one of available, pending, or sold. We can do this by defining an enum. In order to future proof our service, we’ll define the default status as unknown in case we decide to deprecate the field in the future. enum Status { UNKNOWN = 0; AVAILABLE = 1; PENDING = 2; SOLD = 3; } message Pet { string id = 1; string name = 2; Status status = 3; }
  • 16. Repeated Fields and Empty Messages What if we want to return multiple pets? We can do this by using the repeated key. How about instances where we want to send empty messages? We will still need to define a message for those instances, but we will not give it any fields. message Pet { string id = 1; string name = 2; Status status = 3; } message Pets { repeated Pet pets = 1; } message Empty {}
  • 17. Defining Services Now that we’ve defined our messages, we’ll need to define what services we’ll make available. We’ll create services for: ● Retrieving all pets ● Retrieving a pet by ID ● Creating a new pet ● Updating an existing pet ● Deleting a pet service PetStore { rpc GetAll(Empty) returns (Pets) {} rpc GetPet(Pet) returns (Pet) {} rpc CreatePet(Pet) returns (Pet) {} rpc UpdatePet(Pet) returns (Pet) {} rpc DeletePet(Pet) returns (Empty) {} }
  • 18. Putting it all Together syntax = "proto3"; package petstore; service PetStore { rpc GetAll(Empty) returns (Pets) {} rpc GetPet(Pet) returns (Pet) {} rpc CreatePet(Pet) returns (Pet) {} rpc UpdatePet(Pet) returns (Pet) {} rpc DeletePet(Pet) returns (Empty) {} } message Empty {} enum Status { UNKNOWN = 0; AVAILABLE = 1; PENDING = 2; SOLD = 3; } message Pet { string id = 1; string name = 2; Status status = 3; } message Pets { repeated Pet pets = 1; }
  • 19. Loading the Protobuf in Node Building a gRPC Service: Part Two
  • 20. Using Protobufs in Node In Node, there are two options for using Protocol Buffers. Static generation or dynamic loading. With static generation, you’ll use the proto tool to load your protobuf definition and create static JavaScript files that you’ll use for creating your server and client. This is how protobufs are handled in most other supported languages. With Node you also have the option to dynamically load your proto definition, making it function similar to any other dependency you might have. For our example, we’ll use dynamic loading.
  • 21. Loading the Protobuf You’ll need two modules to get started ● grpc ● @grpc/proto-loader When loading the protocol definition, you’ll have a couple of options. ● keepCase: keep field casing instead of converting to camelcase ● longs: Should long numbers be treated as strings or numbers ● enums: Should enums use the string or numeric value ● defaults: Should default values be set
  • 22. Loading the .proto file const { resolve } = require('path') const { loadPackageDefinition } = require('grpc') const { loadSync } = require('@grpc/proto-loader') const PETSTORE_PROTO_PATH = resolve(__dirname, 'petstore.proto') const petstorePackageDefinition = loadSync( PETSTORE_PROTO_PATH, { keepCase: true, longs: String, enums: String, defaults: true, oneofs: true } ) const { petstore } = loadPackageDefinition(petstorePackageDefinition) module.exports = { petstore }
  • 23. Creating the gRPC Server Building a gRPC Service: Part Three
  • 24. Creating the gRPC Server To run a gRPC server, you will need to create one in Node. const grpc = require('grpc') const server = new grpc.Server() Additionally, you must tell it what security to use and what port to run on. For our example, we’ll use the simplest type of security: insecure. server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()) server.start()
  • 25. Adding the Service Definition We now have a running server, but it doesn’t actually do anything. Let’s assume we have a local package that already contains all of the service logic. We’ll also need to load the proto definition from before. const services = require('./services') const proto = require('../proto') For our server to register as a valid petstore server for our protobuf definition, it must implement all of the services we defined.
  • 26. Full Server File const grpc = require('grpc') const proto = require('../proto') const services = require('./services') const server = new grpc.Server() server.addService(proto.petstore.PetStore.service, { getAll: services.getPets, getPet: services.getPet, createPet: services.createPet, updatePet: services.updatePet, deletePet: services.deletePet }) server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()) server.start()
  • 27. Writing Services A service will receive two arguments, a call and a callback. The call contains the request along with some other metadata. The callback has a signature of (error, message). const getPet = (call, callback) => { const pet = db.getPet(call.request) callback(null, pet) }
  • 28. Error Handling We have a service that looks up a pet by ID, but what if we don’t have a record for that pet? Should we send back a pet message with defaulted fields? While that is an option, gRPC has standardized status codes, much like REST. We can build standardized status messages using the @grpc/grpc-js package. const { status } = require('grpc') const grpc = require('@grpc/grpc-js') const getPet = (call, callback) => { const pet = db.getPet(call.request) if (!pet) { const err = new grpc.StatusBuilder().withCode(status.NOT_FOUND).withDetails('Pet Not Found').build() callback(err) return } callback(null, pet) }
  • 29. Unimplemented Methods As stated earlier, for our server to be of type petstore, it must implement all five services, but what if we didn’t want to implement one of them? gRPC provides a status code for unimplemented services. const { status } = require('grpc') const grpc = require('@grpc/grpc-js') const deletePet = (call, callback) => { const err = new grpc.StatusBuilder().withCode(status.UNIMPLEMENTED).withDetails('Service Not Implemented').build() callback(err) }
  • 30. Available Status Messages ● Status (Closest HTTP Equivalent) ● OK (200) ● CANCELLED (499) ● UNKNOWN (500) ● INVALID_ARGUMENT (400) ● DEADLINE_EXCEEDED (504) ● NOT_FOUND (404) ● ALREADY_EXISTS (409) ● PERMISSION_DENIED (403) ● UNAUTHENTICATED (401) ● RESOURCE_EXHAUSTED (429) ● FAILED_PRECONDITION (400) ● ABORTED (409) ● OUT_OF_RANGE (400) ● UNIMPLEMENTED (501) ● INTERNAL (500) ● UNAVAILABLE (503) ● DATA_LOSS (500)
  • 31. Creating the gRPC Client Building a gRPC Service: Part Four
  • 32. Creating the gRPC Client Creating a gRPC client is fairly straightforward. You will just need to load a protocol definition and know where the gRPC server is running. const { credentials } = require('grpc') const proto = require('../proto') const client = new proto.petstore.PetStore('localhost:50051', credentials.createInsecure()) From there, you can make calls to your server and handle any errors. client.getPet({ id:'23' }, (err, pet) => { if (err) { console.log(err.details) // Prints the error message, in this case “Pet Not Found” } console.log(pet) })
  • 34. Health Checks ● gRPC provides a standard protobuf definition for health checks. ● This defines two methods, Check and Watch. ○ Check is standard call. Watch is a streaming call. ● Returns one of three statuses: UNKNOWN, SERVING, NOT_SERVING ● The client can decide how to handle these statuses ○ For instance, if the health check returns NOT_SERVING you can fail the request, or queue it and try again with an exponential backoff
  • 35. Implementing Multiple Server Types A single server can implement multiple server types. For instance, you can implement both petstore and a health check server. const server = new grpc.Server() server.addService(proto.petstore.PetStore.service, { // service functions }) server.addService(proto.health.Health.service, { // service functions })
  • 36. Importing Proto Definitions You can reuse definitions from one proto file by importing them in another. import "myproject/other_protos.proto"; For well known types, you can use the “Any” type which takes a definition URL for deserializing arbitrary JSON and the value { "@type": "type.googleapis.com/google.protobuf.Duration", "value": "1.212s" }
  • 37. Security gRPC supports 3 security schemes: 1. Unsecure 2. SSL/TLS 3. Token-based authentication with Google (should only be used with Google services)