Description
In computer science, a queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. This means the first element added to the queue is the first one to be removed — much like a line of people waiting at a bus stop. Queues are fundamental in computing, appearing in task scheduling, resource management, data buffering, network communication, and many real-time systems.
Conceptually simple but immensely powerful, the queue plays a vital role in the internal mechanics of operating systems, web servers, multithreaded applications, and even user interface rendering.
Queue Characteristics
| Feature | Description |
|---|---|
| FIFO Order | First item in is the first to be processed |
| Enqueue | Operation to insert an item at the rear of the queue |
| Dequeue | Operation to remove and return the item at the front |
| Front/Head | Element to be dequeued next |
| Rear/Tail | Position where new elements are enqueued |
| Dynamic Size | Can grow or shrink dynamically in most implementations |
Real-World Analogy
Think of a checkout line at a grocery store:
- Customers arrive and get in line (enqueue).
- The cashier serves the customer at the front (dequeue).
- New arrivals join at the rear.
- No one jumps the line — fairness and order are preserved.
Queue Operations (Abstract Data Type)
Queue:
- enqueue(element): Add to the end
- dequeue(): Remove and return from the front
- peek(): View front element without removing
- isEmpty(): Check if queue is empty
- size(): Return number of items
Types of Queues
| Type | Description |
|---|---|
| Simple Queue | Basic FIFO structure |
| Circular Queue | Connects rear back to front when end is reached (fixed-size optimization) |
| Priority Queue | Elements are dequeued based on priority, not arrival time |
| Deque (Double-Ended Queue) | Insert/remove from both ends |
| Concurrent Queue | Thread-safe for multithreaded applications |
| Message Queue | Used for inter-process or inter-service communication |
Queue in Programming
Python (Using collections.deque)
from collections import deque
queue = deque()
queue.append("Task1") # Enqueue
queue.append("Task2")
print(queue.popleft()) # Dequeue → "Task1"
Java (Using Queue Interface)
Queue q = new LinkedList<>();
q.add("Task1");
q.add("Task2");
System.out.println(q.remove()); // "Task1"
JavaScript (Custom Queue)
class Queue {
constructor() {
this.items = [];
}
enqueue(item) {
this.items.push(item);
}
dequeue() {
return this.items.shift();
}
}
Queue in Operating Systems
Queues are essential in OS design:
- Job Scheduling: Processes waiting for CPU time are queued.
- Print Spooling: Print jobs are stored in a queue.
- Interrupt Handling: Hardware events are queued and handled sequentially.
- Multitasking: Ready queue, waiting queue, blocked queue for processes.
Queue in Networking
- Router Buffers: Incoming packets are queued before forwarding.
- QoS (Quality of Service): Different traffic classes assigned to priority queues.
- Rate Limiting: API requests placed into queues to enforce limits.
- Asynchronous Messaging: Email, SMS, or IoT events queued until processed.
Message Queues (Asynchronous Communication)
In distributed systems, message queues allow different components or services to communicate without tight coupling.
Popular systems:
| Tool | Description |
|---|---|
| RabbitMQ | Lightweight message broker (AMQP-based) |
| Kafka | Distributed event streaming platform |
| AWS SQS | Scalable, serverless queue service in the cloud |
| Redis | Supports queues via list operations (LPUSH, RPOP) |
Example Use Case:
- A user places an order → the app enqueues a “payment processing” task.
- A worker service consumes the queue asynchronously and handles the payment.
Priority Queue
In a priority queue, each element is associated with a priority, and elements with higher priority are dequeued first.
- Used in Dijkstra’s algorithm for shortest path.
- Employed in task scheduling, CPU job selection, AI pathfinding.
Python with heapq:
import heapq
pq = []
heapq.heappush(pq, (1, "Low"))
heapq.heappush(pq, (0, "High"))
print(heapq.heappop(pq)) # (0, "High")
Circular Queue
Solves the problem of memory wastage in fixed-size queues by treating the queue as a circle.
- When the end is reached, it wraps around to the beginning.
- Used in buffering, embedded systems, telecommunication devices.
Queue in Algorithm Design
Queues are used in:
- Breadth-First Search (BFS)
- Level-order tree traversal
- Simulation and modeling
- Producer-consumer problems
- Load balancing
Visualization of Queue (Simple)
[Head] → [A] → [B] → [C] → [Tail]
enqueue("D") → [A] → [B] → [C] → [D]
dequeue() → returns A → [B] → [C] → [D]
Queue vs Stack
| Feature | Queue (FIFO) | Stack (LIFO) |
|---|---|---|
| Insertion | End (rear) | Top |
| Removal | Front | Top |
| Use Case | Scheduling, buffers | Recursion, backtracking |
Common Pitfalls
- Queue Overflow: Trying to enqueue when full (in static arrays)
- Queue Underflow: Trying to dequeue when empty
- Off-by-One Errors: Especially in circular queue indexing
- Concurrency Issues: Without locks or atomic operations, data corruption may occur
Real-World Systems That Use Queues
| System | Queue Role |
|---|---|
| Web Servers | Queue incoming HTTP requests for worker threads |
| Print Servers | Manage document order in print jobs |
| Task Schedulers | Queue periodic or scheduled system tasks |
| Payment Systems | Queue payment processing and retries |
| Cloud Functions | Triggered by queued messages or events |
Thread-Safe Queues
For concurrent applications:
- Java:
ConcurrentLinkedQueue,BlockingQueue - Python:
queue.Queue - C++:
std::queue+ mutex - Go: Use channels (
chan) for safe queuing
Example in Python:
import queue
q = queue.Queue()
q.put("task1")
q.get() # thread-safe dequeue
Related Terms
- FIFO (First In First Out)
- Stack
- Deque
- Priority Queue
- Scheduler
- Message Broker
- Asynchronous Processing
- Producer-Consumer
- Thread-safe
- Buffer
- Job Queue
- Queue Overflow
- Circular Buffer
- RabbitMQ
- Event Loop









