High Performance iOS Networking, v1.20

2ad55a66b8325eb097612c069ab76231?s=47 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.

2ad55a66b8325eb097612c069ab76231?s=128

Clay Smith

September 07, 2016
Tweet

Transcript

  1. High Performance iOS Networking, v1.2.0 @smithclay / New Relic iOSDevUK

    September 2016
  2. - MENU - ▪APPS TODAY LATENCY WOES PROTOCOLING MORE PROTOCOLING

    DEBUGGING
  3. 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
  4. Apps Today Client Newton Image CC 2.0 SA, flickr.com/photos/moparx/5321857668 Analytics

    Auth Ads Custom Content CDN API Gateway
  5. DANGER Network latency ahead Newton Image CC 2.0 SA, flickr.com/photos/moparx/5321857668

  6. -Physics! "Information can't travel faster than the speed of light."

  7. • 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
  8. Protocols So what's the deal with...

  9. Full TCP Handshake 1. SYN 2. SYN/ACK 3. ACK Data

    { Round trip { Client/Device Server RTT Count: 2
  10. 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!
  11. 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
  12. 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
  13. 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
  14. HTTP/2 So what's the deal with...

  15. iOS 9+ w/ NSURLSession

  16. H2 Connection Multiplexing Client/Device Server Over a single TCP connection,

    multiple HTTP messages can be sent more efficiently. Connection closes Connection opens
  17. H2 ADVANCED FEATURES HEADER COMPRESSION STREAM PRIORITIES (iOS 10) SERVER

    PUSH (iOS 10)
  18. • 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
  19. 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
  20. Caching So what's the deal with...

  21. -Common Sense/Unknown "The fastest network request is one you don't

    have to make."
  22. • NSURLRequestUseProtocolCachePolicy (default policy for NSURLSession) observes the protocol spec.

    • Cache-Control: max-age=<seconds>, public Cache-Control Header
  23. 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
  24. 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
  25. • API Versioning (using headers—backwards compatible) • Horizontally scalable •

    Gateway for other legacy systems • Downside: must be highly available BFF attributes
  26. Debugging So what's the deal with...

  27. CF_NETWORK Diagnostics https://developer.apple.com/library/ios/qa/qa1887/_index.html

  28. Example log

  29. 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/
  30. Wireshark Setup Download: http://wireshark.com/

  31. Total Packet V I S I B I L I

    T Y
  32. Instruments: Network

  33. Not my go-to (sorry)

  34. 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)
  35. 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
  36. Example http://docs.mitmproxy.org/en/latest/mitmproxy.html

  37. 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
  38. 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/
  39. Making apps fast is for people. ?s slides: slack/twitter @smithclay

  40. SP 0:40:00 STOP