Disrupng	  Disruptor	                    By	  	           Azrul	  MADISA	  (azrul.madisa@my.experian.com)	  
About	  me	  •  Call	  me	  Azrul	  •  Soluon	  Architect	  at	  Experian	  Decision	     Analycs	  •  In	  charge	  of	  ...
Menu	  of	  the	  day	  •      Why	  concurrency?	  •      Concurrency	  is	  hard	  •      Producer	  consumer	  •      U...
Why	  concurrency?	  •    Cloud	  •    Mul-­‐core	  •    Mul-­‐channel	  •    Complex	  business	       process	  
Concurrency	  is	  hard!	  Wait	  for	  resource	  A	   to	  be	  available	  Reserve	  resource	  A	  Wait	  for	  resour...
Concurrency	  is	  hard!	  Wait	  for	  resource	  A	              Wait	  for	  resource	  B	   to	  be	  available	      ...
Concurrency	  is	  hard!	  Wait	  for	  resource	  A	                          Wait	  for	  resource	  B	   to	  be	  avai...
Concurrency	  is	  hard!	  •  Random	  decisions	  in	  different	  places	  influence	     each	  other	  •  Influence	  eac...
Solving	  concurrency	  with	  locking	  Wait	  for	  resource	  A	                   Wait	  for	  resource	  B	   to	  be...
Solving	  concurrency	  with	  locking	  •  Locking	  is	  done	  through	     ‘monitors’	  •  OS	  or	  VM	  level	  •  L...
CAS	  to	  the	  rescue	  •    CAS	  =	  Compare-­‐And-­‐Swap	  •    ‘Hardware’	  level	  locking	  •    Atomic	  operaons...
CAS	  to	  the	  rescue	  –	  incremenng	  a	                                  value	           Shared	  memory	          ...
CAS	  to	  the	  rescue	  •  Java	  example	  	  AtomicInteger	  counter	  =	  new	  AtomicInteger(1);	  	  …	  	  int	  c...
PRODUCER	  -­‐	  CONSUMER	  
Producer	  -­‐	  consumer	  Producers	  	  
Producer	  -­‐	  consumer	  Producers	  	                                    Mess	  
Producer	  -­‐	  consumer	  Producers	  	                              Consumer	  (cleaner)	                              ...
Producer	  -­‐	  consumer	  •  Other	  examples	     	     Industry	          Producer	            Consumer	            Ba...
Producer	  -­‐	  consumer	  •  Problems	     –  Producer	  is	  producing	  faster	        than	  consumer	  can	        c...
Soluon	  1:	  Array	  blocking	  queue	  •  Classic	  soluon:	  Using	  array	  blocking	  queue	     –  Producer	  would	...
Soluon	  1:	  Array	  blocking	  queue	  •  Classic	  soluon:	  Using	  array	  blocking	  queue	     –  Advantage:	  Scal...
Soluon	  1:	  Array	  blocking	  queue	  •  Main	  problem:	     –  Queue	  need	  to	  be	  locked	          •  For	  con...
Soluon	  2:	  Using	  ring	  buffer	  &	  CAS	  •  Producer	  will	  never	  be	  blocked	  –	  we	  just	  “wrap	     arou...
Soluon	  2:	  Using	  ring	  buffer	  &	  CAS	  •  Ring	  buffer	  +	  CAS	  =	  Disruptor	  •  Create	  by	  LMAX	      –  ...
USING	  DISRUPTOR	  
Loan	  applicaon	  processing	      Applicaon	  by	  user	   Pre-­‐bureau	  processing	       Call	  credit	  bureau	     ...
Loan	  applicaon	  processing	      Applicaon	  by	  user	                  Applicaon	  by	  user	   Pre-­‐bureau	  proces...
Loan	  applicaon	  processing	      Applicaon	  by	  user	   Pre-­‐bureau	  processing	                                   ...
Loan	  applicaon	  processing	                              Applicaon	  by	  user	                            Pre-­‐bureau...
Loan	  applicaon	  processing	                              Applicaon	  by	  user	                            Pre-­‐bureau...
Loan	  applicaon	  processing	  –	  with	                 Disruptor	                                 Applicaon	  by	  user...
Loan	  applicaon	  processing	  –	  with	                 Disruptor	                                 Applicaon	  by	  user...
Loan	  applicaon	  processing	  –	  with	                     Disruptor	  Disruptor<MyEvent>	  disruptor	  =	  new	  Disru...
Batch	  reading	  Write	  Write	  Write	  
Batch	  reading	               Read	  
USEFUL	  PATTERNS	  
Useful	  paUerns	  •  PaUerns	  I’ve	  experimented	  with	     –  Not	  ‘in	  producon	  yet’	     –  Try	  it	  out	  on...
Topic	  replacement	  
Topic	  replacement	           AND	  
Topic	  replacement	           AND	  
Topic	  replacement	  •  Just	  use	  disruptor’s	  ‘default	  sesng’	  	  
Queue	  replacement	           OR	  
Queue	  replacement	  •  Use	  WorkerPool	  	  WorkerPool<ValueEvent>	  workerPool	  =	  new	  WorkerPool<ValueEvent>(…);	...
Actors	  •  A	  unit	  of	  concurrent	  computaon	  	  •  From	  wikipedia:	      –  “…	  in	  response	  to	  a	  messag...
Actors	  
Actors	               Actor	  =	  Worker	  Pool	  (Queue)	  
Actors	               Actor	  =	  Worker	  Pool	  (Queue)	  
Replicaon	  ‘Topic’	  layout	  
Replicaon	          ‘Topic’	  layout	    JMS	                 JMS	  messaging	  over	  network	  Listener	  
Replicaon	          ‘Topic’	  layout	    JMS	                                                  Guaranteed	  delivery	     ...
Conclusion	  •  Disruptor	  –	  interesng	  and	  ‘disrupve’	     framework	  •  Revenge	  of	  the	  “object	  oriented	 ...
Upcoming SlideShare
Loading in...5
×

