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

High Performance iOS Networking, v1.20

Clay Smith
September 07, 2016

High Performance iOS Networking, v1.20

Every year, iOS apps become more connected to network services used for critical functions like authentication and fetching content. The link between code on devices and the services that power them, however, is often unreliable and slow—especially for apps that have global reach. This talk is an overview of techniques and solutions available to minimize network latency when building high-performance apps, including new protocols available in iOS 9 like HTTP/2, SSL optimization techniques, and using content-delivery networks.

Talk at iOSDevUK, September 2016.

Clay Smith

September 07, 2016
Tweet

More Decks by Clay Smith

Other Decks in Programming

Transcript

  1. The Client Server A E S T H E T

    I C data Server Client Newton Image CC 2.0 SA, flickr.com/photos/moparx/5321857668 data
  2. • Wales to San Francisco: 5,284 mi (8503km) • Speed

    of Light in Fiber Optic Cable: 2*10^8 m/s • Human "this wasn't instantaneous" time: 100ms • We can perceive global network latency even under perfect conditions: YES
  3. Full TCP Handshake 1. SYN 2. SYN/ACK 3. ACK Data

    { Round trip { Client/Device Server RTT Count: 2
  4. Full TCP + TLS Handshake Client/Device Server Data Transfer {

    Round trip 1. SYN 2. SYN/ACK 3. ACK { { TLS, +2 RTTs RTT Count:+2 Required for ATS!
  5. TCP Fast Open (TFO) Client/Device Server 1. SYN 2. SYN/ACK+cookie

    3. ACK Data { Round trip { http://devstreaming.apple.com/videos/wwdc/2015/719ui2k57m/719/719_your_app_and_next_generation_networks.pdf?dl=1 RTT Count: 2
  6. TFO,#2 Req Client/Device Server 1. SYN+cookie 2. SYN/ACK+data { Round

    trip iOS 9+ supports this, but you're probably not going to use the low-level APIs to make this work. RTT Count:1
  7. Protocol Performance Tips for App Devs Generally: reduce connections Use

    latest TLS version $ nscurl --ats-diagnostics <url> Physically closer is better Don't tune (unless you have to)* *If you are going to: https://tools.ietf.org/html/draft-stenberg-httpbis- tcp-01 + https://istlsfastyet.com
  8. H2 Connection Multiplexing Client/Device Server Over a single TCP connection,

    multiple HTTP messages can be sent more efficiently. Connection closes Connection opens
  9. • Likely will need to upgrade your HTTP server... •

    Major Content Delivery Networks (CDNs) support it. • https://tools.keycdn.com/http2-test • https://blog.newrelic.com/2016/02/17/http2- production/ Server-side support
  10. HTTP/2 Tips for iOS Devs H2 is not magic perf

    sauce Server-side push not well understood (yet)* Having metrics in place before switch is critical * Google is helping with this: https://docs.google.com/document/d/1K0NykTXBbbbTlv60t5MyJvXjqKGsCVNYHyLEXIxYMv0/edit Supported in NSURLSession
  11. • NSURLRequestUseProtocolCachePolicy (default policy for NSURLSession) observes the protocol spec.

    • Cache-Control: max-age=<seconds>, public Cache-Control Header
  12. ETag Header Client/Device Server ETag headers can inform clients they

    already have the requested resource. 200 OK Last-Modified: Mon, 22 ETag: ad87... GET /fave-cats.json GET /fave-cats.json If-None-Matched: ad87.. 304 Not Modified
  13. Enabling more rapid server-side change "Backends for frontends" (BFFs) See:

    Netflix, Spotify (and others) Device "iOS Client Service" Ye Olde Systems HTTP2/ gRPC (?) QUIC (?) HTTP 1.1/ Terrible XML Stuff
  14. • API Versioning (using headers—backwards compatible) • Horizontally scalable •

    Gateway for other legacy systems • Downside: must be highly available BFF attributes
  15. Remote Virtual Interface • rvictl creates an interface for packet

    capture tools (i.e. wireshark) • Device must be plugged in using USB • https://developer.apple.com/ library/mac/qa/qa1176/ _index.html • useyourloaf.com/blog/remote- packet-capture-for-ios-devices/
  16. HTTP Proxies • mitmproxy: mitmproxy.org (0.16 supports HTTP/2!) • charlesproxy.com/Fiddler

    (Nice UI, paid, no HTTP/2 support yet) • Will not work with certificate pinning (no MITM permitted)
  17. 1. El Capitan python issues: use brew install python 2.

    For simulator, must set proxy on OS X to 127.0.0.1:8080 to start capturing HTTP(S) traffic. 3. Must install certificate after running proxy: go to http://mitm.it on simulator. mitmproxy install notes
  18. NSURLSession Metrics [new!] New in iOS 10! Thanks Apple! func

    urlSession( _ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) • Number of Redirects, time it took task to complete • Whole lot of goodness in NSURLSessionTaskMetrics • Build your own waterfall! • https://developer.apple.com/reference/foundation/ nsurlsessiontasktransactionmetrics
  19. Thoughts Use NSURLSession. Audit server-side: using latest TLS? Is caching

    configured correctly? HTTP2? Consider using a CDN and measure latency! Reduce number of requests per host. Read https://hpbn.co/