Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Vert.x – The problem of real-time data binding

2,482 views

Published on

As the popularity of any event-driven application increases, the number of concurrent connections may increase. Applications that employ thread-per-client architecture, frustrate scalability by exhausting a server’s memory with excessive allocations and by exhausting a server’s CPU with excessive context-switching. One of obvious solutions, is exorcising blocking operations from such applications. Vert.x is event driven and non blocking toolkit, which may help you to achive this goal. In this talk, we are going to cover it’s core features and develop a primitive application using WebSockets, RxJava and Vert.x.

Published in: Software

Vert.x – The problem of real-time data binding

  1. 1. Vert.x The problem of real-time data binding Alexander Derkach
  2. 2. • Java Developer at CyberVision. Inc • Hands on with Python/C++ • Passionate about clean code & performance About Me alexsderkach
  3. 3. Can you cook a pasta? • 1/2 pound dried spaghetti • 4 slices of thick cut bacon (about 4 oz) • 3/4 cup diced onion • 3 garlic cloves, sliced thin (horizontally) • 1 heaping cup of shredded parmesan cheese • 2 large eggs • 1 tsp freshly ground black pepper
  4. 4. Pasta Web Service #1 Host 1 Host 2 Server Host 3 Processing Thread
  5. 5. Pasta Web Service #1 Host 2 Server Host 3 Processing Thread Host 1 POST /boiled_water
  6. 6. Pasta Web Service #1 Host 2 Server Host 3 Processing Thread Host 1 POST /boiled_water
  7. 7. Pasta Web Service #1 Host 2 Server Host 3 Processing Thread Host 1 POST /boiled_water POST /sause POST /wine
  8. 8. Pasta Web Service #1 Host 2 Server Host 3 Processing Thread Host 1 POST /sause POST /wine POST /boiled_water
  9. 9. Pasta Web Service #2 Host 2 Server Host 3 Processing Thread #1 Host 1 Processing Thread #2 Processing Thread #3 POST /sause POST /wine POST /boiled_water
  10. 10. Pasta Web Service #2 Host 2 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 Processing Thread #3 POST /sause POST /wine POST /boiled_water
  11. 11. Pasta Web Service #2 Host 2 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 Processing Thread #3 POST /sause POST /wine POST /boiled_water
  12. 12. Pasta Web Service #2 Host 2 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 Processing Thread #3 POST /sause POST /wine POST /boiled_water
  13. 13. Pasta Web Service #2 Server Host N Host 1 Processing Thread #1 Processing Thread #N ... ... POST /boiled_water POST /wine POST /**
  14. 14. Pasta Web Service #2 Server Host N Host 1 Processing Thread #1 Processing Thread #N ... ... POST /wine POST /boiled_water POST /**
  15. 15. Oh, Dear • Efficiency: Threading may lead to poor performance due to context switching, synchronization, and data movement • Programming simplicity: Threading may require complex concurrency control schemes
  16. 16. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 POST /wine POST /boiled_water Acceptor Thread Host 2 POST /sause
  17. 17. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 POST /wine POST /boiled_water Acceptor Thread Host 2 POST /sause
  18. 18. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 Acceptor Thread✓ ✓ Host 2 POST /sause
  19. 19. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 Acceptor Thread Host 2 POST /sause ✓ ✓
  20. 20. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 Acceptor Thread Host 2 ✓ ✓ ✓
  21. 21. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 POST /wine POST /boiled_water Acceptor Thread Host 2 POST /sause
  22. 22. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 POST /wine POST /boiled_water Acceptor Thread Host 2 POST /sause
  23. 23. Thread pool Pasta Web Service #3 Server Host 3 Host 1 Processing Thread #1 Processing Thread #2 WebSocket WebSocket Acceptor Thread Host 2 WebSocket
  24. 24. C10K problem The problem of optimizing network sockets to handle a large number of clients at the same time.
  25. 25. The Reactor
  26. 26. The Reactor http://www.cs.wustl.edu/~schmidt/PDF/reactor-siemens.pdf
  27. 27. Advantages + Optimal usage of CPU + Can handle more requests with same hardware + Can scale above C10K limit
  28. 28. Disadvantages - Difficult to understand and structure code - Complex debugging
  29. 29. Implementations Servers: • Nginx • Jetty • Netty Platforms: • Node.js (Javascript) • Tornado, Eventlet (Python) • EventMachine (Ruby)
  30. 30. Pasta Web Service #4 Server Host 3 Host 1 WebSocket WebSocket Host 2 WebSocket Acceptor Dispatcher Water Handler Wine Handler Sause Handler
  31. 31. Pasta Web Service #4 Server Host 1 WebSocket Host 2 WebSocket Acceptor Dispatcher Water Handler Wine Handler Sause Handler Host 3 WebSocket
  32. 32. Pasta Web Service #4 Server Dispatcher Host 3 Host 1 Water Handler Wine Handler WebSocket WebSocket Host 2 WebSocket Sause Handler Acceptor
  33. 33. Pasta Web Service #4 Server Dispatcher Host 3 Host 1 Water Handler Wine Handler WebSocket WebSocket Host 2 WebSocket Sause Handler Acceptor
  34. 34. Pasta Web Service #4 Server Dispatcher Host 3 Host 1 Water Handler Wine Handler WebSocket WebSocket WebSocket Sause Handler Acceptor ...
  35. 35. Pasta Web Service #4 Server Dispatcher Host 3 Host 1 Water Handler Wine Handler WebSocket WebSocket WebSocket Sause Handler Acceptor ...
  36. 36. Pasta Web Service #4 Server Dispatcher Host 3 Host 1 Water Handler Wine Handler WebSocket WebSocket WebSocket Sause Handler Acceptor ...
  37. 37. Problem • Many long-lived connections • Responsive • Message-driven • Non-blocking I/O • Based on Reactor Pattern (maybe)
  38. 38. Problem • Many long-lived connections • Responsive • Message-driven • Non-blocking I/O • Based on Reactor Pattern (maybe)
  39. 39. Reactive Manifesto? Responsive React to Users Resilient React to Failures Elastic React to Load Message Driven Modules / Component Interaction Goal Principles Method
  40. 40. Introducing "Simple but not simplistic"
  41. 41. Vert.x features • Asynchronous by nature • Slim and lightweight core (650kb) • Polyglot (on JVM) • Super simple concurrency model • Based on Reactor Pattern (Multi-Reactor) • Scalability and Fault-Tolerance
  42. 42. Internals
  43. 43. Terminology Verticle Vert.x instance Event bus Handler
  44. 44. Vert.x instance • Usually one per JVM • Entry point into API • Container for Verticles
  45. 45. Verticle • Chunk of code deployed to Vert.x instance • Can be written in any supported language • Each Verticle can have its own ClassLoader • Communicates with other Verticles via Event Bus
  46. 46. Event Bus • One for every Vert.x Instance • Point to Point • Publish/Subscribe • Send/Reply
  47. 47. Don't call us, we'll call you Used to handle all types of asynchronous occurrences
  48. 48. Thinking asynchronously Step 1 Call method with result handler
  49. 49. Thinking asynchronously Step 2 Create method implementation
  50. 50. Thinking asynchronously Step 3 Don't overthink
  51. 51. Big Picture Server JVM Event Bus JVM Vert.x Instance Vert.x Instance Verticle Verticle Verticle Verticle
  52. 52. Demo
  53. 53. Concurrency Execution context - controls the scope and order in which a set of handlers are executed Each Verticle runs in its own context: • Event Loop Context • Worker Context • Multi-Threaded Worker Context
  54. 54. Event Loop Context • Based on Netty's EventLoop • One Handler at a time • Handler will always be executed with the same thread Wait for events Handle events
  55. 55. Event Loop Context Wait for events Handle events • Based on Netty's EventLoop • One Handler at a time • Handler will always be executed with the same thread
  56. 56. Event Loop is not a Silver Bullet Do not EVER: • Thread.sleep() • Waiting on a lock • Waiting on a mutex or monitor • Doing a complex calculation
  57. 57. OrderedExecutor Worker Context • Based on OrderedExecutor • One Handler at a time • Handler will NOT always be executed with the same thread Queue Task 2 Special Task while (!queue.isEmpty()) queue.poll().run(); Thread Pool Executor Task 1
  58. 58. Thread Pool Executor Multi-Threaded Worker Context • Based on shared ThreadPoolExecutor • Many Handlers at the same time • Handler will NOT always be executed with the same threadQueue Task 2Task 1 ... Worker 1 Worker 2 poll poll
  59. 59. Best practice • Use Event Loop by default • Use Worker Pool to execute blocking operations
  60. 60. Demo
  61. 61. Polyglot Ruby JavaScript Groovy Java * Support for new language as a module
  62. 62. Ecosystem Core Web ClusteringIntegration Testing DevOps Data Access Reactive Cloud Services http://vertx.io/docs/
  63. 63. Different strokes for different folks
  64. 64. 1. WebSocket vs SockJS WebSocket <script> var sock = new WebSocket('ws://domain/my_ws'); sock.onopen = function() { console.log('open'); }; sock.onmessage = function(e) { console.log('message', e.data); }; sock.onclose = function() { console.log('close'); }; </script> SockJS <script> var sock = new SockJS('http://domain/my_ws'); sock.onopen = function() { console.log('open'); }; sock.onmessage = function(e) { console.log('message', e.data); }; sock.onclose = function() { console.log('close'); }; </script>
  65. 65. 2. Real-time Metrics Net Client Net Server HTTP Client HTTP Server Datagram Socket Event Bus User defined
  66. 66. 3. Async MongoDB support http://mongodb.github.io/mongo-java-driver/3.0/driver-async/ +
  67. 67. 4. Async Redis client Command 1 Command 2 Command 3 Result 1 Result 2 Result 3 Net Client
  68. 68. 5. Auto redeploy 1. The application is launched in redeploy mode. 2. It listens for file changes 3. When a matching file is changed, it stops the application 4. It executes the on-redeploy actions if any 5. It restarts the application 6. back to (2)
  69. 69. 6. Reactive Streams support ...
  70. 70. Solving Callback Hell
  71. 71. 7. RxJava to the rescue
  72. 72. Event Bus Server JVM Vert.x Instance Verticle Verticle 8. Distributed Event Bus
  73. 73. 8. Distributed Event Bus Event Bus Server JVM Vert.x Instance Verticle Verticle SockJS
  74. 74. 9. Clustering Cluster manager: • Discovery and group membership of nodes • Maintaining cluster wide topic subscriber lists • Distributed data • Is NOT used for Event Bus transportation
  75. 75. Server Server Clustered Event Bus JVM Event Bus JVM Vert.x Instance Vert.x Instance Verticle Verticle Verticle Verticle Cluster Manager
  76. 76. 10. Fault Tolerance • HA mode for any deployed Verticle • HA groups • Network partitions - Quorum
  77. 77. Bonus JSON Config
  78. 78. Demo
  79. 79. Vert.x in 1 slide Event Bus VerticleEvent Loop Handlers Polyglot Distribution & High Availability Vert.x Verticle Handlers Vert.x Worker pool Worker pool Event Loop
  80. 80. Next release - Early June 2016 • HTTP/2 • Event Bus encryption (TLS/SSL) • Cluster member discovery in Docker & Kubernetes • EIP framework support • Microservice toolbox
  81. 81. References • http://www.infoq.com/articles/vertx-3-tim-fox • http://vertx.io/docs/ • https://groups.google.com/forum/#!forum/vertx • https://github.com/vert-x3/wiki/wiki • https://goo.gl/WQ2qpU
  82. 82. Thank you for your attention Q&A alexsderkach

×