소비자 확인 및 재큐잉
소비자 확인을 익히고 처리에 실패했을 때 메시지를 다시 큐에 넣는 방법을 배웁니다. 오류를 우아하게 처리할 수 있는 장애 허용 소비자를 설계합니다.
소비자 확인 및 재큐잉은(는) CoddyKit의 무료 RabbitMQ Messaging & Async Systems 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 RabbitMQ Messaging & Async Systems 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. RabbitMQ Messaging & Async Systems 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Reliable Message Consumption
In distributed systems, ensuring messages are processed correctly is vital. What happens if a consumer crashes mid-processing? Or if a message causes an error?
This lesson explores consumer acknowledgements and requeuing, essential techniques for building fault-tolerant message consumers.
ACKs: The Handshake
A consumer acknowledgement (ACK) is a signal sent by the consumer back to RabbitMQ. It tells the broker: "I've successfully received and processed this message."
- Without an ACK, RabbitMQ assumes the message hasn't been processed.
- This mechanism prevents message loss if a consumer fails before finishing its work.
Automatic vs. Manual ACKs
RabbitMQ offers two ways to acknowledge messages:
- Automatic (
autoAck=true): Messages are acknowledged immediately upon delivery to the consumer. Simple, but risky if processing fails. - Manual (
autoAck=false): The consumer explicitly sends an ACK after successful processing. This is the default and recommended for reliable systems.
We'll focus on manual acknowledgements for robustness.
Sending a Message (Producer)
Let's set up a simple Java producer to send a message. This message will be consumed later, and we'll apply manual acknowledgements.
Run this code to send a "Hello RabbitMQ!" message.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.nio.charset.StandardCharsets;
public class MyProducer {
private final static String QUEUE_NAME = "ack_queue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost"); // Connect to local RabbitMQ
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello RabbitMQ!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
}
}
}Manual Acknowledgements in Action
Now, let's create a consumer that uses manual acknowledgements. Notice the autoAck parameter is set to false when consuming.
The channel.basicAck() call confirms successful processing.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.nio.charset.StandardCharsets;
public class MyConsumerAck {
private final static String QUEUE_NAME = "ack_queue";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + message + "'");
try {
// Simulate processing work
Thread.sleep(1000);
System.out.println(" [x] Done processing '" + message + "'");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); // Manual ACK!
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println(" [!] Processing interrupted.");
// In a real app, you might re-queue or handle error
}
};
// autoAck is set to false for manual acknowledgements
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
}
}When Processing Fails
What if our consumer encounters an error during message processing? If we only use basicAck, the message is lost even if processing wasn't completed.
RabbitMQ provides ways to inform the broker that a message could not be processed successfully, giving us options to handle it.
Requeuing Failed Messages
If a message fails processing due to a transient error (e.g., database connection down), you might want to retry it later. Use channel.basicNack(deliveryTag, multiple, requeue) or channel.basicReject(deliveryTag, requeue).
- Setting
requeuetotruesends the message back to the queue for another consumer to pick up.
Run this consumer. It will intentionally fail twice and then successfully process the message.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
public class MyConsumerRequeue {
private final static String QUEUE_NAME = "ack_queue";
private static int attemptCount = 0;
public static void main(String[] argv) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
long deliveryTag = delivery.getEnvelope().getDeliveryTag();
System.out.println(" [x] Received '" + message + "' (Attempt: " + (++attemptCount) + ")");
try {
if (attemptCount <= 2) { // Simulate failure for first 2 attempts
throw new RuntimeException("Simulated processing error!");
}
// Simulate successful processing
Thread.sleep(1000);
System.out.println(" [x] Successfully processed '" + message + "'");
channel.basicAck(deliveryTag, false); // ACK on success
} catch (Exception e) {
System.err.println(" [!] Error processing '" + message + "': " + e.getMessage());
channel.basicNack(deliveryTag, false, true); // NACK and requeue!
System.out.println(" [!] Message '" + message + "' requeued.");
}
};
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
}
}Discarding Problematic Messages
Sometimes, a message is fundamentally flawed and will *always* cause an error. Requeuing it repeatedly is pointless and can lead to a "poison message" loop.
In such cases, set requeue to false. This discards the message from the queue. Often, these messages are sent to a Dead Letter Exchange (DLX) for later inspection, but we'll cover DLX in a future lesson.
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
public class MyConsumerReject {
private final static String QUEUE_NAME = "ack_queue";
public static void main(String[] argv) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
long deliveryTag = delivery.getEnvelope().getDeliveryTag();
System.out.println(" [x] Received '" + message + "'");
// Simulate an unrecoverable error
System.err.println(" [!] Fatal error for '" + message + "'. Discarding.");
channel.basicNack(deliveryTag, false, false); // NACK and DO NOT requeue!
System.out.println(" [!] Message '" + message + "' rejected (not requeued).");
};
channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
}
}Idempotent Consumers
When you requeue messages, there's a chance a consumer might process the same message multiple times.
It's crucial to design your consumers to be idempotent. This means processing the same message twice (or more) produces the same result as processing it once, without unintended side effects.
Reliability Check
Consider a RabbitMQ consumer configured with manual acknowledgements. If a message is received but the consumer crashes *before* calling channel.basicAck() or channel.basicNack(), what typically happens to that message?
Recap: Building Reliable Consumers
You've learned how to make your RabbitMQ consumers resilient:
- Manual Acknowledgements: Give consumers control over message fate.
- Requeuing: Retry messages for transient failures using
basicNack(..., true). - Discarding: Prevent poison message loops with
basicNack(..., false). - Idempotency: Design consumers to handle duplicate deliveries gracefully.
These techniques are fundamental for building robust, fault-tolerant messaging systems.
자주 묻는 질문
“소비자 확인 및 재큐잉” 강의는 무료인가요?
네 — “소비자 확인 및 재큐잉” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 RabbitMQ Messaging & Async Systems 강의 전체를 잠금 해제할 수 있습니다. RabbitMQ Messaging & Async Systems 강의에는 총 4개의 강의가 포함되어 있습니다.
“소비자 확인 및 재큐잉”에서 뭘 배우나요?
소비자 확인을 익히고 처리에 실패했을 때 메시지를 다시 큐에 넣는 방법을 배웁니다. 오류를 우아하게 처리할 수 있는 장애 허용 소비자를 설계합니다. 브라우저에서 직접 실행하는 실습 코드로 RabbitMQ Messaging & Async Systems을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
RabbitMQ Messaging & Async Systems을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 RabbitMQ Messaging & Async Systems은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“소비자 확인 및 재큐잉” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 RabbitMQ Messaging & Async Systems 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 RabbitMQ Messaging & Async Systems 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 영속 메시지와 대기열
- 신뢰성을 위한 퍼블리셔 확인
- 소비자 확인 및 재큐잉
- 트랜잭션과 발행자 확인 비교