Top Banner
Data transferring faster, stronger, better and not harder
50

Антон Минашкин "Data transfering faster, stronger, better and not harder"

Jan 22, 2018

Download

Technology

fwdays
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Data transferringfaster, stronger, better and not harder

Page 2: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

DisclaimerData transferring optimizations requires both, client side and server side development, could be hard to implement and it could break your system (and may be life)

...But it worth to be done

Page 3: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Is it really a problem?

Page 4: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Is it really a problem?

Page 5: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Partial requests/responses● Minimize data structure● Use streams where possible● Use Binary formats

Page 6: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Partial requests/responses● Minimize data structure● Use streams where possible● Use Binary formats

Page 7: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

GZIP● Network cost vs CPU● Measure it

Page 8: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Partial requests/responses● Minimize data structure● Use streams where possible● Use Binary formats

Page 9: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cache● Client DB● Network client level cache● Server side support (ETag, Cache-Control, etc.)● Use push notifications for cache invalidating

Page 10: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Partial requests/responses● Minimize data structure● Use streams where possible● Use Binary formats

Page 11: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

Page 12: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses● Transfer only what you really need● Control field count (Data Diet)● Pagination is our friend

Page 13: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

https://www.googleapis.com/demo/v1?key=YOUR-API-KEY

Page 14: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

https://www.googleapis.com/demo/v1?key=YOUR-API-KEY

