TrackIt
TrackIt
Contact us
Blogs

Fine-Tuning Real-Time Data Architecture with Apache Kafka and Amazon MSK

Author

Antoine Maillard

Date Published

Every modern business generates data, from transactions and user actions to inventory updates and IoT sensor readings. The challenge is not just collecting this data, but making sense of it in real time, acting instantly on new information, and scaling seamlessly as a business grows. Apache Kafka, together with AWS’s managed service Amazon MSK (Managed Streaming for Apache Kafka), provides that real-time foundation. By offloading cluster management, AWS experts help set up, configure, implement, and optimize MSK, allowing teams to focus on using data rather than managing infrastructure.

The Common Challenges Businesses Face

  • Real-time decision-making: Organizations must detect anomalies, fraud, or stockouts instantly as data arrives, rather than after the fact.
  • Multiple data consumers: Analytics, dashboards, machine learning pipelines, and reporting systems all need access to the same event streams.
  • Scalability and reliability: Systems must ingest millions of events per second from many sources without losing ordering or data.
  • Reprocessing and auditing: Businesses need the ability to replay historical events for model retraining, error correction, or compliance requirements.

How Kafka and MSK Solve These Challenges

Centralized Event Streaming: Every interaction or sensor reading becomes a streaming event. Kafka topics, optionally keyed by user, account, or device, preserve ordering at the key level. Producers publish events to topics, while consumers (analytics engines, dashboards, ML models, etc.) read them independently. Because Kafka stores the full log, any number of consumers can replay or reprocess the data on demand.

Real-Time Processing and Analytics: Streaming tools (like Kafka Streams or AWS Lambda) consume events on the fly. This enables continuous aggregation of metrics, anomaly detection for fraud or system failures, and the triggering of alerts or automated actions with minimal latency. Kafka’s core abstractions make it well suited for building efficient, always-on processing pipelines.

Machine Learning Pipelines: Event streams can feed machine learning models directly for real-time predictions or recommendations. Kafka’s data retention capabilities also allow the same streams to be replayed for historical training, model improvement, or recovery from processing errors.

Operational Dashboards: Multiple teams can consume the same Kafka topics without interfering with each other. Dashboards built on these streams provide near real-time visibility into sales, transactions, customer activity, and system health, ensuring consistent and up-to-date insights across the organization.

Why MSK Makes It Easier

Fully managed Kafka: Amazon MSK operates Kafka broker clusters, handling node provisioning, configuration, patching, and maintenance. This removes the need to manage servers or broker infrastructure manually.

Automatic scaling & high availability: MSK automatically replicates partitions across brokers in multiple Availability Zones (AZs) for fault tolerance. It can scale storage or brokers on demand, ensuring resilience.

AWS Partner expertise: Our team knows how to configure MSK optimally. We manage topics, partitions, ordering, and hot-keys so that your pipelines are reliable and high-performing from day one.

Seamless AWS integration: MSK connects easily to AWS services such as S3 (for tiered storage), Lambda (for serverless processing), Redshift, and SageMaker. It also supports the Kafka protocol, making it simple to plug into existing tools or AWS Glue for schema management.

The Benefits for Any Business


Benefit

Example Applications Across Industries

Real-time visibility

Track inventory levels, financial transactions, IoT sensor status, and operational KPIs instantly.

Personalized actions

Retail recommendations on the fly, real-time fraud alerts in fintech, predictive maintenance signals in manufacturing.

Reliable multi-consumer streams

Power analytics dashboards, reporting systems, ML pipelines, and alerting tools all from the same streams.

Scalable architecture

Handle millions of events per second from many sources (websites, apps, devices) with consistent low latency.

Replayable streams

Reprocess events for ML retraining, audits, or compliance by replaying any time window of the data.

Fine-Tuning Amazon MSK: Best Practices

