Stuff I Learned About Performance

56
Stuff I’ve learnt about software performance @mikeb2701 learned

description

Slides from Auckland JVM meetup.

Transcript of Stuff I Learned About Performance

Page 1: Stuff I Learned About Performance

Stuff I’ve learnt about software performance

@mikeb2701

learned

Page 2: Stuff I Learned About Performance

• What is a financial exchange?

• How to lie with numbers

• Design is (almost) everything

• Everything else is about making trade-offs

Page 3: Stuff I Learned About Performance
Page 4: Stuff I Learned About Performance

TL;DR

1. Low Latency

2. ???

3. Profit

Page 5: Stuff I Learned About Performance

Lies, Damed Lies and Performance Numbers

Page 6: Stuff I Learned About Performance

320µs @ 10,000 tx/s

Page 7: Stuff I Learned About Performance

Customer

FixGateway

LoadBalancer

ExecutionVenue

Record Latency

Parse/Encode FIX

Order MatchingReplicationJournalling

Page 8: Stuff I Learned About Performance

320µs @ 10,000 tx/s

Page 9: Stuff I Learned About Performance
Page 10: Stuff I Learned About Performance

320µs @ 10,000 tx/s

Page 11: Stuff I Learned About Performance

http://www.infoq.com/presentations/latency-pitfalls

Page 12: Stuff I Learned About Performance

What did I learn

• Detail Matters

• Don’t oversimplify the behaviour of your system

Page 13: Stuff I Learned About Performance

If we can really understand the problem, the answer will come out of it, because the answer is

not separate from the problem. !

- Jiddu Krishnamurti

Page 14: Stuff I Learned About Performance

Customer

FixGateway

LoadBalancer

ExecutionVenue

Record Latency

Parse/Encode FIX

Order MatchingReplicationJournalling

Page 15: Stuff I Learned About Performance

Instruction

ExecutionReport

Execution Venue

OrderBook

Order

Execution

Page 16: Stuff I Learned About Performance

Execution Venueinstrument: Carrots

CreateOrderBook<Admin Instruction>

Page 17: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

instrument: Carrots

CreateOrderBook<Admin Instruction>

Page 18: Stuff I Learned About Performance

id : ABCinstrument: Carrotsprice : 110quantity : 5side : BID

PlaceOrder<Instruction>

Execution Venue

instrument: Carrots

OrderBook<Entity>

Page 19: Stuff I Learned About Performance

id : ABCinstrument: Carrotsprice : 110quantity : 5side : BID

PlaceOrder<Instruction>

Execution Venue

instrument: Carrots

OrderBook<Entity>

orderId : 1id : ABCprice : 110quantity : 5side : BIDfilled : 0cancelled : 0

Order<Entity>

Page 20: Stuff I Learned About Performance

id : ABCinstrument: Carrotsprice : 110quantity : 5side : BID

PlaceOrder<Instruction>

Execution Venue

instrument: Carrots

OrderBook<Entity>

orderId : 1id : ABCprice : 110quantity : 5side : BIDfilled : 0cancelled : 0

Order<Entity>id : ABC

Accept<ExecutionReport>

instrument: Carrotsprice : 110quantity : 5

LiquidityAdded<ExecutionReport>

Page 21: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

orderId : 1id : ABCprice : 110quantity : 5side : BIDfilled : 0cancelled : 0

Order<Entity>

originalId: ABC

CancelOrder<Instruction>

Page 22: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

originalId: ABC

CancelOrder<Instruction>

id : ABCinstrument: Carrotsprice : 110quantity : 5

Cancelled<ExecutionReport>

orderId : 1id : ABCprice : 110quantity : 5side : BIDfilled : 0cancelled : 5

Order<Entity>

Page 23: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

originalId: ABC

CancelOrder<Instruction>

id : ABCinstrument: Carrotsprice : 110quantity : 5

Cancelled<ExecutionReport>

Page 24: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

orderId : 1id : ABCprice : 110quantity : 5side : BIDfilled : 0cancelled : 0

Order<Entity>

id : DEFinstrument: Carrotsprice : 110quantity : 5side : ASK

PlaceOrder<Instruction>

Page 25: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

id : DEFinstrument: Carrotsprice : 110quantity : 5side : ASK

PlaceOrder<Instruction>

id : DEF

Accept<ExecutionReport>

price : 110quantity : 5aggressive: DEFpassive : ABC

Trade<ExecutionReport>

orderId : 1id : ABCprice : 110quantity : 5side : BIDfilled : 5cancelled : 0

Order<Entity>

Page 26: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

id : DEFinstrument: Carrotsprice : 110quantity : 5side : ASK

PlaceOrder<Instruction>

id : DEF

Accept<ExecutionReport>

price : 110quantity : 5aggressive: DEFpassive : ABC

Trade<ExecutionReport>

Page 27: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

id : ABCinstrument: Bananasprice : 110quantity : 5side : BID

PlaceOrder<Instruction>

Page 28: Stuff I Learned About Performance

Execution Venue

instrument: Carrots

OrderBook<Entity>

id : ABCinstrument: Bananasprice : 110quantity : 5side : BID

PlaceOrder<Instruction>

id : ABC

Reject<ExecutionReport>

Page 29: Stuff I Learned About Performance

Interesting Properties

• Stable working set

• Fits in memory

• Doesn’t not grow monotonically with time

• All decisions are made from local information

Page 30: Stuff I Learned About Performance

Event-Sourced Service

Journalling

Replication

BusinessLogicMessage Message

