Introduction to Apache Kafka: Distributed Systems, Scalable Architecture, and Messaging Queues
Today's architecture requirements make distributed systems a necessity, and thus scalable architecture is important. One of the most essential aspects of scalable architecture is a messaging system for inter-component communication. Here's a quick look at Apache Kafka, with a focus on messaging queues.
Join the DZone community and get the full member experience.Join For Free
in my previous blog, i wrote about distributed systems and why we choose this path with today’s requirements. one of the most important parts of a scalable architecture is a messaging system which is used for communication between application components, log aggregation, event handling, etc. there are some standards that try to describe different protocols but i will focus on the architecture.
typically, we use publish-subscribe message brokers to handle our message queue and this is a great way to start. the logic behind this is that everything is an event and all the components either produce or consume events. it’s always easy to expand functionality, to add more publishers or subscribers and even base an architecture on events (event-driven architecture). there is a lot of material describing these concepts so i will not get into detail. the problem i'm focusing on is scalability and performance.
one of the crucial parts of any system is data ingestion, especially if it has peaks (as it usually does) and a number of messages are moderate to high (more than 20k/s). in this case, the typical publish-subscribe broker systems are lacking performance and easily become hot-spots. some of the broker implementations do scale but there are some conceptual problems. these problems are something that engineers at linkedin solved with apache kafka. they solved the problem of a typical publish-subscribe system by implementing it as a commit log. what does this mean?
let's look at the rabbitmq which is a great implementation of a messy amqp standard and works great as a distributed message broker. it's robust, provides good overall performance and the cluster is transparent to the client. it really does a good job for the use case it’s built for. one of the key concepts is what requires a change. rabbitmq (and most of the message brokers) presume by design that consumers are mostly online and messages in a queue, which are waiting to be consumed, are held opaquely. it is simply not designed to persist large amounts of messages on the broker.
apache kafka was conceptually designed to partition and persist large amounts of messages disregarding if the consumers are online or not. kafka presumes that producers generate messages at such a rate that it can be thought of as a stream. the main point is not to throttle down producers because consumers are failing to consume data fast enough but to provide a buffer between the flood of events and the system/consumers. consumers in this concept can process events at their own pace either online, in batches or even offline. one of the key advantages is that kafka partitions messages based on topic and across nodes and provides ordered delivery within a partition. amqp standard defines that one of each producer, channel, exchange, queue and consumer are required for ordered delivery. this breaks the philosophy of no single point of failure.
one of the important features with decoupled producers and consumers is that messages can be read on demand and at a consumer’s pace. kafka provides the ability to replay events that enable fallback scenarios in fast data stacks with noetl approach. this is possible because messages are persisted in kafka for a configurable period of time whether they have been consumed or not.
positions of a consumer in the partition are saved in the cluster but the consumer is responsible for changing this position (offset). at any given time, the consumer can decide to replay all the events in a partition. partitioning itself provides a great way to scale on multiple nodes but one partition must fit on a single node. topics, on the other hand, are partitioned and can span multiple nodes. messages are consumed in two different ways. the first one is a queue where we have a pool of consumers and each message is consumed by one of them. the second one is publish-subscribe where messages are being broadcasted to all of the consumers.
kafka provides a couple of important guarantees. message ordering is preserved based on a producer and consumers see these messages in the same sequence as they were produced. replication is defined by replication factor and there is a fault tolerance for up to n-1 server failures. kafka can guarantee "at least once" delivery semantics per partition.
when looking at the performance, kafka can sustain 1mil of messages produced per second on just a couple of nodes keeping the durability and ordered partitioning of data. this performance is considered high and only the top few companies have higher requirements than this.
one of the down-sides is complexity as in any other distributed system. kafka requires zookeeper to run in order to keep nodes in sync. i’m not a fan of single point of failure spots in distributed systems, like zookeeper is, especially when we introduce additional complexity in order to solve single instance problems but then again rely on something like zookeeper to keep things running.
being distributed, kafka has failover mechanisms where if master node is down, one of the existing nodes is automatically voted and promoted into master. scalability is one of the key features and this is where kafka excels and with a fault tolerance of rf-1 (replication factor) nodes it is a great choice for the backbone of data-intensive system. when opting for kafka keep in mind that there aren’t many drivers outside the jvm stack at this point and that you need to run a cluster of nodes in order to benefit from fault tolerant replication. if you need to push large messages or if simplicity and ease of use are what you are after you should consider some of the lightweight brokers, but if you need reliability and performance at scale and are pushing large amounts of data through your system then kafka is the perfect choice.
Published at DZone with permission of Matija Gobec, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.