Top Banner
Disrup’ng Disruptor By Azrul MADISA ([email protected])
51
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Disrupting disruptor

Disrup'ng  Disruptor  

By    Azrul  MADISA  

([email protected])  

Page 2: Disrupting disruptor

About  me  •  Call  me  Azrul  •  Solu'on  Architect  at  Experian  Decision  

Analy'cs  •  In  charge  of  Experian’s  stuff:  

–  BI  –  Business  Ac'vity  Monitoring  –  Integra'on  

•  OSGi  stuff  •  Love  to  read  •  Black  belt  in  Aikido  •  No  concurrency  expert,  

–   just  a  wild  enthusiast!!  

Page 3: Disrupting disruptor

Menu  of  the  day  

•  Why  concurrency?  •  Concurrency  is  hard  •  Producer  consumer  •  Using  disruptor  •  PaUerns    

Page 4: Disrupting disruptor

Why  concurrency?  

•  Cloud  •  Mul'-­‐core  •  Mul'-­‐channel  •  Complex  business  process  

Page 5: Disrupting disruptor

Concurrency  is  hard!  Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Page 6: Disrupting disruptor

Concurrency  is  hard!  Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Page 7: Disrupting disruptor

Concurrency  is  hard!  Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Stuck  here!  

Page 8: Disrupting disruptor

Concurrency  is  hard!  

•  Random  decisions  in  different  places  influence  each  other  

•  Influence  each  other  BADLY!  

Page 9: Disrupting disruptor

Solving  concurrency  with  locking  Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Wait  for  resource  A  to  be  available  

Wait  for  resource  B  to  be  available  

Reserve  resource  B  

Reserve  resource  A  

Use  A  and  B  

Release  A  and  B  

Monitor  

Page 10: Disrupting disruptor

Solving  concurrency  with  locking  

•  Locking  is  done  through  ‘monitors’  

•  OS  or  VM  level  •  Like  a  toilet  –  If  someone  is  using  it,  everyone  else  has  to  wait  

– Acquiring  a  lock  is  generally  SLOW!  

Page 11: Disrupting disruptor

CAS  to  the  rescue  

•  CAS  =  Compare-­‐And-­‐Swap  •  ‘Hardware’  level  locking  •  Atomic  opera'ons  •  Crazy  fast  –  mul'-­‐core  friendly  

Page 12: Disrupting disruptor

CAS  to  the  rescue  –  incremen'ng  a  value  

Get  ‘input’  

Increment    

Compare    

Input  =  21  

Input  =  21  

21  

Shared  memory  

result  =  22  

Set    

Input  =  21  

Same  

Different  

result  =  22  

Changed  by  another  thread  

Page 13: Disrupting disruptor

CAS  to  the  rescue  

•  Java  example  

 AtomicInteger  counter  =  new  AtomicInteger(1);    …    int  currentValue  =  counter.getAndIncrement();  

Page 14: Disrupting disruptor

PRODUCER  -­‐  CONSUMER  

Page 15: Disrupting disruptor

Producer  -­‐  consumer  Producers    

Page 16: Disrupting disruptor

Producer  -­‐  consumer  Producers    

Mess  

Page 17: Disrupting disruptor

Producer  -­‐  consumer  Consumer  (cleaner)  Producers    

Mess  

Page 18: Disrupting disruptor

Producer  -­‐  consumer  

•  Other  examples     Industry   Producer   Consumer  

Banking   Credit  card  applica'on    

User  “credit  worthiness”  verifica'on  

Insurance   Insurance  applica'on  

Underwri'ng  process  

Manufacturing   Raw  material  delivery    

Factory  worker  

Page 19: Disrupting disruptor

Producer  -­‐  consumer  

•  Problems  – Producer  is  producing  faster  than  consumer  can  consumer  

Page 20: Disrupting disruptor

Solu'on  1:  Array  blocking  queue  

•  Classic  solu'on:  Using  array  blocking  queue  – Producer  would  queue  things  if  consumer  is  slow  – Consumer  can  consume  at  its  own  pace    –  If  queue  is  full,  producer  will  be  blocked  

Page 21: Disrupting disruptor

Solu'on  1:  Array  blocking  queue  

•  Classic  solu'on:  Using  array  blocking  queue  – Advantage:  Scalability  =  many  consumers  

Page 22: Disrupting disruptor

Solu'on  1:  Array  blocking  queue  

•  Main  problem:  – Queue  need  to  be  locked  

•  For  consumer  to  read  •  For  producer  to  write  

– Producer  is  blocked  if  queue  is  full  – Lock  =>  Slow  

Page 23: Disrupting disruptor

Solu'on  2:  Using  ring  buffer  &  CAS  

•  Producer  will  never  be  blocked  –  we  just  “wrap  around”  the  circular  queue  (write  over  older  entry)  

