Message Queues
Message queues receive, hold, and deliver messages. If an operation is too slow to perform inline, you can use a message queue with the following workflow:- An application publishes a job to the queue, then notifies the user of job status
- A worker picks up the job from the queue, processes it, then signals the job is complete
Popular Message Queue Solutions
Redis is useful as a simple message broker but messages can be lost. RabbitMQ is popular but requires you to adapt to the ‘AMQP’ protocol and manage your own nodes. Amazon SQS is hosted but can have high latency and has the possibility of messages being delivered twice.Task Queues
Tasks queues receive tasks and their related data, runs them, then delivers their results. They can support scheduling and can be used to run computationally-intensive jobs in the background. Celery has support for scheduling and primarily has python support.Back Pressure
If queues start to grow significantly, the queue size can become larger than memory, resulting in cache misses, disk reads, and even slower performance. Back pressure can help by limiting the queue size, thereby maintaining a high throughput rate and good response times for jobs already in the queue. Once the queue fills up, clients get a server busy or HTTP 503 status code to try again later. Clients can retry the request at a later time, perhaps with exponential backoff.How Back Pressure Works
- Monitor queue size
- When queue reaches capacity threshold, reject new requests
- Return HTTP 503 (Service Unavailable) to clients
- Clients retry with exponential backoff
- System maintains high throughput for existing jobs
Message Queues vs Task Queues: Message queues focus on reliable message delivery between services, while task queues are designed for executing background jobs and computational tasks.
Disadvantages of Asynchronism
Disadvantages of Asynchronism
- Use cases such as inexpensive calculations and realtime workflows might be better suited for synchronous operations, as introducing queues can add delays and complexity.
Use Cases for Asynchronous Processing
- Email notifications
- Image/video processing
- Data analytics and aggregation
- Batch processing
- Long-running computations
- Third-party API calls
- Report generation
