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




                                           Matt Stine
   Tim Fox
                   Enterprise Java/Cloud Consultant
Father of Vert.x
                               matt.stine@gmail.com
                                http://mattstine.com
                                     Twitter: @mstine
The

C10K
 Problem
SERVER
 PUSH
Ryan Dahl
Father of Node.js




  How?!?!
2008:

Thread Per Request/Response
http://www.xtranormal.com/watch/6995033/mongo-db-is-web-scale
Non-blocking UNIX sockets
But Node
has some
shortcomings...

 1. JavaScript!

 2. Vertical Scaling

 3. Interprocess
 Communication

 4. Event Loop
http://vertx.io
Problem #1:

                       JavaScript


   Brendan Eich
Father of JavaScript
WAT
https://www.destroyallsoftware.com/talks/wat
Solution #1:

                                   Polyglot
                                 Programming
           Neal Ford
Father of Polyglot Programming


    http://memeagora.blogspot.com/2006/12/polyglot-programming.html
Hello World
in every Vert.x language!
Java
Ruby
Python
JavaScript
FUTURE



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

    Vertical
    Scaling

Lars Bak
Father of V8
http://nodejs.org/about/
Node: Up and Running
Example 3-11: Using cluster to distribute work
Solution #2:

                 The
                 JVM
James Gosling
Father of Java
The Verticle


               - Unit of Deployment
Verticle

               - Script/Java Class
Vert.x Instance
  How it works it works it works it works
          How      How      How

                             Event loop Event loop Event loop Event loop
Event Loops
    App registers App registers App registers App registers trigger Events trigger Events trigger Events trigg
                                                     Events
      handlers      handlers      handlers      handlershandlers      handlers       handlers       handlers



  • Handlers executed synchronously executed synchronously
              • Handlers executed synchronously executed synchronously
                          • Handlers • Handlers
    – on a single threada single threada single threada single thread
                    – on           – on           – on
  • Use handlersUse handlersUse handlersUse handlers to pass messages
                 • to pass messages messages messages
                                • to pass • to pass
  • Inter/intra application comms
                 • Inter/intra application comms
                                • Inter/intra application comms
                                               • Inter/intra application comms
    – EventBus with EventBus with EventBus with EventBus with messages
                  – messages – messages – messages
    – Safe shared data structuresdata structuresdata structuresdata structures
                  – Safe shared – Safe shared – Safe shared
                             Verticle                                   9         9             9




Runtime.availableProcessors() == 4
Vert.x Instance
              How it works it works it works it works
                      How      How      How

                                         Event loop Event loop Event loop Event loop
         Event Loops
                App registers App registers App registers App registers trigger Events trigger Events trigger Events trigg
                                                                 Events
                  handlers      handlers      handlers      handlershandlers      handlers       handlers       handlers



              • Handlers executed synchronously executed synchronously
                          • Handlers executed synchronously executed synchronously
                                      • Handlers • Handlers
                – on a single threada single threada single threada single thread
                                – on           – on           – on
              • Use handlersUse handlersUse handlersUse handlers to pass messages
                             • to pass messages messages messages
                                            • to pass • to pass
 vertx run    • Inter/intra application comms
                             • Inter/intra application comms
                                            • Inter/intra application comms
                                                           • Inter/intra application comms
 HelloWorld with EventBus with EventBus with EventBus with messages
       – EventBus – messages – messages – messages
       – Safe shared data structuresdata structuresdata structuresdata structures
                     – Safe shared – Safe shared – Safe shared
-instances 4                    Verticle Verticle Verticle Verticle                 9         9             9




       Runtime.availableProcessors() == 4
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.
Tony Hoare
        Father of CSP
        (http://dl.acm.org/citation.cfm?
        doid=359576.359585)




              Problem #3:

  Interprocess
Communication
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...
Solution #3.1:

                       The
                      Event
                       Bus
    Alan Kay
Father of Smalltalk
How it works it works it works it works
        How      How      How

                           Event loop Event loop Event loop Event loop

  App registers App registers App registers App registers trigger Events trigger Events trigger Events trigger
                                                   Events
    handlers      handlers      handlers      handlershandlers      handlers       handlers       handlers



