improving sanity with structured binary formats and protocols

29
Learning how to let go: Improving sanity with structured binary data- interchange formats and protocols Kyle Drake Net Brew Ventures

Upload: kyle-drake

Post on 06-May-2015

6.530 views

Category:

Technology


2 download

DESCRIPTION

Did you know that you can turn a JSON API into a binary one using MessagePack with one line of code? There are a lot of myths surrounding working with binary formats, and a lot of things even the "experts" get wrong. Just as important as making a binary format that is high performance, is making one that is easy for developers to work with. This talk describes how not to implement a binary format, and then introduces structured ways to implement them that will maximize language compatibility and make everybody's lives easier. An early version of this talk was introduced at Realtime Conf Europe, and was recorded here: http://www.youtube.com/watch?v=ZlKrnOD-4TQ

TRANSCRIPT

Page 1: Improving sanity with structured binary formats and protocols

Learning how to let go:Improving sanity with structured binary data-interchange formats and protocols

Kyle DrakeNet Brew Ventures

Page 2: Improving sanity with structured binary formats and protocols

If you’re doing a lot of text-based serialized object passing, and want a way to improve serialization time and payload size, try a binary format.

Page 3: Improving sanity with structured binary formats and protocols

Why• Smaller size, representing the same

information (esp. vs XML)

• Usually faster for CPU as a consequence

• Surprisingly easy to implement (with help)

• Lots of well-tested, streamlined solutions

Page 4: Improving sanity with structured binary formats and protocols

Why Not• No quick language support (if custom)

• Hard to understand without good docs

• Potentially harder to debug low-level if something bad happens (data entanglement/corruption?)

• Binary != Performance (Measure!)

Page 5: Improving sanity with structured binary formats and protocols

Two Approaches• Custom, hand-rolled format you write

• Frameworks (Protocol Buffers, MessagePack, BERT, others)

Page 6: Improving sanity with structured binary formats and protocols

Custom Binary Format• This is often a bad idea.

• No, seriously.

• Requires a very compelling reason IMHO.

Page 7: Improving sanity with structured binary formats and protocols

Let’s look at a poorly designed custom binary format, via: The Apple Push Notification Service.

Page 8: Improving sanity with structured binary formats and protocols

Wait, why not just use JSON for the whole thing then?

Page 9: Improving sanity with structured binary formats and protocols

Success: no responseError: socket disconnectionInterim: unreported data loss

W.T.F.

Page 10: Improving sanity with structured binary formats and protocols

ROFLSCALE TIPS FTW

Page 11: Improving sanity with structured binary formats and protocols
Page 12: Improving sanity with structured binary formats and protocols
Page 13: Improving sanity with structured binary formats and protocols

My solution:

Page 14: Improving sanity with structured binary formats and protocols
Page 15: Improving sanity with structured binary formats and protocols

Meanwhile, at Google:

Page 16: Improving sanity with structured binary formats and protocols

I think this crap is why people hate working with binary so much.But there is a better way.

Page 17: Improving sanity with structured binary formats and protocols

Let’s look at some structured ways to work with binary!

Page 18: Improving sanity with structured binary formats and protocols

Protocol Buffers• Developed by Google

• “3 to 10 times smaller, 20 to 100 times faster than XML”

• Requires a pre-defined .proto file

• In effect, it has a language agnostic “schema”.

Page 19: Improving sanity with structured binary formats and protocols

Protocol Buffers

Page 20: Improving sanity with structured binary formats and protocols

Protocol Buffers

Page 21: Improving sanity with structured binary formats and protocols

Protocol BuffersI’m doing work with the Bitcoin protocol right now. I really wish it used protocol buffers. Not just for size, but for safety and ease of use.

Bitcoin lead core developer is warm to idea:https://bitcointalk.org/index.php?topic=632.msg6656#msg6656

Page 22: Improving sanity with structured binary formats and protocols

Let’s look at some “schemaless” structured binary formats.

Page 23: Improving sanity with structured binary formats and protocols

JSON• Number (50, 2.33)

• String “howdy”

• Boolean (true or false)

• Array [1,2,3]

• Object {“key”: “value”}

• null (empty)

Page 24: Improving sanity with structured binary formats and protocols

MessagePackBasically the same thing.

Except faster and smaller!

Super simple:

Page 27: Improving sanity with structured binary formats and protocols

Painless API Integration

• /route?format=msgpack

• Content-Type: application/x-msgpack

• Accept: application/x-msgpack

• Plugs right in, if you’re using JSON.

Page 28: Improving sanity with structured binary formats and protocols

HAVE A JSON API? TRY A SCHEMALESS STRUCTURED BINARY FORMAT! IT’S EASY.