24
Introducing in Ivar Conradi Østhus @ivarconr Trygve Lie @trygve_lie

Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Introducing in

Ivar Conradi Østhus@ivarconr

Trygve Lie@trygve_lie

Page 2: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Agenda

➔ What is Node.js?➔ Why Node.js?➔ How did we Introduce Node.js in FINN?➔ Standardizing Node.js

Page 3: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Where is Node.js used?

Page 4: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

What is Node.js?

● Server-Side JavaScript● Built on Google’s V8● Created by Ryan Dahl in 2009● First version 2011● Written i C, C++ and JavaScript

Page 5: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Non-blocking I/O

1. const result = db.query('select x from table_y');

2. doSomethingWithResult(result);

3. doSomethingWithOutResult();

1. const result = db.query('select x from table_y', (result) => {

2. doSomethingWithResult(result);

3. });

4. doSomethingWithOutResult();

Blocking I/O

Non-blocking I/O

Page 6: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

What do a webapp typically do?

service service service

It uses most of the time waits for data!

Webapp

Page 7: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Threads are expensive!

Threads have significant overhead○ Context switches ○ Memory footprint○ CPU cycles

Why waste resources on waiting?

Page 8: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Node.js event-driven architecture

The main event loop

● “Single threaded”● Non-blocking I/O

Handles thousands of concurrent connections with minimal overhead (CPU/Memory) on a single process

Page 9: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Threads vs. Event loop

https://blog.webfaction.com/2008/12/a-little-holiday-present-10000-reqssec-with-nginx-2/

Page 10: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Why Node.js?❏ Already use JavaScript in the browser❏ Mental switching❏ Simplicity❏ Modularity❏ Scaling Node.js

Page 11: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

We already use Node.js!

JavaScript in the browser Tools build upon the Node.js ecosystem

Page 12: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Fewer mental context switches

● Client-side and server-side in same language● Possible to reuse code● Learning Node.js is easy

○ Learning JavaScript is the hard part!

Page 13: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

!==

Browser Node.js

Page 14: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

● Fast start-up time, typically less than 1 second.● We don’t deal with threads in our code!● JSON (JavaScript Object Notation) support built in!● Great conventions

○ npm install○ npm run start○ npm run test

● Few abstractions, close to “web”!

Simplicity!

Page 15: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Setting up a web server in Node.js

1. const http = require('http');

2.

3. const server = http.createServer((req, res) => {

4. res.statusCode = 200;

5. res.setHeader('Content-Type', 'text/plain');

6. res.end('Hello World\n');

7. }).listen(3000);

Page 16: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Node.js has a modular ecosystem!

Core Modules

File Modules

Packages

Page 17: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Scaling Node.js!

1. Multiple cores 2. Multiple servers 3. Perfect for cloud!

Page 18: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

How did we introduce ?

Page 19: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Step 1: The Trojan Horse!

Started using Node.js to process frontend resources.

frontend-maven-plugin● Downloads & installs Node and NPM

locally● Correct Node & npm versions in all

build environments.

Page 20: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Step 2: FINN Technology Governance

● We also wanted to use it to build webapps● Define it as an experiment in “FINN technology governance” model

○ Use on a few new non-critical services (Unleash, FINN Hjørner, Bedriftsprofiler)○ Needed to reimplement tools (already implemented for java)

● Set-up internal npm repository

Page 21: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Step 3: Node Performance Rescue Squad

Page 22: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Step 4: Learn From the Best

Node Performance Workshop● How to write performant Node.js applications● How to debug Node.js in production?

○ Heap dumps○ Flame charts○ Remote debugging

● How to safely run Node.js applications in production

Page 23: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Step 5: Educate the Organisation

Page 24: Introducing in - Mediumtech.finn.no/images/2017-03-07-fagkveld-presentasjoner...2017/03/07  · How to write performant Node.js applications How to debug Node.js in production? Heap

Step 6: Standardize