isomorphic react applications: performance and scalability

115
Isomorphic React Applications: Performance & Scalability Denis Izmaylov Nov 23, 2015

Upload: denis-izmaylov

Post on 09-Jan-2017

2.928 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Isomorphic React Applications: Performance And Scalability

Isomorphic React Applications: Performance & Scalability

Denis Izmaylov

Nov 23, 2015

Page 2: Isomorphic React Applications: Performance And Scalability

Denis Izmaylov• 15 years of Software and Web

development experience

• The last 5 years focused exclusively on Front-end, Node.js and architecture

• Created 10+ projects, including Single Page Applications, high-load and React

• Commiter to Redux, webpack and koa

• Speaker at HighLoad++ 2015, MoscowJS

• The author of “Application and Universal Components” and other articles

, CEO

Page 3: Isomorphic React Applications: Performance And Scalability

Why should we stop developing classic

Single Page Applications?

Page 4: Isomorphic React Applications: Performance And Scalability

How isomorphic applications affect your salary?

Page 5: Isomorphic React Applications: Performance And Scalability

What will you do at this weekend?

Page 6: Isomorphic React Applications: Performance And Scalability

You already know1. React 14

2. webpack

3. ES6

4. Node.js

5. Express / koa

6. Isomorphic (Universal) apps6

Page 7: Isomorphic React Applications: Performance And Scalability

Part 1

Page 8: Isomorphic React Applications: Performance And Scalability

Web became a giant

Page 9: Isomorphic React Applications: Performance And Scalability

Art

Web Development

Science

Page 10: Isomorphic React Applications: Performance And Scalability

Before it was easy• Create a page

• Add some scripts

• Submit to Production

Page 11: Isomorphic React Applications: Performance And Scalability

Before it was easy

Server

Browser

11

Page 12: Isomorphic React Applications: Performance And Scalability

Before it was easy

Server

Browser

Does everything

12

Page 13: Isomorphic React Applications: Performance And Scalability

Before it was easy

Server

Browser- HTML - [CSS, JavaScript]

Does everything

13

Page 14: Isomorphic React Applications: Performance And Scalability

It worked

Page 15: Isomorphic React Applications: Performance And Scalability

Single PageApplications

(SPA)

Page 16: Isomorphic React Applications: Performance And Scalability

Single Page Application

Server

Browser

16

Page 17: Isomorphic React Applications: Performance And Scalability

Single Page Application

Server

Browser

Is the page exists?Do we need auth?Do we have access?

17

Page 18: Isomorphic React Applications: Performance And Scalability

Single Page Application

Server

Browser

Is the page exists?Do we need auth?Do we have access?

- Tiny HTML, [CSS] - JavaScript bundle

18

Page 19: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationBenefits

• Easy to start • webpack

• <div id=“root” />

• React, Redux

• build

19

Page 20: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationBenefits

• Easy to start

• Rich features

webpack, <div id=“root” />, React, Redux

20

Page 21: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationBenefits

• Easy to start

• Rich features

• Fast enhancement

webpack, <div id=“root” />, React, Redux

21

Page 22: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationBenefits

• Easy to start

• Rich features

• Fast enhancement

• Responsive UI

webpack, <div id=“root” />, React, Redux

22

Page 23: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationBenefits

• Easy to start

• Rich features

• Fast enhancement

• Responsive UI

• Useful caching

webpack, <div id=“root” />, React, Redux

23

Page 24: Isomorphic React Applications: Performance And Scalability

- Wow. Is it so ideal?

Page 25: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-time Start • JavaScript bundle up to 3-5 Mb

• first request

• execution

• memory

25

Page 26: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-time Start

• Expensive maintenance

• side effects

• memory leak

1st request, CPU, mem

26

Page 27: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-time Start

• Expensive maintenance

• Empty page, one URL

1st request, CPU, mem

side effects, memory leaks

27

Page 28: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-time Start

