Mobile API: Design & Techniques

65
Mobile API Design & Techniques. Fred Brunel CTO

description

Designing a web API is hard, designing a mobile API is even harder. With heavy constraints such as bandwidth, latency and CPU power, developing a mobile API is a challenge for the service provider and the application developer. As mobile devices become ubiquitous and connected, offering the best user experience in mobile application is crucial; optimizing the network is an important part of it. In this talk we'll cover the challenges of designing a mobile API as well as innovative solutions and best practices that can be used by the service provider. We'll share our broad experience in developing connected mobile apps.

Transcript of Mobile API: Design & Techniques

Page 1: Mobile API: Design & Techniques

Mobile APIDesign & Techniques.

Fred BrunelCTO

Page 2: Mobile API: Design & Techniques

Why?

Page 3: Mobile API: Design & Techniques

Though for CPU powerThough for bandwidthLazy designed.Too close to database.

Page 4: Mobile API: Design & Techniques

A mobile device isLow poweredLow bandwidthRuns on battery!

Page 5: Mobile API: Design & Techniques

A the network is the weak link.

Page 6: Mobile API: Design & Techniques

Network conditions change in real-time.

Page 7: Mobile API: Design & Techniques

We want to keep the best user experience at all time.Nobody wants an unresponsive app.

Page 8: Mobile API: Design & Techniques

The features of an API has a huge impact on performances.

Page 9: Mobile API: Design & Techniques

An API is a contract that dictates what can or cannot be done (directly).

Page 10: Mobile API: Design & Techniques

When the API is too lazy, or incomplete; the burden is put on the mobile app.

Page 11: Mobile API: Design & Techniques

Any workaround put a stress on the mobile app to use too much network.

Page 12: Mobile API: Design & Techniques

API = User Interface.

Should be simple and get the job done. Fast.

Page 13: Mobile API: Design & Techniques

Landlord Report.

Page 14: Mobile API: Design & Techniques

Simple

Standard Complete

Page 15: Mobile API: Design & Techniques

Simple

Standard CompleteSOAP

XML-RPC

WS-*

Pure REST

Page 16: Mobile API: Design & Techniques

Simple

Complete

Page 17: Mobile API: Design & Techniques

Trust the OSI model.Works everywhere.And it’s plenty enough.

http://en.wikipedia.org/wiki/OSI_model

Page 18: Mobile API: Design & Techniques

REST-ish API + JSON

Pure REST is a nice to have but not a goal.

Page 19: Mobile API: Design & Techniques

GET/POST + Action + Params is fine.

PUT/DELETE are nice to have.

Page 20: Mobile API: Design & Techniques

Twitter is also REST-ish

POST statuses/createPOST statuses/destroy/:idPOST statuses/update

Page 21: Mobile API: Design & Techniques

REST put an emphasis on actions applied to resources; but the issue is the representation.

Page 22: Mobile API: Design & Techniques

Simplify the life of the implementor.Be pragmatic.

Page 23: Mobile API: Design & Techniques

When designing your API payloads, pay attention to consistency and completeness.

Page 24: Mobile API: Design & Techniques

Consistency means developer know what to expect.Principle of least astonishment.

Page 25: Mobile API: Design & Techniques

Completeness means less roundtrips.

Page 26: Mobile API: Design & Techniques

HTTP latency on 3G

~ 1 second.

Every request count.

Page 27: Mobile API: Design & Techniques

API is NOT a CRUD interface to your SQL database.

It’s a facade.

Page 28: Mobile API: Design & Techniques

DatabaseFacadeAPIApp

DataRepresentation Raw DataDisplay

Page 29: Mobile API: Design & Techniques

The facade answer to high-level questions.

Think services, not objects and methods.

Page 30: Mobile API: Design & Techniques

So, how do we start from here?

Page 31: Mobile API: Design & Techniques

Most of the time, a mobile API will be use to get information to be displayed on a screen.

Page 32: Mobile API: Design & Techniques

Reverse Design.

Start from the UINot the data

Page 33: Mobile API: Design & Techniques

1. Think screens2.Entities to display3.Design entity models4.Design services

Page 34: Mobile API: Design & Techniques
Page 35: Mobile API: Design & Techniques

IDTitleTownCountryRatingThumbnail URLGeocodeWebsiteEmailDescription

