Modern app programming with RxJava and Eclipse Vert.x

44
Modern app programming with RxJava & Eclipse Vert.x

Transcript of Modern app programming with RxJava and Eclipse Vert.x

Page 1: Modern app programming with RxJava and Eclipse Vert.x

Modern app programmingwith RxJava & Eclipse Vert.x

Page 2: Modern app programming with RxJava and Eclipse Vert.x

Who am I?

● Thomas Segismont● tsegismont on GitHub, Twitter, Gmail, Freenode...● Vert.x core team since August 2016● At Red Hat since November 2012 (RHQ and Hawkular)

Page 3: Modern app programming with RxJava and Eclipse Vert.x

Expectations

● Goals– Why should you consider learning Vert.x and RxJava?

– What does RxJava look like? Vert.x?

– How can I combine them to build scalable, efficient and resilient apps?

● Non goals– In-depth study of RxJava or Vert.x

Page 4: Modern app programming with RxJava and Eclipse Vert.x

http://example.com

http://example.com

http://example.com

Page 5: Modern app programming with RxJava and Eclipse Vert.x

Alexandre Duret-Lutz (CC BY-SA 2.0)

Page 6: Modern app programming with RxJava and Eclipse Vert.x

ExecutorService to the rescue

Page 7: Modern app programming with RxJava and Eclipse Vert.x

Asynchronous

● ExecutorService● Future

Page 8: Modern app programming with RxJava and Eclipse Vert.x

http://example.com

http://example.com

http://example.com

Page 9: Modern app programming with RxJava and Eclipse Vert.x

Thread pools, thread pools EVERYWHERE!

Page 10: Modern app programming with RxJava and Eclipse Vert.x

The C10K problem

Page 11: Modern app programming with RxJava and Eclipse Vert.x

Non-blocking

● Stop wasting CPU for waiting– NIO

– CompletableFuture

Page 12: Modern app programming with RxJava and Eclipse Vert.x

Improvements in modern app servers

● Non blocking I/O in connectors and dispatch to worker threads● Continuations (suspend/resume)

Page 13: Modern app programming with RxJava and Eclipse Vert.x

The cost of contention and delay

● Neil J. Gunther, Universal Law of Computational Scalability

Page 14: Modern app programming with RxJava and Eclipse Vert.x

Mechanical sympathy

Page 15: Modern app programming with RxJava and Eclipse Vert.x

The pursuit of answers

● Benchmark by Netflix– Tomcat vs Netty for real web apps

– http://bit.ly/2cJMBsG

● Under load– Less thread migrations

– More instructions per cycle

– Severe latency degradation on Tomcat (unpredictable)

– Higher throughput on Netty

Page 16: Modern app programming with RxJava and Eclipse Vert.x

Event loops

Page 17: Modern app programming with RxJava and Eclipse Vert.x
Page 18: Modern app programming with RxJava and Eclipse Vert.x

New stuff?

● Browser (AJAX, #setTimeout)

● Swing● Node.js

Page 19: Modern app programming with RxJava and Eclipse Vert.x

Reactor pattern

Singlethread

Page 20: Modern app programming with RxJava and Eclipse Vert.x

Event loop benefits

● Mechanical sympathy● Simple concurrency model● Easier to scale

Page 21: Modern app programming with RxJava and Eclipse Vert.x

Vert.x

Page 22: Modern app programming with RxJava and Eclipse Vert.x

What is Vert.x

● Toolkit (lib)● Polyglot (on the JVM)● Reactive

Page 23: Modern app programming with RxJava and Eclipse Vert.x

Events

● Http server request● Buffer read from a file● Server bound to port● Message from the bus

Page 24: Modern app programming with RxJava and Eclipse Vert.x

Multi-Reactor

Page 25: Modern app programming with RxJava and Eclipse Vert.x

Event Bus

● Nervous system of Vert.x● Message passing style● Different messaging paradigms:

– Point to point

– Publish/Subscribe

– Request/Reply

Page 26: Modern app programming with RxJava and Eclipse Vert.x
Page 27: Modern app programming with RxJava and Eclipse Vert.x
Page 28: Modern app programming with RxJava and Eclipse Vert.x

Running blocking code

● In the real world, most JVM libraries have blocking APIs– JDBC

– io.File

– Your legacy libs

– Heavy computation

● With Vert.x you can interact with blocking parts easily● You won’t throw away your code assets!

Page 29: Modern app programming with RxJava and Eclipse Vert.x

Callback based (reactive imperative)

Page 30: Modern app programming with RxJava and Eclipse Vert.x

CALLBACK HELL !

Page 31: Modern app programming with RxJava and Eclipse Vert.x

RxJavaA library for composing asynchronous and event-based programs

using observable sequences for the Java VM.

Page 32: Modern app programming with RxJava and Eclipse Vert.x

Functional programming influence

● Favors composition● Avoids global state● Avoids side effects

Page 33: Modern app programming with RxJava and Eclipse Vert.x

Data and events flows

● It is great at organizing transformation of data and coordination of events

● It makes most sense when many sources of events are involved (our web apps!)

Page 34: Modern app programming with RxJava and Eclipse Vert.x

Push based

Observable Observer

Subscription

● OnNext 0..∞

● Terminal event 0..1– OnComplete

– OnError

Page 35: Modern app programming with RxJava and Eclipse Vert.x

Reactive pull backpressure

Observable Observer

Subscription

Request

Page 36: Modern app programming with RxJava and Eclipse Vert.x

Concurrency

● No concurrent emission of events– Easier to understand

– Some operators need the guarantee (reduce)

Page 37: Modern app programming with RxJava and Eclipse Vert.x

Observable / Single / Completable

Zero One Many

Sync void T Iterable<T>

Async Completable Single<T> Observable<T>

Page 38: Modern app programming with RxJava and Eclipse Vert.x

Observable#map

ReactiveX.io Creative Commons Attribution 3.0 License

Page 39: Modern app programming with RxJava and Eclipse Vert.x

Observable#flatMap

ReactiveX.io Creative Commons Attribution 3.0 License

Page 40: Modern app programming with RxJava and Eclipse Vert.x

Single#zip

ReactiveX.io Creative Commons Attribution 3.0 License

Page 41: Modern app programming with RxJava and Eclipse Vert.x

Observable#observeOn

ReactiveX.io Creative Commons Attribution 3.0 License

Page 42: Modern app programming with RxJava and Eclipse Vert.x

Music Store Demohttps://github.com/tsegismont/vertx-musicstore

Page 43: Modern app programming with RxJava and Eclipse Vert.x

http://example.com

http://example.com

http://example.com

https://coverartarchive.org/

Postgres

Couchbase

App w/ Local Cache

Page 44: Modern app programming with RxJava and Eclipse Vert.x

Thank you!http://vertx.io