Apache Kafka
Big Data
Data Streaming
Scalability
Distributed Systems

apache- kafka with 100 millions of topics

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed event streaming platform that enables its users to publish, subscribe to, store, and process streams of records in real time. It's widely recognized for its high throughput, scalability, and fault tolerance. One of the core components in Kafka is the notion of "topics," through which records are categorized. Each topic is divided into partitions, where each partition is an ordered, immutable sequence of records.

Scalability Challenges with 100 Million Topics

Managing 100 million topics in Apache Kafka is a formidable challenge that raises significant concerns in terms of scalability, performance, and management. Each topic in Kafka, regardless of the number of messages it holds, requires certain overhead. This overhead is in terms of memory and also in terms of the file descriptors that are held open by Kafka processes. As the number of topics grows significantly, these costs can balloon, thereby impacting the performance.

Technical Implications

  1. Metadata Storage: Topics and partitions are managed through metadata which is stored in Zookeeper (though recent Kafka versions are moving towards removing the Zookeeper dependency). With millions of topics, the size of metadata becomes substantial, affecting Zookeeper performance.
  2. Broker Memory Usage: Each topic and partition consumes memory on the broker. With 100 million topics, the memory requirement can exceed practical limits.
  3. Client Connection Overhead: More topics mean more connections for consumers and producers, potentially leading to network and CPU overhead on Kafka brokers.

Design Considerations

To effectively manage an extremely high number of topics in Apache Kafka, careful planning and optimization of the setup are required:

Topic Consolidation

Instead of creating numerous small topics, organize related streams into fewer topics with more partitions. This reduces the load on Kafka's management layer and can aid performance.

Kafka Configuration

Adjusting Kafka settings can also mitigate issues:

  • Increase num.network.threads and num.io.threads to improve network and I/O performance.
  • Adjust socket.request.max.bytes and message.max.bytes to optimize the data throughput and size of messages that Kafka can handle.

Effective Hardware Utilization

Deploying Kafka on high-spec servers with ample memory and fast SSDs can mitigate the storage and memory overhead issues.

Use of Compact Topics

Compact topics (using log compaction feature) reduce the storage needs by retaining only the last message for each key in a partition. This feature is particularly useful in configurations with an extremely large number of sparse topics.

Summary Table

ParameterDescriptionImpact with 100 Million Topics
Metadata OverheadStorage and CPU overhead in handling topic metadataVery High
Broker Memory UsageEach topic/partition uses memory on the brokerExtremely High
Client ConnectionsIncrease in clients connecting to millions of topicsHigh
Topic ConsolidationReduction of total topics by combining related streamsReduce overhead
System Resources (Hardware)Requirement for high-end server specificationsCritical
Kafka Configuration OptimizationsTuning Kafka to handle high loadsEssential

Technical Example: Topic Optimization

Here’s how you might approach the consolidation of topics in a practical scenario:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
5
6Producer<String, String> producer = new KafkaProducer<>(props);
7
8// Instead of creating a new topic for each event type:
9String topicName = "vehicle_sensors"; // Consolidated topic
10
11for (VehicleData data : dataList) {
12    String key = data.getVehicleType(); // Key can be 'Car', 'Truck', etc.
13    producer.send(new ProducerRecord<>(topicName, key, data.toString()));
14}
15
16producer.close();

In the provided example, rather than creating separate topics for cars, trucks, etc., all data is sent to a single topic with keys distinguishing the vehicle types.

Conclusion

Handling 100 million topics in Kafka is highly impractical without significant optimizations and careful infrastructure planning. Consolidating topics, optimizing configurations, and ensuring robust hardware setup are vital steps in managing such a large scale in Kafka environments. With a disciplined approach to design and deployment, Kafka can be scaled to handle very high loads, but the complexity and overhead management become critical factors in such scenarios.


Course illustration
Course illustration

All Rights Reserved.