Asynchronous IO with Vert.x




Peter Ledbrook - SpringSource
New kid on the block




    Javascript   Non-blocking   Single-threaded



                       Reactor pattern


                                                  2
What’s it about?

• C10K Problem
  – How do you handle 10,000+ concurrent connections?
  – Without grinding to a halt?
• Why?
  – All those mobile devices!
  – Rise of long-lived connections

          Real-time chat
                                     Instant messaging

         Twitter
                              Voice and video

                                                         3
Transloadit

• “Flexible, fast and scalable file uploading and encoding”
• Video/audio encoding on the fly
  – 500mb/s




                    Transloadit
           Video
Client                                       S3/Cloudfiles/...




                                                              4
Voxer




•   Walkie-talkie for iOS and Android
•   Live audio
•   Large number of connections
•   Push notifications



                                        5
Javascript and nothing but Javascript




                                        6
What if?

•   Language neutral framework
•   On the JVM
•   Polyglot APIs
•   Easy horizontal scalability
•   Do non-eventy stuff in a non-eventy way




                         Vert.x!

                                              7
Vert.x

• Written in Java
• Built on Netty and NIO 2
  – Java 7 only!
• Multiple language bindings
  – JRuby
  – Groovy
  – Javascript
  – Python/Clojure/Scala planned
• Based on the Reactor pattern




                                   8
How it works

                          Event loop

 App registers                         Events trigger
   handlers                              handlers



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

                                                    9
Features

•   Non-blocking stdio
•   TCP/IP and HTTP servers and clients
•   Web Socket support
•   (Non-blocking) Redis & Mongo modules (busmods)
•   Asynchronous file system access and stream handling

                            Pump
          ReadStream                     WriteStream

                            e.g.
        HTTP Request                     AsyncFile


                                                          10
Example - Echo socket




                        11
Example - Echo client




                        12
Scaling




            App
          instance




                     13
Scaling


                     Vertx node

                      Event bus




            App          App            App
          instance     instance       instance



                 vertx run -instances 3

                                                 14
Scaling


                  Hazelcast

                  Event bus




          Vertx     Vertx     Vertx
          node      node      node




                                      15
Extensibility via (Bus) Mods




                       Work      Web     ...
 Mailer   MongoDB
                       queue    server




                    Event bus




                                               16
Mod structure

• Core mods in $VERTX_HOME/mods
• User mods in $VERTX_MODS




                                  17
A more complex example
Realtime logging app

  Log messages




                    App


           SockJS


     Browser

                          19
Realtime logging app




                       20
Realtime logging app




                       21
Realtime logging app




                       22
Demo
Summary

• Event-driven framework for the JVM
  – Low overhead
  – Handle large number of connections
  – Great for working with streams
• Pick your own language!
  – Which will of course be Groovy ;)
• Published release: 1.0.1
  – Still early days
  – Get involved now!




                                         24
More info

• w:   https://github.com/purplefox/vert.x
       http://purplefox.github.com/vert.x/
• f:   http://groups.google.com/group/vertx

• t:   pledbrook
• b:   http://blog.springsource.com/author/peter-ledbrook/




                                                             25
Thank you!

 Questions?




              26

Vert.x introduction

  • 1.
    Asynchronous IO withVert.x Peter Ledbrook - SpringSource
  • 2.
    New kid onthe block Javascript Non-blocking Single-threaded Reactor pattern 2
  • 3.
    What’s it about? •C10K Problem – How do you handle 10,000+ concurrent connections? – Without grinding to a halt? • Why? – All those mobile devices! – Rise of long-lived connections Real-time chat Instant messaging Twitter Voice and video 3
  • 4.
    Transloadit • “Flexible, fastand scalable file uploading and encoding” • Video/audio encoding on the fly – 500mb/s Transloadit Video Client S3/Cloudfiles/... 4
  • 5.
    Voxer • Walkie-talkie for iOS and Android • Live audio • Large number of connections • Push notifications 5
  • 6.
    Javascript and nothingbut Javascript 6
  • 7.
    What if? • Language neutral framework • On the JVM • Polyglot APIs • Easy horizontal scalability • Do non-eventy stuff in a non-eventy way Vert.x! 7
  • 8.
    Vert.x • Written inJava • Built on Netty and NIO 2 – Java 7 only! • Multiple language bindings – JRuby – Groovy – Javascript – Python/Clojure/Scala planned • Based on the Reactor pattern 8
  • 9.
    How it works Event loop App registers Events trigger handlers handlers • Handlers executed synchronously – on a single thread • Use handlers to pass messages • Inter/intra application comms – EventBus with messages – Safe shared data structures 9
  • 10.
    Features • Non-blocking stdio • TCP/IP and HTTP servers and clients • Web Socket support • (Non-blocking) Redis & Mongo modules (busmods) • Asynchronous file system access and stream handling Pump ReadStream WriteStream e.g. HTTP Request AsyncFile 10
  • 11.
    Example - Echosocket 11
  • 12.
    Example - Echoclient 12
  • 13.
    Scaling App instance 13
  • 14.
    Scaling Vertx node Event bus App App App instance instance instance vertx run -instances 3 14
  • 15.
    Scaling Hazelcast Event bus Vertx Vertx Vertx node node node 15
  • 16.
    Extensibility via (Bus)Mods Work Web ... Mailer MongoDB queue server Event bus 16
  • 17.
    Mod structure • Coremods in $VERTX_HOME/mods • User mods in $VERTX_MODS 17
  • 18.
  • 19.
    Realtime logging app Log messages App SockJS Browser 19
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    Summary • Event-driven frameworkfor the JVM – Low overhead – Handle large number of connections – Great for working with streams • Pick your own language! – Which will of course be Groovy ;) • Published release: 1.0.1 – Still early days – Get involved now! 24
  • 25.
    More info • w: https://github.com/purplefox/vert.x http://purplefox.github.com/vert.x/ • f: http://groups.google.com/group/vertx • t: pledbrook • b: http://blog.springsource.com/author/peter-ledbrook/ 25
  • 26.