BD Conf: Visit speed - Page speed is only the beginning

63
Page Speed is Only the Beginning VISIT-SPEED 1

description

How can we beat the speed of light and make visits faster? Pre-fetching is one way we can make resources available before they're needed. This talk explores challenges in mobile visit performance and discusses the design of a generic pre-fetching system.

Transcript of BD Conf: Visit speed - Page speed is only the beginning

Page 1: BD Conf: Visit speed - Page speed is only the beginning

Page Speed is Only the BeginningVISIT-SPEED

1

Page 2: BD Conf: Visit speed - Page speed is only the beginning

WHO AM I?

WHO CARES IF VISITS ARE SLOW?

WHY ARE VISITS SLOW?

PRE-FETCHING: Making Visit Faster

In this presentation:

RESULTS & DISCUSSION

2

Page 3: BD Conf: Visit speed - Page speed is only the beginning

WHO AM I?

WHO CARES IF VISITS ARE SLOW?

WHY ARE VISITS SLOW?

PRE-FETCHING: Making Visit Faster

RESULTS & DISCUSSION

3

Page 4: BD Conf: Visit speed - Page speed is only the beginning

Peter McLachlanchief architect & co-founder of Mobify

4

Page 5: BD Conf: Visit speed - Page speed is only the beginning

HUGE IMAGE WHERE I INTRODUCE

MYSELF

5

Page 6: BD Conf: Visit speed - Page speed is only the beginning

6

Page 7: BD Conf: Visit speed - Page speed is only the beginning

WHAT IS VISIT-SPEED?

7

Page 8: BD Conf: Visit speed - Page speed is only the beginning

• More than just a single metric.

• How your visitors interact with your website.

• We’re talking about all the waiting time that exists:

1. within pages, while visitors wait for interactive elements to finish loading.

2. between pages, such as when visitors traverse a checkout flow.

VISIT-SPEED IS...

8

Page 9: BD Conf: Visit speed - Page speed is only the beginning

THERE’S A LOT MORE TO USER ENGAGEMENT THAN PAGE SPEED!

User engagement is about the experience.

• experience crosses the page boundary!

We need a heck of a lot more UX research on this subject.

• but you’re not here for a UX talk!

For now, an appeal to intuition: doesn’t it seems intuitive that

people care about the speed of the entire experience?

• first load is just one step along the way.

• rough guess: I’d say it’s 1/3rd of the puzzle or less.

9

Page 10: BD Conf: Visit speed - Page speed is only the beginning

WHO AM I?

WHO CARES IF VISITS ARE SLOW?

WHY ARE VISITS SLOW?

PRE-FETCHING: Making Visit Faster

RESULTS & DISCUSSION

10

Page 11: BD Conf: Visit speed - Page speed is only the beginning

You do. You just don’t know how much yet.

11

Page 12: BD Conf: Visit speed - Page speed is only the beginning

If your users spend 8s waiting for a page to

load instead of 2, here is the impact:

1,992 hours/yr

19,920 hours/yr

199,200 hours/yr

How many pageviews do you get in a month?

100,000?

1,000,000?

10,000,000?

... or about a quarter of a human lifetime

12

Page 13: BD Conf: Visit speed - Page speed is only the beginning

73% of mobile internet users say they’ve encountered a website that was too slow to load.

Loading...

13

Page 14: BD Conf: Visit speed - Page speed is only the beginning

73% of mobile internet users say they’ve encountered a website that was too slow to load.

Data source: kissmetrics.com

Loading...

13

Page 15: BD Conf: Visit speed - Page speed is only the beginning

73% of mobile internet users say they’ve encountered a website that was too slow to load.

Data source: kissmetrics.com

Loading...

27% of internet users are

LIARS

13

Page 16: BD Conf: Visit speed - Page speed is only the beginning

A 1 second delay in page response can result in a 7% reduction in conversions.If an e-commerce site is making $100,000 per day, a 1 second page delay could potentially cost you $2.5 million in lost sales every year.

14

Page 17: BD Conf: Visit speed - Page speed is only the beginning

A 1 second delay in page response can result in a 7% reduction in conversions.If an e-commerce site is making $100,000 per day, a 1 second page delay could potentially cost you $2.5 million in lost sales every year.

Data source: kissmetrics.com

14

Page 18: BD Conf: Visit speed - Page speed is only the beginning

15%

