Apache Kafka | Thi Nguyen's Blog
Apache Kafka | Thi Nguyen's Blog
Kafka introduction
That's where Apache Kafka comes in as an effective solution. Apache Kafka is a publish-
subscribe based durable messaging system developed by Linkedin.
Multiple producers and consumers at any given time without interfering with
each other. This is in contrast to many queuing system where once a message is
consumed by one client
Disk-Based retention:
Consumers do not always need to work in real time. Messages are commited to
disk and stay the for some period of time.
There is no danger of losing data.
Fast: Kafka is a good solution for applications that require a high througput, low
latency messaging solution. Kafka can write up to 2 million requests per second
Scalable:
Expansions can be performed while the cluster is online, with no impact on the
availability of the system as a whole.
High Performance: Excellent performance under high load.
Zero Copy: Basically Kafka calls the OS kernal directly rather than at the application
:
layer to move data fast.
Batch data in chunks: Kafka is all about batching the data into chunks. This
minimises cross machine latency with all the buffering/copying that accompanies
this.
Avoids Random Disk Access: Kafka is designed to access the disk in sequential
manner. This enables it to get similar speeds from a physical disk compared with
memory.
Can scale Horizontally: The ability to have thousands of partitions for a single topic
spread among thousands of machines means Kafka can handle huge loads.
Use Cases
Activity tracking: The original use case for Kafka, designed at LinkedIn, is that of
user activity tracking.
Stream processing: Kafka is extremely good for streaming and processing huge
datasets.
Kafka architecture
Kafka is a message broker. A broker is an intermediary that brings together two parties
that don't necessarily know each other for a mutually beneficicial exchange or deal.
Log
Is a file that Kafka appends incoming records to. A log is an append-only, totally ordered
sequence of records ordered by time
Configuration setting log.dir , specifies where Kafka stores log data on disk.
:
Topics
Topics are logs that are seperated by topic name. Thinks topics as labeled logs. The
closest analogies for a topic are a database table or a folder in a filesystem.
orders
customers
paymments
To help manage the load of messages coming into a topic. Kafka use partitions
Partitions are the way that Kafka provides redundancy and scalability. Each partition
can be hosted on a different server, which means that a single topic can be scaled
horizontally across multiple servers.
:
Partitions
At this time, you can come up with a question. Wait a minute, Aren't Log and Partition the
:
same thing? At first glance, they seems to look the same, but here are the difference:
When a message is sent to kafka, you can specify a key option for that message.
If the key(key will be explained in the next section) isn't null. Kafka uses the following
formula to calculate which partition the message will be sent to.
Records with the same key will always be sent to the same partition and in order.
Kafka stores and retrives message from topic. Doesn't keep any state of producers or
consumers
Messsages are written into Kafka in batches. A batch is just a collection of messages,
all of which are being produced to the same topic and partition.
Producer
The producer does not care what partition a specifict message is written to and will
balance messages over all partitions of a topic evenyly
:
In some case, the producer will direct messages to specific partitions using
message key . Messages with a specified message key will be ensured to come
in the right order in a partition.
Consumer
The consumer subscribes to one or more topics and reads the messages in the order
in which they were produced.
The consumer keeps track of which message it has already consumed by keeping
track of the offset of messages.
The offset is a simple integer number that is used by Kafka to maintain the current
position of a consumer.
:
Consumers work as part of a consumer group , which is one or more consumers that
work together to consume a topic. Group assures that each each partition is only
consumed by one member. If a single consumer fails, the remaning members of group will
rebalance the partitions being consumed to take over the missing member.
Consumer group
For example:
:
:
If the number of consumers in a group exceeds the number of partitions in a topic. Then
there will be some idle consumer and get no messages at all
:
You can create a new consumer group for each application that needs all the messages
from one or more topics
Create a ProducerRecord , which must include the topic we want to send the
record to and a value. Optionally, we can also specify a key and/or a partition.
Then Serialized the key and value objects to ByteArrays so they can be sent over
the network
Data is sent to a partitioner . The partition check if ProducerRecord has a
specifed partition option. If yes, it doesn't do anything an reply the partition
we specify. If not, the partitioner will choose a partition for us.
Once a partition is selected, the producer then add the record to a batch of
records that will also be sent to the same topic and partition.
When broker receives the messages, it sends back a response.
If the messages were successfully writtent to Kafka, return a RecordMetatData
object contains <topic, partition, offset>
If failed, the broker will return an error. The producer may retry sending the
message a few more times before giving up and returning an error.
:
Broker and Clusters
Broker
A single Kafka server is called a broker. The broker receives messages from producers,
assigns offsets to them and commits the messages to storage on disk.
Kafka uses Apache Zookeeper to maintain the list of brokers that are currently members
:
of a cluster. ZooKeeper is a consistent file system for configuration information.
It acts as a centralized service and helps to keep track of the Kafka cluster nodes status,
Kafka topics, and partitions.
Cluster controller
A cluster controller is one of the kafka brokers that in addition to the usual broker
functionality:
Each broker holds a number of partitions and each of these partitions can be either a
leader or a replica for a topic
Follower replica
All replicas for a partition that are not leaders are called followers
Followers don't serve client requests
Only replicate messages from the leader and stay up-to-date with the most recent
message the leader has
When a leader crashes, one of follower replica will be promoted to become the leader
A Follower replica that catch up with the most recent messages of the leader are
callled In-Sync replica
Only in-sync replicas are eligible to be elected as partition leader in case the existing
leader fail
:
When a controller notices that a broker left the cluster. All the partitions that had a
leader on that broker will need a new leader, so the controller will choose a new leader for
all of these partitions
Confugrations
Hardware selection
Disk Throughput
Disk capacity
If the broker is expected to receive 1 TB of traffic each day, with 7 days of retention,
then the broker will need a minimum of 7 TB of usable storage for log segment
Memory
Having more memory available to the system for page cache will improve the performance
of consumer clients'
The two most important parameters when creating a topic: Patition and replication factor
They impact performance and durability of the system overall
Patitions
Guidelines:
ConHgure topic
Retention is the durable storage of messages for some period of time. For example, a
tracking topic might be retained for several days, whereas application metrics might be
reatined for only a few hours.
Retention policy:
Compact: Only stores the most recent value for each key in the topic. Only works on
topics for which applications produce events that contain both a key and a value
log.cleanup.policy=delete :
log.cleanup.policy=compact :
:
Delete based on keys of your message
Will delete old duplicate keys after the active segment is commited
log.retention.hours :
log.retention.bytes :
acks: Controls how many partition replicas must receive the record before the
producer can consider write successful.
acks = 0: the producer will not wait for a reply from the broker before assuming
the message was sent successfully. The message may be lost but it can send
messaes as fast as the network will support, so this setting can be used to
achieve very high throughput
acks=1: With a setting of 1, the producer will consider the write successful
when the leader receives the record. The leader replica will know to immediately
respond the moment it receives the record and not wait any longer.
acks=all: the producer will consider the write successful when all of the in-sync
:
replicas receive the record. This is achieved by the leader broker being smart as
to when it responds to the request — it’ll send back a response once all the in-
sync replicas receive the record themselves.
In-sync replicas: An in-sync replica (ISR) is a replica that has the latest data for
a given partition. A leader is always an in-sync replica. A follower is an in-sync
replica only if it has fully caught up to the partition it’s following.
https://medium.com/better-programming/kafka-acks-explained-
c0515b3b707e#:~:text='acks%3D1',producer%20waits%20for%20a%20response
acks setting is a good way to configure your prefered trade-off between durability
guarantees and performance
buffer memory: this sets the amount of memory the producer will use to buffer
messagse waiting to be sent to brokers.
compression.type: By default, messages are sent uncompressed. We can ues
:
gzip , lz4 . Enabling compression redue network utilization and storage
retries: How many times the producer will retry sending the message
batch.size: The producer will batch them together. When the batch is full, all the
messages in the batch will be sent.
client.id: Use by the brokers to identify messages esnt from the client
Compression is more effective the bigger the batch of message being sent to kafka is
!
compression.type can be none , gzip , lz4 , snappy
Much smaller producer request size
Faster to transfer data over the network => less latency
Better throughput
Better disk utilization in Kafka(stored messages on disk are smaller)
BUT Producers must commit some CPU cycles to compression and decompression
Should use snappy or lz4 , gzip is slow
Always use compression in production and especially if you have high throughput
Consinder tweaking linger.ms and batch.size to have bigger batches, and
therefore more compression and higher throughput
Producer batching
It will have up to 5 requests in flight meanning 5 messages sent at the same time
If more messages have to be sent while others are in flight, Kafka is smart and
will start batching them while they wait to send them all at once
batch.size: Maximum number of bytes that will be included in a batch. The default is
16KB
:
Increase batch size to 32KB or 64KB can help increasing throughput
A batch is allocated per partition, make sure don't set it to a number that's too
high
If the producer produces faster than the broker can take, the records will be buffered in
memory
buffer.memory=33554432(32MB)
If the buffer is full(all 32 MB), .send() method wil start to block
max.block.ms=60000 , the time the .send() will block until throwing an
exception.
Idempotent producer
The producer can introduce duplicate messages in Kafka due to network errors
:
ConHgure consumer
One of the kafka brokers get elected as a group coordinator. When a consumer want to
join a group, it send a request to the group coordinator
During the rebalance activity, none of the consumers are allowed to read any
messages.
rebalance : When a new consumer joins a consumer group the set of consumers
attempt to "rebalance" the load to assign partitions to each consumer.
Consumer offset
Kafka stores the offset at which a consumer group has been reading
The offsets are commited in a Kafka topic named consumeroffsets
Hi, It's Thi
If a consumer dies, it will be able to read back from where it left off thanks to the
commited consumer offset
Consumer can be down so it need to know where to start to read the log
auto.offset.reset=latest : will read the end of the log
auto.offset.reset=earliest : will read from the start of the log
auto.offset.reset=none : Wil throw exception if no offset is found
offset.retention.minutes : Consumer offsets can be lost if a consumer hasn't
read new data in 7 days
If the processing goes wrong, the message will be lost(it won't be read again)
Commits first then read
:
At least once(usually preferred):
Offset management
:
In the event of rebalancing, when a consumer is a asigned the same partition, it should
ask a question where to start. What is already process by the previous owner? That's
where Commited offset comes into play
How to commit?
AutoCommit:
enable.auto.commit
auto.commit.interval.ms Can't avoid processing a record multiple times. If
rebalancing happens before producer hasn't automatically commited
Manual Commit:
Commit sync: block
Commit async: Commit async will not retry
2 strategies
Case study
GetTaxi
Gettaxis is a company that allows people to match with taxi drivers on deman, right-away.
The business wants the following capabilities:
Campaign compare
Mysocial media
MyBank is a company that allows real-time banking for its users. It wants to deploy a
brand new capability to alert users in case of large transactions
The transactions data already exists in a database
Thresholds can be defined by the users
:
Big data ingestion
Kafka internal
Request processing
Kafka has a binary protocol that sepcifies the format of the requests and how brokers
respond to them
Client usually cache this information and priodically refresh this information.(controlled
by data.max.age.ms configuration parameter)
if a new broker was added or some replicas were moved to a new broker. A client will
receives the error Not a Leader and then it will refresh the metadata before try
sending the request again
:
Physical storage
Partition Allocation
Suppose you have 6 brokers and you decide to create a topic with 10 parti- tions and a
replication factor of 3. Kafka now has 30 partition replicas to allocate to 6 brokers.
File Management
partition are splitted into segments. Each segment contains either 1GB or a week of data
segment we are currently writing to is called active segment. Active segment is never
deleted
References
:
[Book] Kafka definitive guide
Previous Next
« Data-intensive system OOP and functional
programming »
Kafka introduction
Use Cases
Kafka architecture
Log
Topics
Partitions
:
Difference between Partition and Log?
Producer
Consumer
Broker
Cluster controller
Replica