Backend Development 10 min read

Practical Application Scenarios of Queues in System Design

This article explains how queues—such as message queues, task queues, and data‑bus queues—are applied in backend systems for asynchronous processing, system decoupling, data synchronization, traffic shaping, buffering, and other architectural patterns to improve scalability and reliability.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Practical Application Scenarios of Queues in System Design

Queues are linear data structures that enable insertion at one end and removal at the other, and are widely used in application layers to solve various problems such as asynchronous processing, system decoupling, data synchronization, traffic shaping, buffering, and rate limiting.

Asynchronous processing : Using queues allows tasks like sending registration emails, updating caches, or writing logs to be processed outside the main request flow, improving response time and enabling batch processing.

System decoupling : After a user completes a payment, multiple downstream services (inventory, invoicing, recommendation, etc.) can be notified via a message or task queue, allowing each service to operate independently and achieve eventual consistency.

Data synchronization : Queues such as data‑bus, Canal, or Otter can synchronize changes from MySQL to Redis, MongoDB, or across data centers while preserving order.

Traffic shaping (peak shaving) : Queues can buffer write‑heavy operations (e.g., stock deduction, order placement) to protect database bottlenecks and implement rate limiting, especially in flash‑sale scenarios.

Buffer queues : Log4j’s buffered writer example shows how a byte buffer can flush to disk, and an AsyncAppender can provide true asynchronous logging.

Task queues : Thread‑pool based queues (LinkedBlockingQueue) and high‑performance Disruptor rings are used to offload non‑critical work, with considerations for persistence, retry, and failure handling.

Message queues : Examples include self‑built JMQ, ActiveMQ, Kafka, and Redis. Common patterns are point‑to‑point and publish‑subscribe, enabling services to react to events like user registration or order status changes without tight coupling.

Request queues : In web environments, requests can be queued for traffic control, prioritization, and isolation, ensuring that failures in one queue do not affect others.

Data‑bus queues : Used for pure data‑level synchronization across systems, guaranteeing ordered delivery.

Hybrid queues : Combining message queues with Redis lists can improve throughput and provide durability; example code shows moving items between waiting and processing queues with rightPopAndLeftPush and handling network exceptions:

try {
    id = queueRedis.opsForList().rightPopAndLeftPush(queueName, processingQueueName);
} catch (Exception e) {
    String msg = queueName + " to " + processingQueueName + " rpoplpush error";
    LOG.error(msg, e);
    // alarm code
}

Failure handling may involve three retry attempts followed by insertion into a fail‑queue that de‑duplicates using a Redis Lua script:

static EventQueueScript ADD_TO_FAIL_QUEUE_REDIS_SCRIPT = new EventQueueScript(
    "redis.call('lrem', KEYS[1], 1, ARGV[1]) redis.call('lrem', KEYS[2], 1, ARGV[1]) return redis.call('lpush', KEYS[2], ARGV[1])"
);

Additional considerations include priority queues, replica queues for replay, mirror queues for scaling, appropriate concurrency settings, push vs. pull design, and message aggregation strategies.

Message Queuedata synchronizationasynchronous processingTask Queuetraffic shapingSystem DecouplingQueues
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.