16%

16%

20%

30%

3%

16-20 seconds

1-5 seconds

11-15 seconds

20+ seconds

6-10 seconds

> One second

Data source: kissmetrics.com

PATIENCE OFMOBILE WEBUSER

15

Page 19: BD Conf: Visit speed - Page speed is only the beginning

WHO AM I?

WHO CARES IF VISITS ARE SLOW?

WHY ARE VISITS SLOW?

PRE-FETCHING: Making Visit Faster

RESULTS & DISCUSSION

16

Page 20: BD Conf: Visit speed - Page speed is only the beginning

Two words for you: Ilya Grigorik (https://twitter.com/igrigorik)

• http://www.igvita.com/2013/01/15/faster-websites-crash-course-

on-web-performance/

But let me briefly recap:

1. Too many HTTP requests

2. High request latency

3. Too many bytes

4. Poor resource cache settings

5. Laggy interactions

LETS START WITH THIS: WHAT MAKES PAGES SLOW? (especially on mobile)

17

Page 21: BD Conf: Visit speed - Page speed is only the beginning

0Mbps

20Mbps

40Mbps

60Mbps

80Mbps

3Mbps

21Mbps

42Mbps

75Mbps

3G HSPA+ HSPA+ LTE

What’s interesting about this

18

Page 22: BD Conf: Visit speed - Page speed is only the beginning

0Mbps

20Mbps

40Mbps

60Mbps

80Mbps

3Mbps

21Mbps

42Mbps

75Mbps

3G HSPA+ HSPA+ LTE

What’s interesting about this

ABSOLUTELYNOTHING

19

Page 23: BD Conf: Visit speed - Page speed is only the beginning

AND HERE’S WHY ...

Source: Ilya Grigorik

0

1000

2000

3000

4000

1mbps 3mbps 5mbps 7mbps 9mbps0

1000

2000

3000

4000

240ms 200ms 160ms 120ms 80ms 40ms 0

Page load time (ms) aslatency (RTT) decreases

Page load time (ms) asbandwidth increases

20

Page 24: BD Conf: Visit speed - Page speed is only the beginning

Why do we care a lot about latency on mobile?

21

Page 25: BD Conf: Visit speed - Page speed is only the beginning

Mobile phone radio behaviour

22

Page 26: BD Conf: Visit speed - Page speed is only the beginning

WHO AM I?

WHO CARES IF VISITS ARE SLOW?

WHY ARE VISITS SLOW?

PRE-FETCHING: Making Visit Faster

RESULTS & DISCUSSION

23

Page 27: BD Conf: Visit speed - Page speed is only the beginning

Pre-fetch verb \preeˈfech\

To fetch web related resources before they are required, the activity of which is to be accomplished while the web browser is not otherwise occupied, outside of the critical rendering path of the page.

24

Page 28: BD Conf: Visit speed - Page speed is only the beginning

•When other performance optimizations have already been applied

•When your site has a flow

•When you use different resources on different pages of your site

• Of course this implies that you DON’T bundle all your JS into a single

file and deliver it in one go to the visitor

WHEN TO PRE-FETCH

25

Page 29: BD Conf: Visit speed - Page speed is only the beginning

Here’s what we need to think about

How should I implement

pre-fetching?

What should I pre-fetch?

PRE-FETCHING?

Do’s, Don’ts andTest results

26

Page 30: BD Conf: Visit speed - Page speed is only the beginning

WHAT SHOULDI PRE-FETCH?

27

Page 31: BD Conf: Visit speed - Page speed is only the beginning

PRE-FETCHINGWhat should we pre-fetch?There are a number of assets available to choose from: HTML, Images, CSS & JavaScript

HTML, ImagesMost large sites have thousands or tens of thousands of pages & images. Hard problem to solve, bad worst case scenario behavior

DNS, JavaScript & CSSThis makes a lot of sense. Most sites have just a few CSS files and less than 100 externally referenced JavaScript

28

Page 32: BD Conf: Visit speed - Page speed is only the beginning

HTML & IMAGES

• HTML

• would be great to pre-fetch - but many sites have uncacheable content

embedded in page HTML or poor caching rules

• complexity of overcoming CMS / eComm platform defaults prohibitive

• Images

• Most sites have lots of them

• Images are 65% of bytes on the average web page

• Seems like a great target, but which ones should you send to the visitor?

• Machine learning may help us here

• But you better have great data on your site usage

• Maybe there’s lower hanging fruit...

29

Page 33: BD Conf: Visit speed - Page speed is only the beginning

DNS

• DNS is great to pre-fetch

• There’s usually less than 50 DNS entries to resolve for an

entire website

• The OS will cache these entries

• All DNS entries are cacheable, at least a little bit• Not many sites with < 1 minute DNS cache times, most are

considerably longer

• There’s even a nice easy bit of markup we an use to do it:• <link rel="dns-prefetch" href=”...”>

30

Page 34: BD Conf: Visit speed - Page speed is only the beginning

CSS & JAVASCRIPT• CSS is blocking

• Definitely great to pre-fetch it!

• But a lot of sites use a single CSS file anyway

• So just make sure you have good caching rules on this

• If you do have multiple CSS files for different parts of your flow: definitely pre-fetch them

• JavaScript: often this is also blocking• I think JavaScript is the “holy grail” of pre-fetching

• With JS pre-fetch we have the possibility of significantly unblocking

parts of a visit flow

• You shouldn’t have too many JS files in your site (right?)

31

Page 35: BD Conf: Visit speed - Page speed is only the beginning

The winners: DNS, CSS & JAVASCRIPT

• OK, now how to select which items to pre-fetch?

• For DNS and CSS you can probably just “get all the things”

• DNS resolution is cheap with good caching behavior

• CSS should be relatively small

• JavaScript: you need a short list of items most likely to be useful during normal visit flows

32

Page 36: BD Conf: Visit speed - Page speed is only the beginning

mobify.com @mobify [email protected]

Pre-fetching

33

Page 37: BD Conf: Visit speed - Page speed is only the beginning

HOW SHOULDI IMPLEMENT

PRE-FETCHING?

34

Page 38: BD Conf: Visit speed - Page speed is only the beginning

HOW SHOULD I IMPLEMENT PRE-FETCHING?

Fully integrated into webapp•You can fully integrate pre-fetching into your platform / app

•An introspection service can analyze log data, URL visits

•Inject pre-fetching directives directly into the HTML

35

Page 39: BD Conf: Visit speed - Page speed is only the beginning

Pros

•You can introspect your environment

•Data sources: filesystem, web server

log files, content database, platform

(CMS, ecomm etc) APIs

•Lots of CPU power at your disposal

HOW SHOULD I IMPLEMENT PRE-FETCHING?

Fully integrated into webapp

Cons

• If you have a complex environment

with a CDN, multiple caching layers,

databases, web platforms, this is a

serious undertaking.

•Requires integration directly into your

platform, typically this means risk and

cost.

36

Page 40: BD Conf: Visit speed - Page speed is only the beginning

Client side generic ( With servier-side service)

HOW SHOULD I IMPLEMENT PRE-FETCHING?

• What if I want to create a completely generic service

• It has no knowledge of my specific app or platform

• But it can still make good recommendations

• In this model clients:

• collect information about resources they see and report back

• inject requests for resources during periods of low activity

37

Page 41: BD Conf: Visit speed - Page speed is only the beginning

Client side generic (With server-side service)

HOW SHOULD I IMPLEMENT PRE-FETCHING?

• What if I want to create a completely generic service

• It has no knowledge of my specific app or platform

• But it can still make good recommendations

• In this model clients:

• collect information about resources they see and report back

• inject requests for resources during periods of low activity

38

Page 42: BD Conf: Visit speed - Page speed is only the beginning

Client side generic ( With servier-side service)

HOW SHOULD I IMPLEMENT PRE-FETCHING?

Pros

• You get accurate information on

what resources visitors are

actually using

• Server-side service is

independent of all the rest of

your architecture, plays nicely

with SoA

• If it breaks, your site still works

Cons

• We’ll get to this!

39

Page 43: BD Conf: Visit speed - Page speed is only the beginning

DO’S, DON’TS AND TEST RESULTS

40

Page 44: BD Conf: Visit speed - Page speed is only the beginning

PRE-FETCHING: THE DETAILS

• Caching:• Divining cache content

• Aging out pre-fetched items

• edge cases

• Recommendation engine• Considerations

• Mystery time!

• What I’m working on

41

Page 45: BD Conf: Visit speed - Page speed is only the beginning

CACHING: DIVINING CACHE CONTENTS

Unfortunately we cannot inspect the browser’s cache

• (yes, there are ways to make guesses)

But we can just use localStorage directly, especially for JavaScript and CSS that have relatively few bytes.

42

Page 46: BD Conf: Visit speed - Page speed is only the beginning

CACHING: DIVINING CACHE CONTENTS

Unfortunately we cannot inspect the browser’s cache

• (yes, there are ways to make guesses)

But we can just use localStorage directly, especially for JavaScript and CSS that have relatively few bytes.

... uh-oh ....

How fast is localStorage? Doesn’t it:

• block the rendering thread?

• have unbounded worst case performance?

42

Page 47: BD Conf: Visit speed - Page speed is only the beginning

Yes, and you should use it anyway.

Details at: http://www.mobify.com/blog/smartphone-localstorage-outperforms-browser-cache/

43

Page 48: BD Conf: Visit speed - Page speed is only the beginning

CACHING & AGING

• You will need a mechanism to manage cache

eviction

• LRU looks to be a good simple algorithm to

implement here

• Perhaps augmented to provide preferential

treatment for smaller objects

44

Page 49: BD Conf: Visit speed - Page speed is only the beginning

CACHING: EDGE CASES

• Whoops. You just did a full site refresh...

• Dynamically generated resources:

• susan.js, bill.js, steve.js, candace.js ...

45

Page 50: BD Conf: Visit speed - Page speed is only the beginning

RECOMMENDATION ENGINE• Could use a very simple algorithm:

• Collect all the things

• Return truncated list based on frequency seen

• How big a list?

• “empirically derived” = “make wild assed guess”

• ... and test.

• Site wide / global

• More sophisticated modifications:

• ML algorithm such as K-nearest neighbor

• Weight by inverse resource size

• Resources must decay

• simplest solution is probably correct

46

Page 51: BD Conf: Visit speed - Page speed is only the beginning

MYSTERY TIME!Who can tell me what else we should worry about with

pre-fetching?

47

Page 52: BD Conf: Visit speed - Page speed is only the beginning

MYSTERY TIME!Who can tell me what else we should worry about with

pre-fetching?

47

Page 53: BD Conf: Visit speed - Page speed is only the beginning

SO, WHAT ARE YOU WORKING ON?

• Service will provide:

• a data collection endpoint for resources to generate recommendations

• a recommendation engine for resources

• As visitors browse your site, refines preferences for which resources to pre-load

• The pre-fetch library provides client-side support for

• collecting information about the page

• recommendation engine

• auto optimizing JSONP resource retrieval service

• auto-caching system using localStorage with correct cache eviction behavior

Pre-fetching recommendation as a service

48

Page 54: BD Conf: Visit speed - Page speed is only the beginning

WHERE AM I AT?

• Beginning “clinical trials”

• Recommendation engine still under consideration

• Testing hand-curated recommendations in field trials on production sites

49

Page 55: BD Conf: Visit speed - Page speed is only the beginning

WHO AM I?

WHO CARES IF VISITS ARE SLOW?

WHY ARE VISITS SLOW?

PRE-FETCHING: Making Visit Faster

RESULTS & DISCUSSION

50

Page 56: BD Conf: Visit speed - Page speed is only the beginning

51

Page 57: BD Conf: Visit speed - Page speed is only the beginning

52

Page 58: BD Conf: Visit speed - Page speed is only the beginning

53

Page 59: BD Conf: Visit speed - Page speed is only the beginning

ETHICAL CONSIDERATIONS

54

Page 60: BD Conf: Visit speed - Page speed is only the beginning

• Network bandwidth, CPU & battery are all resources with scarcity on

mobile

• So don't use them recklessly

• That's a good reason to focus on JS & CSS

• if your site has megs and megs of JS and CSS...

• what the heck are you doing!? Stop!

• limit your pre-fetching to one or two steps ahead

• Remember that using the radio when it's warm and then letting it sleep is

going to be better for battery than waking it up repeatedly

PRE-FETCHING CONSUMES RESOURCES

55

Page 61: BD Conf: Visit speed - Page speed is only the beginning

• Prefetching for HTML, images

• Much harder problems

• But solving them will have significant payoffs

WHAT’S NEXT?

56

Page 63: BD Conf: Visit speed - Page speed is only the beginning

Image credit: XKCD - “relativistic baseball”

No problem, we’ll make the speed of light faster

58