Apache Kafka Description
Apache Kafka Description
Message Subscriber
Message Subscriber
Kafka cluster
Producer Consumer
Kafka cluster
Broker 1
Topic 1 Topic 2
Producer Consumer
Broker 2
Topic 1 Topic 2
Zookeeper
Producer Consumer
Deep dive into high level abstractions
Topic
To balance load, a topic is divided into
multiple partitions and replicated
across brokers.
Partitions are ordered, immutable
sequences of messages that’s
continually appended i.e. a commit log.
The messages in the partitions are each
assigned a sequential id number called
the offset that uniquely identifies each
message within the partition.
Distribution and partitions
Partitions allow a topic’s log to scale beyond a size that will fit on a single server (i.e. a
broker) and act as the unit of parallelism
The partitions of a topic are distributed over the brokers in the Kafka cluster where each
broker handles data and requests for a share of the partitions.
For fault tolerance, each partition is replicated across a configurable number of brokers.
Distribution and fault tolerance
Each partition has one server which acts as the "leader" and zero or more servers
which act as "followers".
The leader handles all read and write requests for the partition while the followers
passively replicate the leader.
If the leader fails, one of the followers will automatically become the new leader.
Each server acts as a leader for some of its partitions and a follower for others so load
is well balanced within the cluster.
Retention
The Kafka cluster retains all published messages—whether or not they have been
consumed—for a configurable period of time; after which it will be discarded to
free up space.
Metadata retained on a per-consumer basis is the position of the consumer in the
log, called the offset; which is controlled by consumer.
Normally a consumer will advance its offset linearly as it reads messages, but it can
consume messages in any order it likes.
Kafka consumers can come and go without much impact on the cluster or on other
consumers.
Producers
Producers publish data to the topics by assigning messages to a partition within the
topic either in a round-robin fashion or according to some semantic partition function
(say based on some key in the message).
Consumers
Kafka offers a single consumer abstraction called consumer group that generalises
both queue and topic.
Consumers label themselves with a consumer group name.
Each message published to a topic is delivered to one consumer instance within each
subscribing consumer group.
If all the consumer instances have the same consumer group, then this works just like
a traditional queue balancing load over the consumers.
If all the consumer instances have different consumer groups, then this works like
publish-subscribe and all messages are broadcast to all consumers.
Consumer groups
Topics have a small number of consumer groups, one for each logical subscriber.
Each group is composed of many consumer instances for scalability and fault tolerance.
Ordering guarantees
Kafka assigns partitions in a topic to consumers in a consumer group so, each partition is
consumed by exactly one consumer in the group.
Limitation: there cannot be more consumer instances in a consumer group than partitions.
Provides a total order over messages within a partition, not between different partitions in
a topic.
Comaprison
The whole job of Kafka is to provide a "shock absorber" between the flood of
events and those who want to consume them in their own way.
Performance benchmark
Horizontally scalable
It’s a distributed system can be elastically and transparently expanded with no downtime
High throughput
High throughput is provided for both publishing and subscribing, due to disk structures
that provide constant performance even with many terabytes of stored messages
Reliable delivery
Persists messages on disk, and provides intra-cluster replication
Supports large number of subscribers and automatically balances consumers in case of
failure.
Use cases
Zookeeper to rescue
Apache Zookeeper: Definition
Distributed consistent data store which favours consistency over everything else.
High availability - Tolerates a minority of an ensemble members being unavailable and
continues to function correctly.
In an ensemble of n members where n is an odd number, the loss of (n-1)/2 members can be
tolerated.
High performance - All the data is stored in memory and benchmarked at 50k ops/sec but
the numbers really depend on your servers and network
Tuned for read heavy write light work load. Reads are served from the node to which a client a
connected.
Provides strictly ordered access for data.
Atomic write guarantees in the order they're sent to zookeeper.
Writes are acknowledged and changes are also seen in the order they occurred.
Apache Zookeeper: Operation basics
Clients create a state full session (i.e. with heartbeats through an open socket) when they
connect to a node of an ensemble. Number of open sockets available on a zookeeper node will
limit the number of clients that connect to it.
When a cluster member dies, clients notice a disconnect event and thus reconnect themselves
to another member of the quorum.
Session (i.e. state of the client connected to node of an ensemble) stay alive when the client
goes down, as the session events go through the leader and gets replicated in a cluster onto
another node.
When the leader goes down, remaining members of the cluster will re-elect a new leader using
a atomic broadcast consensus algorithm. Cluster remains unavailable only when it re-elects a
new leader.
1. Start ZooKeeper
2. Set-up a cluster with 3 brokers
2. Adjust broker
configuration
files
3. Start
kafka server
1
3. Start
kafka server
2
3. Start
kafka server
3
3. Servers 1, 2 and 3 are started and running
4. Create a kakfa topic, list topics and describe one