• Expensive maintenance

• Empty page, one URL

• Legacy Browsers

1st request, CPU, mem

side effects, memory leaks

28

Page 29: Isomorphic React Applications: Performance And Scalability

- Why is it “flaws”?

Page 30: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-Time Start

for business

reduced UX

30

Page 31: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-Time Start

• Expensive Maintenance

for business

reduced UX

risks

31

Page 32: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-Time Start

• Expensive Maintenance

• Empty Page

for business

reduced UX

risks

SEO problems

32

Page 33: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-Time Start

• Expensive Maintenance

• Empty Page

• One URL

for business

reduced UX

risks

SEO problems

SMM problems

33

Page 34: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-Time Start

• Expensive Maintenance

• Empty Page

• One URL

• Legacy Browsers

for business

reduced UX

risks

SEO problems

SMM problems

lost audience

34

Page 35: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

• Long-Time Start

• Expensive Maintenance

• Empty Page

• One URL

• Legacy Browsers

for business

reduced UX

risks

SEO problems

SMM problems

lost audience

35Expenses

Page 36: Isomorphic React Applications: Performance And Scalability

Single Page ApplicationFlaws

for business

reduced UX

risks

SEO problems

SMM problems

lost audience

36Expenses

Page 37: Isomorphic React Applications: Performance And Scalability

- What to do?

Page 38: Isomorphic React Applications: Performance And Scalability

Take the best from the both worlds

Page 39: Isomorphic React Applications: Performance And Scalability

Isomorphic Applications

Page 40: Isomorphic React Applications: Performance And Scalability

Isomorphic Applications

By isomorphic we mean that any given line of code (with notable exceptions) can execute both on the client and the server.

Charlie Robbins,18 Oct 2011

Page 41: Isomorphic React Applications: Performance And Scalability

Templates

Stylesheets

Locale (i18n)

Configuration

Routes

Access Rules

Models

Schemas

Validation

Services

Isomorphic Applications

server.jsNode.js

worker.js

client.jsBrowser

admin.js

Business Logic

Components

API interfaces

Actions, Reducers

Static Files

Page 42: Isomorphic React Applications: Performance And Scalability

Browser

Isomorphic Applications

Front-end Server

Back-end Server

Database Javaetc

Page 43: Isomorphic React Applications: Performance And Scalability

Browser

Isomorphic Applications

Front-end Server

Back-end Server

Database Javaetc

Page 44: Isomorphic React Applications: Performance And Scalability

Browser

Isomorphic Applications

Front-end Server

Back-end Server

Database Javaetc

- HTML - [critical CSS] - …

Page 45: Isomorphic React Applications: Performance And Scalability

Front-end Client

Isomorphic Applications

Front-end Server

Back-end Server

Database Javaetc

- HTML - [critical CSS] - JS Bundle

Page 46: Isomorphic React Applications: Performance And Scalability

Front-end Client

Isomorphic Applications

Front-end Server

Back-end Server

Database Javaetc

- HTML - [critical CSS] - JS Bundle

Page 47: Isomorphic React Applications: Performance And Scalability

Front-end Client

Isomorphic Applications

Front-end Server

• One execution environment

• Shared codebase

• Full control

• Ecosystem

47

Page 48: Isomorphic React Applications: Performance And Scalability

- How can we do that?

Page 49: Isomorphic React Applications: Performance And Scalability

Server-Side Rendering(SSR)

Page 50: Isomorphic React Applications: Performance And Scalability

Server-Side Rendering• Build HTML on Front-end Server

• Render page in user browser immediately, before it loads JavaScript

• When JavaScript will be loaded,React will add event handlers only

• It’s very fast

50

Page 51: Isomorphic React Applications: Performance And Scalability

Server-Side RenderingExample code for Server-Side:

import ReactDOMServer from 'react-dom/server';import Application from './components/application';const body = ReactDOMServer.renderToString( <Application />);

51

