What is Asynchronous Programming with an illustrating Example in Spring Boot
In this week's newsletter, let us explore the concept of asynchronous programming, why it is useful, when it is not useful with an illustrating example in Spring Boot
When you execute your code, the OS creates and assigns a thread where the application runs. This is called the Main Thread or the UI Thread.
When an intensive task such as reading a file or a database call happens and the main thread waits for the operation to complete, it freezes the application making it unavailable for other requests.
Asynchronous programming is a paradigm that solves this issue by letting the main thread to run these blocking operations in the background without making the main thread wait for it.
The main thread is notified when the background operation is complete so that it can resume from where it has split.
It does it in 3 simple steps –
The main thread starts an asynchronous task and gives the blocking operation to it.
It doesn’t wait for the result and moves on with the other requests or code.
When the asynchronous task is complete, the main thread is notified of the result and the main thread then resumes the execution.
Advantages of Asynchronous Programming
Non-blocking Operations
Better Resource Utilization
Improved Scalability
Responsive APIs
Avoiding Thread Contention
Issues with using Asynchronous Programming
Complicates the execution flow
Benefits are minimal for low traffic applications
Exception Handling becomes complex
Transaction management becomes bit tough
That doesn’t mean we shouldn’t use it at all..
Read the complete article here - https://referbruv.com/blog/asynchronous-programming-in-spring-boot-with-an-example/

