designing an api for mobile - socialradar
DESCRIPTION
Designing an API for mobile - Lessons we've learned at SocialRadar. Given at API Craft DMV meetup in April 2014.TRANSCRIPT
![Page 1: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/1.jpg)
Victor Quinn @victorquinn
Lead Node.js Engineer
Designing an API for MobileAPI Craft DMV Meetup, April 28 2014
![Page 2: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/2.jpg)
Download
Gives you real-time information on the people around you
!It’s free!!
!This will be pretty fast-paced
More About SocialRadar
![Page 3: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/3.jpg)
Special considerations for a Mobile API
• Speed really matters • Battery usage - minimize
• Maximizing network utilization • Response size matters • Minimize number of requests
• Stateless
![Page 4: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/4.jpg)
Speed: Problems• Only one thing on screen at a time, perception change
• On desktop other things can distract • Best Case: ~400ms latency for round trip on cell network
• We tested at SocialRadar using a basic ping no-op endpoint
• Verizon LTE, iPhone 5S • Device doesn’t maintain active connection • Average case much worse, ~1000ms round trip
![Page 5: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/5.jpg)
Ideal round tripiPhone 5S on Verizon LTE
User's Phone API
200ms
200ms
200ms
Total round trip time 800ms
200ms
bit of hand waving, I’m not a network expert
![Page 6: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/6.jpg)
Likely round tripiPhone 4S on Verizon 3G
User's Phone API
500ms
500ms
200ms
Total round trip time 1600ms
400ms
![Page 7: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/7.jpg)
Speed: Solutions• Cache anything humanly possible • Respond as quickly as possible, deferring
anything that doesn’t need to be done immediately • Queue most things • Make sure your API can handle high concurrency
(we settled on Node.js to accomplish this, WhatsApp uses Erlang)
![Page 8: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/8.jpg)
Minimize battery usage
!• Brief note about cell battery and network
usage (more) • Response size matters • Minimize number of requests
Maximize network utilization
![Page 9: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/9.jpg)
Response Size: Problems• At SocialRadar, we tested responses of different sizes • Optimal was largest response that would fit into a
single TCP packet on the cell network. • Around 128KB. Differs based on client device and
network • Any longer would jump response time significantly
![Page 10: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/10.jpg)
Response Size: Solutions• Paging sizes — largest
that fit within TCP packet window • Contrast with
Desktop, paging more dependent on server response time and UI
![Page 11: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/11.jpg)
User's Phone API
200ms
200ms
200ms
Total round trip time 800ms
200ms
Page size of 20 users
To retrieve 100 users, ~4s
Paging Size: 20 (Ideal)
![Page 12: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/12.jpg)
Paging Size: 50 (Ideal)
User's Phone API
200ms
200ms
500ms
Total round trip time 1100ms
200ms
Page size of 50 users
To retrieve 100 users, ~2.2s
![Page 13: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/13.jpg)
Paging Size: 20 (Likely)
User's Phone API
500ms
500ms
200ms
Total round trip time 1600ms
400ms
To retrieve 100 users, ~8s
![Page 14: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/14.jpg)
Paging Size: 50 (Likely)
User's Phone API
500ms
500ms
500ms
Total round trip time 1900ms
400ms
To retrieve 100 users, ~3.8s
![Page 15: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/15.jpg)
Number of Requests: Problems• Traditionally parallelized, not (well) on mobile • Battery life is of great concern and every network
request eats into that • Spinning up the cell modem, making request,
waiting for it drains the battery • So if you make 5 requests instead of 1, draining
battery, leaving user waiting
![Page 16: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/16.jpg)
Number of Requests: Solutions
• Paging size as mentioned above, try to maximize response that will fit in a packet to minimize the number of requests • So one request with 20KB much better than 5
with 4KB each • Batch multiple requests into a single one
![Page 17: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/17.jpg)
Batch Request Library
• Finding no great existing batch request library for Node.js, we rolled our own open source library
• http://batch-request.socialradar.com • Allows you to send a single request that represents
multiple • No need to compromise on RESTful principles and
move to a SOA • Handles complex dependencies
![Page 18: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/18.jpg)
Batch Request Library (2)1. npm install batch-request 2. Add one line of code to your API:
app.post('/batch', batch.validate, batch);!3. Send batch request as single POST:
{ "myRequest1": { "method": "GET", "uri": "http://api.mysite.com/users/1/first_name" }, "myRequest2": { "method": "GET", "uri": "http://api.mysite.com/users/1/email" } }
![Page 19: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/19.jpg)
Batch Request Library (3)4. Receive single response:
{ "myRequest1": { “statusCode": 200, “body": “Victor”, “headers”: {…} }, "myRequest2": { “statusCode": 200, “body": “[email protected]”, “headers”: {…} } }
![Page 20: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/20.jpg)
When you pull to refresh, Batch Request to:
• Update user location with
latest GPS reading • Retrieve current City/State • Retrieve most recent stat
numbers • Get some user lists to
cache them
![Page 21: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/21.jpg)
Stateless• Cell networks are spotty
• For performance, want load balance among multiple servers
• Solution: Assign access token to device rather than relying on sessions for user access
• We have an access token which is an encrypted string containing user_id and some other stuff
![Page 22: Designing an API for Mobile - SocialRadar](https://reader034.vdocuments.mx/reader034/viewer/2022051411/5455cc81b1af9f39378b4ac3/html5/thumbnails/22.jpg)
Victor Quinn @victorquinn
Lead Node.js Engineer !
Download our App
Questions?