if you are not ebay 5 reasons to use reactive programming reasons to use...when i (personally) would...

38
5 reasons to use Reactive Programming if you are not eBay Grygoriy Gonchar Software Architect eBay Classifieds Group Motors Vertical @ggonchar @ebaytechberlin

Upload: others

Post on 16-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

5 reasons to use Reactive Programmingif you are not eBayGrygoriy GoncharSoftware ArchitecteBay Classifieds Group Motors Vertical@ggonchar @ebaytechberlin

Page 2: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

177 million monthly buyers >250 million monthly users

Page 3: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Page 4: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Reactive Programming

Page 5: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Reactive Systems

Responsive

Elastic Resilient

Message-Driven

Page 6: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Examplepublic Listing getListing(String id) {

Listing listing = repository.findOne(id);

List<Review> reviews = reviewsService.getSellerReviews(listing.sellerId());

listing.setReviews(reviews)

return listing;

}

Page 7: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Project Reactor

Mono [ 0 | 1 | Err ] like CompletableFuture

Flux [ N | Err ] like Stream with Backpressure

Page 8: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Examplepublic Listing getListing(String id) {

Listing listing = repository.findOne(id);

List<Review> reviews = reviewsService.getSellerReviews(listing.sellerId());

listing.setReviews(reviews)

return listing;

}

// reactive

public Mono<Listing> getListingMono(String id) {

return repository.findOne(id).flatMap(listing -> {

reviewsService.getSellerReviews(listing.sellerId()).map(reviews -> {

listing.setReviews(reviews);

return listing;

});

});

}

Page 9: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Reactive Programming

Async & concurrent programming made easy

Eliminates wasteful blocking calls

Page 10: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Functional & Declarative =

More readable ?

Page 11: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Examplepublic Listing getListing(String id) {

Listing listing = repository.findOne(id);

List<Review> reviews = reviewsService.getSellerReviews(listing.sellerId());

listing.setReviews(reviews)

return listing;

}

// reactive

public Mono<Listing> getListingMono(String id) {

return repository.findOne(id).flatMap(listing -> {

reviewsService.getSellerReviews(listing.sellerId()).map(reviews -> {

listing.setReviews(reviews);

return listing;

});

});

}

Page 12: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Debugging...

Page 13: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Blocking = Slow ?

Page 14: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

How expensive is blocking

Memory overhead: 256k+ of RAM per thread

Context switch overhead: e.g. Tomcat has 200 maxThreads by default

Page 15: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Threads Overhead

http://weblogs.java.net/blog/editor/archive/2012/02/29/look-java-thread-overhead

Page 16: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Reactive programming has its value and its price

Page 17: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

5 reasons to use Reactive ProgrammingIf you are not ebay.com

Page 18: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Use-Case:High-latency IO

Page 19: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

IO latency

200 Threads 100ms latency 2000 req/sec

200 Threads 1000ms latency 200 req/sec

Page 20: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

High-latency example from kijiji autos CA

ext. servicegateway

Topic #1

Topic #2

ext.service

500-1000ms

1

2

3

Page 21: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

External call examplekafkaReceiver.receive().doOnNext(receiver -> {

externalPriceService.getPriceRatings(listing).subscribe(ratings -> {

kafkaProducer.publish(ratings);

receiver.receiverOffset().acknowledge();

}

});

Page 22: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Use-Case:CPU bounded workflows

Page 23: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

How fast is ForkJoin Executor

http://letitcrash.com/post/17607272336/scalability-of-fork-join-pool

Page 24: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Stream processing example from Gumtree UK, Motor Talk DE

Reactive Stream

Page 25: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Stream processing exampleFlux.from(listingRepository)

.filter(Listing::isVisible)

.parallel().runOn(Schedulers.parallel())

.map(this::trackReplyActivity)

.map(this::trackReviewActivity);

Page 26: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Back-pressure and other features

Page 27: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Back-Pressure

SubscriberPublisher

Data

Demand

Page 28: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Stream processing exampleFlux.from(listingRepository)

.buffer(3000) //communicating demand

.filter(Listing::isVisible)

.parallel().runOn(Schedulers.parallel())

.map(this::trackReplyActivity)

.map(this::trackReviewActivity);

Page 29: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Use-Case:Async and parallel requests

Page 30: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Conversation Service Example from mobile.de

ConversationService

ListingService

Fraud Detect.Service

1

2

CentralAnalytics

3

PushNotification

4

Page 31: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Conversation Service Examplevoid createConversation(Message message) {

Mono.zip(

getListingDetails(message.listingId()),

screenForFraud(message)

)

.timeout(Duration.ofSeconds(1))

.map(this::notifyCentralAnalyticsAsync)

.map(this::sendPushNotificationAsync)

.subscribe(this::recordMessage, this::handleFailure);

}

Page 32: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Android BFF Example from Gumtree UK

RxJavaAndroid App

RxJava BFF

Service 1

Service 2

Service N

Page 33: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Client-side reactivity

Quite popular over eBay Classifieds in mobile development

Happens across eBay Classifieds on client-side JavaScript

Page 34: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Reactive programming complements async scenarios

Page 35: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Use-cases for reactive programmingHigh-latency IO

CPU bound workload with some IO

Batch and stream processing

Parallel and async programming

Client-side development

Page 36: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

What might be a good candidate for reactivity

listings

chat search

API Gateway

analytics ext.service gateway

content

payments

Android iOS web

Page 37: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

When I (personally) would NOT use Reactive ProgrammingSequential business process with many low-latency IO calls

Sequential business process with blocking IO

When some extra hardware solves it cheaper

When “green thread” approach is a better fit (Quasar, Kotlin Coroutines)

Page 38: if you are not eBay 5 reasons to use Reactive Programming reasons to use...When I (personally) would NOT use Reactive Programming Sequential business process with many low-latency

@ggonchar @ebaytechberlin

Thank you!Jake Hall @ eBay Classifieds

Larry McKenzie @ eBay Classifieds

Stanislav Kindiakov @ mobile.de

Dev Doongoor @ kijiji

Peter Wildsmith @ Gumtree

Tony Murphy @ Gumtree

Cedric Kalista @ Gumtree

Marek Kulon @ Gumtree

Florian Stefan @ eBay Kleinanzeigen

Anja Kunkel @ Motor-Talk

For more insides https://ebaytech.berlin/