45
JS Execution on the Server, What Could Go Wrong? Brian Geffon Software Engineer, LinkedIn

Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

  • Upload
    ontico

  • View
    397

  • Download
    0

Embed Size (px)

DESCRIPTION

Доклад Брайана Геффона на HighLoad++ 2014.

Citation preview

Page 1: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

JS Execution on the Server, What Could Go Wrong?

Brian GeffonSoftware Engineer, LinkedIn

Page 2: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Hello!

Page 3: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Outline

Page 4: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

LinkedIn in 2003

JAVAJSP

HTML

Data Center

Browser

A single monolithic web application

Page 5: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

LinkedIn in 2010

JAVAJSP

HTML

Data Center

Browser

New frameworks: productivity boost.

RubyERB

GrailsGSP

Page 6: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Difficult to maintain numerous versions of the same template

Make it difficult to share content between apps

New Frameworks: Added productivityAdded complexity

Page 7: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 8: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 9: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Webapps can share UI!

Ability to cache templates on the client– Better performance?

Solution: client side templating, contd.

Page 10: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

So many options!

Page 11: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 12: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Dust.js example

+ =

gets compiled into a JavaScript function

Page 13: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Outline

Page 14: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 15: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

The paradigm shift: Fizzy

Page 16: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

What’s going on here?

Webfz.js

Fizzy Server Profile

ProfileContacts

1 2

34

Page 17: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

What’s does the application return?

Webfz.js

Fizzy Server Profile

ProfileContacts

1 2

34

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: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

What do these embedded components return?

Webfz.js

Fizzy Server Profile

ProfileContacts

1 2

34

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: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

What does the browser see?

Page 20: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Components are now stand alone

Nice UI separation

Reusability

Yay! A fancy new web architecture

Page 21: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

What could possibly go wrong?

Page 22: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Some older browsers would take a very long time executing JS– Many browsers didn’t have optimized JS engines

What could possibly go wrong?

Page 23: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

What could possibly go wrong?

Page 24: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Server Side Rendering (SSR)

Unfortunately we need a way to execute JavaScript on the server

Could potential performance improvements been seen across the board?

Page 25: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

High performance caching HTTP proxy

High performance embeddable JavaScript Engine

The Pieces of SSR

Google V8 JS Engine

Page 26: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Server Side Rendering: What’s going on here?

Webfz.js

Fizzy Server Profile

ProfileContacts

1 2

34

Webfz.js

Fizzy Server Profile

ProfileContacts

1 2

34

SSR

Page 27: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

What’s does the application return?

Webfz.js

Fizzy Server

ProfileContacts

1 2

34

Webfz.js

Fizzy Server Profile

ProfileContacts

1 2

34

SSRHTTP/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: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

What does the browser see?

Page 29: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 30: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Outline

Page 31: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

What could possibly go wrong?

Page 32: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Context Pollution– One malicious request can poison the context of

another– This issue exists with any dynamic language

What could possibly go wrong?

Page 33: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Context Pollution

Silly example but illustrates the need for isolation.

What if we leave off var in JavaScript?

Page 34: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

bootstrap code

Performance Hits?

Context Pollution: The solution

Page 35: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Poorly written JavaScript can take forever to execute!

What could possibly go wrong?

Page 36: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Poorly Written JavaScript: Infinite Loops, Recursion, etc.

Although this is tail recursion and a silly example,

It illustrates the need for stack protection and

time limitations.

Page 37: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 38: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Execution limits (we use 1000ms)

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

Long Running JavaScript: The solution

Page 39: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Garbage Collection!

What could possibly go wrong?

Page 40: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Garbage Collection!

Queue times going through the roof!

Page 41: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

The culprit: Garbage Collection!

Page 42: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

GC tuning: it takes practice.

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

Page 43: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 44: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

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

Page 45: Исполнение JS на сервере при масштабировании - что может пойти не так, Brian Geffon (LinkedIn)

Questions?