Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going Reactive - High Performance JVM code with Reactor

Going Reactive - High Performance JVM code with Reactor

I gave this talk at JAX 2014. It is an introduction into reactive programming, why its needed and how the Reactor framework can help you achieve those goals.

Michael Nitschinger

May 15, 2014
Tweet

More Decks by Michael Nitschinger

Other Decks in Programming

Transcript

  1. Michael  Nitschinger  (@daschl)  
    JVM  Engineer  @  Couchbase,  Inc.  
    Going  Reac6ve  
    High  Performance  JVM  Code  with  Reactor  

    View Slide

  2. 2  
    Reac6ve?  

    View Slide

  3. 3  
    4  Traits  of  Reac6ve  Systems  
    •  h?p://www.reac6vemanifesto.org/  
     
    h9p://www.reac>vemanifesto.org/images/full-­‐reac>[email protected]  

    View Slide

  4. 4  
    Scalable  &  Event-­‐Driven  
    •  Message-­‐Passing  
    •  Asynchronous  
    •  Non-­‐Blocking  
    •  Immutability  
    •  Avoiding  
    Synchroniza6on  
    h9p://www.reac>vemanifesto.org/images/[email protected]  

    View Slide

  5. 5  
    Resilient  
    •  Isola6ng  Failure  
    •  Supervision  Hierachies  
    •  Self-­‐Healing  Components  
    h9p://www.reac>vemanifesto.org/images/[email protected]  

    View Slide

  6. 6  
    Responsive  
    •  Use  Bounded  Queues  &  Backpressure  
    •  Apply  Batching  under  Traffic  Bursts  
    •  Don‘t  forget  the  Big  O!  
    0  
    20  
    40  
    60  
    80  
    100  
    120  
    Hockey  S>ck  
    Linear  
    Leveling  Off  

    View Slide

  7. 7  
    Meet  Reactor.  

    View Slide

  8. 8  
    What  is  Reactor?  
    •  Founda6onal  library  
    •  Lives  between  user  and  low-­‐level  code.  
    •  Suite  for:  drivers,  libraries,  event-­‐driven  systems  
    •  Part  of  Spring  IO  
    •  1.1.0  Released  (h?ps://github.com/reactor/reactor)  

    View Slide

  9. 9  
    Landscape  

    View Slide

  10. 10  
    A  first  look  

    View Slide

  11. 11  
    Dispatchers  
    •  Per  Environment  
    •  Manage  Task  Execu6on.  
     
    ­  ThreadPoolExecutorDispatcher  
    execu>ng  tasks  in  a  thread  pool  
    ­  EventLoopDispatcher  
    mul>ple  event  loops  dispatching  tasks  
    ­  RingBufferDispatcher  
    uses  the  LMAX  Disruptor  RingBuffer  
    ­  ActorDispatcher  
    to  pin  keys  to  threads  
    ­  SynchronousDispatcher  
    for  tes>ng  

    View Slide

  12. 12  
    Selectors  
    •  Lea-­‐hand  side  of  an  equality  comparison.  
    •  Can  be  created  from  any  object.  
    ­  $(object)  
    ­  Selectors.object(obj)  
    •  Can  extract  data  from  the  matched  key.  

    View Slide

  13. 13  
    Selectors  -­‐  Regex  
    •  A  RegexSelector  will  match  a  String  by  execu6ng  the  
    regex.  

    View Slide

  14. 14  
    Selectors  -­‐  Templates  
    •  A  UriTemplateSelector  will  match  a  String  by  extrac6ng  
    bits  from  a  URI.  

    View Slide

  15. 15  
    Selectors  –  Class  Types  
    •  A  ClassSelector  will  match  the  same  or  a  supertype  of  
    the  given  class.  

    View Slide

  16. 16  
    Func6ons  
    •  Perform  work  on  T.  
     
    public  interface  Consumer  {  
     void  accept(T  t);  
    }  
    •  Supply  the  caller  with  T.  
     
    public  interface  Supplier  {  
     T  get();  
    }  
    •  Perform  work  on  T  and  return  V.  
     
    public  interface  Function  {  
     V  apply(T  t);  
    }  
    •  Check  if  the  input  matches  the  criteria.  
     
    public  interface  Predicate  {  
     boolean  test(T  t);  
    }  

    View Slide

  17. 17  
    Promises  
    •  Promises  allow  for  composi6on  of  func6ons  
    ­  Shares  common  func>ons  with  a  Stream  

    View Slide

  18. 18  
    Streams  
    •  Streams  allow  the  composi6on  of  func6ons  
    ­  Callbacks  on  steroids  
    ­  Akka  Streams,  RxJava  Observables,  JDK  8  Stream  

    View Slide

  19. 19  
    Processor  
    •  Thin  wrapper  around  the  Disruptor  RingBuffer  
    ­  Converts  Disruptor  API  to  Reactor  API  
    ­  Really  fast!  

    View Slide

  20. 20  
    Integra6on:  Spring  
    •  Helpers  to  integrate  Reactor  into  ApplicationContext  
    ­  @EnableReactor  
    ­  Wiring  annotated  handlers  
    •  DispatcherTaskExecutor  
    ­  Not  intended  for  “high  scale”  
    ­  Used  to  get  Spring  components  running  in  the  same  thread  as  Reactor  
    consumers.  

    View Slide

  21. 21  
    Integra6on:  Groovy  
    •  First  class  ci6zen  language  implementa6on  
     
    ­  @CompileSta>c  ready  
    ­  Prominent  use  of  Closure  as  Consumers,  Func>ons  and  more  
    ­  Operator  overloading  
    ­  Full  Reactor  system  Builder  

    View Slide

  22. 22  
    Integra6on:  Clojure  
    •  Meltdown:  A  Clojure  binding  by  @michaelklishin  and  
    @ifesdjeen  
    ­  h9ps://github.com/clojurewerkz/meltdown    

    View Slide

  23. 23  
    Wait  –  there  is  more!  

    View Slide

  24. 24  
    Networking  
    •  TCP  and  UDP  Support  
    •  ZeroMQ,  SSL,  Codecs,...  

    View Slide

  25. 25  
    Networking  -­‐  Server  

    View Slide

  26. 26  
    Allocators  
    •  Control  Memory  Usage  (RingBuffer,  Refcoun>ng)  

    View Slide

  27. 27  
    What  else?  
    •  Too  much  to  show  today!  
     
    ­  Buffer  tooling  
    ­  Sequencing/EventOrdering  
    ­  Work  Queue  support  on  top  of  Java  Chronicle  
    ­  Logback  Appender  
    ­  Language  Constructs  (i.e.  Tuples,…)  

    View Slide

  28. 28  
    Demo  

    View Slide

  29. 29  
    Ques6ons?  

    View Slide

  30. 30  

    View Slide