High Performance Browser Networking
what every web developer should know about networking and browser performance
Ilya Grigorik - @igrigorik
Our agenda for the day...
Web performance this, …, that ...
Networks will just get faster and all of our problems will go away. Right?
… sadly, nope.
Speed is a feature (Chapter 1)
Speed of light is not fast enough!
Speed is a feature (Chapter 1)
Route | Distance | Time�light in vacuum | Time�light in fiber | Round-trip time �(RTT) in fiber |
New York to San Francisco | 4,148 km | 14 ms | 21 ms | 42 ms |
New York to London | 5,585 km | 19 ms | 28 ms | 56 ms |
New York to Sydney | 15,993 km | 53 ms | 80 ms | 160 ms |
Equatorial circumference | 40,075 km | 133.7 ms | 200 ms | 200 ms |
Last mile is slow!
Fiber-to-the-home services provided 18 ms round-trip latency on average, while cable-based services averaged 26 ms, and DSL-based services averaged 43 ms. This compares to 2011 figures of 17 ms for fiber, 28 ms for cable and 44 ms for DSL.
Measuring Broadband America - July 2012 - FCC
Last mile == Transatlantic hop? Wat!? Yeah...
Now you know why carriers don’t advertise these numbers…
Speed is a feature (Chapter 1)
(26 + 28) * 2
108 ms RTT!
Worldwide: ~100 ms
US: ~50~60 ms
Average RTT to Google in 2012 was…
and sadly it’s not improving much
4G will save us all! Says marketing guy...
"Users of the Sprint 4G network can expect to experience average speeds of 3 Mbps to 6 Mbps download and up to 1.5 Mbps upload with an average latency of 150 ms. On the Sprint 3G network, users can expect to experience average speeds of 600 Kbps - 1.4 Mbps download and 350 Kbps - 500 Kbps upload with an average latency of 400 ms."
Mobile Networks (Chapter 7)
(everytime I see a “4G speed” advertisement)
Wireless “last mile” is, painful...
| LTE | HSPA+ | HSPA | EDGE | GPRS |
AT&T core network latency | 40-50 ms | 50-200 ms | 150-400 ms | 600-750 ms | 600-750 ms |
Mobile Networks (Chapter 7)
OK. Latency is a problem.
but bandwidth is important too, how we’re doing there?
Brazil: ~2.5Mbps, Australia: ~5Mbps, US, UK: ~8Mbps
State of the Internet - Akamai - 2007-2013
Good news, mobile BW is improving rapidly!
That’s what the ads are all about. �Yes, last-mile latency is also much better (~50 ms), but that’s still worse than most wired connections!
Generation | Data rate |
2G | 100 – 400 Kbit/s |
3G | 0.5 – 5 Mbit/s |
4G | 1 – 50 Mbit/s |
Mobile Networks (Chapter 7)
Bandwidth + Latency ≈ Performance
but what’s the actual relationship between these factors?
Components of an HTTP request
Primer on Web Performance (Chapter 10)
Our pages consist of dozens of assets
Huh?
Primer on Web Performance (Chapter 10)
… (snip 30 requests) ...
Latency vs. Bandwidth impact on Page Load Time
“To speed up the Internet at large, we should look for more ways to bring down RTT. What if we could reduce cross-atlantic RTTs from 150 ms to 100 ms? This would have a larger effect on the speed of the internet than increasing a user’s bandwidth from 3.9 Mbps to 10 Mbps or even 1 Gbps.” - Mike Belshe
Single digit % perf improvement after�5 Mbps
Primer on Web Performance (Chapter 10)
Linear improvement in page load time!
Why? To answer that, let’s dig a bit deeper...
Optimizing transport performance
aka, optimizing TCP and TLS...
SYN, SYN-ACK, ACK
Building blocks of TCP (Chapter 2)
TCP Slow-Start (congestion control)
Building blocks of TCP (Chapter 2)
Let’s do science to it…
How long would it take to transfer 64 KB?
(on a new TCP connection)
Answer: 224 milliseconds + handshake RTT = 280 ms
Ouch.
Building blocks of TCP (Chapter 2)
Let’s fetch a 20 KB HTML file...
236 milliseconds!
Building blocks of TCP (Chapter 2)
Let’s fetch same file over existing TCP connection.
96 milliseconds - 275% improvement!
Building blocks of TCP (Chapter 2)
Re-use existing TCP connections!
Also, notice that we’re (still) nowhere close to our 5 Mbps “bandwidth ceiling”.
Most of the HTTP data flows consist of small, bursty data transfers, whereas TCP is optimized for long-lived connections and bulk data transfers. Network roundtrip time is the limiting factor in TCP throughput and performance in most cases. Consequently, latency is the performance bottleneck for HTTP and most web applications delivered over it.
“Connection view” tells the story...
We’re not BW limited, we’re literally idling, waiting on the network to deliver resources.
Mystery solved...
Optimizing for TCP (Chapter 2)
Now let’s make it secure...
TLS is an unoptimized frontier for most sites!
SYN, SYN-ACK, ACK, … ClientHello, ServerHello, ...
Transport Layer Security (Chapter 4)
You might want to consider re-using TLS connections! ��Just saying...
Session resumption removes one RTT!
Verify that your server (at least) supports session ticket resumption.��
Transport Layer Security (Chapter 4)
“Early termination” is a big win for TLS
Early termination helps non-encrypted traffic also, but it is especially useful for accelerating TLS connections!
Transport Layer Security (Chapter 4)
* Talk to your CDN provider, or, you can roll your own.. Spin up some “cloud servers” and you’re in business.
TLS Performance Checklist (Chapter 4)
Measuring network performance
Gathering data from real users, on real networks, with real devices...
Navigation Timing (W3C)
Synthetic and Real-User Performance Measurement (Chapter 10)
Navigation Timing (W3C)
Synthetic and Real-User Performance Measurement (Chapter 10)
Available in...
Real User Measurement (RUM) with Google Analytics
<script>� _gaq.push(['_setAccount','UA-XXXX-X']);� _gaq.push(['_setSiteSpeedSampleRate', 100]); // #protip� _gaq.push(['_trackPageview']);� </script>
Google Analytics > Content > Site Speed
You have all the power of Google Analytics! Segments, conversion metrics, ...
Full power of Google Analytics to segment, filter, compare, ...
Head into the Technical reports to see the histograms and distributions!
Averages are misleading...
Synthetic and Real-User Performance Measurement (Chapter 10)
Case study: igvita.com server response times
Content > Site Speed > Page Timings > Performance
Bimodal response time distribution?
Theory: user cache vs. database cache vs. full recompute
Synthetic and Real-User Performance Measurement (Chapter 10)
2G, 3G, 4G...
have fundamentally different architecture at the radio layer
Design criteria #1: "Stable" performance + scalability
Design criteria #2: Battery life optimization
Design criteria #1: "Stable" performance + scalability
Design criteria #2: Maximize battery life
Radio Resource Controller (RRC)
Radio Resource Controller
... (some time later) ...
RRC
All communication and power management is centralized and managed by the RRC.
Mobile Networks (Chapter 7)
Control and User-plane latencies
RRC
I want to
send data!
1
2
1-X RTT's of negotiations
3
Application data
Control-plane
User-plane
| LTE | HSPA+ | 3-3.5G |
Idle to connected latency | < 100 ms | < 100 ms | < 2500 ms |
User-plane latency | < 5 ms | < 10 ms | < 50 ms |
Same process happens for incoming data, just reverse steps 1 and 2
Anticipate RRC latency...
Plan for "first packet" delay
Much of the "variability" is explained by RRC delays
| HSPA�(200 ms RTT) | HSPA+ / LTE�(80 ms RTT) |
Control plane | (200-2500 ms) | (50-100 ms) |
DNS lookup | 200 ms | 80 ms |
... | ... | ... |
RRC
Watch those energy tails!
Wasted energy
3G state machine�
Every data transfer, both big and small, will:
Mobile Networks (Chapter 7)
Minimize periodic data transfers
�
Prefetch data
It's all about the battery...
Pandora beacons: 0.2% total bytes == 46% battery
Hands on example....
Measuring energy & radio...
Ping, ping, ping, ... where'd my battery go?
Watch out for...
Mobile Networks (Chapter 7)
* The radio is never fully "off"... It wakes up periodically to listen to broadcasts and notifications!
Physical connectivity (Radio)
Transport connectivity (TCP / UDP)
The radio can be off* while the transport is idle.
window.setInterval(keepAlive(), 1000);
Carrier timeouts: 5-30 minutes.�
Not so good news everybody! ....
HSPA+ will be the dominant network type of the next decade!
Mobile Networks (Chapter 7)
Design for variable network performance & availability
var backoff = backoff.strategy({
randomisationFactor: 0,
initialDelay: 60,
maxDelay: 600
});
backoff.failAfter(10);
Hypertext Transfer Protocol
The good, the bad, … and the bright future ahead (aka, 2.0)
$> telnet igvita.com 80
Connected to 173.230.151.99
GET /archive
Hypertext delivery with HTTP 0.9! - eom.
(connection closed)
HTTP 0.9 is the ultimate MVP - one line, plain-text “protocol” to test drive the “www idea”.
$> telnet ietf.org 80
Connected to 74.125.xxx.xxx
GET /rfc/rfc1945.txt HTTP/1.0
User-Agent: CERN-LineMode/2.15 libwww/2.17b3
Accept: */*
HTTP/1.0 200 OK
Content-Type: text/plain
Content-Length: 137582
Last-Modified: Wed, 1 May 1996 12:45:26 GMT
Server: Apache 0.84
4 years of rapid iteration later… eom.
(connection closed)
HTTP 1.0 is an informational RFC - documents “common usage” of HTTP found in the wild.
$> telnet google.com 80
Connected to 74.125.xxx.xxx
GET /index.html HTTP/1.1
Host: website.org
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4)... (snip)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: __qca=P0-800083390... (snip)
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked
Server: nginx/1.0.11
Content-Type: text/html; charset=utf-8
Date: Wed, 25 Jul 2012 20:23:35 GMT
Expires: Wed, 25 Jul 2012 20:23:35 GMT
Cache-Control: max-age=0, no-cache
100
<!doctype html>
(snip)
HTTP 1.1 ships as RFC standard in 1999 - hyper{t̶e̶x̶t̶}media all the things!
@igrigorik
15 years later!
HTTPbis is getting dangerously close to closing all the outstanding 1.1 bugs. :)
In the meantime a few things have happened since...
Geocities ftw!�(circa 1997-2000)
Then AJAX happened.
(circa 2002-2005)
WebGL
WebRTC
Web Audio
WebSockets
…
Mobile web
(today)
For all its awesome, HTTP 1.X has a lot of perf problems...
@igrigorik
Dozens of competing TCP flows...
In short, there is no upside to this. But that’s what we have to work with...
E.g. Etsy over-sharding...
Too much of a “good thing” can cause harm. In this case, parallel flows are competing and cause a significant amount of spurious retransmissions (duplicate bytes on the client).
Where there’s a will, there’s a way...
we’re an inventive bunch, so we came up with some “optimizations” (read, “hacks”)
… why not fix HTTP instead?
HTTP 2.0 work kicked off in Jan 2012.
"HTTP 2.0 is a protocol designed for low-latency transport of content over the World Wide Web"
HTTP 2.0 in one slide…
@igrigorik
“... we’re not replacing all of HTTP — the methods, status codes, and most of the headers you use today will be the same. Instead, we’re redefining how it gets used “on the wire” so it’s more efficient, and so that it is more gentle to the Internet itself ....”
- Mark Nottingham (HTTPbis chair)
Binary framing crash course in one slide...
@igrigorik
frame = buffer.read(8)
if frame_i_care_about
do_something_smart
else
buffer.skip(frame.length)
end
Basic data flow in HTTP 2.0...
E.g. Please send critical.css with priority 1, please! kittens.jpg with priority 10.
@igrigorik
Connection + stream flow-control!
Stream flow-control enables fine-grained resource control between streams. E.g…�
Client controls how and when the stream and connection window is incremented!
@igrigorik
Server push… is replacing inlining.
Inlining is server push. Except, HTTP 2.0 server push is cacheable! �
@igrigorik
HTTP 2.0 provides header compression!
@igrigorik
* as low as 8 bytes for an identical request.
Byte cost of new stream: 8 bytes! *
tl;dr: framing, mux, prioritization, flow control
same HTTP protocol semantics, complete performance reboot!
Benefits
Opportunities
@igrigorik
�
* or today, via SPDY.
Available in your browser circa ~2014! *
HTTP 2.0 draft 6 implementations in Firefox, Chrome, IE11
has SPDY support (stepping stone). Plus client/server
implementations in C, JavaScript (node.js), Ruby, Perl, etc.
HTTP 2.0 is coming to a client / server near you in 2014.
github.com/igrigorik/http-2
Optimizing Application Delivery (Chapter 13)
Are we fast yet?
P.S. Check out the book for (much) more!
Not fast enough? Ok, let’s talk
WiFi performance
requires its own (unique) set of optimizations...
Wireless LAN, aka Wi-Fi...
WiFi (Chapter 6)
How do we schedule communication?
WiFi (Chapter 6)
Carrier Sense Multiple Access + Collision Detection
There is not much more to it...
WiFi (Chapter 6)
Wi-Fi channels 101
Non-overlapping channels: 1, 6, 11
WiFi (Chapter 6)
Real-world Wi-Fi performance: 2.4 Ghz vs 5 Ghz (YMMV)
Real-world 1st hop latency
| Median (ms) | 95% (ms) | 99% (ms) |
2.4 Ghz | 6.22 | 34.87 | 58.91 |
5 Ghz | 0.90 | 1.58 | 7.89 |
Sample data from my own home Wi-Fi network...
Upgraded router, removed ~50 ms of latency on
first hop!
Adapt to variable bandwidth
�
Adapt to variable latency and jitter