Fronteers 20131205 the realtime web

38
The Realtime Web Bert Wijnants end2end engineer @ Mobile Vikings @bertwww www.newpage.be Fronteers meetup - 5 december 2013

description

 

Transcript of Fronteers 20131205 the realtime web

Page 1: Fronteers 20131205   the realtime web

The Realtime Web

!Bert Wijnants end2end engineer @ Mobile Vikings

@bertwww www.newpage.be

Fronteers meetup - 5 december 2013

Page 2: Fronteers 20131205   the realtime web

wooooooow websockets are so cool!

Live on television

Page 3: Fronteers 20131205   the realtime web
Page 4: Fronteers 20131205   the realtime web

Do web sockets make the internet realtime?

Page 5: Fronteers 20131205   the realtime web

Why is this the realtime web?

Page 6: Fronteers 20131205   the realtime web

The obvious stuff

ticker: realtime push updates

Page 7: Fronteers 20131205   the realtime web

The obvious stuff

chatbox

Page 8: Fronteers 20131205   the realtime web

But there’s a lot more…

super fast search

Page 9: Fronteers 20131205   the realtime web

But there’s a lot more…load content with ajax

Page 10: Fronteers 20131205   the realtime web

TTI: Time To InteractThe perceptual speed for user to be able to start interacting

Page 11: Fronteers 20131205   the realtime web

Time To Interact

<100ms: damn this app is fast, dude!

100ms - 300ms: this app is responding to my input

300ms - 500ms: mmmm, my brother is downloading some torrents again, the internet is slow

>500ms: Hey brother! Stop streaming those torrents, i’m trying to use the internet here.

Page 12: Fronteers 20131205   the realtime web

we live in the intergalactic cloud, right?

Impossible to avoid the latencies

Page 13: Fronteers 20131205   the realtime web

Don’t let the dark side betray youThey will say: “our servers are at full speed, they can’t be faster.”

…though it does not feel fast to your users

Page 14: Fronteers 20131205   the realtime web

Why is your ajax call soooo slow?

http request handshaking

server-side script running on shared resources

network latency to database

database query

http request: data transfer

Page 15: Fronteers 20131205   the realtime web

Stop hugging it!

Back-end developers just love their database

Page 16: Fronteers 20131205   the realtime web

Don’t put your program logic in the back-end

your users are waiting for every decision..

Page 17: Fronteers 20131205   the realtime web

Front-end developers to the rescue!

Page 18: Fronteers 20131205   the realtime web

It’s not that hard nowadays. You don’t need to learn linux or whatever…

Learn some back-end you must

Page 19: Fronteers 20131205   the realtime web

Strategy for realtime appsfor end2end developers

Page 20: Fronteers 20131205   the realtime web

Store data client-side Persist data server-side

query the database once when app starts

download all user-specific data needed

all users get same static content: 100% cache hit, so make sure you’re database caching is on!

this might take a few seconds the first time,.. show some static stuff to user first

Page 21: Fronteers 20131205   the realtime web

Push changes to clients

Page 22: Fronteers 20131205   the realtime web

When data changes

Push changes to clients immediately

use websockets or long polling: always open connection

Page 23: Fronteers 20131205   the realtime web

example: search box

Page 24: Fronteers 20131205   the realtime web

Search box with auto-complete

A field to search for users based on phone number or name

180.000 users

Page 25: Fronteers 20131205   the realtime web

example: auto-complete

On startup: download all users from database (id, name, phone number)

Same query for every one: served directly from database cache!

Not that fast: takes 2 seconds! ouch

Page 26: Fronteers 20131205   the realtime web

What, 2 seconds?!?!

Static content is served from cache and rendered immediately (100ms)

Your user has to move to the search box (1.5s)

Your user must start typing (300ms)

And type up to 3 letters (300ms) before we start querying

It doesn’t change your Time To Interact…

Page 27: Fronteers 20131205   the realtime web

Query on client-side

Searching in 180.000 user takes up 500 ms with a classic AJAX request to database

Search on client-side (<50ms) = instant!

10x faster —> User experience 1000x better!

Page 28: Fronteers 20131205   the realtime web

Where to store client-side?

MVC framework: Backbone, Angular, Ember, …

Store in models

Persist in LocalStorage to reduce startup time and across pages

Page 29: Fronteers 20131205   the realtime web

example: news ticker

Page 30: Fronteers 20131205   the realtime web

Data changes in realtime

Don’t kill your server polling for data

Use websockets or long-polling

Use a fast database with pubsub: redis, mongodb, …

Clients subscribe for updates

Page 31: Fronteers 20131205   the realtime web

fast database + smart database

Use 2 databases

Fast database: redis.io, mongodb, … store things you stream to client

Smart database: mysql, postgresql, … store everything (also in realtime database)

Write to both database

Read from fast database = cache for slow database

Page 32: Fronteers 20131205   the realtime web

Node.js = javascript

Page 33: Fronteers 20131205   the realtime web

node.js + socket.io + redis.io

Super easy to setup!

You don’t need to be a back-end engineer, it’s just javascript that you need to copy-paste

Front-end developers can deliver a fast front-end if they master this part of the app

All the cross-browser shit is already solved

Page 34: Fronteers 20131205   the realtime web

socket.io with node.js

!

“really, it’s just javascript,

dude”

Page 35: Fronteers 20131205   the realtime web

socket.io client-side

!

also just javascript :-)

Page 36: Fronteers 20131205   the realtime web

Conclusion

Not the websockets make your app fast

Avoiding AJAX calls to a slow database do!

Store all data client-side, do your queries client-side

Page 37: Fronteers 20131205   the realtime web

Node.js meetup

Page 38: Fronteers 20131205   the realtime web

Questions?

!Bert Wijnants end2end engineer @ Mobile Vikings

@bertwww www.newpage.be