• Handlers executed synchronously executed synchronously
            • Handlers executed synchronously executed synchronously
                        • Handlers • Handlers
  – on a single threada single threada single threada single thread
                  – on           – on           – on
• Use handlersUse handlersUse handlersUse handlers to pass messages
               • to pass messages messages messages
                              • to pass • to pass
• Inter/intra application comms
               • Inter/intra application comms
                              • Inter/intra application comms
                                             • Inter/intra application comms
  – EventBus with EventBus with EventBus with EventBus with messages
                – messages – messages – messages
  – Safe shared data structuresdata structuresdata structuresdata structures
                – Safe shared – Safe shared – Safe shared
                            Verticle       Verticle Verticle Verticle 9         9             9              9




                                           Event Bus
Addressing


• Simply a String
• Dot-style namespacing recommended
• e.g. “messages.inbound.A”
Handler Registration
                     Handler X




messages.inbound.A   Handler Y




                     Handler Z
Handler Registration
Pub/Sub
                              Handler X




Sender   messages.inbound.A   Handler Y




                              Handler Z
Pub/Sub
P2P
                              Handler X




Sender   messages.inbound.A   Handler Y




                              Handler Z
P2P
Sender:




Receiver:
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
Distributed Vert.x
 orks it works it works it works
 How       How     How                                             How it works it works it works it works
                                                                           How       How     How                                                 How it works it works it works it works
                                                                                                                                                          How     How       How

               Event loop Event loop Event loop Event loop                                   Event loop Event loop Event loop Event loop                                   Event loop Event loop Event loop Event loop

rs App registers App registers App registers trigger Events trigger Events trigger Events trigger registers App registers trigger Events trigger Events trigger Events trigger registers App registers trigger Events trigger Events trigger
                                       Events                    App registers App registers App                    Events                   App registers App registers App                    Events
     handlers      handlers      handlershandlers      handlers handlers
                                                                      handlers handlers
                                                                                     handlers handlers        handlershandlers      handlers handlers
                                                                                                                                                   handlers handlers
                                                                                                                                                                  handlers handlers        handlershandlers      handlers       handlers



executed synchronously executed synchronously
 • Handlers executed synchronously executed synchronously
             • Handlers • Handlers                  • Handlers executed synchronously executed synchronously
                                                                • Handlers executed synchronously executed synchronously
                                                                            • Handlers • Handlers                  • Handlers executed synchronously executed synchronously
                                                                                                                               • Handlers executed synchronously executed synchronously
                                                                                                                                           • Handlers • Handlers
le threada single threada single threada single thread
     – on           – on           – on                             – on a single threada single threada single threada single thread
                                                                                    – on           – on           – on                            – on a single threada single threada single threada single thread
                                                                                                                                                                  – on           – on           – on
ersUsepass messages messages messages messages handlersUsepass messages messages messages messages handlersUse handlersUsepass messages messages messages
 • to handlersUse handlersUsepass
                • to pass • to handlers to pass               • Use          • to handlersUse handlersUsepass
                                                                                            • to pass • to handlers to pass              • Use          • to pass messages
                                                                                                                                                                       • to handlersUse handlers to pass
                                                                                                                                                                                      • to pass
application comms
 • Inter/intra application comms
                • Inter/intra application comms
                               • Inter/intra application comms• Inter/intra application comms
                                                                             • Inter/intra application comms
                                                                                            • Inter/intra application comms
                                                                                                           • Inter/intra application comms Inter/intra application comms
                                                                                                                                         •              • Inter/intra application comms
                                                                                                                                                                       • Inter/intra application comms
                                                                                                                                                                                      • Inter/intra application comms
s with EventBus with EventBus with EventBus with messages
     – messages – messages – messages                               – EventBus with EventBus with EventBus with EventBus with messages
                                                                                  – messages – messages – messages                                – EventBus with EventBus with EventBus with EventBus with messages
                                                                                                                                                                – messages – messages – messages
