Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and...

62
Serialization

Transcript of Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and...

Page 1: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Serialization

Page 2: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

ELCO

Tianjin

Dresden

Oberstenfeld

Page 3: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

From the Sensor to the Human and back againThe source of our success

Page 4: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Serialization

Page 5: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Serialization

Page 6: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Table of contents1. Brief history

2. Serialisation methods in practicea. Custom b. JSONc. Java.io.Serializabled. Protobufe. Flatbuffers

3. Benchmarks

4. Conclusion

Page 7: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

History● Serialization exists since start of computer

sciences

● 1980: First standard - Xerox Network Systems Courier RPC

● 1987: Sun published XDR

● 1990’s: CORBA, COM, RMI distribute

● Late 1990’s: Bigger memory and bandwidths allow human readable formats (XML)

● 2000: Java Script Object Notation (JSON) first standard 2013

● YAML in 2001

● ProtoBuf 2001-2008

● Flatbuffers 2014

● 2019: New Java Serialisation? http://cr.openjdk.java.net/~briangoetz/amber/serialization.html

Page 8: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Serialisation methods

Page 9: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Custom serialization method

Page 10: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

NATS

● Simple, secure and high performance open source messaging system

NATS documentation:

“Unlike traditional messaging systems that use a binary message format that require an API to consume,

the text-based NATS protocol makes it easy to implement clients in a wide variety of programming and scripting languages.”

Page 11: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

NATS

Page 12: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

JSONDouglas Crockford: “It’s not too bad.”

Page 13: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent
Page 14: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Xi - Editor

● Text editor (framework) by Raph Levien

● Initially developed for Fuchsia

● Micro service architecture

Raph Levien:

I considered binary formats, but the actual improvement in performance would be completely in the noise.

Using JSON considerably lowers friction for developing plug-ins, as it’s available out of the box for most modern languages, and there are plenty of the libraries available for the other ones.

Page 15: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

IoTHub Developer

● Usage in REST API and for unknown data structures

● Encoding/Decoding in most language available

● Handling differs from language to language

● Best usage in JS

Use case:

● Transfer a SQL result set between Golang and JavaScript

● Having test validating handling

● Some code ...

Page 16: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Java Serializable“He [Mark Reinhold] estimates that at least a third —maybe even half— of Java

vulnerabilities have involved serialization”

Page 17: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent
Page 18: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Stream magic

Page 19: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Stream version

Page 20: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Flag for new object

Page 21: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Flag for new class

Page 22: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Length of class name

Page 23: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

class name

Page 24: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Serial version id

Page 25: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Some flags

Page 26: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

No of fields in class

Page 27: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Type of first field

Page 28: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Length of field name

Page 29: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Name of field

Page 30: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Field content of title field

Page 31: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Protobuflanguage-neutral, platform-neutral extensible mechanism for serializing structured

data

Page 32: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent
Page 33: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent
Page 34: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Type and number of field

Page 35: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Value of field 1 (118)

Page 36: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Type and number of field

Page 37: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Length of string

Page 38: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Value of second field

Page 39: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Elco IoTHub

● Industrial IoT Platform

● Micro service architecture

● Services written in Go, NodeJs, C#

● Protobuf used for service to service communication and persistence

Reasons for Protobuf:

● Small messages

● Fast (for our use case)

● Language independent

● Build in versioning

● Big community

Page 40: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

IoTHub Developer

● Usage in gRPC between agents and IoTHub

● Big amount of generated code

● In some cases hard work with IDEs (Goland vs. Ryder)

● But usage is not complex

Use case:

● Agent - Gateway communication

● Gateway service with two functions

● Some code ...

Page 41: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Flatbufferslanguage-neutral, platform-neutral extensible mechanism for serializing structured

data

Page 42: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent
Page 43: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent
Page 44: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Position of root table = 20

Page 45: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Rel. position of vtable = -14

Page 46: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Size of vtable = 20

Page 47: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Object size

Page 48: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Relative position of first field (id = 20)

Page 49: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Value of field id = 118

Page 50: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Relative position of second field (name = 12)

Page 51: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Relative position of value of second field (name = 12) Length of value

Page 52: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Facebook - Android● Segments of social graph stored on

devices Reasons for Flatbuffers:

● Story load time from disk cache is reduced from 35 ms to 4 ms per story

● Transient memory allocations are reduced by 75 percent

● Cold start time is improved by 10-15 percent

● We have reduced storage size by 15 percent

Page 53: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

IoTHub Developer● Usage as communication protocol

between JavaScript (V8) and GoLang

● Similar handling in the different target languages

● Strings, vectors need some effort preparing serialization

● Deserialization on the other side is simple

● Access to single fields without dedicated deserialization step

Use case:

● SQL Select query

● Sql.query(db, "SELECT * FROM person;")

● Some code ...

Page 54: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Benchmarks

Page 55: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Benchmarks

Flatbuffers > Protocol Buffers > JSON

https://google.github.io/flatbuffers/flatbuffers_benchmarks.html

Page 56: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Benchmarks

Protocol Buffers > FlatBuffers >> JSON

https://github.com/alecthomas/go_serialization_benchmarks

Page 57: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Benchmarks

Protocol Buffers = FlatBuffers > JSON

https://github.com/smallnest/gosercomp

Page 58: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

XI - Editor

Page 59: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Conclusion

Page 60: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Conclusion

1. Choose one of: JSON, Flatbuffers, Protobuf, Thrift, Avro, Custom, etc.

2.Test it!

Is it:● Available for your

programming language?

● Nice to use?● Good to integrate

into CI?● Maintained?● Made for your use

case? 3. Benchmark it for your

specific use case!

Does it fit your requirements?

4. Use it!

no no

yes yes

Page 61: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Thanks for listening!

Page 62: Serialization - JUG Saxony e.V.€¦ · Protobuf used for service to service communication and persistence Reasons for Protobuf: Small messages Fast (for our use case) Language independent

Sources● https://en.wikipedia.org/wiki/Comparison_of_data-serialization_f

ormats● https://nats-io.github.io/docs/nats_protocol/nats-protocol.html● https://nats-io.github.io/docs/developer/concepts● Douglas Crockford: The JSON Saga,

https://www.youtube.com/watch?v=-C-JoyNuQJs● The Post JavaScript Apocalypse - Douglas Crockford,

https://www.youtube.com/watch?v=NPB34lDZj3E● Xi: an editor for the next 20 years,

https://www.recurse.com/events/localhost-raph-levien● https://github.com/xi-editor/xi-editor● https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.ht

ml● http://cr.openjdk.java.net/~briangoetz/amber/serialization.html● https://www.javaworld.com/article/2072752/the-java-serialization

-algorithm-revealed.html● https://www.infoworld.com/article/3275924/oracle-plans-to-dump

-risky-java-serialization.html● https://developers.google.com/protocol-buffers/docs/overview#a

-bit-of-history

● https://developers.google.com/protocol-buffers/docs/encoding

● https://developers.google.com/protocol-buffers/docs/proto3● https://github.com/mzaks/FlatBuffersSwift/wiki/FlatBuffers-Ex

plained● https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.ht

ml● https://google.github.io/flatbuffers/flatbuffers_internals.html● https://google.github.io/flatbuffers/flatbuffers_white_paper.ht

ml● https://google.github.io/flatbuffers/flatbuffers_benchmarks.ht

ml● https://code.fb.com/android/improving-facebook-s-performan

ce-on-android-with-flatbuffers/● https://github.com/alecthomas/go_serialization_benchmarks● https://github.com/smallnest/gosercomp