Spring Framework
Async Controllers
Synchronous Processing
Web Development
Java

Async Spring Controllers vs normal Controllers

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Spring Framework, with its extensive capabilities, provides multiple ways to handle requests and control behavior via controllers. These controllers can be synchronous or asynchronous, depending on the application's needs. Each type of controller comes with its own set of benefits and trade-offs.

What Are Spring Controllers?

In Spring, a controller is a class that handles HTTP requests and supplies appropriate responses. It typically combines @Controller or @RestController annotations, alongside request mapping annotations (like @RequestMapping).

Normal Controllers

Normal controllers, also known as synchronous controllers, handle request and response sequentially. When a request arrives, the server dedicates a thread to work on it, blocking further execution until the task is complete.

  1. Key Characteristics:
    • Synchronous Processing: Each request is processed in sequence.
    • Blocking I/O: Threads wait for operations (like database queries) to complete before proceeding.
    • Simplicity: Easier to implement and test due to their straightforward nature, making them advantageous for applications with simple business logic.
  2. Example:
    • Asynchronous Processing: Returns a future-like object (such as `CompletableFuture`) so the execution can continue without waiting.
    • Non-blocking I/O: Frees the main thread while I/O is in progress.
    • Concurrency: Useful for high-volume I/O-bound operations.
    • `CompletableFuture`: Used to run asynchronous tasks.
    • `DeferredResult`: Used for returning results asynchronously.
    • `WebAsyncTask`: Provides timeout options along with async processing.
  • Use Sync Controllers when the application handles a moderate amount of traffic, or the operations are primarily CPU-bound.
  • Use Async Controllers for high-latency or I/O-heavy applications, where efficient management of server threads is critical.
    • Asynchronous controllers scale better in some scenarios due to non-blocking I/O, allowing more concurrent requests to be handled.
    • Reduced memory footprint compared to synchronous processing as fewer threads are needed.
    • Take advantage of multi-core processing, leveraging concurrency.
    • Asynchronous code can often become more complex with the introduction of additional constructs like callbacks and futures.
    • Proper configuration is required to handle timeouts and exceptions effectively in asynchronous processing, using methods like `exceptionally` or `handle` in `CompletableFuture`.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.