Top Banner
Vert.x: This ain’t your Dad’s Node! Matt Stine Enterprise Java/Cloud Consultant [email protected] http://mattstine.com Twitter: @mstine Tim Fox Father of Vert.x
74

Vert.x

Jan 28, 2015

Download

Technology

Matt Stine

Vert.x: This ain't your Dad's Node! (As presented at http://sfjava.org on 10/9/2012)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Vert.x

Vert.x:This ain’t your Dad’s Node!

Matt StineEnterprise Java/Cloud Consultant

[email protected]://mattstine.com

Twitter: @mstine

Tim FoxFather of Vert.x

Page 2: Vert.x

C10KThe

Problem

Page 3: Vert.x
Page 4: Vert.x

SERVERPUSH

Page 5: Vert.x

How?!?!Ryan DahlFather of Node.js

Page 6: Vert.x

2008:

Thread Per Request/Response

Page 7: Vert.x

http://www.xtranormal.com/watch/6995033/mongo-db-is-web-scale

Page 8: Vert.x

Non-blocking UNIX sockets

Page 9: Vert.x
Page 10: Vert.x
Page 11: Vert.x
Page 12: Vert.x

But Nodehas some shortcomings...

1. JavaScript!

2. Vertical Scaling

3. InterprocessCommunication

4. Event Loop

Page 13: Vert.x

http://vertx.io

Page 14: Vert.x

Brendan EichFather of JavaScript

JavaScriptProblem #1:

Page 15: Vert.x

WAThttps://www.destroyallsoftware.com/talks/wat

Page 16: Vert.x

Neal FordFather of Polyglot Programming

http://memeagora.blogspot.com/2006/12/polyglot-programming.html

PolyglotProgramming

Solution #1:

Page 17: Vert.x

Hello Worldin every Vert.x language!

Page 18: Vert.x

Java

Page 19: Vert.x
Page 20: Vert.x

Ruby

Page 21: Vert.x

Python

Page 22: Vert.x

JavaScript

Page 23: Vert.x
Page 24: Vert.x

Theoretically, any JVM language or language that compiles to a supported language (e.g. ClojureScript ➔ JavaScript)

FUTURE

Page 25: Vert.x

Lars BakFather of V8

VerticalScaling

Problem #2:

Page 26: Vert.x

http://nodejs.org/about/

Page 27: Vert.x

Node: Up and RunningExample 3-11: Using cluster to distribute work

Page 28: Vert.x

James GoslingFather of Java

TheJVM

Solution #2:

Page 29: Vert.x

Verticle

The Verticle

- Unit of Deployment

- Script/Java Class

Page 30: Vert.x

Vert.x InstanceHow it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle

Event Loops

Runtime.availableProcessors() == 4

Page 31: Vert.x

Vert.x InstanceEvent Loops

Runtime.availableProcessors() == 4

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

vertx run HelloWorld

-instances 4

Page 32: Vert.x

Concurrency

• Verticle instance assigned thread/event loop.

• Verticle instance ALWAYS executes on assigned thread.

• Verticles have isolated classloaders and cannot share global state (static members, global variables, etc.)

• Can write all code assuming single threading.

Page 33: Vert.x

Tony HoareFather of CSP (http://dl.acm.org/citation.cfm?doid=359576.359585)

InterprocessCommunication

Problem #3:

Page 34: Vert.x

Node.js Communication Options

• TCP/UDP/UNIX Sockets

• Redis Pub/Sub (http://redis.io/)

• ZeroMQ (http://www.zeromq.org/)

• Process Signaling/Cluster Module

• Eventing Frameworks: Hook.io (dead), JS-Signals, Bean, etc.

• Memcached

• Etc...

Page 35: Vert.x

Alan KayFather of Smalltalk

TheEventBus

Solution #3.1:

Page 36: Vert.x

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

Event Bus

Page 37: Vert.x

Addressing

• Simply a String

• Dot-style namespacing recommended

• e.g. “messages.inbound.A”

Page 38: Vert.x

Handler Registration

messages.inbound.A

Handler X

Handler Y

Handler Z

Page 39: Vert.x

Handler Registration

Page 40: Vert.x

Pub/Sub

messages.inbound.A

Handler X

Handler Y

Handler Z

Sender

Page 41: Vert.x

Pub/Sub

Page 42: Vert.x

P2P

messages.inbound.A

Handler X

Handler Y

Handler Z

Sender

Page 43: Vert.x

P2P

Page 44: Vert.x

Sender:

Receiver:

Page 45: Vert.x

Message Types

• String

• Primitives (int, long, short, float, double, ...)

• Boxed Primitives

• boolean/Boolean

• org.vertx.java.core.json.JsonObject

• org.vertx.java.core.buffer.Buffer

Page 46: Vert.x

Distributed Vert.xHow it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

Event Bus

Page 47: Vert.x

SockJS Bridge

Into the Browser!How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

Event Bus

Page 48: Vert.x

The Server

Page 49: Vert.x

The Client

Page 50: Vert.x

Demo

Page 51: Vert.x

Rich HickeyFather of Clojure

SharedImmutable

State

Solution #3.2:

Page 52: Vert.x

e.g. In-memory Web Cache

Page 53: Vert.x

Message Passing?FAIL

Page 54: Vert.x

Shared state only dangerous if it is

MUTABLE!

Page 55: Vert.x

Vert.x Shared State• SharedData Object (vertx.sharedData())

• collection of java.util.concurrent.ConcurrentMap<K,V>

• collection of java.util.Set<E> (backed by ConcurrentMap)

• Elements MUST be immutable values (well, sort of...)

• Currently only available within a Vertx. instance, not across a cluster.

Page 56: Vert.x

Allowed Values

• Strings

• Boxed Primitives

•byte[]

•org.vertx.java.core.buffer.Buffer

• Implementors of org.vertx.java.core.shareddata.Shareable

DANGER!

Page 57: Vert.x

Shared Map

Page 58: Vert.x

Shared Set

Page 59: Vert.x

Douglas SchmidtFather of the Reactor Pattern

TheEventLoop

Problem #4:

Page 60: Vert.x

Reactor Pattern ReviewHow it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Event Loop

Applicationregistershandlers...

...eventstrigger

handlers.

Page 61: Vert.x

Reactor Pattern Review

• Single thread / single event loop

• EVERYTHING runs on it

• You MUST NOT block the event loop

Page 62: Vert.x

Reactor Pattern Problems

• Some work is naturally blocking:

• Intensive data crunching

• 3rd-party blocking API’s (e.g. JDBC)

• Pure reactor (e.g. Node.js) is not a good fit for this kind of work!

Page 63: Vert.x

Carl HewittFather of the Actor Model

WorkerVerticles

Solution #4:

Page 64: Vert.x

Worker Verticles• Not assigned a Vert.x event loop thread

• Executes on background thread pool

• Never executed concurrently by more than one thread

• Not allowed to use TCP or HTTP clients/servers

• Communicate using the event bus

• Should be kept to a minimum

Page 65: Vert.x

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

How it works

• Handlers executed synchronously– on a single thread

• Use handlers to pass messages• Inter/intra application comms

– EventBus with messages– Safe shared data structures

9

Event loop

App registers handlers

Events trigger handlers

Verticle Verticle Verticle Verticle

Event Bus

WorkerVerticle

WorkerVerticle

BGPool

BGPool

The “Multi-reactor”

Page 66: Vert.x
Page 67: Vert.x
Page 68: Vert.x

Problem/Solution Summary

• Node.js compels the use of JavaScript

• Vert.x is Polyglot

• Node.js is inherently single-threaded

• Vert.x leverages the multi-threaded JVM

Page 69: Vert.x

Problem/Solution Summary• Node.js doesn’t help much w/ interprocess

communication

• Vert.x features a distributed event bus which reaches all the way into the browser

• Node.js requires all code to run on the event loop

• Vert.x features background workers that allow blocking work to be done off of the event loops

Page 70: Vert.x

Other Goodies• Growing Module

Repository

• web server

• persistors (Mongo, JDBC, ...)

• work queue

• authentication manager

• session manager

• Socket.IO

• TCP/SSL servers/clients

• HTTP/HTTPS servers/clients

• WebSockets support

• SockJS support

• Timers

• Buffers

• Streams and Pumps

• Routing

• Asynchronous File I/O

Page 71: Vert.x

Case Study

https://github.com/mstine/twitx

Page 72: Vert.x

Get Involved!• Google Group: http://groups.google.com/

group/vertx

• GitHub: https://github.com/vert-x/vert.x

• IRC: irc://freenode.net/vertx

• Needs:

• language implementations

• modules (esp. persistence, security, ...)

• examples/blogs/documentation help

Page 73: Vert.x

Vert.x:This ain’t your Dad’s Node!

Matt StineEnterprise Java/Cloud Consultant

[email protected]://mattstine.com

Twitter: @mstine

Please fill out an evaluation:

http://speakerrate.com/talks/16821

Page 74: Vert.x

Image Credits• Tim Fox: https://twitter.com/timfox

• BSOD Phone: http://www.flickr.com/photos/markhillary/3413357033

• V8: http://www.flickr.com/photos/gorbould/3479298062

• Ryan Dahl: http://www.flickr.com/photos/franksvalli/5163205409

• Shortcomings: http://www.flickr.com/photos/hendricksphotos/3340896056

• Brendan Eich: http://www.flickr.com/photos/equanimity/4055148344

• Neal Ford: http://nealford.com/images/Neal-2012-headshot.jpg

• Lars Bak: http://www.flickr.com/photos/niallkennedy/2822229099

• James Gosling: http://www.flickr.com/photos/skrb/2499736195

• Tony Hoare: http://www.flickr.com/photos/adewale_oshineye/3687557065

• Alan Kay: http://www.flickr.com/photos/mwichary/3010026816

• Rich Hickey: http://www.flickr.com/photos/hlship/3603090614

• Douglas Schmidt: http://www.cs.wustl.edu/~schmidt/gifs/SchmidtD.jpg

• Carl Hewitt: http://www.flickr.com/photos/jeanbaptisteparis/3098594954