•  Elements  of  ring  buffer  are  set  using  CAS  •  =>  Ring  buffer  is  very  very  fast  

Page 24: Disrupting disruptor

Solu'on  2:  Using  ring  buffer  &  CAS  

•  Ring  buffer  +  CAS  =  Disruptor  •  Create  by  LMAX  – Doing  High  frequency  trading  

•  “100  k  TPS  at  1  ms  latency”  •  Disruptor  also  includes  a  few  other  op'miza'ons  

Page 25: Disrupting disruptor

USING  DISRUPTOR  

Page 26: Disrupting disruptor

Loan  applica'on  processing  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Page 27: Disrupting disruptor

Loan  applica'on  processing  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Page 28: Disrupting disruptor

Loan  applica'on  processing  

X  100  every  second  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Page 29: Disrupting disruptor

Loan  applica'on  processing  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Page 30: Disrupting disruptor

Loan  applica'on  processing  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Tedious  in  your  typical  applica'on  

server  (resort  to  JMS)  

Page 31: Disrupting disruptor

Loan  applica'on  processing  –  with  Disruptor  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Disruptor  

Page 32: Disrupting disruptor

Loan  applica'on  processing  –  with  Disruptor  

Applica'on  by  user  

Pre-­‐bureau  processing  

Call  credit  bureau    

Approved  /  refer  /  reject    

Save  data    

Disruptor  

Handle  ‘forking’  Handle  parallel  users  Handle  ‘workflow’  

Page 33: Disrupting disruptor

Loan  applica'on  processing  –  with  Disruptor  

Disruptor<MyEvent>  disruptor  =  new  Disruptor<MyEvent>(…);    //read  data  disruptor.handleEventsWith(preBureauProcessing).  //first  do  this  then(callBureau).  //next  do  this  then(  approvedReferReject,  saveData);  //arer  that,  do  these  in  parallel    //run  disruptor  RingBuffer<ValueEvent>  ringBuffer  =  disruptor.start();    //write  (applica'on  by  user)  long  sequence  =  ringBuffer.next();  MyEvent    event  =  ringBuffer.get(sequence);  event.setValue(x);  ringBuffer.publish(sequence);    

Page 34: Disrupting disruptor

Batch  reading  

Write  

Write  

Write  

Page 35: Disrupting disruptor

Batch  reading  

Read  

Page 36: Disrupting disruptor

USEFUL  PATTERNS  

Page 37: Disrupting disruptor

Useful  paUerns  

•  PaUerns  I’ve  experimented  with  – Not  ‘in  produc'on  yet’  – Try  it  out  on  your  own  before  commisng  –   Don’t  sue  me  if  it  blows  J  

Page 38: Disrupting disruptor

Topic  replacement  

Page 39: Disrupting disruptor

Topic  replacement  AND  

Page 40: Disrupting disruptor

Topic  replacement  AND  

Page 41: Disrupting disruptor

Topic  replacement  

•  Just  use  disruptor’s  ‘default  sesng’    

Page 42: Disrupting disruptor

Queue  replacement  OR  

Page 43: Disrupting disruptor

Queue  replacement  

•  Use  WorkerPool    WorkerPool<ValueEvent>  workerPool  =  new  WorkerPool<ValueEvent>(…);    workerPool.start(…)  ;  

Page 44: Disrupting disruptor

Actors  

•  A  unit  of  concurrent  computa'on    •  From  wikipedia:  – “…  in  response  to  a  message  that  it  receives,  an  actor  can  make  local  decisions,  create  more  actors,  send  more  messages,  and  determine  how  to  respond  to  the  next  message  received”  

Page 45: Disrupting disruptor

Actors  

Page 46: Disrupting disruptor

Actors  Actor  =  Worker  Pool  (Queue)  

Page 47: Disrupting disruptor

Actors  Actor  =  Worker  Pool  (Queue)  

Page 48: Disrupting disruptor

Replica'on  

‘Topic’  layout  

Page 49: Disrupting disruptor

Replica'on  

‘Topic’  layout  

JMS  

Listener  

JMS  messaging  over  network  

Page 50: Disrupting disruptor

Replica'on  

‘Topic’  layout  

JMS  

Listener  

JMS  messaging  over  network  

Guaranteed  delivery  

Page 51: Disrupting disruptor

Conclusion  

•  Disruptor  –  interes'ng  and  ‘disrup've’  framework  

•  Revenge  of  the  “object  oriented  programming”  model  J  

•  Do  your  own  experiments  •  Check  out:  –  hUp://code.google.com/p/disruptor/  –  hUp://mar'nfowler.com/ar'cles/lmax.html  –  hUp://mechani's.blogspot.com/2011/07/dissec'ng-­‐disruptor-­‐wiring-­‐up.html