Date post: | 07-Jan-2017 |
Category: |
Software |
Upload: | alexander-derkach |
View: | 693 times |
Download: | 5 times |
Vert.x The problem of real-time data binding
Alexander Derkach
Java Developer at CyberVision. Inc
Hands on with Python/C++
Passionate about clean code & performance
About Me
alexsderkach
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
Pasta Web Service #1
Host 1
Host 2
Server
Host 3
Processing Thread
Pasta Web Service #1
Host 2
Server
Host 3
Processing Thread
Host 1POST /boiled_water
Pasta Web Service #1
Host 2
Server
Host 3
Processing Thread
Host 1POST /boiled_water
Pasta Web Service #1
Host 2
Server
Host 3
Processing Thread
Host 1POST /boiled_water
POST /sause
POST /wine
Pasta Web Service #1
Host 2
Server
Host 3
Processing Thread
Host 1
POST /sause
POST /wine
POST /boiled_water
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
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
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
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
Pasta Web Service #2Server
Host N
Host 1 Processing Thread #1
Processing Thread #N
... ...
POST /boiled_water
POST /wine
POST /**
Pasta Web Service #2Server
Host N
Host 1 Processing Thread #1
Processing Thread #N
... ...POST /wine
POST /boiled_water
POST /**
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
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
POST /wine
POST /boiled_water
Acceptor Thread
Host 2
POST /sause
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
POST /wine
POST /boiled_water
Acceptor Thread
Host 2
POST /sause
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
Acceptor Thread
Host 2
POST /sause
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
Acceptor Thread
Host 2
POST /sause
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
Acceptor Thread
Host 2
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
POST /wine
POST /boiled_water
Acceptor Thread
Host 2
POST /sause
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
POST /wine
POST /boiled_water
Acceptor Thread
Host 2
POST /sause
Thread pool
Pasta Web Service #3Server
Host 3
Host 1
Processing Thread #1
Processing Thread #2
WebSocket
WebSocket Acceptor Thread
Host 2WebSocket
C10K problemThe problem of optimizing network sockets to
handle a large number of clients at the same time.
The Reactor
The Reactor
http://www.cs.wustl.edu/~schmidt/PDF/reactor-siemens.pdf
http://www.cs.wustl.edu/~schmidt/PDF/reactor-siemens.pdf
Advantages
+ Optimal usage of CPU
+ Can handle more requests with same hardware
+ Can scale above C10K limit
Disadvantages
- Difficult to understand and structure code
- Complex debugging
Implementations
Servers:
Nginx
Jetty
Netty
Platforms:
Node.js (Javascript)
Tornado, Eventlet (Python)
EventMachine (Ruby)
Pasta Web Service #4
Server
Host 3
Host 1
WebSocket
WebSocket
Host 2WebSocket
Acceptor
Dispatcher
Water Handler
Wine Handler
Sause Handler
Pasta Web Service #4
Server
Host 1WebSocket
Host 2WebSocket
Acceptor
Dispatcher
Water Handler
Wine Handler
Sause Handler
Host 3WebSocket
Pasta Web Service #4
Server
Dispatcher
Host 3
Host 1
Water Handler
Wine Handler
WebSocket
WebSocket
Host 2WebSocket
Sause Handler
Acceptor
Pasta Web Service #4
Server
Dispatcher
Host 3
Host 1
Water Handler
Wine Handler
WebSocket
WebSocket
Host 2WebSocket
Sause Handler
Acceptor
Pasta Web Service #4
Server
Dispatcher
Host 3
Host 1
Water Handler
Wine Handler
WebSocket
WebSocket
WebSocket
Sause Handler
Acceptor
...
Pasta Web Service #4
Server
Dispatcher
Host 3
Host 1
Water Handler
Wine Handler
WebSocket
WebSocket
WebSocket
Sause Handler
Acceptor
...
Pasta Web Service #4
Server
Dispatcher
Host 3
Host 1
Water Handler
Wine Handler
WebSocket
WebSocket
WebSocket
Sause Handler
Acceptor
...
Problem Many long-lived connections
Responsive
Message-driven
Non-blocking I/O
Based on Reactor Pattern (maybe)
Problem Many long-lived connections
Responsive
Message-driven
Non-blocking I/O
Based on Reactor Pattern (maybe)
Reactive Manifesto?
Responsive React to Users
Resilient React to Failures
Elastic React to Load
Message Driven Modules / Component Interaction
Goal
Principles
Method
Introducing
"Simple but not simplistic"
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
Internals
Terminology
Verticle
Vert.x instance
Event bus
Handler
Vert.x instance
Usually one per JVM
Entry point into API
Container for Verticles
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
Event Bus One for every Vert.x Instance
Point to Point
Publish/Subscribe
Send/Reply
Don't call us, we'll call you
Used to handle all types of asynchronous occurrences
Thinking asynchronouslyStep 1 Call method with result handler
Thinking asynchronouslyStep 2 Create method implementation
Thinking asynchronouslyStep 3 Don't overthink
Big PictureServer
JVM
Event Bus
JVM
Vert.x Instance Vert.x Instance
Verticle Verticle Verticle Verticle
Demo
ConcurrencyExecution 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
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
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
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
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
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
Best practice
Use Event Loop by default
Use Worker Pool to execute blocking operations
Demo
PolyglotRuby
JavaScriptGroovy
Java
* Support for new language as a module
EcosystemCore
Web
ClusteringIntegration
Testing
DevOps
Data Access
Reactive
Cloud Services
http://vertx.io/docs/
http://vertx.io/docs/
Different strokes for different folks
1. WebSocket vs SockJSWebSocket
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'); };
SockJS 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'); };
2. Real-time Metrics
Net Client Net Server
HTTP Client HTTP Server
Datagram Socket Event Bus
User defined
3. Async MongoDB support
http://mongodb.github.io/mongo-java-driver/3.0/driver-async/
+
http://mongodb.github.io/mongo-java-driver/3.0/driver-async/
4. Async Redis client
Command 1 Command 2 Command 3
Result 1 Result 2 Result 3 Net Client
5. Auto redeploy1. 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)
6. Reactive Streams support
...
Solving Callback Hell
7. RxJava to the rescue
Event Bus
Server
JVM
Vert.x Instance
Verticle Verticle
8. Distributed Event Bus
8. Distributed Event Bus
Event Bus
Server
JVM
Vert.x Instance
Verticle Verticle
SockJS
9. ClusteringCluster manager:
Discovery and group membership of nodes
Maintaining cluster wide topic subscriber lists
Distributed data
Is NOT used for Event Bus transportation
Server Server
Clustered Event Bus
JVM
Event Bus
JVM
Vert.x Instance Vert.x Instance
Verticle Verticle Verticle Verticle
Cluster Manager
10. Fault Tolerance
HA mode for any deployed Verticle
HA groups
Network partitions - Quorum
Bonus
JSON Config
Demo
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
Next release - Early June 2016
HTTP/2
Event Bus encryption (TLS/SSL)
Cluster member discovery in Docker & Kubernetes
EIP framework support
Microservice toolbox
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
http://www.infoq.com/articles/vertx-3-tim-foxhttp://vertx.io/docs/https://groups.google.com/forum/#!forum/vertxhttps://github.com/vert-x3/wiki/wikihttps://goo.gl/WQ2qpU
Thank you for your attention
Q&A
alexsderkach