red data structuresdata structuresdata structuresdata structures
     – Safe shared – Safe shared – Safe shared                      – Safe shared data structuresdata structuresdata structuresdata structures
                                                                                  – Safe shared – Safe shared – Safe shared                       – Safe shared data structuresdata structuresdata structuresdata structures
                                                                                                                                                                – Safe shared – Safe shared – Safe shared
                Verticle       Verticle      Verticle    9
                                                             Verticle9              9
                                                                                              Verticle
                                                                                                   9
                                                                                                             Verticle       Verticle    9
                                                                                                                                            Verticle
                                                                                                                                                   9              9
                                                                                                                                                                            Verticle
                                                                                                                                                                                 9
                                                                                                                                                                                           Verticle       Verticle    9
                                                                                                                                                                                                                          Verticle
                                                                                                                                                                                                                                 9         9




                                                                                                       Event Bus
Into the Browser!
 orks it works it works it works
 How       How     How                                             How it works it works it works it works
                                                                           How       How     How

               Event loop Event loop Event loop Event loop                                   Event loop Event loop Event loop Event loop

rs App registers App registers App registers trigger Events trigger Events trigger Events trigger registers App registers trigger Events trigger Events trigger Events trigger
                                       Events                    App registers App registers App                    Events
     handlers      handlers      handlershandlers      handlers handlers
                                                                      handlers handlers
                                                                                     handlers handlers        handlershandlers      handlers       handlers       handlers



executed synchronously executed synchronously
 • Handlers executed synchronously executed synchronously
             • Handlers • Handlers                  • Handlers executed synchronously executed synchronously
                                                                • Handlers executed synchronously executed synchronously
                                                                            • Handlers • Handlers
le threada single threada single threada single thread
     – on           – on           – on                             – on a single threada single threada single threada single thread
                                                                                    – on           – on           – on
ersUsepass messages messages messages messages handlersUsepass messages messages messages messages
 • to handlersUse handlersUsepass
                • to pass • to handlers to pass               • Use          • to handlersUse handlersUsepass
                                                                                            • to pass • to handlers to pass
application comms
 • Inter/intra application comms
                • Inter/intra application comms
                               • Inter/intra application comms• Inter/intra application comms
                                                                             • Inter/intra application comms
                                                                                            • Inter/intra application comms
                                                                                                           • Inter/intra application comms
s with EventBus with EventBus with EventBus with messages
     – messages – messages – messages                               – EventBus with EventBus with EventBus with EventBus with messages
                                                                                  – messages – messages – messages
red data structuresdata structuresdata structuresdata structures
     – Safe shared – Safe shared – Safe shared                      – Safe shared data structuresdata structuresdata structuresdata structures
                                                                                  – Safe shared – Safe shared – Safe shared
                Verticle       Verticle      Verticle    9
                                                             Verticle9              9
                                                                                              Verticle
                                                                                                   9
                                                                                                             Verticle       Verticle    9
                                                                                                                                            Verticle
                                                                                                                                                   9          9              9




                                                                                                                                                                             SockJS Bridge

                                                                                                       Event Bus
The Server
The Client
Demo
Solution #3.2:

                      Shared
                    Immutable
                       State
  Rich Hickey
Father of Clojure
e.g. In-memory Web
        Cache
MessageAI L
        Passing?
      F
Shared state only
dangerous if it is
   MUTABLE!
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.
Allowed Values
• Strings
• Boxed Primitives
•byte[]
                              R !
•org.vertx.java.core.buffer.Buffer
• Implementors of
                        G   E
 Shareable
                      AN
 org.vertx.java.core.shareddata.


                    D
Shared Map
Shared Set
Problem #4:

                                 The
                                Event
                                Loop
     Douglas Schmidt
Father of the Reactor Pattern
Reactor
w it works           Pattern Review

                     Event loop
   Application                     ...events
pp registers
   registers                          Events t
                                      trigger
 handlers                                 handl
   handlers...                    handlers.

                     Event Loop
andlers executed synchronously
on a single thread
se handlers to pass messages
Reactor Pattern Review


