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.

D839d9aa56849a71d8a9aa3d292a6ce6?s=128

Michael Nitschinger

May 15, 2014
Tweet

Transcript

  1. Michael  Nitschinger  (@daschl)   JVM  Engineer  @  Couchbase,  Inc.  

    Going  Reac6ve   High  Performance  JVM  Code  with  Reactor  
  2. 2   Reac6ve?  

  3. 3   4  Traits  of  Reac6ve  Systems   •  h?p://www.reac6vemanifesto.org/

        h9p://www.reac>vemanifesto.org/images/full-­‐reac>ve@2x.png  
  4. 4   Scalable  &  Event-­‐Driven   •  Message-­‐Passing   • 

    Asynchronous   •  Non-­‐Blocking   •  Immutability   •  Avoiding   Synchroniza6on   h9p://www.reac>vemanifesto.org/images/amdahl@2x.png  
  5. 5   Resilient   •  Isola6ng  Failure   •  Supervision

     Hierachies   •  Self-­‐Healing  Components   h9p://www.reac>vemanifesto.org/images/tank@2x.png  
  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  
  7. 7   Meet  Reactor.  

  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)  
  9. 9   Landscape  

  10. 10   A  first  look  

  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  
  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.  
  13. 13   Selectors  -­‐  Regex   •  A  RegexSelector  will

     match  a  String  by  execu6ng  the   regex.  
  14. 14   Selectors  -­‐  Templates   •  A  UriTemplateSelector  will

     match  a  String  by  extrac6ng   bits  from  a  URI.  
  15. 15   Selectors  –  Class  Types   •  A  ClassSelector

     will  match  the  same  or  a  supertype  of   the  given  class.  
  16. 16   Func6ons   •  Perform  work  on  T.  

      public  interface  Consumer<T>  {    void  accept(T  t);   }   •  Supply  the  caller  with  T.     public  interface  Supplier<T>  {    T  get();   }   •  Perform  work  on  T  and  return  V.     public  interface  Function<T,  V>  {    V  apply(T  t);   }   •  Check  if  the  input  matches  the  criteria.     public  interface  Predicate<T>  {    boolean  test(T  t);   }  
  17. 17   Promises   •  Promises  allow  for  composi6on  of

     func6ons   ­  Shares  common  func>ons  with  a  Stream  
  18. 18   Streams   •  Streams  allow  the  composi6on  of

     func6ons   ­  Callbacks  on  steroids   ­  Akka  Streams,  RxJava  Observables,  JDK  8  Stream  
  19. 19   Processor   •  Thin  wrapper  around  the  Disruptor

     RingBuffer   ­  Converts  Disruptor  API  to  Reactor  API   ­  Really  fast!  
  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.  
  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  
  22. 22   Integra6on:  Clojure   •  Meltdown:  A  Clojure  binding

     by  @michaelklishin  and   @ifesdjeen   ­  h9ps://github.com/clojurewerkz/meltdown    
  23. 23   Wait  –  there  is  more!  

  24. 24   Networking   •  TCP  and  UDP  Support  

    •  ZeroMQ,  SSL,  Codecs,...  
  25. 25   Networking  -­‐  Server  

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

     
  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,…)  
  28. 28   Demo  

  29. 29   Ques6ons?  

  30. 30