http://www.infoq.com/presentations/Event-Sourced-Architectures-for-High-Availability

Page 31: Stuff I Learned About Performance

What did I learn• Enough understanding of the problem and

solutions will appear

• Pay attention to:

• Entity Life-cycles

• Number of entities - in real world scenarios

• Try to approach the problem without bias

Page 32: Stuff I Learned About Performance

If you can’t talk about the downside and the trade offs then you’re just a fan person waiving a coffee mug around.  That type of behaviour

really doesn’t benefit anyone. !

- Some guy on the Internet (charmalloc)

Source: http://allthingshadoop.com/2013/12/06/technology-decisions-are-about-trade-offs-and-solving-problems/

Page 33: Stuff I Learned About Performance

Customer

FixGateway

LoadBalancer

ExecutionVenue

Market DataService

10K msg/sec

160K msg/sec

Page 34: Stuff I Learned About Performance

Design for trade-offs

Page 35: Stuff I Learned About Performance

TIMESTAMP,BID_PRICE_1,BID_QTY_1,ASK_PRICE_1,ASK_QTY_1 1405890300827,1.35263,50,1.35296,50 1405890300908,1.35263,50,1.35296,49.9 1405890300909,1.35263,50,1.35296,47.9 1405890301090,1.35263,48.2,1.35296,47.9 1405890301193,1.35263,48.2,1.35296,46.1 1405890301194,1.35263,48.2,1.35296,44.6 1405890301280,1.35263,48.2,1.35296,42.6 1405890301571,1.35263,48.1,1.35296,42.6 1405890303037,1.35263,47.1,1.35296,42.6

Page 36: Stuff I Learned About Performance

List<String[]> lines = ! parseAsList("data.csv");!for (String[] line : lines)!{! double bid = parseDouble(line[1]);! double ask = parseDouble(line[3]);! ! // Stuff...!}!

Page 37: Stuff I Learned About Performance

Iterable<String[]> lines = ! parseAsIterable("data.csv");!for (String[] line : lines)!{! double bid = parseDouble(line[1]);! double ask = parseDouble(line[3]);! ! // Stuff...!}!

Page 38: Stuff I Learned About Performance

public interface CsvObservable!{! default void onError(Exception e)! {! }!! default void onComplete()! {! }!! void onEvent(! int row, int column, ! CharSequence value, boolean endOfLine);!}

Page 39: Stuff I Learned About Performance

parse("data.csv", ! (row, column, value, endOfLine) ->!{! switch (column)! {! case 1:! bid = parseDouble(value);! case 3:! ask = parseDouble(value);! }!! // Stuff...!});

Page 40: Stuff I Learned About Performance

Ops/sec GC Count GC Time (ms)

Iterable 8.20 104 154

Observable 16.75 0 0

Page 41: Stuff I Learned About Performance

public class CsvObservableDelegate ! implements CsvObservable<CharSequence>!{! private CsvObservable<String> delegate;!! public CsvObservableDelegate(CsvObservable<String> delegate)! {! this.delegate = delegate;! }!! public void onEvent(! int row, int column, ! CharSequence value, boolean endOfLine)! {! delegate.onEvent(! row, column, value.toString(), endOfLine);! }!! // Other delegate methods!}!

Page 42: Stuff I Learned About Performance

Garbage CollectionStallin’ your app since 1959

Page 43: Stuff I Learned About Performance
Page 44: Stuff I Learned About Performance
Page 45: Stuff I Learned About Performance

mean 95% 99% 99.99%

JDK6 1.386 1.82 5.75 15.72

JDK7 1.44 1.79 11.75 20.66

Page 46: Stuff I Learned About Performance

Solutions

• Don’t Generate Any • Use Zing (Azul Systems)

Page 47: Stuff I Learned About Performance

Why Use Java? (why not C/C++/..)

Page 48: Stuff I Learned About Performance

Source: https://software.intel.com/en-us/articles/introduction-to-intel-advanced-vector-extensions

Page 49: Stuff I Learned About Performance

public class Doubles !{ ! public static void sum(! double[] sum, ! double[] addendA, ! double[] addendB)! {! for (int i = 0; i < 0 + sum.length; i++)! {! sum[i] = addendA[i] + addendB[i];! }! }!}!

Page 50: Stuff I Learned About Performance

#include <immintrin.h>!!void sum(double* c, double* a, double* b, int len)!{! __m256d rA_AVX, rB_AVX, rC_AVX;!! for (int i = 0; i < len; i += 4)! {! rA_AVX = _mm256_load_pd(&a[i]);! rB_AVX = _mm256_load_pd(&b[i]);! rC_AVX = _mm256_add_pd(rA_AVX, rB_AVX);! _mm256_store_pd(&c[i], rC_AVX);! }!}!

Page 51: Stuff I Learned About Performance
Page 52: Stuff I Learned About Performance
Page 53: Stuff I Learned About Performance

More Java vs. C++

https://github.com/real-logic/simple-binary-encoding

Page 54: Stuff I Learned About Performance

What did I learn?• Details matter (performance measurement) • Let the problem define the solution

• Design without bias • Understand the costs/benefits of trade-offs

• Design to let others make trade-offs • Some trade-offs are not technical

Page 55: Stuff I Learned About Performance

Q&A

Page 56: Stuff I Learned About Performance

Links• Measuring Latency - http://goo.gl/6bllUP

• Event Sourcing - http://goo.gl/VcZI7l

• LMAX Architecture - http://goo.gl/UI5z0T

• Source Code

• http://github.com/mikeb01/qconsf2014