Page 52: Isomorphic React Applications: Performance And Scalability

Server-Side Rendering1. Visitors see a page immediately

2. No additional requests to load data

3. The page could work even without JS

4. Full URL-navigation

5. Meta-tags for SEO and SMM

6. This way keeps all JavaScript features

52

Page 53: Isomorphic React Applications: Performance And Scalability

Part 2

Page 54: Isomorphic React Applications: Performance And Scalability

Performance And Scalability

Page 55: Isomorphic React Applications: Performance And Scalability

Scalability

Page 56: Isomorphic React Applications: Performance And Scalability

Functional Scalability

Page 57: Isomorphic React Applications: Performance And Scalability

Server-Side RenderingThat’s super when we have all data for response:

import ReactDOMServer from 'react-dom/server';import Application from './components/application';const initialState = { siteName: 'Startup Makers' };const body = ReactDOMServer.renderToString( <Application state={initialState} />);

57

Page 58: Isomorphic React Applications: Performance And Scalability

Server-Side RenderingThat’s super when we have all data for response:

import ReactDOMServer from 'react-dom/server';import Application from './components/application';const initialState = { siteName: 'Startup Makers' };const body = ReactDOMServer.renderToString( <Application state={initialState} />);

What if we have to load it async?

58

Page 59: Isomorphic React Applications: Performance And Scalability

Server-Side RenderingHow to get asynchronous State:

1. Manual for each page

2. Facebook Relay

3. redux-catch-promise

59

Page 60: Isomorphic React Applications: Performance And Scalability

Asynchronous StateManual for each page:

• Define which data we have to load for each page

• Load this data and prepare State

• ReactDOMServer.renderToString()

60

Page 61: Isomorphic React Applications: Performance And Scalability

Asynchronous StateFacebook Relay:

1. The framework for building data-driven React applications

2. Declarative. Colocation. Mutations.

3. https://github.com/facebook/relay/issues/136

4. 1Q201661

Page 62: Isomorphic React Applications: Performance And Scalability

Asynchronous Stateredux-catch-promise:

• Redux - state container для React

• Redux: the best for isomorphic apps, MoscowJS 25https://youtu.be/Uyk_8WWna6s

• redux-catch-promise is middleware for Redux

62

Page 63: Isomorphic React Applications: Performance And Scalability

Asynchronous Stateredux-catch-promise:

1. Attach a callback to catch Promise-actions

2. Render the component

3. At the component - create a request to DB (or other data source) and dispatch a Promise of that

4. Collect all promises and wait until they will be finished

5. Render component with data 63

Page 64: Isomorphic React Applications: Performance And Scalability

Asynchronous Stateredux-catch-promise:

1. Example and source code:https://github.com/DenisIzmaylov/redux-catch-promise

2. Installation:

npm install redux-catch-promise

64

Page 65: Isomorphic React Applications: Performance And Scalability

Performance

Page 66: Isomorphic React Applications: Performance And Scalability

Performance

Test stand:

MacBook Pro 15” Retina (Early 2013)

2.4 GHz Intel Core i7

66

Page 67: Isomorphic React Applications: Performance And Scalability

PerformancePage size: 56 238 bytes

Page 68: Isomorphic React Applications: Performance And Scalability

PerformancePage size: 56 238 bytes

Page 69: Isomorphic React Applications: Performance And Scalability

PerformancePage size: 56 238 bytes

Page 70: Isomorphic React Applications: Performance And Scalability

PerformancePage size: 56 238 bytes

Page 71: Isomorphic React Applications: Performance And Scalability

PerformancePage size: 56 238 bytes

Page 72: Isomorphic React Applications: Performance And Scalability

PerformancePage size: 56 238 bytes

Page 73: Isomorphic React Applications: Performance And Scalability

Performance

Test with:

ab -n 100 http://localhost:3000/profile

73

Page 74: Isomorphic React Applications: Performance And Scalability

Performance

Test with:

