SlideShare a Scribd company logo
1 of 32
Reactive Programming with Examples 
London Java Community and Skills Matter eXchange. 
Thursday 20th November 2014 
Peter Lawrey, CEO 
Higher Frequency Trading Ltd.
Agenda 
• What is Reactive Programming? 
• History behind reactive programming 
• What are the traits of reactive programming? 
• Reactive design with state machines.
Reactive means 
Reactive 
a) Readily response to a stimulus. 
-- merriam-webster.com
Reactive means … 
Reactive 
a) Readily response to a stimulus. 
b) Occurring as a result of stress 
or emotional upset. 
-- merriam-webster.com
What is Reactive Programming? 
“In computing, reactive programming is 
a programming paradigm oriented around data 
flows and the propagation of change.” – 
Wikipedia. 
Reactive Systems “are Responsive, Resilient, 
Elastic and Message Driven” – Reactive 
Manifesto.
What is Reactive Programming? 
Reactive Programming and Design is a higher level 
description of the flow of data rather than dealing 
with individual elements or events. 
Map<String, List<Position>> positionBySymbol = 
positions.values().stream() 
.filter(p -> p.getQuantity() != 0) 
.collect(groupingBy(Position::getSymbol));
What Reactive Programming isn’t? 
Procedural Programming 
Polling to check what has changed 
e.g. ad hoc queries. 
Same as event driven programming. 
Same as functional programming
In the beginning there was the Callback 
• Function pointers used in assembly, C and others. 
• Could specify code to call when something changed 
(Event driven) 
• Could specify code to inject to perform an action 
void qsort(void* field, 
size_t nElements, 
size_t sizeOfAnElement, 
int(_USERENTRY *cmpFunc)(const void*, const void*));
Model View Controller architecture 
1970s and 1980s 
• First used in the 1970s by Xerox Parc by Trygve 
Reenskaug. 
• Added to smalltalk-80 with almost no documentation 
• "A Cookbook for Using the Model-View-Controller User 
Interface Paradigm in Smalltalk -80", by Glenn Krasner 
and Stephen Pope in Aug/Sep 1988. 
• Event driven design.
Embedded SQL (1989) 
• Compiler extension to allow SQL to be written in C, C++, 
Fortran, Ada, Pascal, PL/1, COBOL. 
for (;;) { 
EXEC SQL fetch democursor; 
if (strncmp(SQLSTATE, "00", 2) != 0) 
break; 
printf("%s %sn",fname, lname); 
} 
if (strncmp(SQLSTATE, "02", 2) != 0) 
printf("SQLSTATE after fetch is %sn", SQLSTATE); 
EXEC SQL close democursor; 
EXEC SQL free democursor;
Gang of Four, Observer pattern (1994) 
• Described Observerables and Observers. 
• Focuses on event driven, not streams. 
• Added to Java in 1996. 
• No manipulation of observerables. 
Observable o = new Observable(); 
o.addObservable(new MyObserver()); 
o.notifyObservers(new MyEvent());
InputStream/OutputStream in Java (1996) 
• Construct new streams by wrapping streams 
• Socket streams were event driven. 
• TCP/UDP inherently asynchronous. 
• Very low level byte manipulation 
InputStream is = socket.getInputStream(); 
InputStream zipped = new GZIPInputStream(is); 
InputStream objects = new ObjectInputStream(zipped); 
Object o = objects.readObject();
Staged Event-Driven Architecture (2000) 
• Based on a paper by Matt Welsh 
• “Highly Concurrent Server Applications” 
• A set of event driven stages separated by queues. 
• Libraries to support SEDA have been added.
Reactive Extensions in .NET 2009 
• Built on LINQ added in 2007. 
• Combines Observable + LINQ + Thread pools 
• Functional manipulation of streams of data. 
• High level interface. 
var customers = new ObservableCollection<Customer>(); 
var customerChanges = Observable.FromEventPattern( 
(EventHandler<NotifyCollectionChangedEventArgs> ev) 
=> new NotifyCollectionChangedEventHandler(ev), 
ev => customers.CollectionChanged += ev, 
ev => customers.CollectionChanged -= ev);
Reactive Extensions in .NET (cont) 
var watchForNewCustomersFromWashington = 
from c in customerChanges 
where c.EventArgs.Action == NotifyCollectionChangedAction.Add 
from cus in c.EventArgs.NewItems.Cast<Customer>().ToObservable() 
where cus.Region == "WA" 
select cus; 
watchForNewCustomersFromWashington.Subscribe(cus => { 
Console.WriteLine("Customer {0}:", cus.CustomerName); 
foreach (var order in cus.Orders) { 
Console.WriteLine("Order {0}: {1}", order.OrderId, 
order.OrderDate); 
} 
});
• library for composing asynchronous and event-based programs by 
using observable sequences. 
• It extends the observer pattern to support sequences of data/events 
and adds operators that allow you to compose sequences together 
declaratively 
• abstracting away concerns about things like low-level threading, 
synchronization, thread-safety, concurrent data structures, and non-blocking 
I/O 
RxJava 
Observable.from(names).subscribe(new Action1<String>() { 
@Override 
public void call(String s) { 
System.out.println("Hello " + s + "!"); 
} 
});
Akka Framework 
• process messages asynchronously using an event-driven receive loop 
• raise the abstraction level and make it much easier to write, test, 
understand and maintain concurrent and/or distributed systems 
• focus on workflow—how the messages flow in the system—instead of 
low level primitives like threads, locks and socket IO 
case class Greeting(who: String) 
class GreetingActor extends Actor with ActorLogging { 
def receive = { 
case Greeting(who) ⇒ log.info("Hello " + who) 
} 
} 
val system = ActorSystem("MySystem") 
val greeter = system.actorOf(Props[GreetingActor], name = "greeter") 
greeter ! Greeting("Charlie Parker")
Reactor Framework 
• a foundation for asynchronous applications on the JVM. 
• make building event and data-driven applications easier 
• process around 15,000,000 events per second 
• Uses Chronicle Queue for a persisted queue 
// U() is a static helper method to create a UriTemplateSelector 
reactor.on(U("/topic/{name}"), ev -> { 
String name = ev.getHeaders().get("name"); 
// process the message 
});
Reactive System traits 
• Responsive – React in a timely manner 
respond with reliable latencies. 
• Resilient – React to failure, 
handle failure well instead of trying to prevent them 
• Elastic – React to load 
• Message Driven – React to events. 
See the Reactive Manifesto for more details
Messages, Event Driven, Actors 
• A message is a self contain piece of information 
• Messaging systems are concerned about how they are 
delivered, rather than what they contain. 
• A messaging system has a header for meta information.
Messages, Event Driven, Actors 
• Events state what has happened. They are associated with the 
source of an event and need not have a listener. 
• The fact an event happened doesn’t imply an action to take. 
• Similar to Publish/Subscribe messaging. 
• Lose coupling between producer and consumer. 
• Can have multiple consumers for the same event.
Messages, Event Driven, Actors 
• Actors-based messages are commands to be executed by a 
specific target. Actor-based messages imply an action to take 
as well as who should take it. 
• It usually doesn’t have a reason, or trigger associated with it. 
• Similar to asynchronous Point-to-point or Request/Reply 
messaging. 
• Tighter coupling between the producer and an actor.
Reactive principles 
• Avoid blocking on IO (or anything else) use futures 
• Pass blocking tasks to supporting thread. 
• Monitor your core threads to report any delays and their cause. 
E.g. take a stack trace if your event loop takes more than 5 ms. 
• Avoid holding locks (ideally avoid locks) 
• Pre-build your listener layout. Don’t dynamically add/remove 
listeners. Create a structure which is basically static in layout.
Reactive principles – don’t forget testing 
• Reproducable inputs and load. Complete replayability 
• Deterministic behavior, diagnose rare bug in stateful components. 
• Controlled timings, diagnose rare timing issues.
Reactive Performance 
• Event Driven programming improves latency on average and 
worst timings, sometimes at the cost to throughput. 
• There is ways to tune event driven systems to handle bursts in 
load which start to look more procedural. 
• Reactive systems should be performant so they are relatively 
lightly loaded, so they can always be ready to react. 
If you have to respond in 20 ms or 200 μs, you want this to be 
the 99%tile or 99.99%tile latency not the average latency.
Performance considerations 
• Micro burst activity. A system which experiences micro bursts is 
not 1% busy, its 100% busy 1% of the time. 
• Eventual consistency vs strong consistency 
• Process every event, or just the latest state. 
By taking the latest state you can absorb high bursts in load. 
• Reactive systems which is relatively lightly loaded, so they can 
always be ready to react.
Functional Reactive Quality 
• Improves quality of code, esp for more junior developers. 
An Empirical Study on Program Comprehension 
with Reactive Programming – Guido Salvaneschi
Functional Reactive Programming 
• No mutable state 
• Easy to reason about 
• Easy to componentize 
• But … no mutable state.
State Machines 
• Local mutable state 
• Easier to reason about, than shared state 
• Easier to componentize 
• Not as simple as FRP.
FRP with a State Machine 
• Minimum of local mutable state 
• Easier to reason about, than shared state 
• Easier to componentize
A typical trading system
Reactive means always being ready. 
Questions and answers 
Peter Lawrey 
@PeterLawrey 
http://higherfrequencytrading.com