Managing an MSK cluster for optimal performance and cost involves tuning Kafka parameters, sizing resources, and monitoring key metrics. Below are AWS-recommended best practices that we implement to fine-tune MSK deployments:

  • Right-size brokers and partitions: Broker instance types and cluster size should be selected based on expected throughput and data volume. AWS sizing guidelines suggest approximately 1,000 to 4,000 partitions per broker, depending on instance size. Excessive partitions can overload brokers, while too few lead to underutilized resources. Load testing is typically used to determine the optimal number of brokers and partitions.
  • Optimize broker threads: On larger instances such as m5.4xlarge or above, Kafka parameters like num.io.threads and num.network.threads should be aligned with available CPU cores. A common approach is to set I/O threads equal to the number of CPU cores and network threads to roughly half that number. AWS guidance notes that network threads should not be increased without first scaling I/O threads, ensuring balanced throughput across brokers.
  • Ensure high availability: We deploy MSK across at least three AZs for fault tolerance. Topics are configured with a replication factor ≥3 and min.insync.replicas set to replicas − 1 (usually 2 for a 3-replica topic). This ensures continued read and write availability during broker failures or rolling upgrades. Based on AWS recommendations, RF=1 risks downtime, RF=2 risks data loss, so 3 is recommended.
  • Monitor CPU and scale preemptively: We set up CloudWatch metrics for broker CPU (user+system) and keep average usage under 60%. Staying below this threshold ensures headroom for spikes, failovers, or rolling upgrades. When utilization approaches this threshold, scaling is performed by increasing broker instance size first, followed by adding brokers and redistributing partitions if additional capacity is required. AWS typically recommends scaling up first (bigger instances). If necessary, it is also possible to scale out (add brokers and partitions); MSK’s console or APIs make this straightforward.
  • Balance load across brokers: Per-broker CPU usage and partition distribution should be monitored to identify imbalances. Uneven utilization often indicates that certain brokers host a disproportionate number of partition leaders. Tools such as Kafka Cruise Control can rebalance partitions to distribute load more evenly, reducing hotspots and maintaining consistent performance across the cluster.
  • Monitor disk and retention:  Broker disk utilization is monitored closely, with alarms triggered when the KafkaDataLogsDiskUsed metric exceeds approximately 85 percent. To prevent out-of-space conditions, storage can be increased through manual expansion or MSK auto-scaling, or retention policies can be adjusted. Kafka log retention settings such as log.retention.ms and log.retention.bytes are applied at the cluster or topic level to remove old data automatically. For example, we might set a 7-day retention on high-volume topics to limit storage costs, deleting older segments automatically. We also prune unused topics if needed.
  • Use tiered storage if appropriate:  For very high-throughput workloads, MSK’s S3-backed tiered storage can be enabled to offload older log segments once primary storage limits are reached. This provides effectively unlimited retention without provisioning additional brokers while keeping recent data on local disks for performance. This lets you retain data longer (meeting compliance) while still accessing it for reprocessing, at reduced cost.
  • Speed up recoveries:  After an unclean shutdown or broker failure, Kafka replays log segments to restore partitions, which can be time-consuming at scale. Increasing num.recovery.threads.per.data.dir, often up to the number of available CPU cores, allows partitions to recover in parallel. This dramatically cuts restart time after failures.
  • Monitor memory: We track Kafka heap usage (e.g. via the HeapMemoryAfterGC JMX metric) and alarm if it exceeds ~60%. If memory is high (common with many open transactions), we tune Kafka configs (e.g. reduce transactional.id.expiration.ms) to lower the footprint. Keeping memory healthy prevents broker crashes.
  • Encryption and security: MSK supports encryption in transit and at rest, along with IAM- or ACL-based authentication and authorization. TLS is enabled for client and inter-broker communication to protect data in motion, while access controls restrict topic-level permissions. (See AWS documentation on MSK encryption for more details)

Common MSK Misconfigurations and Pitfalls

