Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Clay Smith
January 11, 2016

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

Clay Smith

January 11, 2016
Tweet

More Decks by Clay Smith

Other Decks in Programming

Transcript

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

    View full-size slide

  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.

    View full-size slide

  3. ©2008-15 New Relic, Inc. All rights reserved. 3
    App Network Backend
    Web Server
    The Three Pillars of App Blame

    View full-size slide

  4. ©2008-15 New Relic, Inc. All rights reserved.
    Blame the app.
    4

    View full-size slide

  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...

    View full-size slide

  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

    View full-size slide

  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"]]];
    }

    View full-size slide

  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



    View full-size slide

  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

    View full-size slide

  10. ©2008-15 New Relic, Inc. All rights reserved.
    Waterfalls are for mobile, too!
    10

    View full-size slide

  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

    View full-size slide

  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




    View full-size slide

  13. ©2008-15 New Relic, Inc. All rights reserved.
    Blame the network.
    13

    View full-size slide

  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." ★★ ★ ★ ★
    "不正なアプリケーション" ★ ★ ★ ★ ★

    View full-size slide

  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

    View full-size slide

  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.

    View full-size slide

  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



    View full-size slide

  18. ©2008-15 New Relic, Inc. All rights reserved.
    Blame the backend.
    18

    View full-size slide

  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." ★ ★ ★ ★ ★

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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").




    View full-size slide

  23. ©2008-15 New Relic, Inc. All rights reserved. 23
    Good idea
    Pay attention to the network

    View full-size slide