Disrupting disruptor

1,678

Published on

MOSC 2012 Presentation

Published in: Technology, Economy & Finance
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,678
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
48
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Disrupting disruptor

  1. 1. Disrupng  Disruptor   By     Azrul  MADISA  (azrul.madisa@my.experian.com)  
  2. 2. About  me  •  Call  me  Azrul  •  Soluon  Architect  at  Experian  Decision   Analycs  •  In  charge  of  Experian’s  stuff:   –  BI   –  Business  Acvity  Monitoring   –  Integraon   •  OSGi  stuff  •  Love  to  read  •  Black  belt  in  Aikido  •  No  concurrency  expert,   –   just  a  wild  enthusiast!!  
  3. 3. Menu  of  the  day  •  Why  concurrency?  •  Concurrency  is  hard  •  Producer  consumer  •  Using  disruptor  •  PaUerns    
  4. 4. Why  concurrency?  •  Cloud  •  Mul-­‐core  •  Mul-­‐channel  •  Complex  business   process  
  5. 5. Concurrency  is  hard!  Wait  for  resource  A   to  be  available  Reserve  resource  A  Wait  for  resource  B   to  be  available  Reserve  resource  B   Use  A  and  B   Release  A  and  B  
  6. 6. Concurrency  is  hard!  Wait  for  resource  A   Wait  for  resource  B   to  be  available   to  be  available  Reserve  resource  A   Reserve  resource  B  Wait  for  resource  B   Wait  for  resource  A   to  be  available   to  be  available  Reserve  resource  B   Reserve  resource  A   Use  A  and  B   Use  A  and  B   Release  A  and  B   Release  A  and  B  
  7. 7. Concurrency  is  hard!  Wait  for  resource  A   Wait  for  resource  B   to  be  available   to  be  available  Reserve  resource  A   Reserve  resource  B  Wait  for  resource  B   Stuck  here!   Wait  for  resource  A   to  be  available   to  be  available  Reserve  resource  B   Reserve  resource  A   Use  A  and  B   Use  A  and  B   Release  A  and  B   Release  A  and  B  
  8. 8. Concurrency  is  hard!  •  Random  decisions  in  different  places  influence   each  other  •  Influence  each  other  BADLY!  
  9. 9. Solving  concurrency  with  locking  Wait  for  resource  A   Wait  for  resource  B   to  be  available   to  be  available  Reserve  resource  A   Reserve  resource  B   Monitor  Wait  for  resource  B   Wait  for  resource  A   to  be  available   to  be  available   Reserve  resource  B   Reserve  resource  A   Use  A  and  B   Use  A  and  B   Release  A  and  B   Release  A  and  B  
  10. 10. 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!  
  11. 11. CAS  to  the  rescue  •  CAS  =  Compare-­‐And-­‐Swap  •  ‘Hardware’  level  locking  •  Atomic  operaons  •  Crazy  fast  –  mul-­‐core  friendly  
  12. 12. CAS  to  the  rescue  –  incremenng  a   value   Shared  memory   Get  ‘input’   Input  =  21   21  Changed  by   another   Increment   Input  =  21   result  =  22   thread     Compare   Input  =  21   Different     Same   Set   result  =  22    
  13. 13. CAS  to  the  rescue  •  Java  example    AtomicInteger  counter  =  new  AtomicInteger(1);    …    int  currentValue  =  counter.getAndIncrement();  
  14. 14. PRODUCER  -­‐  CONSUMER  
  15. 15. Producer  -­‐  consumer  Producers    
  16. 16. Producer  -­‐  consumer  Producers     Mess  
  17. 17. Producer  -­‐  consumer  Producers     Consumer  (cleaner)   Mess  
  18. 18. Producer  -­‐  consumer  •  Other  examples     Industry   Producer   Consumer   Banking   Credit  card   User  “credit   applicaon     worthiness”   verificaon   Insurance   Insurance   Underwring   applicaon   process   Manufacturing   Raw  material   Factory  worker   delivery    
  19. 19. Producer  -­‐  consumer  •  Problems   –  Producer  is  producing  faster   than  consumer  can   consumer  
  20. 20. Soluon  1:  Array  blocking  queue  •  Classic  soluon:  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  
  21. 21. Soluon  1:  Array  blocking  queue  •  Classic  soluon:  Using  array  blocking  queue   –  Advantage:  Scalability  =  many  consumers  
  22. 22. Soluon  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  
  23. 23. Soluon  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  
  24. 24. Soluon  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  opmizaons  
  25. 25. USING  DISRUPTOR  
  26. 26. Loan  applicaon  processing   Applicaon  by  user   Pre-­‐bureau  processing   Call  credit  bureau    Approved  /  refer  /  reject     Save  data    
  27. 27. Loan  applicaon  processing   Applicaon  by  user   Applicaon  by  user   Pre-­‐bureau  processing   Pre-­‐bureau  processing   Call  credit  bureau   Call  credit  bureau      Approved  /  refer  /  reject   Approved  /  refer  /  reject       Save  data   Save  data      
  28. 28. Loan  applicaon  processing   Applicaon  by  user   Pre-­‐bureau  processing   X  100  every   Call  credit  bureau     second  Approved  /  refer  /  reject     Save  data    
  29. 29. Loan  applicaon  processing   Applicaon  by  user   Pre-­‐bureau  processing   Call  credit  bureau    Approved  /  refer  /  reject   Save  data      
  30. 30. Loan  applicaon  processing   Applicaon  by  user   Pre-­‐bureau  processing   Tedious  in  your   Call  credit  bureau   typical  applicaon     server  (resort  to  JMS)  Approved  /  refer  /  reject   Save  data      
  31. 31. Loan  applicaon  processing  –  with   Disruptor   Applicaon  by  user   Disruptor   Pre-­‐bureau  processing   Call  credit  bureau     Approved  /  refer  /  reject   Save  data      
  32. 32. Loan  applicaon  processing  –  with   Disruptor   Applicaon  by  user   Disruptor   Handle  ‘forking’   Pre-­‐bureau  processing   Handle  parallel  users   Handle  ‘workflow’   Call  credit  bureau     Approved  /  refer  /  reject   Save  data      
  33. 33. Loan  applicaon  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  (applicaon  by  user)  long  sequence  =  ringBuffer.next();  MyEvent    event  =  ringBuffer.get(sequence);  event.setValue(x);  ringBuffer.publish(sequence);    
  34. 34. Batch  reading  Write  Write  Write  
  35. 35. Batch  reading   Read  
  36. 36. USEFUL  PATTERNS  
  37. 37. Useful  paUerns  •  PaUerns  I’ve  experimented  with   –  Not  ‘in  producon  yet’   –  Try  it  out  on  your  own  before  commisng   –   Don’t  sue  me  if  it  blows  J  
  38. 38. Topic  replacement  
  39. 39. Topic  replacement   AND  
  40. 40. Topic  replacement   AND  
  41. 41. Topic  replacement  •  Just  use  disruptor’s  ‘default  sesng’    
  42. 42. Queue  replacement   OR  
  43. 43. Queue  replacement  •  Use  WorkerPool    WorkerPool<ValueEvent>  workerPool  =  new  WorkerPool<ValueEvent>(…);    workerPool.start(…)  ;  
  44. 44. Actors  •  A  unit  of  concurrent  computaon    •  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”  
  45. 45. Actors  
  46. 46. Actors   Actor  =  Worker  Pool  (Queue)  
  47. 47. Actors   Actor  =  Worker  Pool  (Queue)  
  48. 48. Replicaon  ‘Topic’  layout  
  49. 49. Replicaon   ‘Topic’  layout   JMS   JMS  messaging  over  network  Listener  
  50. 50. Replicaon   ‘Topic’  layout   JMS   Guaranteed  delivery   JMS  messaging  over  network  Listener  
  51. 51. Conclusion  •  Disruptor  –  interesng  and  ‘disrupve’   framework  •  Revenge  of  the  “object  oriented  programming”   model  J  •  Do  your  own  experiments  •  Check  out:   –  hUp://code.google.com/p/disruptor/   –  hUp://marnfowler.com/arcles/lmax.html   –  hUp://mechanis.blogspot.com/2011/07/dissecng-­‐ disruptor-­‐wiring-­‐up.html  
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×