Turning the app up to 11: Optimizing API Performance for Mobile

2ad55a66b8325eb097612c069ab76231?s=47 Clay Smith
January 11, 2016

Turning the app up to 11: Optimizing API Performance for Mobile

2ad55a66b8325eb097612c069ab76231?s=128

Clay Smith

January 11, 2016
Tweet

Transcript

  1. 2.

    2 ©2008-15 New Relic, Inc. All rights reserved. What this

    talk is about Mobile is everywhere! Network-related performance problems affect customer experience. Nobody wants 
 to get 1-star 
 app reviews. Covers network 
 request best 
 practices in 
 mobile.
  2. 3.

    ©2008-15 New Relic, Inc. All rights reserved. 3 App Network

    Backend Web Server The Three Pillars of App Blame
  3. 5.

    ©2008-15 New Relic, Inc. All rights reserved. 5 "App is

    unresponsive." ★ ★ ★ ★ ★ "Pages are slow to reload.” ★★★★★ "It takes forever when I tap login." ★★★★★ "The app freezes all the time." ★ ★ ★ ★ ★ Reviews you might read...
  4. 6.

    Confidential ©2008-15 New Relic, Inc. All rights reserved. 6 ▪

    Don't block the main thread Lesson #1: CC A ND https://www.flickr.com/photos/buechertiger/6499330057
  5. 7.

    ©2008-15 New Relic, Inc. All rights reserved. 7 Introducing... the

    world's slowest viewDidLoad: - (void) viewDidLoad { // Sync Example #1 NSHTTPURLResponse *response = nil; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://nr/api/v1/data"]; cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0]; NSData *conn = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // Sync Example #2 NSData *data = [NSData dataWithContentsOfURL:@"http://nr/api/data"]; // Sync Example #3 cell.photo.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://nr/bar.png"]]]; }
  6. 8.

    ©2008-15 New Relic, Inc. All rights reserved. Main Thread Blocking

    Checklist No synchronous network calls happen on the main thread. Use a decent open-source networking library that does the threading for you. If the UI stutters or scrolling performance seems degraded, start looking for code that's blocking the main (UI) thread. 8 ▪ ▪ ▪
  7. 9.

    Confidential ©2008-15 New Relic, Inc. All rights reserved. 9 ▪

    Do go chasing waterfalls Lesson #2: CC A ND https://www.flickr.com/photos/salford_ian/3053537527
  8. 11.

    ©2008-15 New Relic, Inc. All rights reserved. How many parallel

    requests are too many? • Per-host limit on iOS and Android. Fuzzy documentation. • Generally you'll get limited to 4-5 depending on OS version, library or SDK (NSURLSession vs NSURLConnection). • Rule: just bundle requests and don't flood hosts. 11
  9. 12.

    ©2008-15 New Relic, Inc. All rights reserved. API Client Request

    Best Practices Issue network requests in parallel (with limits). Have a single API request per screen. HTTP cache headers are your friend and API clients 
 can use them intelligently. Pay attention to request size (and gzip compression). 12 ▪ ▪ ▪ ▪
  10. 14.

    ©2008-15 New Relic, Inc. All rights reserved. Reviews you might

    read... 14 "Bonjour. Your app is slow on Orange 3G." ★ ★ ★ ★ ★ "Takes 15 seconds to login in Singapore." ★ ★ ★ ★ ★ "App is slow when not on WiFi." ★★ ★ ★ ★ "不正なアプリケーション" ★ ★ ★ ★ ★
  11. 15.

    Confidential ©2008-15 New Relic, Inc. All rights reserved. ▪ 15

    Never trust the network Lesson #3: CC A ND https://www.flickr.com/photos/ian1231/33443537527
  12. 16.

    ©2008-15 New Relic, Inc. All rights reserved. Information can't travel

    faster than the speed of light 16 CC0 Problem: 
 your datacenter is in Virginia and customers are in Singapore. Advanced: 
 Consider implementing CDN caching of API requests 
 as well. Content-delivery networks (CDNs) can move your static content physically closer to customers.
  13. 17.

    ©2008-15 New Relic, Inc. All rights reserved. Unreliable Network Best

    Practices Always test your app in airplane mode. Pay attention to user experience differences across 
 different regions, countries, and providers. Re-create bad networks locally using open-source tools 
 (i.e. augmented-traffic-control, Network Link Conditioner) 17 ▪ ▪ ▪
  14. 19.

    ©2008-15 New Relic, Inc. All rights reserved. Reviews you might

    read... 19 "Search takes foooooorever." ★ ★ ★ ★ ★ "The progress indicator keeps spinning." ★ ★ ★ ★ ★ "Keeps saying operation can't be completed." ★★ ★ ★ ★ "I get weird error messages about parsing JSON." ★ ★ ★ ★ ★
  15. 20.

    Confidential ©2008-15 New Relic, Inc. All rights reserved. 20 ▪

    "Mobile APIs are forever" (credit: @alperkomen) Lesson #4: CC A ND https://www.flickr.com/photos/torkildr/3462607995
  16. 21.

    ©2008-15 New Relic, Inc. All rights reserved. "This is clear

    to anyone who uses this API" 21 curl -H "a_token=aea22f80344147c4" http:// api.bad-api.horse/mobile/create/user?name=Bob {"status": "fail"} 200 OK Request
  17. 22.

    ©2008-15 New Relic, Inc. All rights reserved. 22 API Design

    for Mobile Best Practices Have clear, consistent documentation. Make mobile clients uniquely identifiable by the 
 User-Agent HTTP header. Return standard error codes (200s, 400s, and 500s). Consider versioned APIs ("mobile is forever"). ▪ ▪ ▪ ▪
  18. 24.
  19. 25.