protocol buffers and microservices

97
Protocol buffers and Microservices Vladimir Dejanović Voxxed Days Bristol 02Mar2017 @VladimirD_42 #VDB17

Upload: vladimir-dejanovic

Post on 19-Mar-2017

136 views

Category:

Technology


4 download

TRANSCRIPT

Protocol buffers and Microservices

Vladimir Dejanović

Voxxed Days Bristol 02Mar2017

@VladimirD_42#VDB17

Let’s Meet

@VladimirD_42#VDB17

Agenda

@VladimirD_42#VDB17

Agenda

Before Protocol Buffers

@VladimirD_42#VDB17

Agenda

Before Protocol Buffers

Protocol Buffers

@VladimirD_42#VDB17

Agenda

Before Protocol Buffers

Protocol Buffers

Should I care about Protocol Buffers

@VladimirD_42#VDB17

Agenda

Before Protocol Buffers

Protocol Buffers

Should I care about Protocol Buffers

Questions

@VladimirD_42#VDB17

In the beginning there was a message

@VladimirD_42#VDB17

In the beginning there was a message

@VladimirD_42#VDB17

In the beginning there was a message

@VladimirD_42#VDB17

In the beginning there was a message

@VladimirD_42#VDB17

In the beginning there was a message

@VladimirD_42#VDB17

Message

In the beginning there was a message

@VladimirD_42#VDB17

Message

Message => Format

Fast Forward to 1996

@VladimirD_42#VDB17

Fast Forward to 1996

XML - Extensible Markup Language

@VladimirD_42#VDB17

Fast Forward to 1996

XML - Extensible Markup Language

@VladimirD_42#VDB17

Both Human-readable and Machine-readable

@VladimirD_42#VDB17

<attendee> <firstName>John</firstName> <lastName>Doe</lastName> <address> <city>Bristol</city> <street>Canon's Road</street> <number>1</number> </address></attendee>

XML Example

@VladimirD_42#VDB17

<attendee> <firstName>John</firstName> <lastName>Doe</lastName> <address> <city>Bristol</city> <street>Canon's Road</street> <number>1</number> </address></attendee>

XML Example

@VladimirD_42#VDB17

<attendee> <firstName>John</firstName> <lastName>Doe</lastName> <address> <city>Bristol</city> <street>Canon's Road</street> <number>1</number> </address></attendee>

XML Example

@VladimirD_42#VDB17

<xsd:element name="attendee" type="AttendeeType"/> <xsd:complexType name="AttendeeType"> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="lastName" type="xsd:string"/> <xsd:element name="address" type="AddressType"/> </xsd:sequence> </xsd:complexType>

<xsd:complexType name="AddressType"> <xsd:sequence> <xsd:element name="city" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="number" type="xsd:decimal"/> </xsd:sequence> </xsd:complexType>

@VladimirD_42#VDB17

<attendee> <firstName>John</firstName> <lastName>Doe</lastName> <address> <city>Bristol</city> <street>Canon's Road</street> <number>1</number> </address></attendee>

XML Example

@VladimirD_42#VDB17

<attendee> <firstName>John</firstName> <lastName>Doe</lastName> <address> <city>Bristol</city> <street>Canon's Road</street> <number>1</number> </address></attendee>

XML Example

159

@VladimirD_42#VDB17

<attendee> <firstName>John</firstName> <lastName>Doe</lastName> <address> <city>Bristol</city> <street>Canon's Road</street> <number>1</number> </address></attendee>

XML Example

159

68

Fast Forward to 2000 ( sort of )

@VladimirD_42#VDB17

Fast Forward to 2000 ( sort of )

@VladimirD_42#VDB17

JSON

Fast Forward to 2000 ( sort of )

@VladimirD_42#VDB17

JSON“JavaScript Object Notation” (sometimes)

Fast Forward to 2000 ( sort of )

@VladimirD_42#VDB17

JSON“JavaScript Object Notation” (sometimes)

Both Human-readable and Machine-readable

Fast Forward to 2000 ( sort of )

@VladimirD_42#VDB17

JSON“JavaScript Object Notation” (sometimes)

Both Human-readable and Machine-readable

Developer Friendly

JSON Example

@VladimirD_42#VDB17

{ firstName : "John", lastName: "Doe", address: {

city: "Bristol",street: "Canon's Road",number: 1

}}

JSON Example

@VladimirD_42#VDB17

{ firstName : "John", lastName: "Doe", address: {

city: "Bristol",street: "Canon's Road",number: 1

}}

90 vs 159

JSON Example

@VladimirD_42#VDB17

{ firstName : "John", lastName: "Doe", address: {

city: "Bristol",street: "Canon's Road",number: 1

}}

90 vs 159

68

No JSON Schema

@VladimirD_42#VDB17

No JSON Schema

@VladimirD_42#VDB17

JSON Schema is an Internet Draft currently in its 5th version, which was released on October 13, 2016

No JSON Schema

@VladimirD_42#VDB17

JSON Schema is an Internet Draft currently in its 5th version, which was released on October 13, 2016

