Kafka
REST API
Error Handling
Message Consumption
Service Disruption

How to stop consuming message from kafka and stop calling REST API call of another service in case of failure

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When working with Kafka as a messaging system and REST APIs for external services, handling failures efficiently is crucial to maintain data integrity, performance, and reliability of applications. Below, we discuss strategies and technical implementations to stop consuming messages from Kafka and halt REST API calls in the event of failures.

Understanding Kafka and REST API Failure Dynamics

Kafka is a distributed streaming platform capable of handling high volumes of data and is widely used for building real-time streaming data pipelines and applications. REST APIs, on the other hand, are used to enable communication between different software applications over the internet.

The integration typically involves consuming messages from Kafka, processing these messages, and subsequently making REST API calls based on the processed data. However, failures can occur due to various reasons such as network issues, data deserialization errors, and failures in downstream systems handled by the REST APIs.

Key Strategies to Handle Failures

1. Error Handling in Kafka Consumers

In Kafka, consumer error handling is crucial. Depending on the nature of the failure, the consumer has a few strategies to choose from:

  • Commit Management: Kafka consumers can manage offset commits manually. In case of a processing error, the consumer can refrain from committing the offset of the faulty message. This ensures that the message is re-read and reprocessed until successful.
  • Retry Mechanisms: Implement a retry mechanism where the consumer retries consuming and processing the message a specified number of times before giving up or signaling an alert.
  • Dead Letter Queue: Utilizing a Kafka topic as a dead letter queue to store messages that couldn't be processed after several retries.

2. Circuit Breaker for REST API Calls

Implement a circuit breaker pattern when making REST API calls. This pattern prevents an application from performing operations that are likely to fail, rather allows it to continue running without waiting for the failed service to recover. The circuit breaker maintains a state:

  • Closed: The API calls pass through without issues.
  • Open: If continuous failures reach a threshold, further calls are stopped for a predetermined time.
  • Half-open: After the time-out, a limited number of test calls are allowed. If these succeed, the circuit closes again; if they fail, it remains open for another period.

Implementations

Let's consider a Java-based Kafka consumer application using Spring Kafka and a circuit breaker implementation using Resilience4J for handling REST API calls.

java
1@Service
2public class KafkaConsumerService {
3    private final RestService restService;
4    
5    @Autowired
6    public KafkaConsumerService(RestService restService) {
7        this.restService = restService;
8    }
9
10    @KafkaListener(topics = "your_topic_name", groupId = "your_group_id")
11    public void consume(String message) {
12        try {
13            DataObject data = processData(message);
14            restService.callExternalAPI(data);
15        } catch (Exception e) {
16            // Possible handling: log error, send to a dead letter queue etc.
17        }
18    }
19
20    private DataObject processData(String message) {
21        // Deserialize and process the message
22        return new DataObject();
23    }
24}
25
26@Service
27public class RestService {
28
29    @CircuitBreaker(name = "serviceName", fallbackMethod = "fallbackMethod")
30    public void callExternalAPI(DataObject data) {
31        // Logic to perform REST API call
32    }
33
34    private void fallbackMethod(DataObject data, Throwable t) {
35        // Fallback method when API call fails
36    }
37}

Table Summary

StrategyPurposeImplementation
Kafka Offset ManagementPrevent faulty message lossManual offset control
Retry MechanismsRedo operations before failRetry with KafkaListener
Dead Letter QueueIsolate undigestible messagesExtra Kafka topic for failures
Circuit BreakerStop cascading service failuresResilience4J with REST services

Additional Considerations

  • Monitoring & Alerting: Implement monitoring to catch and visualize these failures in real-time, coupled with alerting mechanisms for immediate action.
  • Configuration & Testing: Properly configure retry intervals, thresholds for circuit breaker. Test these mechanisms under simulated failure scenarios.

In conclusion, by integrating error handling and strategic recovery plans into your architectural design, consumers can ensure the robustness and reliability of applications leveraging Kafka for messaging and REST APIs for external communications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design