Save 37% off PRO during our Black Friday Sale! »

High-Performance iOS Networking

High-Performance iOS Networking

Talk about building fast networking iOS apps and how to debug them at UIKonf in Berlin on 23 May 2016.


Clay Smith

May 23, 2016


  1. High Performance iOS Networking @smithclay · New Relic UIKonf ·

    23 May 2016 · Berlin
  2. This talk: tech notes • iOS 9 only (changes in

    iOS 10?) • NSURLSession is assumed (AFNetworking 2.0+) • Not about the absence of a network connection. • No code—focus on the network layer and server-side. Don't block the main thread.
  3. Your App Backend HTTP(S) Vintage client—server

  4. Your App Authentication Messaging Dynamic Content Analytics Advertising HTTP 2016

  5. c Max speed limit

  6. Full TCP handshake Device Server 1. SYN 2. SYN/ACK 3.

    ACK Data { Round trip {
  7. San Francisco 9142 km Singapore 16093 km Latency

  8. How do we reduce network latency?

  9. TCP Fast Open

  10. TCP handshake + TFO Device Server 1. SYN 2. SYN/ACK+cookie

    3. ACK Data { Round trip {
  11. TCP handshake, #2 Request Device Server 1. SYN+cookie 2. SYN/ACK+data

    { Round trip
  12. TCP Fast Open Guide • Only for idempotent data (HTTP

    GET) • Needs Linux Kernel v4.1 • wwdc2015/719/ • 2015/719ui2k57m/ 719/719_your_app_and_next_generation_net works.pdf?dl=1 (Implementation details)
  13. Does it work with NSURLSession? No :(

  14. Transport Layer Security (TLS)

  15. App Transport Security nscurl --ats- diagnostics <url> TLS 1.2, Strong

  16. Full TCP + TLS handshake Device Server Data Transfer {

    Round trip 1. SYN 2. SYN/ACK 3. ACK { { TLS, +2 RTTs Cool demo (browser-based):
  17. Use a CDN if possible TLS tuning for iOS (server-side)

    Reduce RTTs (False start, session resumption) Coming soon (?): TLS 1.3 Visit
  18. HTTP/2

  19. None
  20. TCP Multiplexing in HTTP/2 Device Server* Connection closes Connection opens *TLS Required
  21. Nice H2 features Header Compression (HPACK) Server Push (not supported

    on iOS... yet) Stream Priorities (not supported on iOS)
  22. HTTP/2 backend support? openssl s_client -alpn h2 -connect

    | grep ALPN · * OpenSSL 1.0.2g or greater needed *
  23. Use a CDN (shortcut) H2 implementation tips Consider server-side TCP

    tuning (initcwnd) Use keep-alive for HTTP 1.1 clients Measure before + after
  24. Caching with NSURLSession

  25. Use the Cache-Control header • NSURLRequestUseProtocolCachePolicy (default policy for NSURLSession)

    observes the protocol spec. • Cache-Control: max-age=<seconds>, public
  26. The ETag Header Device Server 200 OK Last-Modified: Mon, 22

    ETag: ad87... GET /fave-cats.json GET /fave-cats.json If-None-Matched: ad87.. 304 Not Modified
  27. Debugging


  29. Example log

  30. Remote Virtual Interface • rvictl creates an interface for packet

    capture tools (i.e. wireshark) • Device must be plugged in using USB
  31. Wireshark setup Download:

  32. ...see all the packets

  33. Instruments: Network Must test on a real device.

  34. Meh. Must test on a real device.

  35. HTTP Proxies • mitmproxy: (0.16 supports HTTP/2!) •

    (Nice UI, paid, no HTTP/2 support yet)
  36. mitmproxy: install notes 1.El Capitan python issues: use brew install

    python 2. For simulator, must set proxy on OS X to to start capturing HTTP(S) traffic. 3.Must install certificate after running proxy: go to on simulator.
  37. Inspect request/response

  38. Other tools: bad networks • augmented-traffic-control • network-link-conditioner/

  39. Checklist Repeat this 5 times: the network is unreliable*. Reduce

    the number of TCP connections. Don't implement your own caching scheme: use the protocol features. Test and measure network traffic regularly. *
  40. Last thought The person who uses your app doesn't care

    about network stuff. They just want it to be fast.
  41. Thanks. @smithclay · New Relic UIKonf · 23 May 2016

    · Berlin Slides will posted on Twitter.