Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

- MENU - ■APPS TODAY LATENCY WOES PROTOCOLING MORE PROTOCOLING DEBUGGING

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Apps Today Client Newton Image CC 2.0 SA, flickr.com/photos/moparx/5321857668 Analytics Auth Ads Custom Content CDN API Gateway

Slide 5

Slide 5 text

DANGER Network latency ahead Newton Image CC 2.0 SA, flickr.com/photos/moparx/5321857668

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

• 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

Slide 8

Slide 8 text

Protocols So what's the deal with...

Slide 9

Slide 9 text

Full TCP Handshake 1. SYN 2. SYN/ACK 3. ACK Data { Round trip { Client/Device Server RTT Count: 2

Slide 10

Slide 10 text

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!

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Protocol Performance Tips for App Devs Generally: reduce connections Use latest TLS version $ nscurl --ats-diagnostics 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

Slide 14

Slide 14 text

HTTP/2 So what's the deal with...

Slide 15

Slide 15 text

iOS 9+ w/ NSURLSession

Slide 16

Slide 16 text

H2 Connection Multiplexing Client/Device Server Over a single TCP connection, multiple HTTP messages can be sent more efficiently. Connection closes Connection opens

Slide 17

Slide 17 text

H2 ADVANCED FEATURES HEADER COMPRESSION STREAM PRIORITIES (iOS 10) SERVER PUSH (iOS 10)

Slide 18

Slide 18 text

• 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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Caching So what's the deal with...

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

• NSURLRequestUseProtocolCachePolicy (default policy for NSURLSession) observes the protocol spec. • Cache-Control: max-age=, public Cache-Control Header

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

• API Versioning (using headers—backwards compatible) • Horizontally scalable • Gateway for other legacy systems • Downside: must be highly available BFF attributes

Slide 26

Slide 26 text

Debugging So what's the deal with...

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Example log

Slide 29

Slide 29 text

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/

Slide 30

Slide 30 text

Wireshark Setup Download: http://wireshark.com/

Slide 31

Slide 31 text

Total Packet V I S I B I L I T Y

Slide 32

Slide 32 text

Instruments: Network

Slide 33

Slide 33 text

Not my go-to (sorry)

Slide 34

Slide 34 text

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)

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Example http://docs.mitmproxy.org/en/latest/mitmproxy.html

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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/

Slide 39

Slide 39 text

Making apps fast is for people. ?s slides: slack/twitter @smithclay

Slide 40

Slide 40 text

SP 0:40:00 STOP