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  
  2. 4   Scalable  &  Event-­‐Driven   •  Message-­‐Passing   • 

    Asynchronous   •  Non-­‐Blocking   •  Immutability   •  Avoiding   Synchroniza6on   h9p://www.reac>vemanifesto.org/images/[email protected]  
  3. 5   Resilient   •  Isola6ng  Failure   •  Supervision

     Hierachies   •  Self-­‐Healing  Components   h9p://www.reac>vemanifesto.org/images/[email protected]  
  4. 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  
  5. 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)  
  6. 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  
  7. 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.  
  8. 13   Selectors  -­‐  Regex   •  A  RegexSelector  will

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

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

     will  match  the  same  or  a  supertype  of   the  given  class.  
  11. 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);   }  
  12. 17   Promises   •  Promises  allow  for  composi6on  of

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

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

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

     by  @michaelklishin  and   @ifesdjeen   ­  h9ps://github.com/clojurewerkz/meltdown    
  18. 24   Networking   •  TCP  and  UDP  Support  

    •  ZeroMQ,  SSL,  Codecs,...  
  19. 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,…)