• Single thread / single event loop
• EVERYTHING runs on it
• You MUST NOT block the event loop
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!
Solution #4:

                             Worker
                            Verticles
       Carl Hewitt
Father of the Actor Model
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
The “Multi-reactor”
How it works it works it works it works
        How       How     How

                           Event loop Event loop Event loop Event loop

                                                                                       BG            BG
  App registers App registers App registers App registers trigger Events trigger Events trigger Events trigger
                                                    Events
    handlers      handlers      handlers      handlershandlers      handlers
                                                                                      Pool
                                                                                   handlers
                                                                                                    Pool
                                                                                                  handlers



• Handlers executed synchronously executed synchronously
            • Handlers executed synchronously executed synchronously
                        • Handlers • Handlers
  – on a single threada single threada single threada single thread
                  – on           – on           – on
• Use handlersUse handlersUse handlersUse handlers to pass messages
               • to pass messages messages messages
                              • to pass • to pass
• Inter/intra application comms
               • Inter/intra application comms
                              • Inter/intra application comms
                                             • Inter/intra application comms
  – EventBus with EventBus with EventBus with EventBus with messages
                – messages – messages – messages
  – Safe shared data structuresdata structuresdata structuresdata structures
                – Safe shared – Safe shared – Safe shared
                                                                                     Worker        Worker
                            Verticle       Verticle       Verticle        Verticle
                                                                                     Verticle      Verticle
                                                                      9          9            9              9




                                                     Event Bus
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
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
Other Goodies
• Growing Module        • TCP/SSL servers/clients
  Repository            • HTTP/HTTPS servers/
                          clients
 •  web server

 • persistors (Mongo,   • WebSockets support
    JDBC, ...)          • SockJS support
 • work queue           • Timers
 • authentication       • Buffers
    manager
                        • Streams and Pumps
 • session manager      • Routing
 • Socket.IO            • Asynchronous File I/O
Case Study




https://github.com/mstine/twitx
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
Vert.x:
                                     This ain’t your Dad’s Node!




                                                             Matt Stine
                                     Enterprise Java/Cloud Consultant
   Please fill out an evaluation:
                                                 matt.stine@gmail.com
                                                  http://mattstine.com
http://speakerrate.com/talks/16821
                                                       Twitter: @mstine
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

