Skip to content

Commit dfa5e45

Browse files
hsaliakLisaFC
authored andcommitted
Adding post about flatbuffers support for gRPC. (grpc#550)
* Adding post about flatbuffers support for gRPC. * Updated post from first round of review comments. * Fixed formatting issue. * Removed footnote. * Changes based on review comments from Lisa.
1 parent 5f69084 commit dfa5e45

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
layout: post
3+
title: Announcing out of the box support for gRPC in the Flatbuffers serializaton library.
4+
published: true
5+
permalink: blog/flatbuffers
6+
author: Wouter van Oortmerssen
7+
company: Google
8+
company-link: https://www.google.com
9+
---
10+
11+
The recent release of Flatbuffers [version 1.7](https://github.com/google/flatbuffers/releases) introduced truly zero-copy support for gRPC out of the box.
12+
13+
[Flatbuffers](https://google.github.io/flatbuffers/) is a serialization library that allows you to access serialized data without first unpacking it or allocating any
14+
additional data structures. It was originally designed for games and other resource constrained applications, but is now finding more general use, both by teams within Google and in other companies such as Netflix and Facebook.
15+
16+
<!--more-->
17+
18+
Flatbuffers enables maximum throughput by directly using gRPC's slice buffers with zero-copy for common use cases. An incoming rpc can be processed directly from gRPCs internal buffers, and constructing a new message will write directly to these buffers without intermediate steps.
19+
20+
This is currently, fully supported in the C++ implementation of FlatBuffers, with more languages to come. There is also an implementation in Go, which is not entirely zero copy, but still very low on allocation cost (see below).
21+
22+
23+
## Example Usage
24+
Let's look at an example of how this works.
25+
26+
### Use Flatbuffers as an IDL
27+
Start with an `.fbs` schema (similar to .proto, if you are familiar with protocol buffers) that declares an RPC service:
28+
29+
```
30+
table HelloReply {
31+
message:string;
32+
}
33+
34+
table HelloRequest {
35+
name:string;
36+
}
37+
38+
table ManyHellosRequest {
39+
name:string;
40+
num_greetings:int;
41+
}
42+
43+
rpc_service Greeter {
44+
SayHello(HelloRequest):HelloReply;
45+
SayManyHellos(ManyHellosRequest):HelloReply (streaming: "server");
46+
}
47+
```
48+
49+
To generate C++ code from this, run: `flatc --cpp --grpc example.fbs`, much like in protocol buffers.
50+
51+
#### Generated Server Implementation
52+
The server implementation is very similar to protocol buffers, except now the request and response messages are of type `flatbuffers::grpc::Message<HelloRequest> *`.
53+
Unlike protocol buffers, where these types represent a tree of C++ objects, here they are merely handles to a flat object in the underlying gRPC slice. You can access the data directly:
54+
55+
```
56+
auto request = request_msg->GetRoot();
57+
auto name = request->name()->str();
58+
```
59+
60+
Building a response is equally simple
61+
```
62+
auto msg_offset = mb_.CreateString("Hello, " + name);
63+
auto hello_offset = CreateHelloReply(mb_, msg_offset);
64+
mb_.Finish(hello_offset);
65+
*response_msg = mb_.ReleaseMessage<HelloReply>();
66+
```
67+
68+
The client code is the same as that generated by protocol buffers, except for the FlatBuffer access and construction code.
69+
70+
71+
See the full example [here](https://github.com/google/flatbuffers/tree/master/grpc/samples/greeter). To compile it, you need gRPC.
72+
The same repo has a [similar example](https://github.com/google/flatbuffers/blob/master/grpc/tests/go_test.go) for Go.
73+
74+
Read more about using and building FlatBuffers for your platform [on the flatbuffers site](https://google.github.io/flatbuffers/).

0 commit comments

Comments
 (0)