Kafka Streams and RPC is calling REST service in map() operator considered an anti-pattern?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka, an open-source stream processing platform developed by the Apache Software Foundation, is designed to provide a high-throughput, low-latency platform for handling real-time data feeds. Kafka Streams is a client library for building applications and microservices where the input and output data are stored in Kafka clusters. It enables developers to create scalable, fault-tolerant streaming applications.
Kafka Streams and its Usage
Kafka Streams simplifies the development of streaming applications by providing a straightforward DSL (Domain Specific Language) that interacts with the core Kafka API. What makes Kafka Streams powerful is its ability to process records as they arrive, making it highly suitable for event-driven architectures.
RPC and REST in Distributed Systems
Remote Procedure Call (RPC) has been a fundamental concept in the design of networked communications. RPC allows a program to cause a procedure to execute in another address space, which is commonly on another physical machine. REST (Representational State Transfer) is an architectural style that uses standard HTTP methods and is popular for building scalable web services. In the context of microservices and streaming applications, external API calls, such as REST, are frequently employed to enrich, filter, or transform the data.
Using REST in Kafka Streams Map Function
The map() operation in Kafka Streams is a stateless transformation that takes one record and produces one output record. It is typically used for modification of records such as changing a field, filtering records, or simple mappings.
Despite the feasibility, triggering an RPC or REST call within a map() or any other operator in Kafka Streams might not always align with best practices for several reasons:
Performance Concerns
Making a REST call introduces significant latency. Kafka Streams applications are designed to be low latency and handle high throughput. External calls can throttle the performance of the entire stream processing application, leading to bottlenecks.
Fault Tolerance
Depending on external services within a stream processing job can reduce the reliability and robustness of your application. If the external service is down or slow, it impacts the stream application directly.
Increases Complexity
Integrating synchronous external calls during stream processing adds complexity to error handling, retries, and backpressure mechanisms. It demands additional logic to manage partial failures and inconsistencies.
Scalability
REST APIs often have rate limits and can handle limited concurrent requests. As the load on the Kafka Streams application increases, it might surpass the API limits, leading to failed requests or delayed processing.
Better Approaches
- Asynchronous Processing: If external calls are necessary, consider delegating them to a separate service and use Kafka topics to transfer the external service results back to the stream processing service.
- Caching: When possible, caching external call results locally or in a distributed cache can significantly reduce the need for repeated calls.
- Database Lookup instead of API Calls: For enrichment that doesn't require real-time data from external systems, using a local database or a global KTable for lookups can be more efficient.
Summary Table
| Feature | REST in map() | Recommended Methods |
| Latency | High | Low (with caching/KTable) |
| Fault Tolerance | Low | High |
| Complexity | High | Medium to low |
| Scalability | Low | High |
Conclusion
While technically possible, using REST services within Kafka Streams' map() function is generally an anti-pattern due to its adverse impacts on performance, fault tolerance, complexity, and scalability. Alternative approaches such as asynchronous communication, caching, or using KTables for lookups should be considered to build robust and scalable streaming applications.

