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.
5. Pasta Web Service #1
Host 2
Server
Host 3
Processing
Thread
Host 1
POST
/boiled_water
6. Pasta Web Service #1
Host 2
Server
Host 3
Processing
Thread
Host 1
POST
/boiled_water
7. Pasta Web Service #1
Host 2
Server
Host 3
Processing
Thread
Host 1
POST
/boiled_water
POST
/sause
POST
/wine
8. Pasta Web Service #1
Host 2
Server
Host 3
Processing
Thread
Host 1
POST
/sause
POST
/wine
POST
/boiled_water
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. 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. 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. 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. Pasta Web Service #2
Server
Host N
Host 1 Processing
Thread #1
Processing
Thread #N
... ...
POST
/boiled_water
POST
/wine
POST
/**
14. Pasta Web Service #2
Server
Host N
Host 1 Processing
Thread #1
Processing
Thread #N
... ...
POST
/wine
POST
/boiled_water
POST
/**
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. 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. 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. Thread pool
Pasta Web Service #3
Server
Host 3
Host 1
Processing
Thread #1
Processing
Thread #2
Acceptor Thread✓
✓
Host 2
POST
/sause
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. Thread pool
Pasta Web Service #3
Server
Host 3
Host 1
Processing
Thread #1
Processing
Thread #2
Acceptor Thread
Host 2
✓
✓
✓
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. 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. 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. C10K problem
The problem of optimizing network sockets to
handle a large number of clients at the same time.
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
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. Event Bus
• One for every Vert.x Instance
• Point to Point
• Publish/Subscribe
• Send/Reply
47. Don't call us, we'll call you
Used to handle all types of asynchronous occurrences
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. 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. 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. 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. 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. 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. Best practice
• Use Event Loop by default
• Use Worker Pool to execute blocking operations
65. 2. Real-time Metrics
Net Client Net Server
HTTP Client HTTP Server
Datagram Socket Event Bus
User defined
66. 3. Async MongoDB support
http://mongodb.github.io/mongo-java-driver/3.0/driver-async/
+
67. 4. Async Redis client
Command 1 Command 2 Command 3
Result 1 Result 2 Result 3 Net Client
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)
73. 8. Distributed Event Bus
Event Bus
Server
JVM
Vert.x Instance
Verticle Verticle
SockJS
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. Server Server
Clustered Event Bus
JVM
Event Bus
JVM
Vert.x Instance Vert.x Instance
Verticle Verticle Verticle Verticle
Cluster Manager
76. 10. Fault Tolerance
• HA mode for any deployed Verticle
• HA groups
• Network partitions - Quorum
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. Next release - Early June 2016
• HTTP/2
• Event Bus encryption (TLS/SSL)
• Cluster member discovery in Docker & Kubernetes
• EIP framework support
• Microservice toolbox