ab -n 100 http://localhost:3000/profile

Executing…

74

Page 75: Isomorphic React Applications: Performance And Scalability

Performance

Test with:

ab -n 100 http://localhost:3000/profile

Executing…

Time per request: 61.850 ms

75

Page 76: Isomorphic React Applications: Performance And Scalability

Performance

61.850 msIs it slow or fast?

76

Page 77: Isomorphic React Applications: Performance And Scalability

Performance

61.850 msIs it slow or fast?

The same template in Handlebars:

8.385 ms

86% less

77

Page 78: Isomorphic React Applications: Performance And Scalability

Performance

61.850 msIs it slow or fast?

The same template in Handlebars:

8.385 ms

86% less

78

Page 79: Isomorphic React Applications: Performance And Scalability

Performance1. Try to search in Google - nothing

2. Try to ask in Twitter - silence:

79

Page 80: Isomorphic React Applications: Performance And Scalability

Performance

Ok, what if we do that?

NODE_ENV=production

Executing…

80

Page 81: Isomorphic React Applications: Performance And Scalability

Performance

Ok, what if we do that?

NODE_ENV=production

Executing…

Time per request: 37.943 ms(vs 61.850 ms)

39% less81

Page 82: Isomorphic React Applications: Performance And Scalability

Performance

Looks better.

But it’s still not funny.

82

Page 83: Isomorphic React Applications: Performance And Scalability

Go ahead

Page 84: Isomorphic React Applications: Performance And Scalability

GitHub issues

Page 85: Isomorphic React Applications: Performance And Scalability

Performance

• “Server rendering is slower with npm react”https://github.com/facebook/react/issues/812

85

Page 86: Isomorphic React Applications: Performance And Scalability

Performance

• “Server rendering is slower with npm react”https://github.com/facebook/react/issues/812Solution:use directly react/dist/react.min.js

86

Page 87: Isomorphic React Applications: Performance And Scalability

PerformanceCreate node_modules/react.js:if (process.env.NODE_ENV === 'production') {

module.exports = require('react/dist/react.min.js'); } else { module.exports = require('react/dist/react.js'); }

87

Page 88: Isomorphic React Applications: Performance And Scalability

PerformanceCreate node_modules/react.js:if (process.env.NODE_ENV === 'production') {

module.exports = require('react/dist/react.min.js'); } else { module.exports = require('react/dist/react.js'); }

88

Page 89: Isomorphic React Applications: Performance And Scalability

How can it influenceon results?

Page 90: Isomorphic React Applications: Performance And Scalability

Performance

Server rendering is slower with npm react

react/dist/react.min.js

Executing…

90

Page 91: Isomorphic React Applications: Performance And Scalability

Performance

Server rendering is slower with npm react

react/dist/react.min.js

Executing…

Time per request: 38.253 ms(vs 37.943 ms)0.08% more

91

Page 92: Isomorphic React Applications: Performance And Scalability

Performance

Server rendering is slower with npm react

react/dist/react.min.js

Executing…

Time per request: 38.253 ms(vs 37.943 ms)0.08% moreFAILED

92

Page 93: Isomorphic React Applications: Performance And Scalability

0

17,5

35

52,5

70

38,25337,943

8,385

61,85

React SSR Handlebars production react.min.js

Results

Page 94: Isomorphic React Applications: Performance And Scalability

0

17,5

35

52,5

70

38,25337,943

8,385

61,85

React SSR Handlebars production react.min.js

ResultsNODE_ENV=production

39% less

Page 95: Isomorphic React Applications: Performance And Scalability

Part 3

Page 96: Isomorphic React Applications: Performance And Scalability

Advanced Solutions

Page 97: Isomorphic React Applications: Performance And Scalability

Advanced Solutions

1. Precompilation + Cache 2. Rendering Separation 3. Progressive Rendering 4. Facebook BigPipe 5. HAProxy

97

Page 98: Isomorphic React Applications: Performance And Scalability

