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

Oracle Webex 23 Nov 2018: Why Reactive & Introd...

Avatar for Ashok Koyi Ashok Koyi
November 23, 2018

Oracle Webex 23 Nov 2018: Why Reactive & Introduction To Project Reactor

Presentation on why we need to consider using programming models that enable us to get the best out of the hardware by embracing asynchronous programming model. Starts from basics & builds on hardware primitives such as caches & moves up the programming abstraction all the way till Java

Avatar for Ashok Koyi

Ashok Koyi

November 23, 2018

Other Decks in Technology

Transcript

  1. Donald Knuth "Premature optimization is the root of all evil

    (or at least most of it) in programming"
  2. SYNCHRONOUS SYNCHRONOUS Code executes in the caller thread (or) Caller

    thread waits till the other thread to signal
  3. SYNCHRONOUS SYNCHRONOUS Code executes in the caller thread (or) Caller

    thread waits till the other thread to signal ADVANTAGES ADVANTAGES
  4. SYNCHRONOUS SYNCHRONOUS Code executes in the caller thread (or) Caller

    thread waits till the other thread to signal ADVANTAGES ADVANTAGES Simpler & Easy to understand
  5. SYNCHRONOUS SYNCHRONOUS Code executes in the caller thread (or) Caller

    thread waits till the other thread to signal ADVANTAGES ADVANTAGES Simpler & Easy to understand Easy to debug due to stack traces availability
  6. ASYNCHRONOUS ASYNCHRONOUS Caller thread does not block while the other

    thread signals later DISADVANTAGES DISADVANTAGES
  7. ASYNCHRONOUS ASYNCHRONOUS Caller thread does not block while the other

    thread signals later DISADVANTAGES DISADVANTAGES Our code gets executed by other thread in future
  8. ASYNCHRONOUS ASYNCHRONOUS Caller thread does not block while the other

    thread signals later DISADVANTAGES DISADVANTAGES Our code gets executed by other thread in future Callback style
  9. ASYNCHRONOUS ASYNCHRONOUS Caller thread does not block while the other

    thread signals later DISADVANTAGES DISADVANTAGES Our code gets executed by other thread in future Callback style Not easy to reason about, stack traces gets wiped out
  10. SPEED MISMATCH SPEED MISMATCH CPU 60%/year. DRAM 7%/year. 1 10

    100 1000 1980 1981 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 … 2002 2003 2004 2005 2006 DRAM CPU 1982 Processor-Memory Performance Gap: (grows 50% / year) Performance Year “Moore’s Law” Memory Access vs CPU Speed Source: M. Bahi and C. Eisenbeis, "High Performance by Exploiting Information Locality through Reverse Computing," doi:10.1109/SBAC-PAD.2011.10
  11. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION
  12. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION Lets add an indirection
  13. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION Lets add an indirection So CPU caches are born
  14. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION Lets add an indirection So CPU caches are born L1
  15. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION Lets add an indirection So CPU caches are born L1 L2
  16. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION Lets add an indirection So CPU caches are born L1 L2 L3
  17. ADDRESSING MISMATCH ISSUE ADDRESSING MISMATCH ISSUE OBSERVATION OBSERVATION More than

    90% of code accesses small area of memory SOLUTION SOLUTION Lets add an indirection So CPU caches are born L1 L2 L3 L4 (mainframes)
  18. COSTS INVOLVED IN CONTEXT SWITCH COSTS INVOLVED IN CONTEXT SWITCH

    RAM ALU + Registers L2 L1 CPU Save register state
  19. COSTS INVOLVED IN CONTEXT SWITCH COSTS INVOLVED IN CONTEXT SWITCH

    RAM ALU + Registers L2 L1 CPU Save register state Scheduler overhead
  20. COSTS INVOLVED IN CONTEXT SWITCH COSTS INVOLVED IN CONTEXT SWITCH

    RAM ALU + Registers L2 L1 CPU Save register state Scheduler overhead Fetch memory lines for new Thread
  21. MULTI CORE CPU MULTI CORE CPU L3 RAM CPU ALU

    + Registers L2 L1 CORE 2 ALU + Registers L2 L1 CORE 1 ALU + Registers L2 L1 CORE 3 ALU + Registers L2 L1 CORE 4
  22. compiler can inline to static int max(int first, int second)

    { return first > second ? first : second; } static int min(int first, int second) { return first < second ? first : second; } int maxWithCeiling(int first, int second, int ceiling) { return min(max(first, second), ceiling); } int maxWithCeiling(int first, int second, int ceiling) { int temp = first > second ? first : second; return temp < ceiling ? temp : ceiling; }
  23. JIT OPTIMIZATION EXAMPLE JIT OPTIMIZATION EXAMPLE class Processor { final

    boolean unix; public Processor() { this.unix = PlatformUtil.isUnix(); } void process() { if (unix) { doUnixSpecificProcessing(); } else { doOtherProcessing(); } } }
  24. can be transformed into a er first run by JIT

    on Unix machines void process() { if (unix) { doUnixSpecificProcessing(); } else { doOtherProcessing(); } } void process() { doUnixSpecificProcessing(); }
  25. REORDERING INTUITIVE REORDERING INTUITIVE A = 2 r1 = B

    B = 1 r2 = A Thread 1 Thread 2 A = 2 r1 = B t B = 1 A = 2 r2 = A r1 = B B 1 A 2 r2 0 r1 1 t B = 1 r2 = A B 1 A 2 r2 2 r1 0 A = 2 r1 = B t B = 1 r1 = B B 1 A 2 r2 0 r1 1 t r2 = A B 1 A 2 r2 2 r1 1 A = 2 r2 = A t B = 1 B 1 A 2 r2 2 r1 1 A = 2 r2 = A r1 = B B = 1 A = 2 t r2 = A B 1 A 2 r2 2 r1 1 B = 1 r1 = B
  26. REORDERING COUNTER INTUITIVE REORDERING COUNTER INTUITIVE B = 1 r2

    = A Thread 1 Thread 2 A = 2 r1 = B B = 1 Thread 1 Thread 2 A = 2 r2 = A r1 = B B 1 A 2 r2 0 r1 1 1 2 2 1 1 2 2 0 t B = 1 A = 2 B 1 A 2 r2 0 r1 0 r2 = A r1 = B JMM compatible transformation Previous Interleaved Execution Values Valid execution B 1 A 2 r2 0 r1 1 1 2 2 1 1 2 2 0 1 2 0 0 Final State Table
  27. PERFORMANCE VS CORRECTNESS PERFORMANCE VS CORRECTNESS What if two threads

    needs to update same variable simultaneously? What if one thread updates & another thread reads almost immediately?
  28. PERFORMANCE VS CORRECTNESS PERFORMANCE VS CORRECTNESS What if two threads

    needs to update same variable simultaneously? What if one thread updates & another thread reads almost immediately? What if they just need to update a single integer without any race?
  29. PERFORMANCE VS CORRECTNESS PERFORMANCE VS CORRECTNESS What if two threads

    needs to update same variable simultaneously? What if one thread updates & another thread reads almost immediately? What if they just need to update a single integer without any race? What if there are multiple variables whose state has to be consistent together?
  30. JAVA MEMORY MODEL JAVA MEMORY MODEL Optimizers can execute in

    any order consistent with sequentially execution
  31. QUITE COMPLEX QUITE COMPLEX In a Nutshell volatile writes are

    visible to all subsequent reads Unlocked state will be visible to subsequent lock acquirer
  32. QUITE COMPLEX QUITE COMPLEX In a Nutshell volatile writes are

    visible to all subsequent reads Unlocked state will be visible to subsequent lock acquirer All final variables should be initialized by all threads that reference
  33. QUITE COMPLEX QUITE COMPLEX In a Nutshell volatile writes are

    visible to all subsequent reads Unlocked state will be visible to subsequent lock acquirer All final variables should be initialized by all threads that reference Thread will see the state at the time of start() of thread that spawned it
  34. QUITE COMPLEX QUITE COMPLEX In a Nutshell volatile writes are

    visible to all subsequent reads Unlocked state will be visible to subsequent lock acquirer All final variables should be initialized by all threads that reference Thread will see the state at the time of start() of thread that spawned it Thread that waited to join() another thread will state that other thread on join
  35. JAVA & THREADS JAVA & THREADS Every Java thread has

    a corresponding OS level thread
  36. JAVA & THREADS JAVA & THREADS Every Java thread has

    a corresponding OS level thread So, Lets understand how OS allocates memory
  37. PROCESS WITH THREADS PROCESS WITH THREADS Guard page Thread1 Stack

    Heap Program Text Guard page Thread2 Stack Fixed size Guard page Thread3 Stack
  38. PROBLEMS WITH JAVA PROBLEMS WITH JAVA and many other languages

    Lets understand by looking at How servers allocate resources
  39. TCP SOCKETS TCP SOCKETS TCP socket is defined by a

    unique combination of (source ip, source port, dest ip, dest port)
  40. TCP SOCKETS TCP SOCKETS TCP socket is defined by a

    unique combination of (source ip, source port, dest ip, dest port) Ports on a machine/ip = ~216
  41. TCP SOCKETS TCP SOCKETS TCP socket is defined by a

    unique combination of (source ip, source port, dest ip, dest port) Ports on a machine/ip = ~216 232 IPs for IPv4 = ~4B
  42. TCP SOCKETS TCP SOCKETS TCP socket is defined by a

    unique combination of (source ip, source port, dest ip, dest port) Ports on a machine/ip = ~216 232 IPs for IPv4 = ~4B 2128 IPs for IPv6 = VERY large number
  43. THEORETICAL MAXIMUM THEORETICAL MAXIMUM For a fixed destination IP &

    PORT For IPv4, 2(32+16 = 48) sockets For IPv6, 2(128+16 = 144) sockets
  44. MAXIMUM NUMBER OF THREADS MAXIMUM NUMBER OF THREADS Limited by

    system resources Atleast 1MB stack overhead/thread
  45. MAXIMUM NUMBER OF THREADS MAXIMUM NUMBER OF THREADS Limited by

    system resources Atleast 1MB stack overhead/thread For 4000 concurrent requests, 4000 * 1MB = 4GB just for Thread Stack
  46. MAXIMUM NUMBER OF THREADS MAXIMUM NUMBER OF THREADS Limited by

    system resources Atleast 1MB stack overhead/thread For 4000 concurrent requests, 4000 * 1MB = 4GB just for Thread Stack What about heap allocation?
  47. MAXIMUM NUMBER OF THREADS MAXIMUM NUMBER OF THREADS Limited by

    system resources Atleast 1MB stack overhead/thread For 4000 concurrent requests, 4000 * 1MB = 4GB just for Thread Stack What about heap allocation? What if most of threads are blocked on IO?
  48. COMPARISON COMPARISON OS can create only limited threads based on

    available system memory (RAM) handle almost billions of sockets
  49. COMPARISON COMPARISON OS can create only limited threads based on

    available system memory (RAM) handle almost billions of sockets Scalability is dependent on efficient use of OS Threads
  50. REQUEST HANDLING REQUEST HANDLING Lets look at three most common

    models Thread per Connection (TPC) Thread per Request (TPR)
  51. REQUEST HANDLING REQUEST HANDLING Lets look at three most common

    models Thread per Connection (TPC) Thread per Request (TPR) Shared Threads (ST)
  52. THREAD PER CONNECTION (TPC) THREAD PER CONNECTION (TPC) Thread is

    dedicated for whole connection cycle ADVANTAGES ADVANTAGES
  53. THREAD PER CONNECTION (TPC) THREAD PER CONNECTION (TPC) Thread is

    dedicated for whole connection cycle ADVANTAGES ADVANTAGES Extremely low conceptual overhead
  54. THREAD PER CONNECTION (TPC) THREAD PER CONNECTION (TPC) Thread is

    dedicated for whole connection cycle ADVANTAGES ADVANTAGES Extremely low conceptual overhead Extremely low latency as only thread scheduling overhead exists
  55. TPC CONT. TPC CONT. LIMITATIONS LIMITATIONS Thread blocked till connection

    closed/timesout In typical web setting, performs very badly as most of the connections would be idle
  56. TPC CONT. TPC CONT. LIMITATIONS LIMITATIONS Thread blocked till connection

    closed/timesout In typical web setting, performs very badly as most of the connections would be idle Wasted resources as threads are reserved for whole connection period
  57. TPC CONT. TPC CONT. LIMITATIONS LIMITATIONS Thread blocked till connection

    closed/timesout In typical web setting, performs very badly as most of the connections would be idle Wasted resources as threads are reserved for whole connection period USECASES USECASES
  58. TPC CONT. TPC CONT. LIMITATIONS LIMITATIONS Thread blocked till connection

    closed/timesout In typical web setting, performs very badly as most of the connections would be idle Wasted resources as threads are reserved for whole connection period USECASES USECASES Real time gaming
  59. THREAD PER REQUEST (TPR) THREAD PER REQUEST (TPR) Thread is

    dedicated for request-response cycle ADVANTAGES ADVANTAGES
  60. THREAD PER REQUEST (TPR) THREAD PER REQUEST (TPR) Thread is

    dedicated for request-response cycle ADVANTAGES ADVANTAGES Low conceptual overhead & Low scalability
  61. THREAD PER REQUEST (TPR) THREAD PER REQUEST (TPR) Thread is

    dedicated for request-response cycle ADVANTAGES ADVANTAGES Low conceptual overhead & Low scalability Better than TPC for many web usecases
  62. TPR CONT. TPR CONT. LIMITATIONS LIMITATIONS Thread gets block if

    we attempt to get data from DB/make a external http request
  63. TPR CONT. TPR CONT. LIMITATIONS LIMITATIONS Thread gets block if

    we attempt to get data from DB/make a external http request Limited concurrency as each thread has a fixed 1MB overhead
  64. TPR CONT. TPR CONT. LIMITATIONS LIMITATIONS Thread gets block if

    we attempt to get data from DB/make a external http request Limited concurrency as each thread has a fixed 1MB overhead USECASES USECASES
  65. TPR CONT. TPR CONT. LIMITATIONS LIMITATIONS Thread gets block if

    we attempt to get data from DB/make a external http request Limited concurrency as each thread has a fixed 1MB overhead USECASES USECASES Low volume enterprise applications
  66. SHARED THREADS (ST) SHARED THREADS (ST) No dedicated thread for

    request response cycle ADVANTAGES ADVANTAGES
  67. SHARED THREADS (ST) SHARED THREADS (ST) No dedicated thread for

    request response cycle ADVANTAGES ADVANTAGES Uses non blocking IO
  68. SHARED THREADS (ST) SHARED THREADS (ST) No dedicated thread for

    request response cycle ADVANTAGES ADVANTAGES Uses non blocking IO Single request-response cycle can be handled by multiple threads
  69. SHARED THREADS (ST) SHARED THREADS (ST) No dedicated thread for

    request response cycle ADVANTAGES ADVANTAGES Uses non blocking IO Single request-response cycle can be handled by multiple threads Very high concurrency as no thread is ever blocked
  70. ST CONT. ST CONT. LIMITATIONS LIMITATIONS Quite complex due to

    thread switching is assumed default Requires developers to know the consequences of thread switch
  71. ST CONT. ST CONT. LIMITATIONS LIMITATIONS Quite complex due to

    thread switching is assumed default Requires developers to know the consequences of thread switch USECASES USECASES
  72. ST CONT. ST CONT. LIMITATIONS LIMITATIONS Quite complex due to

    thread switching is assumed default Requires developers to know the consequences of thread switch USECASES USECASES High volume websites/applications
  73. ST CONT. ST CONT. LIMITATIONS LIMITATIONS Quite complex due to

    thread switching is assumed default Requires developers to know the consequences of thread switch USECASES USECASES High volume websites/applications Chat/push applications
  74. INTERFACES INTERFACES Publisher - Source of 1 (or) more events

    Subscriber - Consumer of events Subscription - Connection between Publisher & Subscriber
  75. INTERFACES INTERFACES Publisher - Source of 1 (or) more events

    Subscriber - Consumer of events Subscription - Connection between Publisher & Subscriber Processor - Both Publisher & Subscriber
  76. REACTIVE STREAMS: PROTOCOL REACTIVE STREAMS: PROTOCOL (SIMPLIFIED) (SIMPLIFIED) FEW ASPECTS

    FEW ASPECTS Backpressure Guarantees on onNext, onComplete & onError are quite helpful for common scenarios
  77. REACTIVE STREAMS: PROTOCOL REACTIVE STREAMS: PROTOCOL (SIMPLIFIED) (SIMPLIFIED) FEW ASPECTS

    FEW ASPECTS Backpressure Guarantees on onNext, onComplete & onError are quite helpful for common scenarios Read the spec here
  78. TYPES OF IMPLEMENTATIONS TYPES OF IMPLEMENTATIONS Highly efficient & Low

    resource implementation RxJava 2 & Reactor Heavyweight abstractions
  79. TYPES OF IMPLEMENTATIONS TYPES OF IMPLEMENTATIONS Highly efficient & Low

    resource implementation RxJava 2 & Reactor Heavyweight abstractions Akka
  80. BASICS BASICS FLUX FLUX Publisher of one or more events

    MONO MONO Publisher of zero or one events
  81. EXAMPLE 2 EXAMPLE 2 Flux.zip Zip elements with another stream

    elements zipWith A B D 1 2 C 4 3 E 1 A 2 B 3 C 4 D
  82. BEFORE SUBSCRIPTION BEFORE SUBSCRIPTION UPSTREAM DOWNSTREAM Backpressure Backpressure Subscriber Publisher

    2 Publisher 1 Merge Operator Similar to fitting all pipes in a plumbing
  83. BEFORE SUBSCRIPTION BEFORE SUBSCRIPTION UPSTREAM DOWNSTREAM Backpressure Backpressure Subscriber Publisher

    2 Publisher 1 Merge Operator Similar to fitting all pipes in a plumbing Nothing happens until you subscribe
  84. AFTER SUBSCRIPTION AFTER SUBSCRIPTION UPSTREAM DOWNSTREAM Backpressure Backpressure Subscriber Publisher

    2 Publisher 1 Merge Operator Elements flow only a er subscription Analogous to switching on motor & opening tap
  85. AFTER SUBSCRIPTION CONT. AFTER SUBSCRIPTION CONT. UPSTREAM DOWNSTREAM Backpressure Backpressure

    Subscriber Publisher 2 Publisher 1 Merge Operator Backpressure exerted thru Subscription, like valve
  86. CONCLUSION CONCLUSION We only touched the surface in this presentation

    Refer to documentation of & for more info Flux Mono
  87. CONCLUSION CONCLUSION We only touched the surface in this presentation

    Refer to documentation of & for more info Connect with me Flux Mono