Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What are Dead Letter Queues (DLQ) and how do you handle errors in message processing? Dead Letter Queues (DLQ) क्या हैं और message processing में errors कैसे handle करते हैं?

Answer

Dead Letter Queues hold messages that failed processing after retries. Spring Cloud Stream provides @ServiceActivator for DLQ handling, error recovery, and retry policies. Essential for production message processing reliability.

// Dead Letter Queue Configuration

@Configuration
public class DLQConfiguration {
    
    // Main queue
    @Bean
    public Queue mainQueue() {
        return new Queue('order.queue');
    }
    
    // Dead letter queue
    @Bean
    public Queue deadLetterQueue() {
        return new Queue('order.dlq');
    }
    
    @Bean
    public TopicExchange dlxExchange() {
        return new TopicExchange('order.dlx');
    }
    
    @Bean
    public Binding dlBinding() {
        return BindingBuilder.bind(deadLetterQueue())
            .to(dlxExchange())
            .with('*');
    }
}

// Consumer with DLQ handling
@Service
public class OrderProcessor {
    
    @RabbitListener(queues = 'order.queue')
    public void processOrder(Order order) throws Exception {
        try {
            if (order.getAmount() <= 0) {
                throw new InvalidOrderException('Invalid amount');
            }
            System.out.println('Processing order: ' + order.getId());
        } catch (Exception e) {
            // Will be sent to DLQ after max retries
            throw e;
        }
    }
    
    @RabbitListener(queues = 'order.dlq')
    public void handleFailedOrder(Order order) {
        System.err.println('Order failed: ' + order.getId());
        // Log, alert, manual intervention
    }
}

// Spring Cloud Stream DLQ
spring:
  cloud:
    stream:
      kafka:
        bindings:
          input:
            consumer:
              enable-dlq: true
              dlq-name: order.dlq
              max-attempts: 3
              backoff-initial-delay: 1000
              backoff-max-delay: 10000
              backoff-multiplier: 2.0

// Error handler
@Component
public class OrderErrorHandler {
    @ServiceActivator(inputChannel = 'order.errors')
    public void handleError(Message<?> message) {
        System.err.println('Error: ' + message.getPayload());
        // Send to DLQ, alert administrators
    }
}

// Retry configuration
@Configuration
public class RetryConfiguration {
    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();
        
        ExponentialBackOffPolicy backOffPolicy = 
            new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(1000);
        backOffPolicy.setMaxInterval(10000);
        backOffPolicy.setMultiplier(2.0);
        retryTemplate.setBackOffPolicy(backOffPolicy);
        
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(3);
        retryTemplate.setRetryPolicy(retryPolicy);
        
        return retryTemplate;
    }
}

// Benefits of DLQ:
// 1. Message preservation - don't lose failed messages
// 2. Debugging - analyze failure causes
// 3. Monitoring - alert on DLQ messages
// 4. Recovery - reprocess after fixing issues
// 5. Separation - keep main queue clean
Dead Letter Queue:

Failed messages को special queue में भेजना

Configuration:
1. Create DLQ
2. Set max-attempts (3 retries)
3. Backoff policy (exponential)
4. Handler for DLQ messages

Retry Policy:
Initial delay: 1 second
Max delay: 10 seconds
Multiplier: 2.0 (exponential)

When use करें:
- Message processing failures
- Invalid data
- Service unavailable
- Timeout errors

Benefits:
- Message preservation
- Debugging capability
- Automated recovery
- Monitoring alert

Was this answer clear?