More Related Content

What's hot

Reactive programming
Reactive programmingReactive programming
Reactive programmingSUDIP GHOSH
 
Understanding Reactive Programming
Understanding Reactive ProgrammingUnderstanding Reactive Programming
Understanding Reactive ProgrammingAndres Almiray
 
Concurrent Programming Using the Disruptor
Concurrent Programming Using the DisruptorConcurrent Programming Using the Disruptor
Concurrent Programming Using the DisruptorTrisha Gee
 
Monitoring MySQL Replication lag with Prometheus & pt-heartbeat
Monitoring MySQL Replication lag with Prometheus & pt-heartbeatMonitoring MySQL Replication lag with Prometheus & pt-heartbeat
Monitoring MySQL Replication lag with Prometheus & pt-heartbeatJulien Pivotto
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomyDongmin Yu
 
Distributed applications using Hazelcast
Distributed applications using HazelcastDistributed applications using Hazelcast
Distributed applications using HazelcastTaras Matyashovsky
 
Building a Streaming Microservice Architecture: with Apache Spark Structured ...
Building a Streaming Microservice Architecture: with Apache Spark Structured ...Building a Streaming Microservice Architecture: with Apache Spark Structured ...
Building a Streaming Microservice Architecture: with Apache Spark Structured ...Databricks
 