{ "kind": "demo",

...

"items": [

{

"title": "First title",

"comment": "First comment.",

"characteristics": {

"length": "short",

"accuracy": "high",

"followers": ["Jo", "Will"],

}, ... ]

}

Page 15: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

https://www.googleapis.com/demo/v1?key=YOUR-API-KEY&fields=kind,items(title,characteristics/length)

Page 16: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

https://www.googleapis.com/demo/v1?key=YOUR-API-KEY&fields=kind,items(title,characteristics/length)

{ "kind": "demo",

"items": [

{

"title": "First title",

"characteristics": {

"length": "short"

}

}, ... ]

}

Page 17: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

{ "kind": "demo",

...

"items": [

{

"title": "First title",

"comment": "First comment.",

"characteristics": {

"length": "short",

"accuracy": "high",

"followers": ["Jo", "Will"],

}, ... ]

}

{ "kind": "demo",

"items": [

{

"title": "First title",

"characteristics": {

"length": "short"

}

}, ... ]

}

Page 18: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Partial requests/responses

{ "kind": "demo",

...

"items": [

{

"title": "First title", "comment": "First comment.", "characteristics": { "length": "short", "accuracy": "high", "followers": ["Jo", "Will"], }, ... ]

}

{ "kind": "demo",

"items": [

{

"title": "First title", "characteristics": { "length": "short" }

}, ... ]

}

Page 19: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Fields parameter syntax summary● Use a comma-separated list to select multiple fields.● Use a/b to select a field b that is nested within field a; use a/b/c to select a

field c nested within b. ● Exception: For API responses that use "data" wrappers, where the response

is nested within a data object that looks like data: { ... }, do not include "data" in the fields specification. Including the data object with a fields specification like data/a/b causes an error. Instead, just use a fields specification like a/b.

Page 20: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Fields parameter syntax summary● Use a sub-selector to request a set of specific sub-fields of arrays or objects

by placing expressions in parentheses "( )".

For example: fields=items(id,author/email) returns only the item ID and author's email for each element in the items array. You can also specify a single sub-field, where fields=items(id) is equivalent to fields=items/id.

Page 21: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Fields parameter syntax summary● Use wildcards in field selections, if needed.

For example: fields=items/pagemap/* selects all objects in a pagemap.

Page 22: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Use PATCH (yes, there is a HTTP Verb “PATCH”)● https://tools.ietf.org/html/rfc5789● If you’ve changed one field - send one field● Retrofit support included ;)● Could be used with Partial Responses

Page 23: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Minimize data● Minimize data structure● Use streams where possible● Use Binary formats

Page 24: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Minimize data structure

● Remove spaces● Remove long field’ names● Remove braces● Remove data (see previous slides)

Page 25: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Make you data homogeneousWhat is an Homogenous Collection?

Before...

[

{"a":"A","b":"B"},

{"a":"C","b":"D"},

{"a":"E","b":"F"}

]

After…

[2,"a","b","A","B","C","D","E","F"]

Page 26: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Tools● CJSON Algorithm● JSON.hpack Algorithm● Brain (use it carefully)

Page 27: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Partial requests/responses● Minimize data structure● Use streams where possible● Use Binary formats

Page 28: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Why streams?● Start parsing from the first byte● Reduce memory usage● Most of the tools support it by default

Page 29: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

General advice● GZIP● Cache (!!!)● Partial requests/responses● Minimize data structure● Use streams where possible● Use Binary formats

Page 30: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Binary formats● Fast● Small● Optimized for every platform● Static typed

Page 31: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Binary formats● Protobuf● Thrift (Never used it :( )● Cap’n proto

Page 32: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Protobufmessage Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

}

Page 33: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

ProtobufPerson john = Person.newBuilder()

.setId(1234)

.setName("John Doe")

.setEmail("[email protected]")

.build();

output = new FileOutputStream(args[0]);

john.writeTo(output);

Page 34: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Protobuf● Advanced android support (Retrofit, Wire, etc.)● Obj-c/Swift/JS are also supported● Easy to implement on server side

Page 35: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto● Faster than Protobuf● Same author● No Obj-C/Swift support (use C++ and suffer)● No Android specific realization (Java only)● BETA

Page 36: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Why it worth to check● Incremental reads● Random access● Mmap support● Tiny generated code● Tiny runtime library

Page 37: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Why you shouldn’t use it in production● Still BETA● Has not yet had a security review● The Cap’n Proto programming interface may still change in ways that break

existing code● Performance (“Currently it only beats Protobufs in realistic-ish end-to-end

benchmarks by around 2x-5x. We can do better”)

Page 38: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Schemastruct Person { name @0 :Text = “Bob”; birthdate @3 :Date;

email @1 :Text = “[email protected]”; phones @2 :List(PhoneNumber);

struct PhoneNumber { number @0 :Text; type @1 :Type;

enum Type { mobile @0; home @1; work @2; } }}

Page 39: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Schema● Types come after names● @N annotations● # Comments also are supported (should appear after the declaration)

Page 40: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Built-in Types● Void: Void● Boolean: Bool● Integers: Int8, Int16, Int32, Int64● Unsigned integers: UInt8, UInt16, UInt32, UInt64

● Floating-point: Float32, Float64

● Blobs: Text, Data

● Lists: List(T)

Page 41: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Unionsstruct Shape { area @0 :Float64;

union { circle @1 :Float64; # radius square @2 :Float64; # width }}

Page 42: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Unions● Unions members are numbered in the same number space as fields● “Void” is useful if no value is needed● By default, when a struct is initialized, the lowest-numbered field in the union

is “set”

Page 43: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Groupsstruct Person { # ...

# Note: This is a terrible way to use groups, and meant # only to demonstrate the syntax. address :group { houseNumber @8 :UInt32; street @9 :Text; city @10 :Text; country @11 :Text; }}

Page 44: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Groups● Behaves as one field of other structure (address :Address)● Group is not separated object!● Essentially, a group is just a namespace● Useful with Unions

Page 45: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Groupsstruct Shape { area @0 :Float64;

union { circle :group { radius @1 :Float64; } rectangle :group { width @2 :Float64; height @3 :Float64; } }}

Page 46: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Other features● Dynamically-typed Fields● Enums● Interfaces (RPC)● Generic Types● Generic Methods (RPC)● Constants● Nesting, Scope, and Aliases● Imports● Annotations

Page 47: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Cap’n proto: Other features

Page 48: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Summary● Don’t transfer data at all● If you should transfer something - get only part that you need● ...then minimize data structure of that part● ...then compress it● ...then optimize data parsing● ...then cache result it and see p.1● Use binary data!

Page 49: Антон Минашкин "Data transfering  faster, stronger, better and not harder"
Page 50: Антон Минашкин "Data transfering  faster, stronger, better and not harder"

Thanks!URLs:

https://capnproto.org/https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=enhttps://developers.google.com/site-verification/v1/performancehttps://developers.google.com/protocol-buffers/

Contact me:

Twitter: @AntonMinashkinEmail: [email protected]: https://www.facebook.com/anton.minashkin.1