Reactive microservice end to end from Rx Java

  • Slides: 38
Download presentation
Reactive microservice end to end from Rx. Java to the wire with g. RPC

Reactive microservice end to end from Rx. Java to the wire with g. RPC Ryan Michela @ Salesforce

Let’s poll the room g. RP C

Let’s poll the room g. RP C

Let’s poll the room Rx. Jav a

Let’s poll the room Rx. Jav a

Let’s poll the room g. RP C Rx. Jav a

Let’s poll the room g. RP C Rx. Jav a

M g. RPC E About me Rx. Jav a Microservices practitioner Reactive programming enthusiast

M g. RPC E About me Rx. Jav a Microservices practitioner Reactive programming enthusiast

What’s in this talk 1. The story of reactive-grpc 2. An overview of g.

What’s in this talk 1. The story of reactive-grpc 2. An overview of g. RPC and Rx. Java 3. Some great demos 4. A deep dive into backpressure and flow control 5. An even better demo

Where did Reactive-g. RPC come from? 20 year old monolith to Microservices Bespoke REST

Where did Reactive-g. RPC come from? 20 year old monolith to Microservices Bespoke REST APIs Apache Thrift g. RPC + Mesh

What is g. RPC? § High-performance, open source, RPC framework § Protobuf and HTTP/2

What is g. RPC? § High-performance, open source, RPC framework § Protobuf and HTTP/2 § Big features - Contract-first microservices - Polyglot code generation - Unary and bi-directional streaming § https: //grpc. io/

What is Rx. Java? § API for asynchronous programming with observable streams § Everything

What is Rx. Java? § API for asynchronous programming with observable streams § Everything is a stream (Observable) § Combine, filter, transform § Subscribe (Observer) § http: //reactivex. io/

What is Rx. Java? § API for asynchronous programming with observable streams § Everything

What is Rx. Java? § API for asynchronous programming with observable streams § Everything is a stream (Observable) § Combine, filter, transform Java. Fx. Observable. events. Of(scene, § Subscribe (Observer) Mouse. Event. CLICK) . debounde(1, Time. Unit. SECONDS). map(event -> new Robot. Command(event)). subscribe(command -> robot. do(command));

1+1=3 https: //imgur. com/gallery/c 7 NJRa 2

1+1=3 https: //imgur. com/gallery/c 7 NJRa 2

g. RP C Reactive g. RPC Rx. Jav a Protobuf compiler plugin + new

g. RP C Reactive g. RPC Rx. Jav a Protobuf compiler plugin + new stubs

Questions so far?

Questions so far?

Hello World demo - What will we see? § Only g. RPC § Reactive

Hello World demo - What will we see? § Only g. RPC § Reactive g. RPC § Protobuf IDL § Protoc code generator § Single<T> and Flowable<T>

Demo summary § Hello. proto defines operations and data structures § Protoc compiler generates

Demo summary § Hello. proto defines operations and data structures § Protoc compiler generates java stubs from proto during build § Client and server build on generated stubs § Reactive-g. RPC generates Rx. Java stubs, extends protoc compiler § Rx. Java makes writing streaming services easy

Questions so far?

Questions so far?

Backpressure § How downstream consumers signal to producers to slow down https: //cheezburger. com/7944963840

Backpressure § How downstream consumers signal to producers to slow down https: //cheezburger. com/7944963840

Three Backpressure Strategies Reactive g. RPC

Three Backpressure Strategies Reactive g. RPC

Producer? Consumer?

Producer? Consumer?

HTTP/2 backpressure and flow control § Stream-based flow control – per stream and per

HTTP/2 backpressure and flow control § Stream-based flow control – per stream and per connection § Initial flow control window size 65, 535 bytes § Producer stops when window closes § Consumer asks for more bytes by sending a WINDOW_UPDATE frame Flow Control Window Producer BYTES WINDOW_UPDATE Flow Control Window Consumer

g. RPC backpressure and flow control § Message-based flow control over HTTP/2 § Producer