http://json-schema.org/latest/json-schema-core.html

Fast Forward some more ….

@VladimirD_42#VDB17

Fast Forward some more ….

@VladimirD_42#VDB17

Welcome to Microservice Era

Fast Forward some more ….

@VladimirD_42#VDB17

Welcome to Microservice Era (Present Time)

Fast Forward some more ….

@VladimirD_42#VDB17

Welcome to Microservice Era

Monolith is OUT

(Present Time)

Fast Forward some more ….

@VladimirD_42#VDB17

Welcome to Microservice Era

Micro is IN

Monolith is OUT

(Present Time)

In the Nutshell

@VladimirD_42#VDB17

In the Nutshell

@VladimirD_42#VDB17

In the Nutshell

@VladimirD_42#VDB17

In the Nutshell

@VladimirD_42#VDB17

Network Call, Message and Format

@VladimirD_42#VDB17

Network Call, Message and Format

@VladimirD_42#VDB17

Which format should we use

Network Call, Message and Format

@VladimirD_42#VDB17

Which format should we use

XML or JSON

XML vs JSON

@VladimirD_42#VDB17

XML vs JSON

@VladimirD_42#VDB17

XML - security for valid messages ( XSD )

XML vs JSON

@VladimirD_42#VDB17

XML - security for valid messages ( XSD )

JSON - we hope all messages will be good

XML vs JSON

@VladimirD_42#VDB17

XML - potentially huge size

XML vs JSON

@VladimirD_42#VDB17

XML - potentially huge size

JSON - smaller size

XML vs JSON

@VladimirD_42#VDB17

High traffic Financial system

XML vs JSON

@VladimirD_42#VDB17

Most systems with high traffic

and not “critical” data

New Contender

@VladimirD_42#VDB17

New Contender

@VladimirD_42#VDB17

Protocol Buffers

New Contender

@VladimirD_42#VDB17

Protocol Buffers

Google public release V2 in 2008

Protocol Buffer

@VladimirD_42#VDB17

Protocol Buffer

@VladimirD_42#VDB17

Protocol buffers are a flexible, efficient, automated mechanism for serializing

structured data – think XML, but smaller, faster, and simpler.

Protocol Buffer

@VladimirD_42#VDB17

Protocol buffers are a flexible, efficient, automated mechanism for serializing

structured data – think XML, but smaller, faster, and simpler.

Protocol Buffer

@VladimirD_42#VDB17

Generate Code

$ protoc --java_out=src_dir attendee.proto

@VladimirD_42#VDB17

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

@VladimirD_42#VDB17

syntax = "proto2";package voxxed.bristol;option java_package = "com.voxxed.bristol.pojo";option java_outer_classname = "AttendeeProto";

message Attendee { required string first_name = 1; required string last_name = 2; message Address { required string city = 1; required string street = 2; required int32 number = 3; } optional Address address = 3;}

Generated Code

Message Classes are all immutable

@VladimirD_42#VDB17

Generated Code

Each Message Class has Builder

@VladimirD_42#VDB17

Generated Code

static Attendee parseFrom(byte[] data)

static Attendee parseFrom(InputStream input)

@VladimirD_42#VDB17

Generated Code

byte[] toByteArray()

void writeTo(OutputStream output)

@VladimirD_42#VDB17

Backward Compatibility Don'ts

@VladimirD_42#VDB17

Backward Compatibility Don'ts

Change the tag numbers of any existing fields

@VladimirD_42#VDB17

Backward Compatibility Don'ts

Change the tag numbers of any existing fields

Add or Delete any required fields

@VladimirD_42#VDB17

Backward Compatibility Do's

@VladimirD_42#VDB17

Backward Compatibility Do's

Delete optional or repeated fields.

@VladimirD_42#VDB17

Backward Compatibility Do's

Delete optional or repeated fields.

Add new optional or repeated fields

@VladimirD_42#VDB17

Backward Compatibility Do's

Delete optional or repeated fields.

Add new optional or repeated fields (But you must use fresh tag numbers )

@VladimirD_42#VDB17

Demo

@VladimirD_42#VDB17

Conclusion

@VladimirD_42#VDB17

Conclusion

JSON and XML are Human friendly

@VladimirD_42#VDB17

Conclusion

JSON and XML are Human friendly

@VladimirD_42#VDB17

Protocol Buffers proto file needed

Conclusion

Protocol Buffers compact and fast

@VladimirD_42#VDB17

Conclusion

Protocol Buffers compact and fast

@VladimirD_42#VDB17

JSON is everywhere

Conclusion

Protocol Buffers vs JSON

@VladimirD_42#VDB17

Conclusion

Protocol Buffers vs JSON

@VladimirD_42#VDB17

Protocol Buffers vs XML

Conclusion

Protocol Buffers would not be a good way to model a text-based document with markup

@VladimirD_42#VDB17

Questions

@VladimirD_42

[email protected]

@VladimirD_42#VDB17

Thank You

@VladimirD_42#VDB17