Apache Kafka is an open-source publish-subscribe messaging application initially developed by LinkedIn in early 2011. It is a famous Scala-coded data processing tool that offers low latency, extensive throughput, and a unified platform to handle the data in real-time. It is a message broker application and a logging service that is distributed, segmented, and replicated. Kafka is a popular and growing technology that offers IT professionals ample job opportunities. In this article , we have discussed the detailed Kafka interview questions that can help you to ace your upcoming interview.
This article was published as a part of the Data Science Blogathon.
No, we can’t bypass the ZooKeeper and connect directly to the Kafka Server, and even we can’t process the client requests if the Zookeeper is down due to any reason.
The default maximum size for any message in Kafka is 1 Megabyte, which can be changed in the Apache Kafka broker settings but the optimal size is 1KB because Kafka handles very small messages.
In the producer API, when the messages sent by the producer to the Kafka broker are at a pace that the broker can’t handle, the exception that occurs is known as QueueFullException. This exception can be resolved by adding more brokers so that they can handle the pace of messages coming in from the producer side.
Apache Kafka uses a high-performance, language-agnostic TCP protocol to initiate client and server communication.
For any Kafka topic, the optimal number of partitions are those that must be equal to the number of consumers.
Command to list all the topics after starting the ZooKeeper:
bin/Kafka-topics.sh --list --zookeeper localhost:2181
You can view the message in Apache Kafka by executing the below command:-
bin/Kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
To add a topic configuration:
bin/Kafka-configs.sh --zookeeper localhost:2181
--topics --name_of_the_topic --alter --add-config a=b
To remove a topic configuration:
bin/Kafka-configs.sh --zookeeper localhost:2181
--topics --name_of_the_topic --alter --delete-config a
Note:- Here, a denotes the particular configuration key that needs to be changed.
The Zookeeper’s daemon name is Quorumpeermain.
Replications are considered critical in Kafka because they ensure that every published message must not be lost and should be consumed in any program/machine error.
Although we have many traditional message queues, like JMS and RabbitMQ, Kafka is a key messaging framework for transmitting messages from sender to receiver. When it comes to message retention, traditional queues eliminate messages as soon as the consumer confirms them, but Kafka stores them for a default period of 7 days after the consumer has received them.
Also, nowadays, we have to deal with transactional data like online shopping carts, orders, and inventory, which are augmented with things such as one-click go, likes, web searches, and recommendations. This data must be analyzed carefully to predict consumer behavior and feed it to the predictive engines. So, we need smooth messaging services to process this transactional data and handle the large volume of messages for smooth delivery.
Below are the key points to prove why we can rely on Kafka even though we have many traditional services:-
The 4 significant components of Kafka’s Architecture include:
Apache Kafka offers four main APIs-
The concept of leader and follower is very important in Kafka to handle load balancing. In the Kafka server, every partition has one server that plays the role of a leader and one or more servers that behaves as followers. The leader’s responsibility is to perform all the read-and-write data operations for a specific partition, and the follower’s responsibility is to replicate the leader.
In any partition, the number of followers varies from zero to n, which means a partition cannot have a fixed number of followers; rather, it can have zero followers, one follower, or more than one follower. The reason for the same is if there is any failure in the leader, then one of the followers can be assumed to be in leadership.
Follow the below steps to start the Apache Kafka server on your personal computers:-
Step 1: To download the latest version of Apache Kafka(3.4.0), first click on the below site and download the software as per your operating system, and once it gets downloaded, extract the file to perform the installation process.
Link- Click here
Step 2: To run Kafka, you must have Java 8+ version installed on your local environment.
Step 3: Now you have to run the below commands in the same order to start the Kafka server:
Firstly you have to run this command to start the ZooKeeper service:
$bin/zookeeper-server-start.sh config/zookeeper.properties
Then you need to open another terminal and run the below command to start the Kafka broker service:
$ bin/Kafka-server-start.sh config/server.properties
The major difference between Partitions and Replicas in the Kafka cluster is that the Partitions are used to increase the throughput in Kafka. In contrast, Replicas are used to ensure fault tolerance in the cluster. Basically, in Kafka, partitions are nothing but topics divided into parts to enable consumers to read data from servers in parallel. The responsibility of the read and write operations are managed on a single server called the leader for that partition. That leader is followed by zero or more followers, where replicas of the data will be created.
Replicas are nothing but copies of the data in a specific partition. The followers have just to copy the leaders; they’re not required to read or write the partitions individually.
We have the below two possible ways to list out all the available Kafka brokers in an Apache Kafka cluster:
zookeeper-shell.sh:2181 ls /brokers/ids
We will get the below output after running this shell command:
WATCHER:: WatchedEvent state: SyncConnected type: None path: null [0, 1, 2, 3]
This shows the availability of four alive brokers – 0,1,2 and 3.
First, we need to log in to the ZooKeeper client
zkCli.sh -server:2181
Now we have to run the below command to list all the available brokers:
ls /brokers/ids
To name the topics in Apache Kafka, there are some legal rules which are defined by Kafka to be followed:-
The maximum length of the name of any Kafka topic is 255 characters (including symbols and letters). In Kafka version 0.10, this length has been reduced from 255 to 249.
We can use special characters like. (dot), _ (underscore), and – (hyphen) in the name of a Kafka topic. Although we must avoid combining these special characters because the topics with a dot (.) and underscore ( _) symbol could cause some confusion with internal data structures.
ISR stands for in-synch replicas. It refers to all the replicated partitions of Kafka that are fully synced up with the leader within a configurable amount of time. Basically, a defined period of time is given to followers to catch up with the leader; by default, this time is 10 seconds; after that, the leader will continue the writing on the remaining replicas in the ISR by dropping the follower from its ISR. If the follower revisits the ISR, it has to truncate its log to the last point, which was checked, and after reaching the last checkpoint from the leader, it will catch up on all the messages. The leader will add it back to the ISR only when the follower completely catches up with the leader.
We have the leader and follower nodes to ensure the load balancing in the Apache Kafka server. As we already discussed, the role of leader nodes is to do the writing/reading of data in a given partition. In contrast, follower systems perform the same task in passive mode to ensure data replication across different nodes. So that if any failure occurs due to any reasoning system or software upgrade, the data remains available.
Whenever the leader system fails or goes down for any reason, irrespective of any internal outage, the follower system is responsible for data persistence by becoming the new leader. Like a load balancer distributes loads across multiple systems in a caseload, Kafka replicates messages on multiple systems. When the leader system goes down, the other follower system becomes the leader to ensure the availability of data to subscribers’ systems and balance the loads.
This blog on interview questions covers most of the frequently asked Apache Kafka interview questions that could be asked in data science, Kafka developer, Data Analyst, and big data developer interviews. Using these interview questions as a reference, you can better understand the concept of Apache Kafka and start formulating practical answers for upcoming interviews. The key takeaways from this Kafka blog are:-
I hope you liked my interview questions guide. If you have more questions or suggestions, please post them in the comments below.