g. RPC backpressure and flow control § Message-based flow control over HTTP/2 § Producer - On. Ready. Handler produces and serializes proto messages until HTTP/2 flow control window is exhausted or there are no more messages to send - WINDOW_UPDATE frames from consumer re-trigger On. Ready. Handler Producer On. Ready Handler on. Next(message) Ready? HTTP/2 Flow Control Window BYTES WINDOW_UPDATE Flow Control Window

g. RPC backpressure and flow control § Message-based flow control over HTTP/2 § Consumer

g. RPC backpressure and flow control § Message-based flow control over HTTP/2 § Consumer - Consumer calls request() - g. RPC deserializes messages and calls on. Next() - g. RPC sends WINDOW_UPDATE frames to receive more bytes Producer On. Ready Handler on. Next(message) Consumer on. Next(message) Ready? HTTP/2 Flow Control Window BYTES WINDOW_UPDATE Flow Control Window request()

Rx. Java backpressure and flow control § Message-based flow control over Java method invocations

Rx. Java backpressure and flow control § Message-based flow control over Java method invocations § Consumer calls request() § Producer calls on. Next() § Intermediate operators propagate both calls Producer Observable request() on. Next() Operator Map request() on. Next() Operator Reduce request() on. Next() Consumer Observer

Reactive-g. RPC backpressure and flow control § Message-based flow control over Rx. Java over

Reactive-g. RPC backpressure and flow control § Message-based flow control over Rx. Java over g. RPC over HTTP/2 Rx. Java Observer Observable on. Next() request() Operator on. Next() request() g. RPC On. Ready Handler on. Next() Consumer on. Next() Ready? HTTP/2 Flow Control Window request()

Reactive-g. RPC backpressure and flow control § Message-based flow control over Rx. Java over

Reactive-g. RPC backpressure and flow control § Message-based flow control over Rx. Java over g. RPC over HTTP/2 Rx. Java Observer Observable on. Next() request() Operator on. Next() request() g. RPC On. Ready Handler on. Next() Consumer on. Next() Ready? HTTP/2 Flow Control Window https: //imgur. com/gallery/k. W 0 tz. FN request()

Questions so far?

Questions so far?

Backpressure demo – What will we see? § Slow consumer § Fast producer

Backpressure demo – What will we see? § Slow consumer § Fast producer

Demo summary § Fast producer § Slow consumer § Producer outpaces consumer § Producer

Demo summary § Fast producer § Slow consumer § Producer outpaces consumer § Producer slows down § Backpressure is free (for you)

Questions so far?

Questions so far?

Applied Reactive-g. RPC demo – What will we see? § IRC-like Chat § Command

Applied Reactive-g. RPC demo – What will we see? § IRC-like Chat § Command line § Java. FX Desktop § Android Mobile

Applied Reactive-g. RPC demo – What will we see? Server § Chat. Message ➜

Applied Reactive-g. RPC demo – What will we see? Server § Chat. Message ➜ Empty § Empty ➜ stream Chat. Message

Demo summary § Reactive-g. RPC make chat server trivial to write § Three different

Demo summary § Reactive-g. RPC make chat server trivial to write § Three different clients, nearly identical code - Command line - Java. FX - Android

To Conclude § g. RPC makes microservices easy -. proto IDL - Code generation

To Conclude § g. RPC makes microservices easy -. proto IDL - Code generation § Rx. Java is a different way of thinking - Streams of events - Function composition § Reactive-g. RPC bridges both technologies

Why Reactive-g. RPC? Async by default

Why Reactive-g. RPC? Async by default

Why Reactive-g. RPC? Unified programming model

Why Reactive-g. RPC? Unified programming model

Why Reactive-g. RPC? Simplified programming model

Why Reactive-g. RPC? Simplified programming model

Why Reactive-g. RPC? Consistent programming model

Why Reactive-g. RPC? Consistent programming model

Thank you! § Resources - https: //grpc. io/ - http: //reactivex. io/ - https:

Thank you! § Resources - https: //grpc. io/ - http: //reactivex. io/ - https: //github. com/salesforce/reactive-grpc - �� Demo code Questions?