Skip to main content

Queue

Queue is a data structure or service that temporarily holds tasks, messages, or requests in a First-In-First-Out (FIFO) manner until they can be processed. It acts as a buffer between components that produce and consume data at different speeds.

Why Use a Queue

  1. Decoupling

    • Producers (e.g., web servers) and consumers (e.g., workers) operate independently.
    • A failure or slowdown in one does not directly impact the other.
  2. Scalability

    • Multiple consumers can be added to process messages in parallel.
  3. Load Buffering

    • Sudden spikes in traffic can be absorbed by the queue instead of overwhelming the system.
  4. Reliability and Persistence

    • Messages can be persisted until processed, ensuring tasks aren’t lost on failure.
  5. Asynchronous Processing

    • Time-consuming tasks (e.g., video rendering, email sending) can be deferred without blocking user requests.

Types of Queue

Queue TypeDescriptionExample Use Case
Message QueueCarries messages between services.RabbitMQ, ActiveMQ, Amazon SQS
Task QueueHolds jobs/tasks for background workers.Celery (Python), Sidekiq (Ruby)
Priority QueueTasks with higher priority go first.Support ticketing system
Delay QueueMessages are held for a period before sent.Scheduled notifications

Where does a queue build up

Layer / ComponentWhere the Queue BuildsReason
Web Server / Load BalancerIncoming request queueToo many simultaneous user requests
Message Queue System (e.g., Kafka, RabbitMQ, SQS)Task queueWorker is slow or unavailable
DatabaseConnection pool queueToo many concurrent queries, slow queries
Thread PoolsThread/task execution queueCPU-bound or I/O-bound bottlenecks
Disk / File I/OFile write buffer queueSlow disks, too many write ops
API GatewayRequest queue for backend serviceBackend throttled or overloaded
Cache Layer (e.g., Redis)Command execution queueToo many cache ops / eviction pressure

Example of Queue

A user uploads an image, and the system must resize it into multiple resolutions (thumbnail, medium, high-res).

Without a Queue (Synchronous):

  • Upload → Resize → Save → Respond
  • Slow, blocks user request

With a Queue (Asynchronous):

  1. User uploads image
  2. API Server stores image metadata and sends a task to the queue:
{
"image_id": "123",
"resize_sizes": ["100x100", "300x300"]
}
  1. Queue holds the task
  2. Worker picks up the task, resizes the image, and saves the results
  3. User gets a fast response, and image processing happens in the background

Queue = buffer between upload and processing

Common Queue Tools

ToolDescription
RabbitMQOpen-source message broker
KafkaDistributed streaming platform
Amazon SQSFully managed message queue service
RedisCan be used for simple in-memory queues
CeleryDistributed task queue in Python

Strategy to Prevent Queue

StrategyPreventsExample Tool/Tech
Auto-scale consumersSlow processingAWS Lambda, Kubernetes
Rate limitingBurst producer loadAPI Gateway, NGINX
Queue monitoringSilent queue growthPrometheus, CloudWatch
BackpressureSystem overloadNode.js, Kafka
DLQRetry storm, poisoned messagesSQS DLQ, RabbitMQ
Timeouts + exponential retryWorker hang, retry floodsCelery, Sidekiq
Priority or multiple queuesStarvation of urgent tasksRabbitMQ, Redis, Kafka