Spark streaming with Kafka - createDirectStream vs createStream
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Spark Streaming in conjunction with Apache Kafka is a popular choice for real-time data processing needs. Spark provides powerful streaming capabilities, which can be used effectively to consume and process data streams emitted by Kafka. Spark provides primarily two methods to integrate with Kafka: createDirectStream and createStream. Both of these approaches allow developers to consume data from Kafka, but they differ significantly in terms of their design and impact on performance, scalability, and ease of use.
Understanding Kafka Integration in Spark Streaming
Kafka, a distributed publish-subscribe messaging system, is designed to handle vast amounts of data while providing high throughput and scalability. Spark Streaming, on the other hand, is an extension of the core Spark API that enables scalable, high-throughput, fault-tolerant stream processing of live data streams. Integration between Kafka and Spark Streaming is traditionally done using either of the two discussed methods: createDirectStream and createStream.
createDirectStream Approach
createDirectStream is part of the Direct Approach (also called the Direct API) and was introduced as an alternative that provides several significant improvements over the earlier createStream approach. Here, Spark directly interacts with Kafka, and offsets are managed within the streaming application rather than using Zookeeper or Kafka’s high-level API. This means that Spark is directly responsible for tracking the offsets and consuming the data, providing better control and lower latency compared to the createStream approach.
Technical Example:
createStream Approach
This is also known as the Receiver-based Approach. In this method, Spark uses a Receiver to consume messages. The data is stored in receivers with the help of a Write Ahead Log (WAL) to ensure fault tolerance. Since it uses high-level Kafka APIs (rather than a direct integration), createStream is simpler and abstracts much of the processing from the developer. However, it suffers issues with scalability and efficiency in large-scale applications due to its reliance on storing offsets in Zookeeper.
Technical Example:
Comparison Table: createDirectStream vs createStream
| Feature | createDirectStream | createStream |
| Kafka Offset Management | Managed by Spark | Managed by Zookeeper |
| Fault Tolerance | Superior (no data duplication) | Uses WAL for fault tolerance |
| Ease of Setup | Requires more configuration | Simpler to set up |
| Performance | Higher throughput & efficiency | Comparatively lower efficiency |
| Scalability | Better | Limited by receiver storage |
Additional Considerations
- Performance and Scalability: Direct API (
createDirectStream) generally offers better performance. It processes data directly from Kafka, handling larger volumes of data more effectively. - Fault Tolerance: Direct API does not store data redundantly unless explicitly configured, reducing storage overhead and enhancing system performance.
- API Stability: Receiver-based approach (
createStream) relies on older Kafka high-level APIs, which may be less stable over time as Kafka evolves.
In sum, while both methods provide mechanisms to integrate Spark Streaming with Kafka, createDirectStream is often preferred for serious production environments due to its enhanced control over offset management, superior fault tolerance, and better performance characteristics. However, for simpler applications or ones where legacy code and simplicity are more critical, createStream might still be a viable option.
Related reading
- Spark Structured Streaming - Limitations? (Source Performance, Unsupported Operations, Spark UI)
- Spark Structured Streaming + Kafka Integration MicroBatchExecution PartitionOffsets Error
- Spark Structured Streaming app has no jobs and no stages
- Spark structured streaming app reading from multiple Kafka topics
- spark structured streaming avro to avro and custom Sink
- Spark Structured Streaming Checkpoint Compatibility
- Spark structured streaming kafka convert JSON without schema (infer schema)
- Spark Structured Streaming Kafka Offset Management

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.