Vert.x

  • 1.
    Vert.x: This ain’t your Dad’s Node! Matt Stine Tim Fox Enterprise Java/Cloud Consultant Father of Vert.x matt.stine@gmail.com http://mattstine.com Twitter: @mstine
  • 2.
  • 4.
  • 5.
    Ryan Dahl Father ofNode.js How?!?!
  • 6.
  • 7.
  • 8.
  • 12.
    But Node has some shortcomings... 1. JavaScript! 2. Vertical Scaling 3. Interprocess Communication 4. Event Loop
  • 13.
  • 14.
    Problem #1: JavaScript Brendan Eich Father of JavaScript
  • 15.
  • 16.
    Solution #1: Polyglot Programming Neal Ford Father of Polyglot Programming http://memeagora.blogspot.com/2006/12/polyglot-programming.html
  • 17.
    Hello World in everyVert.x language!
  • 18.
  • 20.
  • 21.
  • 22.
  • 24.
    FUTURE Theoretically, any JVMlanguage or language that compiles to a supported language (e.g. ClojureScript ➔ JavaScript)
  • 25.
    Problem #2: Vertical Scaling Lars Bak Father of V8
  • 26.
  • 27.
    Node: Up andRunning Example 3-11: Using cluster to distribute work
  • 28.
    Solution #2: The JVM James Gosling Father of Java
  • 29.
    The Verticle - Unit of Deployment Verticle - Script/Java Class
  • 30.
    Vert.x Instance How it works it works it works it works How How How Event loop Event loop Event loop Event loop Event Loops App registers App registers App registers App registers trigger Events trigger Events trigger Events trigg Events handlers handlers handlers handlershandlers handlers handlers handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers – on a single threada single threada single threada single thread – on – on – on • Use handlersUse handlersUse handlersUse handlers to pass messages • to pass messages messages messages • to pass • to pass • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms – EventBus with EventBus with EventBus with EventBus with messages – messages – messages – messages – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared Verticle 9 9 9 Runtime.availableProcessors() == 4
  • 31.
    Vert.x Instance How it works it works it works it works How How How Event loop Event loop Event loop Event loop Event Loops App registers App registers App registers App registers trigger Events trigger Events trigger Events trigg Events handlers handlers handlers handlershandlers handlers handlers handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers – on a single threada single threada single threada single thread – on – on – on • Use handlersUse handlersUse handlersUse handlers to pass messages • to pass messages messages messages • to pass • to pass vertx run • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms HelloWorld with EventBus with EventBus with EventBus with messages – EventBus – messages – messages – messages – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared -instances 4 Verticle Verticle Verticle Verticle 9 9 9 Runtime.availableProcessors() == 4
  • 32.
    Concurrency • Verticle instanceassigned 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.
  • 33.
    Tony Hoare Father of CSP (http://dl.acm.org/citation.cfm? doid=359576.359585) Problem #3: Interprocess Communication
  • 34.
    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...
  • 35.
    Solution #3.1: The Event Bus Alan Kay Father of Smalltalk
  • 36.
    How it worksit works it works it works How How How Event loop Event loop Event loop Event loop App registers App registers App registers App registers trigger Events trigger Events trigger Events trigger Events handlers handlers handlers handlershandlers handlers handlers handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers – on a single threada single threada single threada single thread – on – on – on • Use handlersUse handlersUse handlersUse handlers to pass messages • to pass messages messages messages • to pass • to pass • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms – EventBus with EventBus with EventBus with EventBus with messages – messages – messages – messages – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared Verticle Verticle Verticle Verticle 9 9 9 9 Event Bus
  • 37.
    Addressing • Simply aString • Dot-style namespacing recommended • e.g. “messages.inbound.A”
  • 38.
    Handler Registration Handler X messages.inbound.A Handler Y Handler Z
  • 39.
  • 40.
    Pub/Sub Handler X Sender messages.inbound.A Handler Y Handler Z
  • 41.
  • 42.
    P2P Handler X Sender messages.inbound.A Handler Y Handler Z
  • 43.
  • 44.
  • 45.
    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
  • 46.
    Distributed Vert.x orksit works it works it works How How How How it works it works it works it works How How How How it works it works it works it works How How How Event loop Event loop Event loop Event loop Event loop Event loop Event loop Event loop Event loop Event loop Event loop Event loop rs App registers App registers App registers trigger Events trigger Events trigger Events trigger registers App registers trigger Events trigger Events trigger Events trigger registers App registers trigger Events trigger Events trigger Events App registers App registers App Events App registers App registers App Events handlers handlers handlershandlers handlers handlers handlers handlers handlers handlers handlershandlers handlers handlers handlers handlers handlers handlers handlershandlers handlers handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers le threada single threada single threada single thread – on – on – on – on a single threada single threada single threada single thread – on – on – on – on a single threada single threada single threada single thread – on – on – on ersUsepass messages messages messages messages handlersUsepass messages messages messages messages handlersUse handlersUsepass messages messages messages • to handlersUse handlersUsepass • to pass • to handlers to pass • Use • to handlersUse handlersUsepass • to pass • to handlers to pass • Use • to pass messages • to handlersUse handlers to pass • to pass application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms• Inter/intra application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms Inter/intra application comms • • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms s with EventBus with EventBus with EventBus with messages – messages – messages – messages – EventBus with EventBus with EventBus with EventBus with messages – messages – messages – messages – EventBus with EventBus with EventBus with EventBus with messages – messages – messages – messages red data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared Verticle Verticle Verticle 9 Verticle9 9 Verticle 9 Verticle Verticle 9 Verticle 9 9 Verticle 9 Verticle Verticle 9 Verticle 9 9 Event Bus
  • 47.
    Into the Browser! orks it works it works it works How How How How it works it works it works it works How How How Event loop Event loop Event loop Event loop Event loop Event loop Event loop Event loop rs App registers App registers App registers trigger Events trigger Events trigger Events trigger registers App registers trigger Events trigger Events trigger Events trigger Events App registers App registers App Events handlers handlers handlershandlers handlers handlers handlers handlers handlers handlers handlershandlers handlers handlers handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers le threada single threada single threada single thread – on – on – on – on a single threada single threada single threada single thread – on – on – on ersUsepass messages messages messages messages handlersUsepass messages messages messages messages • to handlersUse handlersUsepass • to pass • to handlers to pass • Use • to handlersUse handlersUsepass • to pass • to handlers to pass application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms• Inter/intra application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms s with EventBus with EventBus with EventBus with messages – messages – messages – messages – EventBus with EventBus with EventBus with EventBus with messages – messages – messages – messages red data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared Verticle Verticle Verticle 9 Verticle9 9 Verticle 9 Verticle Verticle 9 Verticle 9 9 9 SockJS Bridge Event Bus
  • 48.
  • 49.
  • 50.
  • 51.
    Solution #3.2: Shared Immutable State Rich Hickey Father of Clojure
  • 52.
  • 53.
    MessageAI L Passing? F
  • 54.
    Shared state only dangerousif it is MUTABLE!
  • 55.
    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.
  • 56.
    Allowed Values • Strings •Boxed Primitives •byte[] R ! •org.vertx.java.core.buffer.Buffer • Implementors of G E Shareable AN org.vertx.java.core.shareddata. D
  • 57.
  • 58.
  • 59.
    Problem #4: The Event Loop Douglas Schmidt Father of the Reactor Pattern
  • 60.
    Reactor w it works Pattern Review Event loop Application ...events pp registers registers Events t trigger handlers handl handlers... handlers. Event Loop andlers executed synchronously on a single thread se handlers to pass messages
  • 61.
    Reactor Pattern Review •Single thread / single event loop • EVERYTHING runs on it • You MUST NOT block the event loop
  • 62.
    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!
  • 63.
    Solution #4: Worker Verticles Carl Hewitt Father of the Actor Model
  • 64.
    Worker Verticles • Notassigned 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
  • 65.
    The “Multi-reactor” How itworks it works it works it works How How How Event loop Event loop Event loop Event loop BG BG App registers App registers App registers App registers trigger Events trigger Events trigger Events trigger Events handlers handlers handlers handlershandlers handlers Pool handlers Pool handlers • Handlers executed synchronously executed synchronously • Handlers executed synchronously executed synchronously • Handlers • Handlers – on a single threada single threada single threada single thread – on – on – on • Use handlersUse handlersUse handlersUse handlers to pass messages • to pass messages messages messages • to pass • to pass • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms • Inter/intra application comms – EventBus with EventBus with EventBus with EventBus with messages – messages – messages – messages – Safe shared data structuresdata structuresdata structuresdata structures – Safe shared – Safe shared – Safe shared Worker Worker Verticle Verticle Verticle Verticle Verticle Verticle 9 9 9 9 Event Bus
  • 68.
    Problem/Solution Summary • Node.jscompels the use of JavaScript • Vert.x is Polyglot • Node.js is inherently single-threaded • Vert.x leverages the multi-threaded JVM
  • 69.
    Problem/Solution Summary • Node.jsdoesn’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
  • 70.
    Other Goodies • GrowingModule • TCP/SSL servers/clients Repository • HTTP/HTTPS servers/ clients • web server • persistors (Mongo, • WebSockets support JDBC, ...) • SockJS support • work queue • Timers • authentication • Buffers manager • Streams and Pumps • session manager • Routing • Socket.IO • Asynchronous File I/O
  • 71.
  • 72.
    Get Involved! • GoogleGroup: 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
  • 73.
    Vert.x: This ain’t your Dad’s Node! Matt Stine Enterprise Java/Cloud Consultant Please fill out an evaluation: matt.stine@gmail.com http://mattstine.com http://speakerrate.com/talks/16821 Twitter: @mstine
  • 74.
    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