Sei sulla pagina 1di 2

Spring Bean Scope: Singleton by default (use for stateless) otherwise

prototype(stateful like shopping cart)

A transient failure is a failure which is short term and unpredictable and corrects
itself automatically (e.g. application restarts or network connectivity fluctuations or
timeouts).It becomes worse when there is chain of dependency between
applications and one application failing due to a transient failure results into
cascading failures.

On the other hand there are certain scenarios where failures can be long term and
might take couple of hours or even days to fix .In such scenarios it is not practical
to keep calling dependent service resulting in failed requests as this would
consume unnecessary resources (memory ,threads, db connections etc.) and
bandwidth.

There are specific patterns which can deal with both of these situations.

Retry pattern
Retry pattern deals with the first situation where we don’t want our application to
fail because of transient faults.In such case a retry pattern can just retry (either
endlessly or for a particular number of times) and overcome the transient fault

Circuit Breaker pattern


This pattern deals with the second situation where you don’t want to keep calling a
service which is down and it will take significant time to bring it up. Such a pattern
can be configured to allow certain number of exceptions to occur before it breaks
the circuit i.e. it does not allow the dependent service to be called any more.This is
called as circuit is in open state.

o you implement a retry pattern to deal with transient failures but in case problem
is not solved on multiple retries circuit breaker comes into play and breaks the
circuit.
What’s a CompletableFuture?

CompletableFuture is used for asynchronous programming in Java.


Asynchronous programming is a means of writing non-blocking code by
running a task on a separate thread than the main application thread and
notifying the main thread about its progress, completion or failure.
This way, your main thread does not block/wait for the completion of the
task and it can execute other tasks in parallel.

you might be wondering that - Well, I know that


the runAsync() and supplyAsync() methods execute their tasks in a
separate thread. But, we never created a thread right?
Yes! CompletableFuture executes these tasks in a thread obtained from the
global ForkJoinPool.commonPool().

Transforming and acting on a CompletableFuture

The CompletableFuture.get() method is blocking. It waits until the


Future is completed and returns the result after its completion.
But, that’s not what we want right? For building asynchronous systems we
should be able to attach a callback to the CompletableFuture which should
automatically get called when the Future completes.
That way, we won’t need to wait for the result, and we can write the logic
that needs to be executed after the completion of the Future inside our
callback function.
You can attach a callback to the CompletableFuture
using thenApply() , thenAccept() and thenRun() methods -

Potrebbero piacerti anche