Kafka Topics and Partitions Configuration: A Tuning Guide

Kafka Topics and Partitions Configuration: A Tuning Guide

Development and maintenance of all types of websites:

Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:

Frequently Asked Questions

Latest works

  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1286
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1243
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    983
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    1034
  • image_website-sbh_0.webp
    Website development for SBH Partners
    1107
  • image_website-_0.webp
    Website development for Red Pear
    555

Kafka Topics and Partitions Configuration: A Tuning Guide

Imagine: you go live in production, and consumer lag grows, partitions are skewed, one broker is overloaded. A typical scenario with incorrect Kafka topic configuration. We'll walk through how to design a topic schema to avoid these issues.

Partition count and replication factor are two parameters that you cannot easily change after creating a topic. Reducing the number of partitions is impossible without fully recreating the topic. So correct initial configuration is vital. We guarantee that with proper design, you won't face hot spots or unnecessary delays.

Why Correct Partition Configuration Matters

Each partition is a unit of parallelism. One consumer in a group processes one partition. If a topic has 6 partitions, at most 6 consumers in the group can read in parallel. Extra consumers sit idle. Plus, writes within a partition are strictly ordered. Global ordering across a topic is not guaranteed—only within a partition. This is critical for events that must be processed sequentially (e.g., all actions of a single user).

Example: topic user-events with 6 partitions. Events from a single user (user:101) could land in different partitions (0 and 1), breaking processing order. A message key solves this: hash(user_id) % num_partitions always yields the same partition.

More on choosing partition count A practical rule: `num_partitions = max(throughput_target / throughput_per_partition, num_consumers_target)`. Typical throughput per partition: 10–50 MB/s for writes (depends on hardware and broker configuration). Example: need to handle 200 MB/s with peaks up to 400 MB/s and keep the ability to scale to 20 consumers → take 24 partitions (a multiple of 6, 8, 12 for easy scaling). Too many partitions is also bad: each partition demands filehandles, buffer memory, and stresses the controller during leader elections.

How to Create and Configure Topics

Via kafka-topics.sh

# Basic topic for user events kafka-topics.sh --bootstrap-server kafka-1:9092 \ --create \ --topic user-events \ --partitions 12 \ --replication-factor 3 \ --config retention.ms=604800000 \ --config retention.bytes=10737418210 \ --config compression.type=lz4 \ --config min.insync.replicas=2 \ --config message.max.bytes=1048576 # Compact topic — for storing the latest state by key kafka-topics.sh --bootstrap-server kafka-1:9092 \ --create \ --topic user-profiles \ --partitions 24 \ --replication-factor 3 \ --config cleanup.policy=compact \ --config min.cleanable.dirty.ratio=0.1 \ --config segment.ms=3600000 \ --config delete.retention.ms=86400000 # High-priority queue with short retention kafka-topics.sh --bootstrap-server kafka-1:9092 \ --create \ --topic order-processing-priority \ --partitions 6 \ --replication-factor 3 \ --config retention.ms=3600000 \ --config max.message.bytes=102400 

Programmatic management via Admin API (Java/Kotlin) Creating topics programmatically is ideal for applications that create topics dynamically:

Properties props = new Properties(); props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka-1:9092,kafka-2:9092,kafka-3:9092"); props.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 5000); props.put(AdminClientConfig.DEFAULT_API_TIMEOUT_MS_CONFIG, 10000); try (AdminClient admin = AdminClient.create(props)) { NewTopic userEvents = new NewTopic("user-events", 12, (short) 3); userEvents.configs(Map.of( "retention.ms", "604800000", "compression.type", "lz4", "min.insync.replicas", "2" )); NewTopic deadLetter = new NewTopic("user-events-dlq", 3, (short) 3); deadLetter.configs(Map.of( "retention.ms", "2592000000", // 30 days "retention.bytes", "-1" )); CreateTopicsResult result = admin.createTopics(List.of(userEvents, deadLetter)); result.all().get(30, TimeUnit.SECONDS); } 

Modifying an existing topic's configuration

# Increase retention kafka-configs.sh --bootstrap-server kafka-1:9092 \ --alter \ --entity-type topics \ --entity-name user-events \ --add-config retention.ms=1209600000 # Add partitions (only increase!) kafka-topics.sh --bootstrap-server kafka-1:9092 \ --alter \ --topic user-events \ --partitions 24 # Caution: adding partitions breaks ordering for keyed messages. # Existing keys will go to the same partitions (hash % 12), # new keys will be distributed across 24 partitions. # View topic configuration kafka-configs.sh --bootstrap-server kafka-1:9092 \ --describe \ --entity-type topics \ --entity-name user-events 

Leader Management and Skew Mitigation

Uneven distribution of leaders among brokers leads to hot spots:

# Check leader distribution kafka-topics.sh --bootstrap-server kafka-1:9092 \ --describe --topic user-events # Preferred replicas — rebalance leaders kafka-leader-election.sh --bootstrap-server kafka-1:9092 \ --election-type preferred \ --all-topic-partitions # Or for a specific topic via JSON cat > election.json << 'EOF' { "partitions": [ {"topic": "user-events", "partition": 0}, {"topic": "user-events", "partition": 1} ] } EOF kafka-leader-election.sh --bootstrap-server kafka-1:9092 \ --election-type preferred \ --path-to-json-file election.json 

If consumer lag varies significantly across partitions, check the keys: a poor hash function causes skew. Solution: increase the number of partitions and use UniformStickyPartitioner (available from Kafka 2.4+). Alternative: switch to RoundRobinPartitioner for logging. Monitor partitions using kafka-consumer-groups.sh:

# Consumer lag — group lag kafka-consumer-groups.sh --bootstrap-server kafka-1:9092 \ --describe --group my-consumer-group # Total lag > 10,000 for critical topics — trigger for alert 

Typical Configurations by Data Type

Topic Type Partitions Replication Cleanup Retention
Transactions 12–24 3 (min.isr=2) delete 7–30 days
Audit logs 6–12 3 (min.isr=2) delete 90–365 days
Profiles (CDC) 24–48 3 compact unlimited
Metrics 12 2 delete 24–48 hours
Notifications 6 3 delete 1–3 days

Comparison: Kafka vs RabbitMQ for Data Streams

Apache Kafka is better than RabbitMQ by 3–5 times in throughput under high load (hundreds of MB/s). RabbitMQ wins in flexible routing (exchanges) and support for queues with different priorities. For event sourcing and CDC, Kafka is the de facto standard; for microservices with complex routing, RabbitMQ may be more convenient.

Our Process and Timeline

With over 10 years of experience and 40+ successful Kafka deployments, our clients typically save $30,000 per year by optimizing Kafka topics. Our approach is:

  • Requirements analysis: evaluate throughput, consumer count, ordering needs, retention. Design topic schema. Duration: 1 day.
  • Topic creation: configure partitions, replication, compaction. Set up ACLs if authentication is needed. Duration: 1–2 days.
  • Monitoring: configure consumer lag alerts, document schema for the team. Duration: 1 day.

We offer turnkey configuration: from design to documentation. We can assess your project in 1 day — contact us.

What's Included?

  • Architectural documentation: topic schema, keys, retention.
  • Broker and topic configuration.
  • Monitoring and alert setup.
  • Team training (2–4 hours).
  • One month of post-launch support.

Timeline

Phase Duration
Requirements analysis and design 1 day
Topic creation and ACL setup 1–2 days
Monitoring and documentation 1 day

Proper partition configuration reduces latency by 30% compared to suboptimal setups (based on our data). Get a consultation for your project — reach out to us.

Learn more about Apache Kafka at Wikipedia.