When it comes to microservices, there’re a lot of things worth keeping in mind. Designing such fine-grained, loosely-coupled services requires paying lots of attention to various patterns and approaches to make them future-proof. A very important thing to consider, is the way those services will communicate with each-other in production. Usually the communication is done over the network using a technology-agnostic protocol. At the next level the service should provide an API for its friend services. Then, the data should be serialized without altering its meaning and transferred to the picked endpoint.
Nowadays, exposing a REST API that operates with JSON over plain HTTP is a usual way to lay the grounds of communication for the services. It is easy to accomplish, but it has some drawbacks. First of all, JSON is a human readable format, and it’s not as other serialization approaches. Also, with JSON it’s not possible to natively enforce the schema, and evolving the API may be painful.
This talk’s purpose is to describe in deep detail the benefits of protocol buffers, that offer us for free an easy way to define the API messages in the proto format, and then reuse them inside different services, without even being locked to use the same programming language for them. Moreover, with gRPC we can define the API’s endpoints easily in the same proto format. All these offer us a robust schema enforcement, compact binary serialization, and easy backward compatibility.
21. Protocol Buffers?
“Protocol buffers are Google's language-neutral,
platform-neutral, extensible mechanism for serializing
structured data – think XML, but smaller, faster, and
simpler.”
— https://developers.google.com/protocol-buffers/
22. Protocol Buffers?
“Protocol buffers are Google's language-neutral,
platform-neutral, extensible mechanism for serializing
structured data – think XML, but smaller, faster, and
simpler.”
— https://developers.google.com/protocol-buffers/
23. Protocol Buffers?
“Protocol buffers are Google's language-neutral,
platform-neutral, extensible mechanism for serializing
structured data – think XML, but smaller, faster, and
simpler.”
— https://developers.google.com/protocol-buffers/
JSON
39. How to…
message Person {
string name = 1;
int32 id = 2;
repeated string aliases = 3;
}
Proto message definition
40. How to…
message Person {
string name = 1;
int32 id = 2;
repeated string aliases = 3;
}
Person john = Person.newBuilder()
.setId(1234)
.setName("John Doe")
.addAliases("ionel")
.build();
Proto message definition
41. How to…
message Person {
string name = 1;
int32 id = 2;
repeated string aliases = 3;
}
Person john = Person.newBuilder()
.setId(1234)
.setName("John Doe")
.addAliases("ionel")
.build();
Proto message definition
Object creation
42. How to…
message Person {
string name = 1;
int32 id = 2;
repeated string aliases = 3;
}
john = Person()
john.id = 1234
john.name = 'John Doe'
john.aliases.add(‘ionel’)
Proto message definition
Object creation
43. How to…
message Person {
string name = 1;
int32 id = 2;
repeated string aliases = 3;
}
john = Person(id=1234,
name='John Doe',
aliases=['ionel'])
Proto message definition
Object creation
44. Communication (REST-ish)
1. URI: https://api.example.com/person/42
2. Make an HTTP GET Request
3. Receive a plaintext JSON
4. Parse it
5. …
50. Distributed objects
“First Law of Distributed Object Design:
don't distribute your objects”.
— Martin Fowler
https://martinfowler.com/articles/distributed-objects-microservices.html
51. The 8 Fallacies of distributed computing
1. The network is reliable.
2. Latency is zero.
3. Bandwidth is infinite.
4. The network is secure.
5. Topology doesn't change.
6. There is one administrator.
7. Transport cost is zero.
8. The network is homogeneous.
http://www.rgoarchitects.com/Files/fallacies.pdf
138. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050TS: 3600000
139. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045TS: 3600000
140. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
40 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045TS: 3600000
141. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
40 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045
TS: 3600085
TS: 3600000
142. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
20 ms 40 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045
TS: 3600085
TS: 3600000
143. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
20 ms 40 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045
TS: 3600105 TS: 3600085
TS: 3600000
144. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
40 ms 20 ms 40 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045
TS: 3600105 TS: 3600085
TS: 3600000
145. Deadline propagation
Client Service A
20 ms 30 ms
B
15 ms
40 ms 20 ms 40 ms
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600020 TS: 3600050 TS: 3600045
TS: 3600105 TS: 3600085TS: 3600145
TS: 3600000
153. Deadline propagation
Client Service A
150ms 100 ms
B
DEADLINE_EXCEEDED
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600150 TS: 3600250TS: 3600000
X
154. Deadline propagation
Client Service A
150ms 100 ms
B
DEADLINE_EXCEEDED DEADLINE_EXCEEDED
Start TS: 3600000
Timeout: 200
Deadline: 3600200
TS: 3600150 TS: 3600250TS: 3600000
X
168. Benefits
–Focus on the API design & contract
–HTTP2 is awesome
–Bi-directional streaming
169. Benefits
–Focus on the API design & contract
–HTTP2 is awesome
–Bi-directional streaming
–Freedom to pick any language
170. Benefits
–Focus on the API design & contract
–HTTP2 is awesome
–Bi-directional streaming
–Freedom to pick any language
–Service-to-Service and Service-to-Mobile friendly
171. Benefits
–Focus on the API design & contract
–HTTP2 is awesome
–Bi-directional streaming
–Freedom to pick any language
–Service-to-Service and Service-to-Mobile friendly
–Production ready