I am implementing a backend with a typescript framework called nestjs and I was reading some documentation about their usage of queues. https://docs.nestjs.com/techniques/queues I have never used queues between services and I don't understand why or when I should either. (hence my question)
So I had some thoughts regarding some of the statements regarding why queues are useful in the documentation. This is just to provide context to my question.
Smooth out processing peaks. For example, if users can initiate resource-intensive tasks at arbitrary times, you can add these tasks to a queue instead of performing them synchronously.
- If a user has to initiate a cpu intensive task on the server, and the queue should handle performing the code asynchronously (waiting until the server isn't under load), then I don't see it being directly beneficial, since instead of a lot of users should wait a bit for the user asking for the backend to do the syncrhonous task to be performed then the user asking for it would potentially wait a long time. If neither of the outcomes were relevant, then the queue wouldn't matter in the first place?
Break up monolithic tasks that may otherwise block the Node.js event loop. For example, if a user request requires CPU intensive work like audio transcoding, you can delegate this task to other processes, freeing up user-facing processes to remain responsive.
- So if a user would maybe upload some music to a external service out of the scope of the nestjs application, then the queue api would assign the task to the queue and respond a 201 to the user? Isn't most webservers implemented with queues in the first place? Since a server can respond to multiple requests asynchronously? Would this only be relevant if the external service uses another protocol than http then?
So why and when is queues used in backend architectures?