High Frequency Trading and NoSQL database
High Frequency Trading and NoSQL databaseHigh Frequency Trading and NoSQL database
High Frequency Trading and NoSQL databasePeter Lawrey
 
Running Kafka as a Native Binary Using GraalVM with Ozan Günalp
Running Kafka as a Native Binary Using GraalVM with Ozan GünalpRunning Kafka as a Native Binary Using GraalVM with Ozan Günalp
Running Kafka as a Native Binary Using GraalVM with Ozan GünalpHostedbyConfluent
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootKashif Ali Siddiqui
 
From Mainframe to Microservice: An Introduction to Distributed Systems
From Mainframe to Microservice: An Introduction to Distributed SystemsFrom Mainframe to Microservice: An Introduction to Distributed Systems
From Mainframe to Microservice: An Introduction to Distributed SystemsTyler Treat
 
Reactive Programming for Real Use Cases
Reactive Programming for Real Use CasesReactive Programming for Real Use Cases
Reactive Programming for Real Use CasesAlex Soto
 
Webinar slides: An Introduction to Performance Monitoring for PostgreSQL
Webinar slides: An Introduction to Performance Monitoring for PostgreSQLWebinar slides: An Introduction to Performance Monitoring for PostgreSQL
Webinar slides: An Introduction to Performance Monitoring for PostgreSQLSeveralnines
 
Introduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQIntroduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQDmitriy Samovskiy
 
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018Scrum Breakfast Vietnam
 
Tips and Tricks for SAP Sybase ASE
Tips and Tricks for SAP Sybase ASETips and Tricks for SAP Sybase ASE
Tips and Tricks for SAP Sybase ASEDon Brizendine
 
RocksDB compaction
RocksDB compactionRocksDB compaction
RocksDB compactionMIJIN AN
 
Low level java programming
Low level java programmingLow level java programming
Low level java programmingPeter Lawrey
 

What's hot (20)

Reactive programming
Reactive programmingReactive programming
Reactive programming
 
Understanding Reactive Programming
Understanding Reactive ProgrammingUnderstanding Reactive Programming
Understanding Reactive Programming
 
Introduction to Aerospike
Introduction to AerospikeIntroduction to Aerospike
Introduction to Aerospike
 
Concurrent Programming Using the Disruptor
Concurrent Programming Using the DisruptorConcurrent Programming Using the Disruptor
Concurrent Programming Using the Disruptor
 
Monitoring MySQL Replication lag with Prometheus & pt-heartbeat
Monitoring MySQL Replication lag with Prometheus & pt-heartbeatMonitoring MySQL Replication lag with Prometheus & pt-heartbeat
Monitoring MySQL Replication lag with Prometheus & pt-heartbeat
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
Distributed applications using Hazelcast
Distributed applications using HazelcastDistributed applications using Hazelcast
Distributed applications using Hazelcast
 
Building a Streaming Microservice Architecture: with Apache Spark Structured ...
Building a Streaming Microservice Architecture: with Apache Spark Structured ...Building a Streaming Microservice Architecture: with Apache Spark Structured ...
Building a Streaming Microservice Architecture: with Apache Spark Structured ...
 
High Frequency Trading and NoSQL database
High Frequency Trading and NoSQL databaseHigh Frequency Trading and NoSQL database
High Frequency Trading and NoSQL database
 
Running Kafka as a Native Binary Using GraalVM with Ozan Günalp
Running Kafka as a Native Binary Using GraalVM with Ozan GünalpRunning Kafka as a Native Binary Using GraalVM with Ozan Günalp
Running Kafka as a Native Binary Using GraalVM with Ozan Günalp
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring Boot
 
From Mainframe to Microservice: An Introduction to Distributed Systems
From Mainframe to Microservice: An Introduction to Distributed SystemsFrom Mainframe to Microservice: An Introduction to Distributed Systems
From Mainframe to Microservice: An Introduction to Distributed Systems
 
GraalVM
GraalVMGraalVM
GraalVM
 
Reactive Programming for Real Use Cases
Reactive Programming for Real Use CasesReactive Programming for Real Use Cases
Reactive Programming for Real Use Cases
 
Webinar slides: An Introduction to Performance Monitoring for PostgreSQL
Webinar slides: An Introduction to Performance Monitoring for PostgreSQLWebinar slides: An Introduction to Performance Monitoring for PostgreSQL
Webinar slides: An Introduction to Performance Monitoring for PostgreSQL
 
Introduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQIntroduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQ
 
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
 
Tips and Tricks for SAP Sybase ASE
Tips and Tricks for SAP Sybase ASETips and Tricks for SAP Sybase ASE
Tips and Tricks for SAP Sybase ASE
 
RocksDB compaction
RocksDB compactionRocksDB compaction
RocksDB compaction
 
Low level java programming
Low level java programmingLow level java programming
Low level java programming
 

Viewers also liked

Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Peter Lawrey
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5Peter Lawrey
 
GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconPeter Lawrey
 
Low latency for high throughput
Low latency for high throughputLow latency for high throughput
Low latency for high throughputPeter Lawrey
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaKasun Indrasiri
 

Viewers also liked (6)

Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016
 
Introduction to Reactive Java
Introduction to Reactive JavaIntroduction to Reactive Java
Introduction to Reactive Java
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5
 
GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @Geecon
 
Low latency for high throughput
Low latency for high throughputLow latency for high throughput
Low latency for high throughput
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
 

Similar to Reactive programming with examples

20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the tradeshinolajla
 
Springone2gx 2014 Reactive Streams and Reactor
Springone2gx 2014 Reactive Streams and ReactorSpringone2gx 2014 Reactive Streams and Reactor
Springone2gx 2014 Reactive Streams and ReactorStéphane Maldini
 
Nelson: Rigorous Deployment for a Functional World
Nelson: Rigorous Deployment for a Functional WorldNelson: Rigorous Deployment for a Functional World
Nelson: Rigorous Deployment for a Functional WorldTimothy Perrett
 
Akka london scala_user_group
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_groupSkills Matter
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13Dave Gardner
 
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017Codemotion
 
Performance Test Driven Development with Oracle Coherence
Performance Test Driven Development with Oracle CoherencePerformance Test Driven Development with Oracle Coherence
Performance Test Driven Development with Oracle Coherencearagozin
 
Planning to Fail #phpuk13
Planning to Fail #phpuk13Planning to Fail #phpuk13
Planning to Fail #phpuk13Dave Gardner
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Building large scale, job processing systems with Scala Akka Actor framework
Building large scale, job processing systems with Scala Akka Actor frameworkBuilding large scale, job processing systems with Scala Akka Actor framework
Building large scale, job processing systems with Scala Akka Actor frameworkVignesh Sukumar
 
Reactive Streams - László van den Hoek
Reactive Streams - László van den HoekReactive Streams - László van den Hoek
Reactive Streams - László van den HoekRubiX BV
 
Multi-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and QuasarMulti-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and QuasarGal Marder
 
Beyond Fault Tolerance with Actor Programming
Beyond Fault Tolerance with Actor ProgrammingBeyond Fault Tolerance with Actor Programming
Beyond Fault Tolerance with Actor ProgrammingFabio Tiriticco
 
Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...
Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...
Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...Codemotion
 
Distributed Performance testing by funkload
Distributed Performance testing by funkloadDistributed Performance testing by funkload
Distributed Performance testing by funkloadAkhil Singh
 
The End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional ManagementThe End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional ManagementRicardo Jimenez-Peris
 
StackWatch: A prototype CloudWatch service for CloudStack
StackWatch: A prototype CloudWatch service for CloudStackStackWatch: A prototype CloudWatch service for CloudStack
StackWatch: A prototype CloudWatch service for CloudStackChiradeep Vittal
 

Similar to Reactive programming with examples (20)

20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the trade
 
Springone2gx 2014 Reactive Streams and Reactor
Springone2gx 2014 Reactive Streams and ReactorSpringone2gx 2014 Reactive Streams and Reactor
Springone2gx 2014 Reactive Streams and Reactor
 
Nelson: Rigorous Deployment for a Functional World
Nelson: Rigorous Deployment for a Functional WorldNelson: Rigorous Deployment for a Functional World
Nelson: Rigorous Deployment for a Functional World
 
Akka london scala_user_group
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_group
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13
 
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
Mario Fusco - Reactive programming in Java - Codemotion Milan 2017
 
Performance Test Driven Development with Oracle Coherence
Performance Test Driven Development with Oracle CoherencePerformance Test Driven Development with Oracle Coherence
Performance Test Driven Development with Oracle Coherence
 
Planning to Fail #phpuk13
Planning to Fail #phpuk13Planning to Fail #phpuk13
Planning to Fail #phpuk13
 
Scale up your thinking
Scale up your thinkingScale up your thinking
Scale up your thinking
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Building large scale, job processing systems with Scala Akka Actor framework
Building large scale, job processing systems with Scala Akka Actor frameworkBuilding large scale, job processing systems with Scala Akka Actor framework
Building large scale, job processing systems with Scala Akka Actor framework
 
Sharing-akka-pub
Sharing-akka-pubSharing-akka-pub
Sharing-akka-pub
 
