Search code examples
springasynchronouswebsocketspring-websocket

Advantages / Drawbacks of Spring Async vs. Websocket


What are the main differences, advantages and drawbacks of using Spring @Async and Spring Websockets for long background tasks like database queries? Are both equally used or are there special cases where one of them fits better?


Solution

  • Relatively infrequent long background tasks are cases for @Async. Websocket is more appropriate for intensive communication between client and server like chat clients.

    From the Spring docu:

    The best fit for WebSocket is in web applications where the client and server need to exchange events at high frequency and with low latency. Prime candidates include, but are not limited to, applications in finance, games, collaboration, and others. Such applications are both very sensitive to time delays and also need to exchange a wide variety of messages at a high frequency.

    For other application types, however, this may not be the case. For example, a news or social feed that shows breaking news as it becomes available may be perfectly okay with simple polling once every few minutes. Here latency is important, but it is acceptable if the news takes a few minutes to appear.

    Even in cases where latency is crucial, if the volume of messages is relatively low (e.g. monitoring network failures) the use of long polling should be considered as a relatively simple alternative that works reliably and is comparable in terms of efficiency (again assuming the volume of messages is relatively low).

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html