Top Banner
Disrup’ng Disruptor By Azrul MADISA (azrul.madisa@my.experian.com)
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
  • 1. Disrupng Disruptor By Azrul MADISA (azrul.madisa@my.experian.com)

2. About me Call me Azrul Soluon Architect at Experian Decision Analycs In charge of Experians stu: BI Business Acvity Monitoring Integraon OSGi stu Love to read Black belt in Aikido No concurrency expert, just a wild enthusiast!! 3. Menu of the day Why concurrency? Concurrency is hard Producer consumer Using disruptor PaUerns 4. Why concurrency? Cloud Mul-core Mul-channel Complex business process 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. 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. 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. Concurrency is hard! Random decisions in dierent places inuence each other Inuence each other BADLY! 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. 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. CAS to the rescue CAS = Compare-And-Swap Hardware level locking Atomic operaons Crazy fast mul-core friendly 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 Dierent Same Set result = 22 13. CAS to the rescue Java example AtomicInteger counter = new AtomicInteger(1); int currentValue = counter.getAndIncrement(); 14. PRODUCER - CONSUMER 15. Producer - consumer Producers 16. Producer - consumer Producers Mess 17. Producer - consumer Producers Consumer (cleaner) Mess 18. Producer - consumer Other examples Industry Producer Consumer Banking Credit card User credit applicaon worthiness vericaon Insurance Insurance Underwring applicaon process Manufacturing Raw material Factory worker delivery 19. Producer - consumer Problems Producer is producing faster than consumer can consumer 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. Soluon 1: Array blocking queue Classic soluon: Using array blocking queue Advantage: Scalability = many consumers 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. Soluon 2: Using ring buer & CAS Producer will never be blocked we just wrap around the circular queue (write over older entry) Elements of ring buer are set using CAS => Ring buer is very very fast 24. Soluon 2: Using ring buer & CAS Ring buer + CAS = Disruptor Create by LMAX Doing High frequency trading 100 k TPS at 1 ms latency Disruptor also includes a few other opmizaons 25. USING DISRUPTOR 26. Loan applicaon processing Applicaon by user Pre-bureau processing Call credit bureau Approved / refer / reject Save data 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. Loan applicaon processing Applicaon by user Pre-bureau processing X 100 every Call credit bureau second Approved / refer / reject Save data 29. Loan applicaon processing Applicaon by user Pre-bureau processing Call credit bureau Approved / refer / reject Save data 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. Loan applicaon processing with Disruptor Applicaon by user Disruptor Pre-bureau processing Call credit bureau Approved / refer / reject Save data 32. Loan applicaon processing with Disruptor Applicaon by user Disruptor Handle forking Pre-bureau processing Handle parallel users Handle workow Call credit bureau Approved / refer / reject Save data 33. Loan applicaon processing with Disruptor Disruptor disruptor = new Disruptor(); //read data disruptor.handleEventsWith(preBureauProcessing). //rst do this then(callBureau). //next do this then( approvedReferReject, saveData); //arer that, do these in parallel //run disruptor RingBuer ringBuer = disruptor.start(); //write (applicaon by user) long sequence = ringBuer.next(); MyEvent event = ringBuer.get(sequence); event.setValue(x); ringBuer.publish(sequence); 34. Batch reading Write Write Write 35. Batch reading Read 36. USEFUL PATTERNS 37. Useful paUerns PaUerns Ive experimented with Not in producon yet Try it out on your own before commisng Dont sue me if it blows J 38. Topic replacement 39. Topic replacement AND 40. Topic replacement AND 41. Topic replacement Just use disruptors default sesng 42. Queue replacement OR 43. Queue replacement Use WorkerPool WorkerPool workerPool = new WorkerPool(); workerPool.start() ; 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. Actors 46. Actors Actor = Worker Pool (Queue) 47. Actors Actor = Worker Pool (Queue) 48. Replicaon Topic layout 49. Replicaon Topic layout JMS JMS messaging over network Listener 50. Replicaon Topic layout JMS Guaranteed delivery JMS messaging over network Listener 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