{"id":58064,"date":"2023-08-26T17:56:51","date_gmt":"2023-08-26T12:26:51","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58064"},"modified":"2023-08-31T17:59:14","modified_gmt":"2023-08-31T12:29:14","slug":"why-not-completablefuture-reactive-programming-is-the-rescue","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/why-not-completablefuture-reactive-programming-is-the-rescue\/","title":{"rendered":"Why not CompletableFuture? Reactive Programming is the Rescue!"},"content":{"rendered":"<p id=\"2712\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">For many use cases, when you have to perform compute-intensive work, make network calls, or execute some parallel processing, we tend to go with multi-threading. Some temporary threads are spawned to process smaller tasks and merged back to the main request thread post-processing.<\/p>\n<h3 id=\"cf4a\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\"><strong class=\"kz fp\">CompletableFutures<\/strong><\/h3>\n<p id=\"3ddf\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\"><a class=\"af lv\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/concurrent\/CompletableFuture.html\" target=\"_blank\" rel=\"noopener ugc nofollow\">CompletableFutures<\/a>\u00a0were introduced in Java 8 (2014).<\/p>\n<p id=\"f7e8\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">The CompletableFuture API is a high-level API for asynchronous programming in Java. This API supports pipelining (also known as chaining or combining) of multiple asynchronous computations into a single result without the mess of nested callbacks (\u201ccallback hell\u201c). This API also is an implementation of the future\/promise concurrency constructs in Java.<\/p>\n<p id=\"5c05\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">You can use them to tell some worker thread to \u201cgo do some task X, and when you\u2019re done, go do the task Y, i.e. dependent on the result of X\u201d. Using CompletableFutures, you can do something with the result of the previous operation, without actually blocking a thread to wait for the result.<\/p>\n<p id=\"21fe\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">CompletableFuture class belongs to the java.util.concurrent package.<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-58061\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_1-1.png\" alt=\"\" width=\"706\" height=\"215\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_1-1.png 706w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_1-1-300x91.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_1-1-624x190.png 624w\" sizes=\"(max-width: 706px) 100vw, 706px\" \/><\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-58062\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_2.png\" alt=\"\" width=\"698\" height=\"469\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_2.png 698w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_2-300x202.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_2-624x419.png 624w\" sizes=\"(max-width: 698px) 100vw, 698px\" \/><\/p>\n<p data-selectable-paragraph=\"\">The above two future run asynchronously<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-58063\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_3.png\" alt=\"\" width=\"687\" height=\"291\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_3.png 687w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_3-300x127.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Blog1_3-624x264.png 624w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/p>\n<p id=\"f538\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">This shows the blocking nature of Concurrency APIs \u2014 Completable Future<\/p>\n<p id=\"11f9\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">Therefore, the Completable Future fails for the many transactions per second requests. Example 10K T\/s<\/p>\n<p id=\"e8f7\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\"><strong class=\"kz fp\">Reactive Programming is the Rescue!<\/strong><\/p>\n<p id=\"bbb5\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\"><strong class=\"kz fp\">Reactive programming is a programming paradigm where the focus is on developing asynchronous and non-blocking components<\/strong>. The core of reactive programming is a data stream that we can observe and react to, and even apply back pressure to as well. This leads to non-blocking execution and better scalability with fewer threads of execution.<\/p>\n<p id=\"a293\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">Blocking calls in any program often\u00a0<strong class=\"kz fp\">results in critical resources just waiting for things to happen<\/strong>. These include database calls, calls to web services, and file system calls. If we can free up threads of execution from this waiting and provide a mechanism to circle back once results are available, it\u2019ll yield much better resource utilization.<\/p>\n<p id=\"44f3\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">This is what adopting the reactive programming paradigm does for us. While it\u2019s possible to switch over to a reactive library for many of these calls, it may not be possible for everything. For us, fortunately, Spring makes it much easier to use reactive programming with MongoDB and REST APIs.<\/p>\n<p id=\"42f8\" class=\"pw-post-body-paragraph kx ky fo kz b la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr ls lt lu fh bj\" data-selectable-paragraph=\"\">The four characteristics of a Reactive System are:<\/p>\n<ul class=\"\">\n<li id=\"d6c2\" class=\"kx ky fo kz b la lb lc ld le lf lg lh ma lj lk ll mb ln lo lp mc lr ls lt lu ms mt mu bj\" data-selectable-paragraph=\"\"><strong><em class=\"lz\">Responsive<\/em><\/strong>: The adoption of the reactive programming paradigm should help us achieve end-to-end non-blocking and, hence, a responsive application.<\/li>\n<li id=\"e96d\" class=\"kx ky fo kz b la mv lc ld le mw lg lh ma mx lk ll mb my lo lp mc mz ls lt lu ms mt mu bj\" data-selectable-paragraph=\"\"><strong><em class=\"lz\">Resilient<\/em><\/strong>: Kubernetes deployment with ReplicaSet of the desired number of pods should provide resilience against random failures.<\/li>\n<li id=\"85af\" class=\"kx ky fo kz b la mv lc ld le mw lg lh ma mx lk ll mb my lo lp mc mz ls lt lu ms mt mu bj\" data-selectable-paragraph=\"\"><strong><em class=\"lz\">Elastic<\/em><\/strong>: Kubernetes cluster and resources should provide us the necessary support to be elastic in the face of unpredictable loads.<\/li>\n<li id=\"c13a\" class=\"kx ky fo kz b la mv lc ld le mw lg lh ma mx lk ll mb my lo lp mc mz ls lt lu ms mt mu bj\" data-selectable-paragraph=\"\"><strong><em class=\"lz\">Message-Driven<\/em><\/strong>: Having all service-to-service communication handled asynchronously through a Kafka broker should help us here.<\/li>\n<\/ul>\n<p>Please refer to our blogs for more insightful content and comment if you have any questions regarding the topic.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>For many use cases, when you have to perform compute-intensive work, make network calls, or execute some parallel processing, we tend to go with multi-threading. Some temporary threads are spawned to process smaller tasks and merged back to the main request thread post-processing. CompletableFutures CompletableFutures\u00a0were introduced in Java 8 (2014). The CompletableFuture API is a [&hellip;]<\/p>\n","protected":false},"author":1623,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":423},"categories":[446],"tags":[5352,1557,4844,5351],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58064"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1623"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58064"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58064\/revisions"}],"predecessor-version":[{"id":58251,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58064\/revisions\/58251"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}