Page 36: Mobile API: Design & Techniques

Then, format the representation to be as efficient as possible.

Page 37: Mobile API: Design & Techniques

Each JSON entity should have the same consistent representation.

Be coherent!

Page 38: Mobile API: Design & Techniques

"person": { "id": 1234, "name": "Fred", "lastname": "Brunel", "company": "WhereCloud"}

Page 39: Mobile API: Design & Techniques

"book": { "name": "Steve Jobs", "author": "Walter Isaacson", "lenders" = [{ "person_id": 1234, "person_name": "Fred", "person_lastname": "Brunel" }]}

BAD

Page 40: Mobile API: Design & Techniques

"book": { "name": "Steve Jobs", "author": "Walter Isaacson", "lenders" = [{ "id": 1234, "name": "Fred", "lastname": "Brunel" }]}

GOOD

Page 41: Mobile API: Design & Techniques

..."user_mentions": [{ "id": 22548447, "id_str": "22548447", "screen_name": "rno", "name": "Arnaud Meunier", "indices": [0, 4]]}...

Page 42: Mobile API: Design & Techniques

Pick the right granularity.

Denormalize!

Page 43: Mobile API: Design & Techniques

"result": { ... "categories" = [{ "id": 2 }], "images": [{ "id": 317171 }], "tags": [{ "id": 555 }] ...}

Page 44: Mobile API: Design & Techniques

"result": { ... "categories": [{ "id": 2 "name" = "food" }], "images" = [{ "id": 317171 "url": "http://image.com/a.png" }], ...}

Page 45: Mobile API: Design & Techniques

Denormalize the most common fields.Avoid unnecessary roundtrips.

Page 46: Mobile API: Design & Techniques

Don’t make the app connects to 10 3rd-party systems.Aggregate on the backend.

Page 47: Mobile API: Design & Techniques

The backend has the power, bandwidth and knowledge.Use it!

Page 48: Mobile API: Design & Techniques

Make it fast!

Some good techniques to be aware of.

Page 49: Mobile API: Design & Techniques

JSON is fast to parse, but still, compress everything.

Page 50: Mobile API: Design & Techniques

Use Cache-Control on every response that can be cached.

Page 51: Mobile API: Design & Techniques

Partial Responses & Partial Updates

Let the client decides what to get/update.

Page 52: Mobile API: Design & Techniques

GEThttp://www.google.com/calendar/feeds/[email protected]/private/full?fields=entry(title,gd:when)

Page 53: Mobile API: Design & Techniques

PATCH /myfeed/1/1/Content-Type: application/xml

<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google...' gd:fields='description'> <title>New title</title></entry>

Page 54: Mobile API: Design & Techniques

Batch Requests

Send multiple operations, get one answer.

Page 55: Mobile API: Design & Techniques

Persistent Connections.

Keep a connection nailed up.

Page 56: Mobile API: Design & Techniques

“If you’re serious about network, you should make your own protocol.”—Fake Alan Kay.

Page 57: Mobile API: Design & Techniques

The fabric of the Internet is TCP/IP, not HTTP.

Page 58: Mobile API: Design & Techniques

Make your own Binary Protocol.

Lot faster than text + compression. Sorry!

Page 59: Mobile API: Design & Techniques

Message-based API

Custom TLVMessagePackProtocolBuffers

Page 60: Mobile API: Design & Techniques

TAG LENGTH VALUE

32 bits16 bits n bits

TLV TLV TLV TLV TLV

TLV TLV TLV TLV TLV

messages streaming

a message

Page 61: Mobile API: Design & Techniques

message Person { required string name = 1; required int32 id = 2; optional string email = 3;

enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; }

message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; }

repeated PhoneNumber phone = 4;}

Page 62: Mobile API: Design & Techniques

Person person;person.set_name("John Doe");person.set_id(1234);person.set_email("[email protected]");fstream output("myfile", ios::out | ios::binary);person.SerializeToOstream(&output);

fstream input("myfile", ios::in | ios::binary);Person person;person.ParseFromIstream(&input);cout << "Name: " << person.name() << endl;cout << "E-mail: " << person.email() << endl;

Page 63: Mobile API: Design & Techniques

So.

They are tons of efficient solutions and techniques.

Page 64: Mobile API: Design & Techniques

Remember.Be pragmatic.Be consistentBe complete.Be fast.