Reactive Streams - László van den Hoek
Reactive Streams - László van den HoekReactive Streams - László van den Hoek
Reactive Streams - László van den Hoek
 
Multi-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and QuasarMulti-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and Quasar
 
Beyond Fault Tolerance with Actor Programming
Beyond Fault Tolerance with Actor ProgrammingBeyond Fault Tolerance with Actor Programming
Beyond Fault Tolerance with Actor Programming
 
Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...
Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...
Beyond fault tolerance with actor programming - Fabio Tiriticco - Codemotion ...
 
Distributed Performance testing by funkload
Distributed Performance testing by funkloadDistributed Performance testing by funkload
Distributed Performance testing by funkload
 
The End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional ManagementThe End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional Management
 
StackWatch: A prototype CloudWatch service for CloudStack
StackWatch: A prototype CloudWatch service for CloudStackStackWatch: A prototype CloudWatch service for CloudStack
StackWatch: A prototype CloudWatch service for CloudStack
 

More from Peter Lawrey

Chronicle accelerate building a digital currency
Chronicle accelerate   building a digital currencyChronicle accelerate   building a digital currency
Chronicle accelerate building a digital currencyPeter Lawrey
 
Chronicle Accelerate Crypto Investor conference
Chronicle Accelerate Crypto Investor conferenceChronicle Accelerate Crypto Investor conference
Chronicle Accelerate Crypto Investor conferencePeter Lawrey
 
Deterministic behaviour and performance in trading systems
Deterministic behaviour and performance in trading systemsDeterministic behaviour and performance in trading systems
Deterministic behaviour and performance in trading systemsPeter Lawrey
 
Determinism in finance
Determinism in financeDeterminism in finance
Determinism in financePeter Lawrey
 
Legacy lambda code
Legacy lambda codeLegacy lambda code
Legacy lambda codePeter Lawrey
 
Responding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaResponding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaPeter Lawrey
 
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyStreams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyPeter Lawrey
 
Advanced off heap ipc
Advanced off heap ipcAdvanced off heap ipc
Advanced off heap ipcPeter Lawrey
 
Open HFT libraries in @Java
Open HFT libraries in @JavaOpen HFT libraries in @Java
Open HFT libraries in @JavaPeter Lawrey
 
Introduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupIntroduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupPeter Lawrey
 
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)Peter Lawrey
 
Using BigDecimal and double
Using BigDecimal and doubleUsing BigDecimal and double
Using BigDecimal and doublePeter Lawrey
 
Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Peter Lawrey
 

More from Peter Lawrey (13)

Chronicle accelerate building a digital currency
Chronicle accelerate   building a digital currencyChronicle accelerate   building a digital currency
Chronicle accelerate building a digital currency
 
Chronicle Accelerate Crypto Investor conference
Chronicle Accelerate Crypto Investor conferenceChronicle Accelerate Crypto Investor conference
Chronicle Accelerate Crypto Investor conference
 
Deterministic behaviour and performance in trading systems
Deterministic behaviour and performance in trading systemsDeterministic behaviour and performance in trading systems
Deterministic behaviour and performance in trading systems
 
Determinism in finance
Determinism in financeDeterminism in finance
Determinism in finance
 
Legacy lambda code
Legacy lambda codeLegacy lambda code
Legacy lambda code
 
Responding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaResponding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in Java
 
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyStreams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
 
Advanced off heap ipc
Advanced off heap ipcAdvanced off heap ipc
Advanced off heap ipc
 
Open HFT libraries in @Java
Open HFT libraries in @JavaOpen HFT libraries in @Java
Open HFT libraries in @Java
 
Introduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupIntroduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users Group
 
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
 
Using BigDecimal and double
Using BigDecimal and doubleUsing BigDecimal and double
Using BigDecimal and double
 
Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)
 