Even with the right architecture in place, simple configuration mistakes can cause big problems. Our AWS team often sees these common misconfigurations that lead to performance or security issues:

  • No Encryption or Authentication:  Leaving a cluster in plaintext, without TLS or client authentication, is a common oversight. Without TLS or IAM or SCRAM authentication, data can be intercepted and malicious clients can connect. In production, in-transit encryption and IAM or SCRAM authentication should always be enabled. Disabling encryption, even between brokers, provides negligible performance gains and violates compliance requirements.
  • Single-Broker Connection: Hardcoding a single broker address instead of using the full bootstrap server list can silently break pipelines. If that broker goes down, producers and consumers lose access to the cluster entirely. Clients should be configured with MSK bootstrap endpoints, which include multiple brokers, to enable automatic failover. In practice, applications have stalled because they were configured to connect to only one broker and could not recover when it failed.
  • Insufficient Monitoring and Alerting: Waiting until something breaks is a recipe for disaster. Without proactive CloudWatch metrics and alarms on key indicators (CPU, disk usage, under-replicated partitions, consumer lag, etc.), issues such as replica lag or storage exhaustion can go unnoticed. We recommend immediately enabling broker and topic metrics, and setting alarms so the team is alerted (for example) when disk usage exceeds 85% or when in-sync replicas drop. Catching issues early prevents downtime and data loss.
  • Version Incompatibilities: Using mismatched Kafka client and broker versions can introduce subtle bugs. We have seen new clients behave strangely against older MSK releases (especially around group coordination). Always match your client library version to the cluster’s Kafka version, and test any upgrades in a non-production environment before rolling them out. Following MSK’s supported version matrix avoids protocol mismatches.
  • Networking/Security Group Issues:Networking misconfigurations are common. MSK brokers require specific ports, such as 9092 or 9094 for TLS, to be open between clients and brokers. If security groups block this traffic, producers/consumers simply time out. Ensure your Kafka broker SG allows inbound traffic on the Kafka ports from your application subnets, and that clients can resolve the cluster DNS (use AmazonProvidedDNS for VPCs). In private or cross-VPC setups, double-check your peering or Transit Gateway and DNS settings. We often debug connection issues by using tools like telnet on the broker endpoints to verify reachability.
  • Neglecting Kafka ACLs: By default, MSK does not restrict who can read or write topics. In a multi-team environment, forgetting to enable and configure ACLs can allow one team’s producers or consumers to access others’ topics. We enforce IAM-based or SCRAM ACLs so that each client only has access to the topics it needs. For example, you might configure an ACL that only allows TeamA’s IAM role to write to Topic-A. Without ACLs, debugging cross-talk issues can become very tricky.
  • Improper Replication Settings: Smaller MSK clusters can expose flaws in default Kafka configs. For instance, on a 2-broker cluster, MSK’s defaults set default.replication.factor=1 and min.insync.replicas=1. This means if one broker goes down, no ISR remains and producers may fail or data can be lost. In one real case, a transactional producer hung because the cluster wasn’t set up with enough replication. We always align these settings to the cluster size: for 2 brokers, we’d set the replication factor to 2 and min-insync to 2; in production, AWS recommends using RF=3 for all critical topics to ensure durability. Always verify that critical internal topics (offsets, transaction logs) have the correct replication too.

Conclusion

Across industries such as retail, banking, fintech, and IoT, Apache Kafka on Amazon MSK can serve as the backbone of a real-time data strategy. With our guidance as an AWS Partner, you can:

  • Set up MSK with the right configuration for your workload (optimal broker types, partitions, and storage).
  • Ensure high availability and data integrity with multi-AZ replication, proper min.insync.replicas, and evenly distributed partitions.
  • Tune performance by adjusting Kafka parameters (threads, compression, retention, etc.) and monitoring metrics (CPU, disk, memory).
  • Build reliable, scalable pipelines for analytics, dashboards, ML, and alerting systems, all consuming the same event streams.

Apache Kafka and Amazon MSK function as more than messaging infrastructure. We see them as the foundation for real-time, data-driven systems that enable organizations to respond quickly, operate reliably, and scale with confidence.