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.
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.
Table Summary
| Strategy | Purpose | Implementation |
| Kafka Offset Management | Prevent faulty message loss | Manual offset control |
| Retry Mechanisms | Redo operations before fail | Retry with KafkaListener |
| Dead Letter Queue | Isolate undigestible messages | Extra Kafka topic for failures |
| Circuit Breaker | Stop cascading service failures | Resilience4J 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
- How to stop consuming message from selective queue - RabbitMQ
- How to stop Python Kafka Consumer in program?
- how to stop rabbitmq servers
- How to store only latest key values in a kafka topic
- How to store best models checkpoints, not only newest 5, in Tensorflow Object Detection API?
- How To Stream Chunked Response With Spring Boot RestController
- How to stop docker under Linux
- How to stop Replicaset from restarting?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.