server execution of javascript: what could possibly go...

45
©2014 LinkedIn Corporation. All Rights Reserved. Server execution of JavaScript: What could possibly go wrong? Brian Geffon Staff Software Engineer

Upload: others

Post on 17-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Server execution of JavaScript: What could possibly go wrong? Brian Geffon Staff Software Engineer

Page 2: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Hello!

2

Page 3: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Introductions Ø Brief History §  The paradigm shift §  Problems! §  Where we are today §  Closing thoughts and Questions

Outline

3

Page 4: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

LinkedIn in 2003

4

JAVA JSP

HTML

Data Center

Browser

A single monolithic web application

Page 5: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

LinkedIn in 2010

5

JAVA JSP

HTML

Data Center

Browser

New frameworks: productivity boost.

Ruby ERB

Grails GSP

Page 6: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Difficult to maintain numerous versions of the same template

§  Make it difficult to share content between apps

New Frameworks: Added productivity Added complexity

6

Page 7: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Do these web app frameworks share anything?

§  How can we ensure that we remain D.R.Y.

§  What language can be supported across each architecture?

Solution: a single templating language

7

Page 8: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Web applications return JSON data

§  Templates are compiled to JavaScript

§  JSON Data is consumed by JavaScript templates which will execute on the client side.

Solution: client side templating

8

Page 9: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Webapps can share UI!

§  Ability to cache templates on the client –  Better performance?

Solution: client side templating, contd.

9

Page 10: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

So many options!

10

Page 11: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Dust is a logicless JavaScript templating language

§  Dust is extensible

§  Dust is inherently D.R.Y.

https://github.com/linkedin/dustjs

The winner: Dust.js

11

Page 12: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Dust.js example

+ =

gets compiled into a JavaScript function

Page 13: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Introductions §  Brief History Ø The paradigm shift §  Problems! §  Where we are today §  Closing thoughts and Questions

Outline

13

Page 14: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Reusable UI gives rise to component sharing across apps

§  Components are now separated from data models

§  Ability to avoid RTT for components embedded in page.

The paradigm shift

14

Page 15: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

The paradigm shift: Fizzy

15

Page 16: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

What’s going on here?

16

Web fz.js

Fizzy Server Profile

Profile Contacts

1 2

3 4

Page 17: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

What’s does the application return?

17

Web fz.js

Fizzy Server Profile

Profile Contacts

1 2

3 4

HTTP/1.1 200 OKContent-Type: text/htmlX-FS-Page-Parse: 1X-FS-Page-Id: profile-view-fsX-FS-Host-Id: ela4-appxxxx.prod

Page 18: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

What do these embedded components return?

Web fz.js

Fizzy Server Profile

Profile Contacts

1 2

3 4

HTTP/1.1 200 OKContent-Type: application/jsonX-FS-Page-Id: profile-activityX-FS-Host-Id: ela4-appxxxx.prodX-FS-TL: http://cdn-host/hash-of-template1.jsX-FS-Template-Keys: __default__=hash-of-template1

Page 19: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

What does the browser see?

Page 20: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Components are now stand alone

§  Nice UI separation

§  Reusability

Yay! A fancy new web architecture

20

Page 21: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Large JSON payloads caused many problems with IE7 –  IE7 doesn’t have a native JSON parser!

What could possibly go wrong?

21

Page 22: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Some older browsers would take a very long time

executing JS –  Many browsers didn’t have optimized JS engines

What could possibly go wrong?

22

Page 23: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Search Engine Optimization –  JS in GoogleBot Yes, many others: No

What could possibly go wrong?

23

Page 24: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Server Side Rendering (SSR)

24

§  Unfortunately we need a way to execute JavaScript on the server

§  Could potential performance improvements been seen across the board?

Page 25: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

The Pieces of SSR

25

§  High performance caching HTTP proxy

§  High performance embeddable JavaScript Engine

Google V8 JS Engine

Page 26: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Server Side Rendering: What’s going on here?

26

Web fz.js

Fizzy Server Profile

Profile Contacts

1 2

3 4

Web fz.js

Fizzy Server Profile

Profile Contacts

1 2

3 4

SSR

Page 27: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

What’s does the application return?

27

Web fz.js

Fizzy Server

Profile Contacts

1 2

3 4

Web fz.js

Fizzy Server Profile

Profile Contacts

1 2

3 4

SSR HTTP/1.1 200 OKContent-Type: text/htmlX-FS-Page-Parse: 1X-FS-Page-Id: profile-view-fsX-FS-Host-Id: ela4-appxxxx.prod

Page 28: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

What does the browser see?

Page 29: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  We can now support old web browsers

§  We can now gracefully handle SEO

§  It turns out that even for modern browsers sometimes we can execute JavaScript faster!

Yay! A fancy new web architecture

29

Page 30: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Introductions §  Brief History §  The paradigm shift Ø Problems §  Where we are today §  Closing thoughts and Questions

Outline

30

Page 31: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  A shared JS engine gives rise to issues and vulnerabilities that don’t affect browsers that execute JS.

What could possibly go wrong?

31

Page 32: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Context Pollution –  One malicious request can poison the context of another –  This issue exists with any dynamic language

What could possibly go wrong?

32

Page 33: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Context Pollution

33

Silly example but illustrates the need for isolation. What if we leave off var in JavaScript?

Page 34: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Each request requires it’s own context –  Completely reload the environment and bootstrap code

§  Performance Hits?

Context Pollution: The solution

34

Page 35: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Poorly written JavaScript can take forever to execute!

What could possibly go wrong?

35

Page 36: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Poorly Written JavaScript: Infinite Loops, Recursion, etc.

36

Although this is tail recursion and a silly example, It illustrates the need for stack protection and time limitations.

Page 37: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Enforce stack size limits that allow you to gracefully kill a VM

§  Sandbox: accept that apps will misbehave and allow them to only hurt themselves.

Long Running JavaScript: The solution

37

Page 38: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Execution limits (we use 1000ms)

§  Exponentially decay the execution limit to prevent taking down the entire site!

38

Long Running JavaScript: The solution

Page 39: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Garbage Collection!

What could possibly go wrong?

39

Page 40: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

Garbage Collection!

40

Queue times going through the roof!

Page 41: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

The culprit: Garbage Collection!

41

Page 42: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

GC tuning: it takes practice.

42

Avg Queue times < 0.3ms, P99.99 < 2ms.

Page 43: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  Adjust old generation to be several order of magnitudes less than new generation

§  New generation is critical because of the short lived jobs and contexts.

§  More Threads!

GC Tuning

43

Page 44: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

©2014 LinkedIn Corporation. All Rights Reserved.

§  User load times are actually improved with SSR: do it 100% of the time.

§  A generic JS engine: allow apps to return any JavaScript, not just Dust.js

Ideas for the future

44

Page 45: Server execution of JavaScript: What could possibly go wrong?velocity.oreilly.com.cn/2014/ppts/ssr_velocity_beijing.pdf · 2017. 12. 18. · ©2014 LinkedIn Corporation. All Rights

Questions?