Recently uploaded

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Recently uploaded (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

Reactive programming with examples

  • 1. Reactive Programming with Examples London Java Community and Skills Matter eXchange. Thursday 20th November 2014 Peter Lawrey, CEO Higher Frequency Trading Ltd.
  • 2. Agenda • What is Reactive Programming? • History behind reactive programming • What are the traits of reactive programming? • Reactive design with state machines.
  • 3. Reactive means Reactive a) Readily response to a stimulus. -- merriam-webster.com
  • 4. Reactive means … Reactive a) Readily response to a stimulus. b) Occurring as a result of stress or emotional upset. -- merriam-webster.com
  • 5. What is Reactive Programming? “In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change.” – Wikipedia. Reactive Systems “are Responsive, Resilient, Elastic and Message Driven” – Reactive Manifesto.
  • 6. What is Reactive Programming? Reactive Programming and Design is a higher level description of the flow of data rather than dealing with individual elements or events. Map<String, List<Position>> positionBySymbol = positions.values().stream() .filter(p -> p.getQuantity() != 0) .collect(groupingBy(Position::getSymbol));
  • 7. What Reactive Programming isn’t? Procedural Programming Polling to check what has changed e.g. ad hoc queries. Same as event driven programming. Same as functional programming
  • 8. In the beginning there was the Callback • Function pointers used in assembly, C and others. • Could specify code to call when something changed (Event driven) • Could specify code to inject to perform an action void qsort(void* field, size_t nElements, size_t sizeOfAnElement, int(_USERENTRY *cmpFunc)(const void*, const void*));
  • 9. Model View Controller architecture 1970s and 1980s • First used in the 1970s by Xerox Parc by Trygve Reenskaug. • Added to smalltalk-80 with almost no documentation • "A Cookbook for Using the Model-View-Controller User Interface Paradigm in Smalltalk -80", by Glenn Krasner and Stephen Pope in Aug/Sep 1988. • Event driven design.
  • 10. Embedded SQL (1989) • Compiler extension to allow SQL to be written in C, C++, Fortran, Ada, Pascal, PL/1, COBOL. for (;;) { EXEC SQL fetch democursor; if (strncmp(SQLSTATE, "00", 2) != 0) break; printf("%s %sn",fname, lname); } if (strncmp(SQLSTATE, "02", 2) != 0) printf("SQLSTATE after fetch is %sn", SQLSTATE); EXEC SQL close democursor; EXEC SQL free democursor;
  • 11. Gang of Four, Observer pattern (1994) • Described Observerables and Observers. • Focuses on event driven, not streams. • Added to Java in 1996. • No manipulation of observerables. Observable o = new Observable(); o.addObservable(new MyObserver()); o.notifyObservers(new MyEvent());
  • 12. InputStream/OutputStream in Java (1996) • Construct new streams by wrapping streams • Socket streams were event driven. • TCP/UDP inherently asynchronous. • Very low level byte manipulation InputStream is = socket.getInputStream(); InputStream zipped = new GZIPInputStream(is); InputStream objects = new ObjectInputStream(zipped); Object o = objects.readObject();
  • 13. Staged Event-Driven Architecture (2000) • Based on a paper by Matt Welsh • “Highly Concurrent Server Applications” • A set of event driven stages separated by queues. • Libraries to support SEDA have been added.
  • 14. Reactive Extensions in .NET 2009 • Built on LINQ added in 2007. • Combines Observable + LINQ + Thread pools • Functional manipulation of streams of data. • High level interface. var customers = new ObservableCollection<Customer>(); var customerChanges = Observable.FromEventPattern( (EventHandler<NotifyCollectionChangedEventArgs> ev) => new NotifyCollectionChangedEventHandler(ev), ev => customers.CollectionChanged += ev, ev => customers.CollectionChanged -= ev);
  • 15. Reactive Extensions in .NET (cont) var watchForNewCustomersFromWashington = from c in customerChanges where c.EventArgs.Action == NotifyCollectionChangedAction.Add from cus in c.EventArgs.NewItems.Cast<Customer>().ToObservable() where cus.Region == "WA" select cus; watchForNewCustomersFromWashington.Subscribe(cus => { Console.WriteLine("Customer {0}:", cus.CustomerName); foreach (var order in cus.Orders) { Console.WriteLine("Order {0}: {1}", order.OrderId, order.OrderDate); } });
  • 16. • library for composing asynchronous and event-based programs by using observable sequences. • It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively • abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O RxJava Observable.from(names).subscribe(new Action1<String>() { @Override public void call(String s) { System.out.println("Hello " + s + "!"); } });
  • 17. Akka Framework • process messages asynchronously using an event-driven receive loop • raise the abstraction level and make it much easier to write, test, understand and maintain concurrent and/or distributed systems • focus on workflow—how the messages flow in the system—instead of low level primitives like threads, locks and socket IO case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) ⇒ log.info("Hello " + who) } } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker")
  • 18. Reactor Framework • a foundation for asynchronous applications on the JVM. • make building event and data-driven applications easier • process around 15,000,000 events per second • Uses Chronicle Queue for a persisted queue // U() is a static helper method to create a UriTemplateSelector reactor.on(U("/topic/{name}"), ev -> { String name = ev.getHeaders().get("name"); // process the message });
  • 19. Reactive System traits • Responsive – React in a timely manner respond with reliable latencies. • Resilient – React to failure, handle failure well instead of trying to prevent them • Elastic – React to load • Message Driven – React to events. See the Reactive Manifesto for more details
  • 20. Messages, Event Driven, Actors • A message is a self contain piece of information • Messaging systems are concerned about how they are delivered, rather than what they contain. • A messaging system has a header for meta information.
  • 21. Messages, Event Driven, Actors • Events state what has happened. They are associated with the source of an event and need not have a listener. • The fact an event happened doesn’t imply an action to take. • Similar to Publish/Subscribe messaging. • Lose coupling between producer and consumer. • Can have multiple consumers for the same event.
  • 22. Messages, Event Driven, Actors • Actors-based messages are commands to be executed by a specific target. Actor-based messages imply an action to take as well as who should take it. • It usually doesn’t have a reason, or trigger associated with it. • Similar to asynchronous Point-to-point or Request/Reply messaging. • Tighter coupling between the producer and an actor.
  • 23. Reactive principles • Avoid blocking on IO (or anything else) use futures • Pass blocking tasks to supporting thread. • Monitor your core threads to report any delays and their cause. E.g. take a stack trace if your event loop takes more than 5 ms. • Avoid holding locks (ideally avoid locks) • Pre-build your listener layout. Don’t dynamically add/remove listeners. Create a structure which is basically static in layout.
  • 24. Reactive principles – don’t forget testing • Reproducable inputs and load. Complete replayability • Deterministic behavior, diagnose rare bug in stateful components. • Controlled timings, diagnose rare timing issues.
  • 25. Reactive Performance • Event Driven programming improves latency on average and worst timings, sometimes at the cost to throughput. • There is ways to tune event driven systems to handle bursts in load which start to look more procedural. • Reactive systems should be performant so they are relatively lightly loaded, so they can always be ready to react. If you have to respond in 20 ms or 200 μs, you want this to be the 99%tile or 99.99%tile latency not the average latency.
  • 26. Performance considerations • Micro burst activity. A system which experiences micro bursts is not 1% busy, its 100% busy 1% of the time. • Eventual consistency vs strong consistency • Process every event, or just the latest state. By taking the latest state you can absorb high bursts in load. • Reactive systems which is relatively lightly loaded, so they can always be ready to react.
  • 27. Functional Reactive Quality • Improves quality of code, esp for more junior developers. An Empirical Study on Program Comprehension with Reactive Programming – Guido Salvaneschi
  • 28. Functional Reactive Programming • No mutable state • Easy to reason about • Easy to componentize • But … no mutable state.
  • 29. State Machines • Local mutable state • Easier to reason about, than shared state • Easier to componentize • Not as simple as FRP.
  • 30. FRP with a State Machine • Minimum of local mutable state • Easier to reason about, than shared state • Easier to componentize
  • 32. Reactive means always being ready. Questions and answers Peter Lawrey @PeterLawrey http://higherfrequencytrading.com