Precompilation + Cache

• UI = f(state)

• f = React Component

• state = path + [actions] + …

1. Simple solution: redis

2. Deferred server-side rendering:redis + kue.js + workers 98

Page 99: Isomorphic React Applications: Performance And Scalability

Rendering Separation

99

Page 100: Isomorphic React Applications: Performance And Scalability

Progressive Rendering

100

Page 101: Isomorphic React Applications: Performance And Scalability

Progressive Rendering

• React DOM Stream

• Flushing the Document Early

• “Streams make this library as much as 47% faster in sending down a full page than ReactDOM.renderToString”

• Target - 108KB page on Heroku

• Time To First Byte (TTFB) - 55% faster

• https://github.com/aickin/react-dom-stream101

Page 102: Isomorphic React Applications: Performance And Scalability

Facebook BigPipe• Bundle a page during it’s loading • Assets is loading parallel • Resistants to errors

Page 103: Isomorphic React Applications: Performance And Scalability

Facebook BigPipe• Bundle a page during it’s loading • Assets is loading parallel • Resistants to errors

Page 104: Isomorphic React Applications: Performance And Scalability

Facebook BigPipe• Bundle a page during it’s loading • Assets is loading parallel • Resistants to errors

Page 105: Isomorphic React Applications: Performance And Scalability

Facebook BigPipe

105

Page 106: Isomorphic React Applications: Performance And Scalability

HAProxy• Multiple Node.js instance

• Ask your DevOps engineer

106

Page 107: Isomorphic React Applications: Performance And Scalability

Epilogue

Page 108: Isomorphic React Applications: Performance And Scalability

Recommendations• Find and join local JavaScript

communities

• Improve your English skills

• Read original articles and technical blogs (Facebook, AirBnB, Netflix, etc)

• Join and integrate Twitter and GitHub to your life

108

Page 109: Isomorphic React Applications: Performance And Scalability

Useful Links1. Supercharging page load (100 Days of Google Dev)

https://youtu.be/d5_6yHixpsQ 2. Making Netflix.com Faster

http://techblog.netflix.com/2015/08/making-netflixcom-faster.html

3. New technologies for the new LinkedIn home pagehttps://engineering.linkedin.com/frontend/new-technologies-new-linkedin-home-page

4. Improving performance on Twitter.comhttps://blog.twitter.com/2012/improving-performance-on-twittercom

5. Scaling Isomorphic Javascript Codehttp://blog.nodejitsu.com/scaling-isomorphic-javascript-code/

109

Page 110: Isomorphic React Applications: Performance And Scalability

Useful Links6. From AngularJS to React: The Isomorphic Way

https://blog.risingstack.com/from-angularjs-to-react-the-isomorphic-way/

7. Isomorphic JavaScript: The Future of Web Appshttp://nerds.airbnb.com/isomorphic-javascript-future-web-apps/

8. React server side rendering performancehttp://www.slideshare.net/nickdreckshage/react-meetup

9. The Lost Art of Progressive HTML Renderinghttp://blog.codinghorror.com/the-lost-art-of-progressive-html-rendering/

10. Extract and inline Critical Path CSS in HTML pages https://github.com/addyosmani/critical

110

Page 111: Isomorphic React Applications: Performance And Scalability

Quotes“Almost algorithm problems

could be solved by changing a data structure”

“Changes is our work”,Jake Archibald, Google

Page 112: Isomorphic React Applications: Performance And Scalability

Why should we stop developing classic

Single Page Applications?

Page 113: Isomorphic React Applications: Performance And Scalability

[email protected]

Submit short information about you

Page 114: Isomorphic React Applications: Performance And Scalability

Thank you

Denis Izmaylov

@DenisIzmaylov

https://github.com/DenisIzmaylov

http://startup-makers.com

denis_izmaylov

[email protected]

Page 115: Isomorphic React Applications: Performance And Scalability

Secret Slide