Kafka Full Course | Apache Kafka Tutorial for Beginners with Hands-On — Transcript
Full transcript
- 0:00Apache Kafka is widely used in backend
- 0:02systems. It handles real-time data
- 0:04between multiple services. But to
- 0:06understand how Kafka actually works, you
- 0:08first need to understand its complete
- 0:10architecture and the role of each
- 0:12component. So in this video, I'll break
- 0:15down the complete Kafka architecture
- 0:16step by step and we'll understand the
- 0:19role of each component. First of all,
- 0:21what is Kafka? Kafka is a distributed
- 0:24event streaming platform. If you search
- 0:26for Kafka, you'll find the same
- 0:28definition everywhere with these four
- 0:30words, distributed event streaming
- 0:32platform. In this video, I'll explain
- 0:35what each of these words actually means
- 0:37using a simple e-commerce application
- 0:39example. Let's start with event. An
- 0:41event is a record of something that has
- 0:43happened in a system. For example, a
- 0:45user placed an order. This is an event.
- 0:48Now suppose our e-commerce application
- 0:50has thousands of users. Orders are
- 0:52continuously being placed. So events are
- 0:55being generated continuously. Instead of
- 0:57waiting for all these events to
- 0:59accumulate, we can process them in real
- 1:01time. This continuous flow of events is
- 1:03called event streaming. Now whenever an
- 1:06event happens in our application, we
- 1:08might want other parts of the system to
- 1:10know about it. For example, when an
- 1:12order is placed, we might want the
- 1:14payment service to process the payment,
- 1:16the inventory service to update the
- 1:18inventory, and the notification service
- 1:20to send a confirmation to the user. So
- 1:23in a way, the order service needs to
- 1:25notify all the services that an order
- 1:27has been placed. In more technical
- 1:29terms, we say that this event needs to
- 1:32be produced by one application and
- 1:34consumed by other applications. Now we
- 1:36need some platform that can actually
- 1:38handle these events, something that
- 1:40allows our application to produce
- 1:41events, store events, and allow other
- 1:44applications to consume those events.
- 1:46Kafka provides all of this functionality
- 1:49and it allows these events to be
- 1:50processed continuously in real time.
- 1:53That's why we call Kafka an event
- 1:54streaming platform. It is also called
- 1:57distributed because we can run multiple
- 1:59Kafka together. Having multiple servers
- 2:02allows us to horizontally scale Kafka
- 2:04and also avoid having a single point of
- 2:06failure. These servers are called Kafka
- 2:09brokers. I'll explain this in more
- 2:11detail when we get to the Kafka
- 2:12architecture.
- 2:14Now that we understand what Kafka is,
- 2:16let's understand why do we actually need
- 2:18Kafka. Let's continue with the same
- 2:20e-commerce application. When a user
- 2:22places an order, the order service needs
- 2:24to notify the other services. Why can't
- 2:27it directly make API calls? Order
- 2:29service can call the payment service,
- 2:31then it can call the inventory service,
- 2:33and finally it can call the notification
- 2:35service. Yes, initially this might work
- 2:37perfectly fine, but as our application
- 2:39grows, we start running into problems.
- 2:42The first problem is performance and
- 2:44downtime. When the order service
- 2:46directly calls other services, the
- 2:48performance and availability of those
- 2:50services can directly affect the order
- 2:52service.
- 2:53For example, imagine the payment service
- 2:55is taking a long time to respond. The
- 2:57order service has already created the
- 2:59order, but now it has to wait for the
- 3:01payment service to complete its request.
- 3:04So even if the order service itself is
- 3:06fast, the overall request becomes slow
- 3:08because one of the downstream services
- 3:10is slow. And this can become even more
- 3:12problematic when a service is
- 3:14unavailable. For example, suppose the
- 3:16order service successfully creates the
- 3:18order and then tries to call the
- 3:19inventory service, but the inventory
- 3:21service is temporarily down. Now the
- 3:23order service has to deal with this
- 3:25failure. Should it retry the request?
- 3:28Maybe, but how many times? It can retry
- 3:30a few times, but what happens if the
- 3:32inventory service is still down?
- 3:34Eventually the request will fail.
- 3:36So in this case, the request isn't
- 3:38automatically saved somewhere so that
- 3:40the inventory service can process it
- 3:42later when it comes back up.
- 3:45Unless we build a separate mechanism to
- 3:47store and retry these failed requests,
- 3:49that information is essentially lost
- 3:51from the perspective of the inventory
- 3:53service.
- 3:54The second problem is tight coupling.
- 3:56The order service now needs to know
- 3:58about every service that needs to react
- 4:00to an order. Today it might be payment
- 4:02service, inventory service, and
- 4:04notification service. But tomorrow, we
- 4:06might add some analytics service or
- 4:08recommendation service. Now, every time
- 4:10we introduce a new service, we need to
- 4:12modify the order service because we have
- 4:15to integrate the API call.
- 4:17This is what tight coupling means.
- 4:19Instead, we should have a platform where
- 4:21the order service can simply publish the
- 4:23information that an order has been
- 4:25placed, and it shouldn't need to know
- 4:27which services are interested in this
- 4:29information. The payment service can
- 4:31consume it and process the payment. The
- 4:33inventory service can consume it and
- 4:34update the inventory, and the
- 4:36notification service can consume it and
- 4:38send the notification to the user.
- 4:41And tomorrow, if we introduce an
- 4:42analytics service, it can simply start
- 4:44consuming these events as well.
- 4:46We don't have to modify the order
- 4:48service at all. This makes our services
- 4:50much more loosely coupled. Kafka solves
- 4:53both these problems by acting as an
- 4:55intermediary between our services.
- 4:57Instead of the order service directly
- 4:59calling the payment, inventory, and
- 5:00notification services, it can send the
- 5:03information to Kafka. For example, when
- 5:05an order is placed, the order service
- 5:07can publish an event to Kafka that an
- 5:10order has been placed. Kafka stores this
- 5:12event. Now the order service doesn't
- 5:14have to wait for the payment service,
- 5:16inventory service, or notification
- 5:18service to process it. The order service
- 5:20can simply publish the event and
- 5:22continue with its own work. The
- 5:24different services can then consume this
- 5:26event from Kafka whenever they are
- 5:28ready. For example, the payment service
- 5:30can consume the event and process the
- 5:31payment. The inventory service can
- 5:33consume the same event and update the
- 5:35inventory, and the notification service
- 5:37can consume it and send the order
- 5:39confirmation. If the inventory service
- 5:41is slow, the order service doesn't have
- 5:44to wait for it. And if the inventory
- 5:45service is temporarily down, the event
- 5:48can remain stored in Kafka. When the
- 5:50inventory service comes back up, it can
- 5:52consume the event and process it.
- 5:54So the event is not simply lost because
- 5:57the downstream service was unavailable.
- 5:59And Kafka also helps us solve the tight
- 6:01coupling problem. The order service
- 6:03doesn't need to know which services are
- 6:05consuming the event. It only needs to
- 6:07know that it has to publish an order
- 6:09placed event to Kafka. If tomorrow we
- 6:11introduce an analytic service, we don't
- 6:13need to modify the order service. The
- 6:16analytic service can simply start
- 6:17consuming the same event from Kafka.
- 6:20So now instead of services directly
- 6:22communicating with each other, they
- 6:23communicate through Kafka. And this is
- 6:26the basic idea behind using Kafka in a
- 6:28distributed system. Now let's take a
- 6:30look at this diagram. This is the
- 6:32complete architecture of Apache Kafka in
- 6:34an e-commerce application. And honestly,
- 6:37when you look at it for the first time,
- 6:39it might seem a little overwhelming
- 6:41because there are so many components
- 6:42here, producers, consumers, brokers,
- 6:45topics, and many more. But don't worry,
- 6:47you don't need to understand everything
- 6:49at once. What I'll do in this video is
- 6:51I'll pick up each component one by one
- 6:54and we'll gradually build this entire
- 6:55architecture from scratch. Okay? So
- 6:58let's start with the producer. A
- 7:00producer is an application that sends
- 7:02events to Kafka. Imagine we have order
- 7:05service in our e-commerce application
- 7:07and a customer has just placed an order.
- 7:09Order service processes this order first
- 7:12and once it's done, other services also
- 7:14need to perform their respective tasks.
- 7:16For example, the payment service needs
- 7:18to process the payment, the inventory
- 7:20service needs to update the stock, and
- 7:22the notification service needs to notify
- 7:24the customer. So our order service needs
- 7:26to share the order information with
- 7:28these services. Hence the order service
- 7:30sends an event to Kafka containing the
- 7:32order information. And since it's
- 7:34sending events to Kafka, we call the
- 7:36order service a producer. Now what's a
- 7:39topic?
- 7:40A topic is a place where Kafka stores
- 7:42events of a particular type. So far,
- 7:44order service has sent an event to Kafka
- 7:46containing the order information. Kafka
- 7:49needs to store this event somewhere so
- 7:51that it can be consumed later on. For
- 7:53that, it uses topic. We can create a
- 7:56topic named order events. This topic
- 7:58will be used to store all the order
- 8:00related events. So whenever a new order
- 8:02is placed, the order service can publish
- 8:05an event to this topic. And any service
- 8:07that needs this order information can
- 8:09read the events from this topic and
- 8:11perform its respective tasks.
- 8:13Now let's take another example. Suppose
- 8:15we have a user service in our
- 8:17application which handles user
- 8:18registrations. Whenever a new user
- 8:21registers, the user service processes
- 8:23the registration and sends an event to
- 8:25Kafka containing the user information.
- 8:28Now this is a different type of event.
- 8:30It contains user-related information
- 8:32rather than order-related information.
- 8:34So instead of storing it in our order
- 8:36events topic, we can create another
- 8:39topic called user-hyphen-events.
- 8:41Now any service that might be interested
- 8:43in this user information can read the
- 8:45events from this topic. For example,
- 8:47analytics service might want to track
- 8:49the new user's registrations or
- 8:51marketing service might want to send any
- 8:53discount offers to newly registered
- 8:55users. So we have two topics now, order
- 8:58events and user events. The idea is
- 9:00simple. We can create different topics
- 9:02to organize different type of events,
- 9:04making it easier for the respective
- 9:06services to consume the events they are
- 9:08interested in. Now let's talk about
- 9:10partitions. A topic is divided into
- 9:13multiple partitions to distribute the
- 9:15events across them, allowing Kafka to
- 9:17process events in parallel.
- 9:19Let's understand this with our example.
- 9:21Imagine our e-commerce application is
- 9:23getting thousands of orders every
- 9:25second. It's becoming difficult for the
- 9:27topic to handle all these events. So
- 9:29Kafka allows us to divide a topic into
- 9:31multiple partitions. Let's say we divide
- 9:34our order events topic into three
- 9:35partitions, partition zero, one, and
- 9:37two. Our first order event arrives,
- 9:40Kafka stores it in partition zero. Then
- 9:42the second order event arrives, Kafka
- 9:44stores it in partition one. Next the
- 9:46third order event arrives, Kafka stores
- 9:48it in partition two. And then the fourth
- 9:50order event arrives, Kafka again stores
- 9:52it in partition zero. So this goes on in
- 9:55a round-robin manner.
- 9:57Now please note that round-robin is one
- 9:58of the partitioning strategy. There are
- 10:00other partitioning strategies as well
- 10:02which we'll explore in later videos. Now
- 10:04you might be wondering why are we doing
- 10:06this?
- 10:07So partitions gives us two advantages.
- 10:09First is that data is now distributed
- 10:12and second is that they enable parallel
- 10:14processing. But what processing? Topics
- 10:16were just storing data. Yes, but in
- 10:19future consumers are going to read this
- 10:21data. That's where we'll have parallel
- 10:23processing. We can define multiple
- 10:25consumers and they can read events from
- 10:27different partitions simultaneously.
- 10:29We'll explore how this works when we
- 10:31discuss consumer and consumer groups.
- 10:33Similarly, we can also divide the user
- 10:35events topic into multiple partitions
- 10:37depending on the user registration
- 10:39traffic.
- 10:40For our example, let's divide it into
- 10:42two partitions.
- 10:44Now let's talk about consumers and
- 10:45consumer groups. A consumer reads events
- 10:48from Kafka, while a consumer group is a
- 10:50group of consumers that process events
- 10:53in parallel.
- 10:54Let's continue with our example.
- 10:56Consider the payment service. Its job is
- 10:58to read these order events and process
- 11:00the payments. So we can define a
- 11:02consumer inside payment service that
- 11:05reads events from the order events topic
- 11:07and processes the payment. As we
- 11:09discussed earlier, our application is
- 11:11receiving thousands of orders every
- 11:13second. A single consumer might not be
- 11:15able to process all these events fast
- 11:17enough. So we can define three consumers
- 11:19all belonging to the payment service.
- 11:21Their responsibility is to read the
- 11:23order events in parallel and process the
- 11:25payments. Together these three consumers
- 11:28form a consumer group. Let's call it
- 11:30payment group.
- 11:31Now there's one common confusion I want
- 11:32to clear up here. A lot of people think
- 11:35that when we say three consumers, one
- 11:37consumer should handle payments, another
- 11:39should handle inventory, and the third
- 11:40should handle notifications. No, that's
- 11:43not how it works. All three consumers
- 11:45within this group belong to the payment
- 11:47service and perform the same task
- 11:49processing payments.
- 11:51Okay?
- 11:52Now Kafka distributes all the partitions
- 11:54among these consumers. Like our topic
- 11:56has three partitions, so each consumer
- 11:58can read from a different partition.
- 12:00This means all three consumers are
- 12:02reading all together different events
- 12:04and that too parallelly.
- 12:06Now whatever consumer goes down,
- 12:08Kafka will redistribute the partitions
- 12:10among the remaining consumers. One of
- 12:12the consumer will read from two
- 12:13partitions. Consumer two reads one event
- 12:15from partition one and then one event
- 12:17from partition two. Also, what happens
- 12:20if we create more consumers than the
- 12:21number of partitions? Then the extra
- 12:23consumers will remain idle because each
- 12:26partition can be assigned to only one
- 12:27consumer within a consumer group at a
- 12:29time.
- 12:30That's why the rule of thumb is number
- 12:32of partitions should be greater than
- 12:34equal to number of consumers in a
- 12:35consumer group.
- 12:37Okay.
- 12:38Now let's bring our inventory service
- 12:39into the picture. It also needs to read
- 12:42the same order events, but its job is to
- 12:44update the stock. So we'll define
- 12:46another consumer group called inventory
- 12:48group with three consumers. These
- 12:50consumers will also read same events
- 12:52from the same order events topic in
- 12:54parallel and update the inventory
- 12:56accordingly.
- 12:57Similarly, we can create another
- 12:58consumer group called notification group
- 13:01again with three consumers to handle
- 13:03customer notifications.
- 13:05So all three consumer groups are reading
- 13:07same events from the same topic, but
- 13:09each group processes those events to
- 13:11perform its own task.
- 13:13The payment group processes payments,
- 13:14the inventory group updates the stock
- 13:16and the notification group sends the
- 13:17notifications. And that's how multiple
- 13:20consumer groups can consume the same
- 13:22events from a single Kafka topic.
- 13:24Similarly, we can have a consumer group
- 13:26called user analytics group with two
- 13:29consumers that reads events from user
- 13:31events topic in parallel and process
- 13:33them for analytics. And another consumer
- 13:35group called marketing group which also
- 13:38reads events from user events and
- 13:40process them for marketing.
- 13:42Now what is offset? Whenever Kafka
- 13:44stores an event inside a partition, it
- 13:46assigns a unique number to that event
- 13:48within that partition. This number is
- 13:50called an offset. Let's understand this
- 13:52with an example. Suppose partition zero
- 13:55currently got three events. The first
- 13:57event has offset zero, the second has
- 13:59offset one, and the third has offset
- 14:01two. Similarly, partition one and two
- 14:03maintain their own offsets. Now why do
- 14:05we need these offsets? Remember,
- 14:08consumers read events from Kafka.
- 14:10Offsets help consumers keep track of
- 14:12which events they have already processed
- 14:14and where they need to continue reading
- 14:16from.
- 14:17For example, if a consumer has processed
- 14:18events up to offset two, it can track
- 14:20its progress and continue reading from
- 14:23the next offset.
- 14:24And that's it. An offset is simply a
- 14:26position that identifies an event within
- 14:28a particular partition.
- 14:30Now, what exactly is a broker?
- 14:32A broker is simply a server running
- 14:34Kafka.
- 14:35So far, our entire Kafka system is
- 14:37running on a single server. All our
- 14:39topics, partitions, and events are
- 14:41managed by a single server. Now, what if
- 14:43that server crashes?
- 14:45Our entire Kafka system could become
- 14:47unavailable and we could even lose
- 14:49access to the data stored on that
- 14:50server.
- 14:51That why Kafka is designed as a
- 14:53distributed system with multiple
- 14:55servers. These servers are called Kafka
- 14:58brokers.
- 14:59Now, how does it work? Let's say we have
- 15:01three Kafka brokers, broker one, two,
- 15:03and three.
- 15:04Now, remember our order events topic,
- 15:06which has three partitions, partition
- 15:07zero, one, and two. We can distribute
- 15:10these partitions across our three
- 15:11brokers.
- 15:12For example, partition zero can be
- 15:14stored on broker one, partition one on
- 15:16broker two, and partition two on broker
- 15:18three.
- 15:19This way, our data is distributed across
- 15:21multiple servers instead of being
- 15:23managed by a single server. This means
- 15:25that whenever a producer sends an event
- 15:27to partition zero, broker one handles
- 15:29that request. Similarly, when a consumer
- 15:32wants to read events from partition
- 15:33zero, it fetches that data from broker
- 15:36one.
- 15:36Now, the data is distributed, but
- 15:38there's still a problem. What if broker
- 15:40one crashes? Partition one and two are
- 15:42safe, but we still lost partition zero.
- 15:45No?
- 15:46So even though we have distributed our
- 15:48data across multiple servers, we still
- 15:50need a way to handle server failures.
- 15:52And that's where replication comes into
- 15:54the picture. Let's understand how
- 15:56replication works. Suppose you configure
- 15:58the replication factor of a topic to
- 16:00three.
- 16:01This means each partition will have
- 16:03three replicas. Partition zero will have
- 16:05three replicas, one on broker one, one
- 16:07on broker two, and one on broker three.
- 16:10Similarly, partition one will also have
- 16:11three replicas distributed across these
- 16:13brokers. And same goes for partition
- 16:15two. But now you might think we took
- 16:17every partition and assigned it to every
- 16:19broker. See, this is the important part.
- 16:22We might have made these replicas to
- 16:24avoid data loss, true. But the produce
- 16:26and consume requests are still
- 16:28distributed. Let me explain. Out of the
- 16:31three replicas of a partition, Kafka
- 16:33elects one replica as the leader and the
- 16:35remaining two replicas act as followers.
- 16:38For example, partition zero is leader in
- 16:40broker one and follower in broker two
- 16:42and three.
- 16:43Partition one is leader in broker two
- 16:45and follower in broker one and three.
- 16:48Partition two is leader in broker three
- 16:50and follower in broker one and two. So
- 16:52even though we have three copies of
- 16:54partition zero, the read and write
- 16:56request for that partition are handled
- 16:58by broker one only.
- 17:00Similarly, read and write request for
- 17:01partition one is handled by broker two
- 17:04and read and write request for partition
- 17:05two is handled by broker three. Now if
- 17:08broker one suddenly crashes, we don't
- 17:10necessarily lose partition zero's data
- 17:12and we don't have to stop sending
- 17:14requests to it either. But how?
- 17:16As soon as broker one crashes, partition
- 17:18zero's replica on broker two becomes the
- 17:21new leader. Partition one is already the
- 17:23leader on broker two. So now both
- 17:25partition zero and partition one will be
- 17:27handled by broker two. This means all
- 17:29the produce and consume request for both
- 17:32these partitions will now be handled by
- 17:34broker two. And that's how Kafka uses
- 17:36replication to handle broker failures
- 17:39and keep our system running.
- 17:40Now let's take our second topic, user
- 17:42events. Remember, we have divided this
- 17:44topic into two partitions, partition
- 17:46zero and one. And just like our order
- 17:48events topic, we can also distribute
- 17:51these partitions across our three
- 17:52brokers. For example, partition zero can
- 17:54be stored on broker one and partition
- 17:56one can be stored on broker two.
- 17:58Now you might be wondering, what about
- 18:00broker three? Well, that's completely
- 18:01fine. We don't necessarily need to use
- 18:03every broker to store the partitions of
- 18:05a particular topic. Again, we can keep a
- 18:08replication factor for this topic as
- 18:09well if we want.
- 18:11So this was all about brokers.
- 18:13Now all these brokers working together
- 18:15are called a Kafka cluster. Simple.
- 18:18The best way to learn Kafka is by
- 18:20working with it practically, rather than
- 18:22just understanding the theory. So in
- 18:24this video we'll learn how to set up
- 18:26Kafka in our system. The important part
- 18:28is that I'll be using Kraf for the
- 18:30setup. It's a modern way of using Kafka.
- 18:33Earlier we used to use ZooKeeper, but
- 18:35now it has changed. Okay. So in this
- 18:38video we'll first download Apache Kafka,
- 18:40then we'll explore the various scripts
- 18:41and configuration files that Kafka
- 18:43provides. I'll also briefly explain you
- 18:45the difference between ZooKeeper and
- 18:47Kraf. Then we'll run the Kafka server
- 18:50using some terminal commands and one by
- 18:52one I'll explain you what each command
- 18:54actually does. Then we'll create a
- 18:56topic, we'll start a producer and a
- 18:58consumer, and finally we'll produce some
- 19:00data to see how Kafka actually works in
- 19:03practice.
- 19:04All right. So let's get started.
- 19:06So we'll go to the browser and we'll
- 19:08search for download Apache Kafka. Here
- 19:11we'll have this link. We'll open this
- 19:12link and here we have different versions
- 19:15of Kafka. Okay, so I'm going to use this
- 19:17one, 4.3.1. I'll just click on this
- 19:19link, binary download. It gives me a TGZ
- 19:22file. Okay, so I'll just click on this
- 19:25and it will be downloaded. Okay, then
- 19:27I'll visit this folder. Now this is a
- 19:29TGZ file. I have to extract it.
- 19:31Okay, so I'll double click on this if
- 19:33I'm on Mac and it will be extracted. So
- 19:35here I got a folder for Kafka.
- 19:38Okay. Now inside this folder we have
- 19:40different other folders, like we have
- 19:42this bin, we have this config. So mostly
- 19:45all our scripts are written inside this
- 19:46bin folder. Scripts are used to perform
- 19:49Kafka operations, like if you want to
- 19:51start the Kafka server for that we have
- 19:53the script Kafka server start.sh.
- 19:56Similarly, we have this one to stop our
- 19:58Kafka server. Then we have other scripts
- 20:00as well, like if you want to run a
- 20:02consumer on our console, on the Kafka
- 20:04console, we have this script. Similarly,
- 20:06we have one for producer as well. Okay,
- 20:08so there are different scripts for
- 20:09different type of operations. Okay.
- 20:12Similarly, we have a config folder.
- 20:14Inside this folder we have various
- 20:16configuration files, like I'll just open
- 20:18this one, server.properties.
- 20:23Okay, so this is the file where we can
- 20:25configure our Kafka system. Okay we can
- 20:27give various configurations. So I'll
- 20:29just zoom it a little bit. Here we give
- 20:31the server basic configurations. Then
- 20:33here we have the socket server settings.
- 20:36For example here we have set up the
- 20:38listener. Okay so the listener is set up
- 20:40to 9092. This means the Kafka server
- 20:43runs on 9092 port. Okay so these type of
- 20:46things we can configure. Similarly here
- 20:48I have some log configurations. So here
- 20:51we can configure the log directory. Like
- 20:53here all the logs will be saved in this
- 20:55folder. Okay slash temp slash craft
- 20:57combined logs.
- 20:59Okay so this also you can configure and
- 21:01we have a few more things like here we
- 21:03have num dot partitions. Okay so
- 21:05whenever we create a topic if we don't
- 21:07specifically define any partitions this
- 21:10will be the default partitions. Okay so
- 21:12if if the number of partitions have not
- 21:14been specified then the partitions will
- 21:16be considered as one.
- 21:18Okay so this also we can configure over
- 21:19here. Okay so similarly we have a lot of
- 21:21properties that are configured over
- 21:23here. I'll not change any of these for
- 21:25now. I'll keep everything as default.
- 21:27Okay now one thing before moving ahead
- 21:29earlier Kafka was dependent on something
- 21:32called ZooKeeper.
- 21:33What is ZooKeeper? ZooKeeper is a
- 21:35service that Kafka used to maintain
- 21:37cluster metadata and support
- 21:39coordination among the brokers. In
- 21:41simple words it manages the Kafka
- 21:43cluster.
- 21:44See Kafka runs on multiple servers. Now
- 21:46which brokers are currently part of the
- 21:48cluster? Which partition will be the
- 21:50leader in a particular broker? What
- 21:52happens if a broker fails? Who will
- 21:54handle that particular partition? All
- 21:56these things were handled by ZooKeeper.
- 21:58Apart from this all the cluster metadata
- 22:01that is information about the topics,
- 22:02partitions, their replicas these were
- 22:05also managed by ZooKeeper only. But
- 22:07there was a problem with ZooKeeper.
- 22:09ZooKeeper worked perfectly but it was a
- 22:11separate service. So Kafka had to
- 22:13operate two separate systems Kafka
- 22:16itself and ZooKeeper for coordination
- 22:18and metadata management. This meant
- 22:21additional infra configuration,
- 22:22monitoring and maintenance. So Kafka's
- 22:25developers wanted to simplify the
- 22:27architecture. That's why Kraft is
- 22:29introduced. Now what is Kraft? Kraft
- 22:32stands for Kafka raft. It is Kafka's
- 22:34built-in metadata management
- 22:36architecture. Instead of depending on an
- 22:38external Zookeeper system, Kafka uses
- 22:40its own controllers to manage cluster
- 22:43metadata. So going forward, we are going
- 22:45to do the setup using Kraft instead of
- 22:47Zookeeper. Now let's start with the
- 22:49setup. I've opened terminal and first
- 22:52I'll go to the place where I have
- 22:54downloaded Kafka.
- 22:55Okay, so it's inside downloads, then
- 22:57this folder CD Kafka.
- 23:00Okay, so we are in this folder. Now here
- 23:02we have to run a few commands to do the
- 23:03setup.
- 23:04Okay, so these are the steps. I have
- 23:06written all the steps over here. Okay,
- 23:08so the first command is that we have to
- 23:10generate a cluster ID. Okay, so we are
- 23:12creating a Kafka cluster and we need to
- 23:14give a unique identifier to that
- 23:16cluster. For that, we create a cluster
- 23:18ID and save that cluster ID inside this
- 23:20variable.
- 23:22Okay, so this is what I'm going to do.
- 23:23I'll just copy this command
- 23:25and
- 23:26we are going to paste this in terminal
- 23:28over here. Okay, this is a one-time
- 23:30step. I'll press enter
- 23:33and a cluster ID has been created, a
- 23:34random UUID, and it has been saved
- 23:36inside this variable.
- 23:38Okay, now I'll just do the second step.
- 23:42It says format Kafka storage. All right.
- 23:45So as I told you earlier, now we are not
- 23:47using Zookeeper, now we are using Kraft.
- 23:50Okay, so we need a dedicated space to
- 23:52save our cluster metadata inside Kafka
- 23:54only. Okay, inside Kraft. So for that,
- 23:57we need to do this format Kafka storage.
- 23:59Basically, by formatting, what I mean is
- 24:01that we initialize the directories, we
- 24:03prepare some directories where we are
- 24:05going to save the metadata and we assign
- 24:07this storage to the cluster ID. Okay, so
- 24:10the cluster which we have created will
- 24:12assign a dedicated storage for that.
- 24:14Okay, so I'll just copy this command
- 24:18and I'll paste it over here. Okay, so
- 24:20you can see that what we do is inside
- 24:21this bin, inside this bin, we have the
- 24:23script Kafka storage.sh. We run this
- 24:26script and we are associating it with
- 24:28this cluster ID, which is present in
- 24:31this variable. Okay, Kafka cluster ID,
- 24:33which we already saved over here.
- 24:35Okay, so that's what we are actually
- 24:36doing, that we are running this script
- 24:38and we are
- 24:40we are basically this script initializes
- 24:41a storage and we are associating this
- 24:43storage with this cluster ID, which is
- 24:46present in this Kafka cluster ID.
- 24:48Okay, and the properties which we are
- 24:50considering for all our commands that
- 24:52will be this config server dot
- 24:53properties. Okay, the file which I
- 24:56showed you earlier. Okay, this is this
- 24:58file server dot properties was present
- 24:59in the config folder.
- 25:01Okay, so I'll press enter.
- 25:04And great, we have defined a directory
- 25:06where we are going to save all our
- 25:08cluster metadata and all the storage has
- 25:10been initialized. Okay, so both these
- 25:12steps were a one-time step. We don't
- 25:13have to do it again. Now we have a
- 25:15cluster ID and we have a storage. Now we
- 25:18are going to start Kafka. Okay, so
- 25:19earlier first we needed to start
- 25:21ZooKeeper as a different service, then
- 25:23only we could start Kafka. But now it's
- 25:25not necessary because we have we are
- 25:27using KRaft.
- 25:28Okay, so the third command will be to
- 25:30start the Kafka server. For this, I'll
- 25:32use this command.
- 25:33Okay, so it's very simple. Inside this
- 25:36bin, we have this script Kafka server
- 25:38start dot sh and as I told you, the
- 25:41server dot properties we are considering
- 25:43from this file. Okay, the configuration
- 25:45we are
- 25:46we are considering these configurations.
- 25:48Okay, so I'll just click enter.
- 25:52And great, our Kafka server has started.
- 25:54Okay, and if you'll search for it,
- 25:56you'll see that it's actually started on
- 25:58port 9092.
- 26:00All right. So let me start by creating a
- 26:02topic first. So for that, I'll be using
- 26:05this command. Okay, I'll just copy it
- 26:07and I'll paste it here in the terminal.
- 26:10Okay, so what we are doing here is first
- 26:12inside this bin folder, we have this
- 26:14script Kafka topics dot sh. Okay, we are
- 26:17running this script and I'm saying that
- 26:19we have to create a topic and here we
- 26:21provide the topic name. Okay, so I am
- 26:23creating a topic named order slash
- 26:25events. Okay, so this means that
- 26:27anything related to order can be
- 26:29published on this topic. Okay, that's
- 26:31what I'm creating this topic for. Okay,
- 26:33so whenever any order will be placed, a
- 26:35producer will produce a message on this
- 26:37topic.
- 26:38Okay. Now here I'm specifying the
- 26:40partitions for this topic. So I'm
- 26:42keeping it as three. Now why three?
- 26:44Because a single partition won't be able
- 26:46to handle much traffic. Okay, in future
- 26:48if traffic increases, so I want multiple
- 26:51partitions for that. So here I'm keeping
- 26:53it as three.
- 26:54Okay, and we are running this topic on
- 26:55localhost 9092.
- 26:57Okay, so I'll just press enter
- 26:59and our topic has been created.
- 27:01Okay, so I can just verify this. For
- 27:03that I have another command that
- 27:04verifies the topic. I'll just copy this
- 27:07command.
- 27:08Now again, what we are doing here is
- 27:10that
- 27:11we are running this script inside this
- 27:13bin folder only, kafka topics.sh and I
- 27:16have given this command slash list,
- 27:18okay, hyphen hyphen list. Okay, so this
- 27:20will list all the topics
- 27:22and as you can see as of now there are
- 27:23two topics. One is the order events
- 27:25which we created and there is a default
- 27:27topic that Kafka creates itself, which
- 27:29is this consumer underscore offsets.
- 27:32Okay, so here Kafka stores all the
- 27:33offsets. Okay, so this is created by
- 27:35Kafka and this one is created by us.
- 27:38Okay. Now one more thing which we can do
- 27:40is that we can inspect the number of
- 27:41partitions. So if you want to inspect a
- 27:44topic in a more detailed way, then we
- 27:46can just use this command.
- 27:48All right. So I'll again copy and paste
- 27:50it. Again, we are running the same
- 27:51script and I have given the command
- 27:53describe. This describes the topic order
- 27:56events. Okay, so I'll just press enter
- 27:59and here you can see that we have all
- 28:01the details of this topic.
- 28:03Okay, so like in this topic we have
- 28:04created three partitions. So the first
- 28:06partition is partition zero, then
- 28:08partition one, then partition two.
- 28:10Okay, and there are information about
- 28:12leader and replicas as well. Okay, so
- 28:14this is this comes into play when we
- 28:16have multiple Kafka servers. Okay, so
- 28:18right now we have only one server, that
- 28:19's why we'll just not look into these.
- 28:22Okay, so but now we know that there are
- 28:23three partitions and their names are
- 28:25partition zero, one and two.
- 28:27Okay, so let me just clear all this.
- 28:30And now let's start a producer.
- 28:32Okay.
- 28:33So for that I'll another command. I'll
- 28:35just copy this
- 28:38and let's paste it over here. Okay. So
- 28:40for that we have the script kafka
- 28:42console producer.sh. We'll run this
- 28:44script and here we have to mention the
- 28:47topic
- 28:48on which it will produce. Okay. So I'm
- 28:50saying that we are going to produce on
- 28:51this order events. Okay. So that's why
- 28:53here we have to mention that and we are
- 28:56running it on localhost 9092 and I have
- 28:58added another property that just gives
- 29:01some name to this producer. Okay. We are
- 29:02just giving some ID. So I'm calling it
- 29:05as order service. Okay. So whenever an
- 29:07order comes to this service, it will
- 29:09just produce an event to this topic
- 29:11order events. Okay. That's what we are
- 29:13trying to
- 29:14you know reproduce.
- 29:16Okay.
- 29:17So I'll just press enter and if you see
- 29:19this sign, it means now you can enter
- 29:21the message over here. Okay. Let's say I
- 29:23can say hello or not hello. Actually I
- 29:25can say that order placed
- 29:28with ID
- 29:29101. Okay. I can write some message
- 29:32something like this. Okay. But right now
- 29:33I'll not press enter. I'll not produce
- 29:35this message. First I want some
- 29:37consumers that are actually going to
- 29:39consume these messages.
- 29:41Okay. So consider it like this that we
- 29:43have an order service that produces the
- 29:44message on this topic order events. Now
- 29:47there could be some service which needs
- 29:49information about the order. Okay. Like
- 29:51there could be a payment service which
- 29:53wants to know that they have to make an
- 29:55payment for this order. Okay. So they
- 29:57will consume the message. So let me
- 29:59create a consumer group for that. I'll
- 30:00just open another tab and here we can
- 30:03start creating a consumer group.
- 30:05Okay. So we'll name this group as
- 30:07payment group and I'll be using this
- 30:09command for that.
- 30:11Okay.
- 30:13All right.
- 30:14So again let me explain inside the bin
- 30:17folder we have the script kafka console
- 30:19consumer.sh. We run this script. Here
- 30:22also we have to specify the topic from
- 30:24which it's going to consume the
- 30:25messages. Okay. So here we'll specify
- 30:27that it's going to read from order
- 30:29hyphen events
- 30:31and we are assigning a group to this
- 30:32consumer. So the group's name is payment
- 30:34group
- 30:35and again we are running on bootstrap
- 30:37server 9092.
- 30:38Then I've given a client ID to this. Now
- 30:40this is the first consumer which I'm
- 30:42creating inside this group. Okay, so in
- 30:45total we are going to create three
- 30:46consumers. You can see I have written
- 30:47three commands over here. Payment
- 30:49consumer two, payment consumer three and
- 30:51here is payment consumer one. Okay, so
- 30:53now again why are we creating three
- 30:54consumers? Because as I told you, when
- 30:56the traffic increases, a single
- 30:58partition won't be able to handle all
- 31:00the traffic. Similarly, a single
- 31:01consumer will also not be able to handle
- 31:03this much traffic.
- 31:05Okay, so we define three consumers and
- 31:06each consumer will read the event from
- 31:08one one topic, from one partition.
- 31:12Okay, so that's what I'm going to show
- 31:13you practically over here. Okay, so
- 31:15right now let's create one consumer and
- 31:16let's see what happens. Okay, so here I
- 31:18have added one more property over here
- 31:20that says print partition is equal to
- 31:22true. Okay, so when the messages will be
- 31:24consumed using this property, we'll be
- 31:27able to know that the messages are
- 31:28coming from which partition. Okay, it
- 31:30will print that. Okay, so I'll show you
- 31:32this. Let's just start this consumer
- 31:33first.
- 31:35Okay.
- 31:36All right. So the consumer has been
- 31:38started.
- 31:39Okay. So let me just produce a message.
- 31:42Let's say
- 31:44order
- 31:45placed
- 31:46with ID
- 31:48101. Okay, something like this.
- 31:51All right.
- 31:52So you will see that this order has been
- 31:54placed and this message has been
- 31:56consumed in this consumer
- 31:58payment consumer one.
- 32:00Okay, and the partition on which the
- 32:02order was sent was partition one.
- 32:04Okay, so Kafka randomly decides that I
- 32:07can place another order. Maybe order
- 32:09placed with ID 102.
- 32:13And this time also it went to partition
- 32:15one. Again, let me place another order.
- 32:17Order
- 32:19order placed with
- 32:22ID maybe 103 this time.
- 32:25And again it went to partition one. Now
- 32:26why is it going to the same partition?
- 32:28Why is it not going to partition zero or
- 32:30partition two? See, right now there is
- 32:32not much traffic. Okay, we are I'm just
- 32:34writing these orders one by one and
- 32:37Kafka is able to perform all this with a
- 32:38single partition only. Okay, so if I
- 32:41have to equally distributed, then for
- 32:43that I have to actually run Kafka in a
- 32:45different manner. I have to run producer
- 32:47in a different manner and I will use
- 32:49something called keys. Okay, so these
- 32:51are the keys. Let me show you this.
- 32:54Okay, so I'll just stop this producer.
- 32:57Okay, so how do we stop it? We use
- 32:59control C if we are on Mac. Okay,
- 33:01control C and the producer has been
- 33:03stopped. Okay, now I'll again, I'll
- 33:06again
- 33:07add this producer. I'll start this
- 33:09producer. This time I have added a
- 33:11property that says reader property parse
- 33:13key is equal to true. Okay, so what is a
- 33:16key? See, when we send a message to a
- 33:18topic, we can also send a key along with
- 33:20that. Now what what Kafka does is it
- 33:23hashes this key and based on that hash,
- 33:26it decides on which partition the
- 33:27message should go. Okay, so this helps
- 33:29us divide the messages in a in a more
- 33:32equal manner. Okay, it's not necessary
- 33:34that it will go one by one to each
- 33:35partition, but still it will be
- 33:37distributed in a much better fashion.
- 33:39Okay, so I'll parse a key as well and
- 33:41here I'm specifying a key separator
- 33:44where I'm saying that this is equal to
- 33:45colon. Okay, so what does this mean?
- 33:47That when I'll write the message over
- 33:49here, like I'll just press enter and
- 33:51you'll get to know. Like here I'll write
- 33:53a message. So first I'll say let's say I
- 33:54have I'll keep my order ID as the key.
- 33:57Okay, so I've written 101. Then I'll add
- 33:59a colon. So whatever I'm writing before
- 34:02this colon, that will be considered as
- 34:04the key and whatever I'm writing over
- 34:06here, like here now I can say
- 34:08order placed with ID 101, something like
- 34:12this. Okay, so this will be considered
- 34:14as the message.
- 34:16All right, so I'll not
- 34:17I'll not press enter as of now. Let me
- 34:20start three consumers and now we'll see
- 34:22what happens. Okay, so let me just
- 34:24remove this consumer as well. I'll again
- 34:26print enter control C. So I have stopped
- 34:29the consumer.
- 34:31Let me clear this. Now one by one I'll
- 34:33start three consumers this time.
- 34:35Okay, so this is the consumer group one.
- 34:39Let me just paste this.
- 34:41Okay, this is consumer one.
- 34:45Now in another tab in in this tab I'll
- 34:48start a second consumer.
- 34:50Okay, so let me just copy and paste
- 34:53this. So here as well, it's the same
- 34:55payment group. Inside this group, I'm
- 34:57starting a second consumer, payment
- 34:58consumer two.
- 35:00Okay, I'll press enter.
- 35:02Then another tab in this tab I'll open
- 35:05consumer three.
- 35:07Okay.
- 35:09So this is the consumer three now.
- 35:11All right, so we have we are creating
- 35:13three consumers all inside a single
- 35:15consumer group as of now and the
- 35:16consumer group's name is payment group.
- 35:19Okay, so let's just verify all this.
- 35:20I'll I'll open one more tab and let's
- 35:22verify all this over here. Okay, so here
- 35:24we can list all the consumers in a
- 35:26particular group. So I'll be using this
- 35:28command for that.
- 35:29Let's me just copy it and paste it.
- 35:31Okay, so it says consumer groups.sh. We
- 35:33are running this script and I'm just
- 35:35describing the consumer group. And here
- 35:37I specified the group name which is the
- 35:39payment group. Okay, so I'll just press
- 35:41enter.
- 35:43All right.
- 35:44So what what we can see over here is
- 35:46that we have three consumers, consumer
- 35:49one two one two and three.
- 35:52Okay, so
- 35:53that is what I have done. I have created
- 35:54three consumers inside this payment
- 35:56group.
- 35:57Okay, and I can also verify one more
- 35:58thing
- 35:59that which consumer is going to read
- 36:00from which partition. This also we can
- 36:02check. Okay, so for that I'll be using
- 36:04this command. Again, we are running the
- 36:06script Kafka consumer groups.sh. I'm
- 36:09saying describe. I have specified the
- 36:11payment the group name and I have just
- 36:13added this verbose as well. Okay, so now
- 36:15when I press enter
- 36:18great. So we have a lot of information
- 36:19over here. Let me show you what it
- 36:22exactly says. So it says that consumer
- 36:25two will read from one partition.
- 36:26Consumer one will also read from one
- 36:28partition and consumer three will also
- 36:29read from one one partition. So each
- 36:31consumer will read from one partition.
- 36:33Here is the number of partitions which
- 36:35are specified. And here we have
- 36:37specified from which partition it will
- 36:39read. Like this is order events one.
- 36:42Okay, so this this means that this
- 36:44consumer two, this consumer two is
- 36:46reading from partition one. And this
- 36:48consumer one is going to read from
- 36:49partition zero and this consumer three
- 36:52is going to read from partition two.
- 36:54Okay, so this Kafka decides
- 36:55automatically. Earlier all this was
- 36:57managed by Zookeeper, but now Kafka
- 36:59decides it on its own.
- 37:01Okay. So
- 37:03we can remember this and let I'll just
- 37:05clear this out for now.
- 37:07Or let's keep it like this. Okay. So
- 37:08here we have consumer three.
- 37:11Here we have consumer two and here we
- 37:13have consumer one. Okay, right now I
- 37:14have not produced any message. So this
- 37:16is my first message. What what Kafka is
- 37:18going to do is that it will just use
- 37:20this key 101 and using this key it will
- 37:23create a hash and it will randomly send
- 37:25it to any of the partition. Okay. And we
- 37:27have already decided that which
- 37:29partition from which partition which
- 37:31consumer is going to read that has
- 37:32already been decided.
- 37:34Okay, so let's let's start producing
- 37:36messages. Let's say I entered this.
- 37:39Okay. So this went to consumer three.
- 37:42Okay, you will see it went to consumer
- 37:43three because it was on partition two.
- 37:45And as you can see from partition two,
- 37:48from this order events two, consumer
- 37:50three is going to read. Okay, that's why
- 37:52the message came to consumer three and
- 37:54you will see this is consumer two.
- 37:55Message is not here. This is consumer
- 37:57one. Message is again not here. Okay, so
- 37:59in a in one consumer group a single
- 38:02message is read by one of the consumers
- 38:05only.
- 38:05Okay, this is how it it happens. Okay,
- 38:07now let me change it. Let's say 105 this
- 38:10time. Okay, so I say
- 38:13order placed
- 38:15with ID 105.
- 38:18Okay. So by chance again it went to
- 38:20partition two. That's why it's send over
- 38:23here. This time let me change it a bit.
- 38:25Let's say 110
- 38:27order placed with ID 110.
- 38:33Okay, so this time it went to partition
- 38:34one. And partition one who is going to
- 38:36read from partition one? This is
- 38:38consumer two. Okay, let's verify it here
- 38:40as well. From partition one consumer
- 38:44two is going to read. Okay, this is the
- 38:45consumer two.
- 38:47Okay? So similarly, any message can go
- 38:49to any of the partition and we have
- 38:51already decided from which partition,
- 38:52which particular consumer is going to
- 38:54read the message. Okay, let's try one
- 38:56two more as well. Let it be 109 maybe.
- 39:00Okay?
- 39:01Order placed with ID 109. Let's see
- 39:05where it went. Okay, this time on the
- 39:09partition zero and partition zero means
- 39:11consumer one. Okay?
- 39:13Partition zero here means consumer one.
- 39:16All right. So this message which went
- 39:18over here, this is not present in in
- 39:20this as well as in in the in any of
- 39:22other consumers, okay? So one message is
- 39:25read by one of the consumers only in a
- 39:26single consumer group.
- 39:28All right.
- 39:29Okay.
- 39:30Now let me do one thing. Let's say I add
- 39:33one more consumer.
- 39:34Okay, so I'll just copy this command. We
- 39:36have this command to create consumers.
- 39:38Okay, I'll just copy this.
- 39:40And let's say
- 39:42I'll just enter a
- 39:44I'll create another tab and here maybe I
- 39:46create consumer four.
- 39:49Okay?
- 39:50So what's going to happen now? We have
- 39:52four consumers but three partitions.
- 39:54Okay? So let's just run this command
- 39:57again
- 39:58where we have allocated these partitions
- 40:00to these consumers and let's see what
- 40:01happens.
- 40:02Okay? So I'm again describing the Kafka
- 40:04consumer groups.sh and we have this
- 40:07payment group and I'm using this verbose
- 40:09command. Okay? So let see what's going
- 40:11to happen.
- 40:13Okay?
- 40:13So you can see from from the partition
- 40:16one, consumer two is going to read. From
- 40:18partition zero, consumer one is going to
- 40:19read. From partition two, consumer three
- 40:22is going to read. But this consumer
- 40:23four, this does not have any partition.
- 40:26It is not assigned any partition. Okay,
- 40:28so this is a very important interview
- 40:30question. Interviewer asks you the
- 40:32question in this way only that if you
- 40:33have three partitions and four consumers
- 40:35that what then what is going to happen
- 40:37to the fourth consumer? So actually this
- 40:39consumer is going to sit idle. There is
- 40:41no point in creating this consumer
- 40:43because it will not be assigned any
- 40:45partition because one from one partition
- 40:48on max to max one consumer can read.
- 40:50Okay. So, there is no point in creating
- 40:52the consumers greater than the number of
- 40:54partitions.
- 40:55Okay. Now, let me just delete this
- 40:58consumer.
- 41:00Okay. Or let me just stop this. This is
- 41:02the consumer and I'll just stop this by
- 41:04saying control C.
- 41:06Okay. I'll just again run this command
- 41:08and now you will see that again there
- 41:09are only three consumers now.
- 41:11Okay. So, this one is reading from
- 41:13partition one. This is reading from
- 41:14partition zero and this is reading from
- 41:16partition two.
- 41:17Okay. Now, what if what if I just let me
- 41:20first close this tab. Yeah. So, this one
- 41:22was for consumer three, right? Let's say
- 41:25I just remove consumer three as well.
- 41:27Okay. So, now we have two consumers but
- 41:30three partitions. So, what is going to
- 41:32happen now? Let's again run this
- 41:33command. First let me clear all this.
- 41:36Okay.
- 41:37So, if I run it again.
- 41:40All right. So, from the second partition
- 41:43consumer two is going to read but from
- 41:45zero and one consumer one is going to
- 41:47read. Okay. So, in this case when when
- 41:50the number of consumers are less than
- 41:51the number of partitions then what
- 41:53happens is that one consumer is going to
- 41:55read from two partitions. Okay. So, in
- 41:57this case like it has assigned it like
- 41:59this that consumer one is going to read
- 42:01both the messages from the consume from
- 42:03partition zero as well as from partition
- 42:04one and consumer two is going to read
- 42:06only from partition two.
- 42:08Okay. And if I'll remove one more then
- 42:10that single consumer is going to read
- 42:12from all the three partitions. Okay.
- 42:13This is how it works. Okay. So, let me
- 42:16just show it as well. So, here we had
- 42:18the producer, right?
- 42:20Let me create one more message. Let's
- 42:21say it is
- 42:23112
- 42:25maybe. Okay. Order placed with ID 112.
- 42:33Okay. So, it went over here. So, as you
- 42:35can see earlier it was reading from
- 42:36partition two partition one but now it's
- 42:38reading from partition two as well.
- 42:40Consumer two you can see over here that
- 42:42consumer two is going to read from
- 42:45partition two. Okay. Partition two has
- 42:46been assigned to it.
- 42:48And let say I I
- 42:50create one more maybe 105 again or maybe
- 42:53106 I'll use.
- 42:56Order
- 42:58placed
- 43:00with ID 106.
- 43:05All right, so it went over here.
- 43:07So this is the partition zero and who is
- 43:09going to read from partition zero?
- 43:10Consumer one. Okay, who is going to read
- 43:12from partition zero? This partition zero
- 43:14consumer one is going to read from
- 43:15partition. And you can see this number
- 43:17also here it's two written. So what does
- 43:19it mean? That this consumer this is
- 43:20reading from one partition. This
- 43:22consumer it is reading from two
- 43:24partitions.
- 43:25Okay, so this is how Kafka works. If the
- 43:27number of consumers are less than the
- 43:29number of partitions, then one consumer
- 43:31can be assigned to multiple partitions
- 43:33as well.
- 43:34All right. Now so far we have created
- 43:36one consumer group called the payment
- 43:38group. Now the responsibility of payment
- 43:40group is to process all the payments.
- 43:42Okay, dedicatedly belongs to the payment
- 43:44service. But let's say there are other
- 43:46services as well. For example, there
- 43:48might be an inventory service which also
- 43:51wants to consume the same information.
- 43:53Okay, because whenever an order is
- 43:54placed, the inventory service also wants
- 43:57to update their stock at their end.
- 43:59Okay, so they will also consume the same
- 44:01message. Similarly, there could be a
- 44:02notification service as well. They also
- 44:05want to consume the same messages
- 44:07because they want to send some
- 44:08notifications to the user whenever some
- 44:10order is placed. Okay, so we can have
- 44:13different groups for different types of
- 44:15services. Okay, we already have a
- 44:17payment group for the payment service.
- 44:19Similarly, we can create another
- 44:21consumer groups for different services.
- 44:23So let me do that.
- 44:25First, let me just
- 44:27close all these
- 44:29consumers that I've created so far.
- 44:31Okay, we'll do it again. So this was the
- 44:32producer. Okay, I'll just say control C
- 44:36and let me clear this out
- 44:38and let me just close all these tabs.
- 44:40Okay, by closing them, I can simply stop
- 44:43them. You could either press control C
- 44:46or you could just close the tabs. It
- 44:47works the same.
- 44:49Okay, so here Kafka is running and here
- 44:51we don't have anything as of now. Okay,
- 44:53so I'll just start again. I'll first
- 44:55create a producer.
- 44:57Okay. So
- 44:59this was the producer.
- 45:03All right. I'll I'll just start the
- 45:04producer again. Okay. I've created it
- 45:05earlier as well, but now I'm starting it
- 45:07again. Okay. So the producer has been
- 45:09started. I'll open another tab. Here
- 45:12I'll first create consumer group one.
- 45:14Okay. I'll start it again.
- 45:16So here we have all the commands.
- 45:20Okay.
- 45:22So this one is the payment group and
- 45:24this is the payment consumer one. Okay.
- 45:27I've started it. Then in the next tab,
- 45:30I'll just say consumer two.
- 45:33Okay.
- 45:34And in the third tab, I'll say
- 45:37consumer three.
- 45:39Okay. So we have three consumers in the
- 45:41consumer group, payment group.
- 45:44Okay. Then in the next tab, I'll just
- 45:46paste this only. This time I'll change
- 45:48it to
- 45:49inventory consumer. Okay. Inventory
- 45:52consumer one and I'll even change the
- 45:54group.
- 45:55Okay. So this will be
- 45:58inventory group.
- 46:00Okay. And let me just copy this for
- 46:01future.
- 46:03All right.
- 46:05Okay.
- 46:05So again, we have this inventory group
- 46:07and this will be inventory consumer two.
- 46:10Then in the next tab, I'll just again
- 46:12paste this and this will be inventory
- 46:14consumer three.
- 46:16Okay. So in the inventory group as well,
- 46:18I have created three different
- 46:20consumers.
- 46:21All right. Now in the next tab, I'll
- 46:23just change it to
- 46:26notification consumer one.
- 46:29Okay. And the group name also, this
- 46:31should be
- 46:34notification
- 46:35group.
- 46:36Okay. Let me just copy this.
- 46:39All right.
- 46:41Okay. So this is notification consumer
- 46:42one.
- 46:43Similarly, notification consumer two
- 46:48and
- 46:50notification consumer
- 46:53three.
- 46:54All right. So I've opened a lot of tabs
- 46:56and I've created in total nine
- 46:58consumers, three of each, three
- 47:00belonging to one group, three belonging
- 47:02to second group and the next three
- 47:03belonging to the third group. Okay, so
- 47:05let's just verify first that what all
- 47:07have been created. Okay, so we have a
- 47:10command that lists all the consumer
- 47:12groups as well. Okay, so earlier we
- 47:14listed all the consumers inside a single
- 47:16group, but we have this command that
- 47:18lists all the consumer groups as well.
- 47:20Because I'm going to use that. First,
- 47:22let's see which are our consumer groups.
- 47:25Okay, so we have three different groups,
- 47:27payment, notification and inventory.
- 47:29Okay.
- 47:30Let me just clear this.
- 47:32Now let's see
- 47:34what each group has, okay, and which
- 47:36partition they are reading from.
- 47:38All right. So these were the this was
- 47:41the command to check the partitions.
- 47:43All right. So first we are checking the
- 47:45payment group.
- 47:47Okay.
- 47:48So these are the details for payment
- 47:49group. These are different partitions
- 47:51and these are the different consumers.
- 47:54Now similarly I can check for
- 47:58this will be inventory group.
- 48:01Inventory group.
- 48:03Okay, so again we have different
- 48:05partitions and different consumers are
- 48:06reading from these.
- 48:08And I'll also paste
- 48:10the command again and this time we'll
- 48:12check for notification group.
- 48:14Okay, notification group.
- 48:18All right. Okay, so here as well these
- 48:20partitions are assigned to different
- 48:22consumers inside the notification
- 48:24consumer group.
- 48:26Okay, so I'll just keep this page open
- 48:27so we can refer to these.
- 48:29And I'll go back to the producer. So it
- 48:32was somewhere here in the beginning.
- 48:35Yeah, okay. So here we have the
- 48:37producer.
- 48:38All right. So this time I'll just say
- 48:40101 maybe
- 48:42order
- 48:44placed
- 48:45for [snorts] ID 101.
- 48:48Okay.
- 48:49So when I produce this message,
- 48:51this key will be used, a hash value will
- 48:53be created and it will be sent to some
- 48:54partition.
- 48:56Okay. So
- 48:58this message has not been read here,
- 49:01it's not consumed over here, it has not
- 49:02been consumed over here as well. It has
- 49:05been consumed over here in the payment
- 49:07group in payment consumer three. Okay,
- 49:09so why? Because it came to partition
- 49:12two. Okay, so remember that the message
- 49:14came to partition two and this payment
- 49:17consumer two was reading from partition
- 49:19two. Sorry, payment consumer three was
- 49:21reading from partition two. That's why
- 49:23we got the message over here.
- 49:25Okay, now let's see the inventory group.
- 49:27So in the consumer one there is no
- 49:29message. Then we have
- 49:31the consumer two again no message. In
- 49:33the consumer three there is order placed
- 49:35for ID 101. Okay, so you can see that
- 49:38the same message is read by the payment
- 49:40group as well as the inventory group.
- 49:42Okay, and in fact, we'll just check the
- 49:44notification group as well. Okay, so not
- 49:46in the consumer one
- 49:48and not in two. It is again in three.
- 49:51Okay, so it's it's been read by the
- 49:53notification consumer as well. So what
- 49:56do we learn from this? That inside a
- 49:58single group only one of the consumer
- 50:01reads that message. But when we have
- 50:03different consumer groups, all of them
- 50:05read the same message. Okay, that's
- 50:07their purpose. So you design them like
- 50:09that that inside a single group if we
- 50:11have multiple consumers that we have
- 50:13created to handle high traffic. Okay,
- 50:15because we can achieve parallelism. They
- 50:17can
- 50:18they can read these messages parallelly
- 50:20different consumers when there will be a
- 50:21lot of traffic.
- 50:22Okay, so they can the the request get
- 50:24distributed among the partitions and
- 50:26these consumers can read from those
- 50:28partitions simultaneously. But we create
- 50:31different consumer groups
- 50:33so that different services can perform
- 50:35their own duty. Okay, so you should it
- 50:37should always be clear to you that what
- 50:39is the exact difference between a
- 50:40consumer and a consumer group. Okay,
- 50:42consumer groups are created for
- 50:44different services to perform their
- 50:46duties and consumers are added in a
- 50:48consumer group to increase the
- 50:50scalability and to increase the
- 50:53parallelism.
- 50:54Okay, so I hope this is clear. Now let's
- 50:57verify once that which which topic was
- 50:59reading from which partition. So
- 51:01in the payment group, this the payment
- 51:03group. The message came to partition
- 51:05two, right? So, this was the partition
- 51:07two and as you can see, consumer three
- 51:09was reading from here. Similarly, in in
- 51:12this as well, in inventory group as
- 51:13well, if we see,
- 51:15okay, this partition two was read by
- 51:17consumer three and that's what happened
- 51:18over there. And similarly here as well,
- 51:21this partition two was read by consumer
- 51:24three in notification group as well.
- 51:27Okay, so this is how it works. Let me
- 51:28just produce one more message. Let's
- 51:30just verify it again. Okay, maybe 110
- 51:34order placed for ID 110.
- 51:40Okay, so this time it's partition one.
- 51:42The message went to partition one and
- 51:44it's being read by consumer two in the
- 51:46payment group. Okay, payment consumer
- 51:47two. Similarly here, it has it's being
- 51:50read it's being read by inventory
- 51:52consumer two. And here as well, I guess,
- 51:57yeah, in the notification consumer two.
- 52:00Okay. So this is how it works.
- 52:03Now so far we have seen how different
- 52:04consumers and consumer groups work. Now
- 52:07let me show you a few more commands that
- 52:09comes in handy when we are working with
- 52:10Kafka. Okay, so we'll learn how to
- 52:12delete a consumer group.
- 52:14Okay, so we have this command for that.
- 52:17All right. So
- 52:18what I'm going to do that first before
- 52:21running this command, I need to close
- 52:22these consumers. Okay, so one thing
- 52:24which you should understand is that
- 52:26closing a consumer and deleting a
- 52:28consumer group are two different things.
- 52:30Okay, so like this consumer is open. So
- 52:32it means that it it is able to read the
- 52:34messages. So if I'll just do control C,
- 52:37it means that now this consumer has
- 52:38stopped. Okay. So similarly, I'll just
- 52:41stop all of them. Okay, I can also close
- 52:43the terminal as well. That also works.
- 52:46Okay, so I'll terminate these processes
- 52:47one by one.
- 52:49All right.
- 52:51Okay.
- 52:53So we created nine consumers and I'll
- 52:55just delete all of them. I'll just stop
- 52:58all of them one by one.
- 53:00All right. Okay, let me clear this as
- 53:02well. Okay, so So have a producer over
- 53:04here and we don' have any consumer up
- 53:06and running right now, but you'll see
- 53:08that if I just list all the consumers.
- 53:11Okay. I'll just list them. You will see
- 53:14that we still have these consumer
- 53:16groups. Okay. Because I've not deleted
- 53:18them. I have only stopped the consumers.
- 53:21Okay. So now I'm going to delete these
- 53:22consumer groups. For that we can just
- 53:25use this command. We run this script,
- 53:28this Kafka consumer groups.sh and I just
- 53:31write delete then the group name over
- 53:33here and it will just simply delete the
- 53:36payment group.
- 53:37Okay. Now if I list them again.
- 53:41Okay. Now I only have two of them.
- 53:44Okay. So similarly I can also delete
- 53:47uh
- 53:48the notification as well as the
- 53:49inventory group. Let me show it to you.
- 53:52This is the inventory group. I'll delete
- 53:55this as well
- 53:56and I'll also delete the
- 53:59notification group.
- 54:02All right.
- 54:04Great. Okay. So now if I just list all
- 54:08these consumers, you will see none of
- 54:10them.
- 54:12Okay. There are no consumers. Now let's
- 54:15do one more thing. We have already
- 54:17created this topic named order events.
- 54:19Let's create another topic and maybe
- 54:22call it user events.
- 54:24Okay. So we had this order events to
- 54:27process all the order related events.
- 54:29Similarly we can have another topic user
- 54:31events that processes all the user
- 54:33related events. Okay. Like for example,
- 54:35if a user registers on your platform, so
- 54:37the user service can produce a message
- 54:39on this topic.
- 54:40And let's keep the partitions as two.
- 54:43Okay.
- 54:44So I've created this topic. Similarly
- 54:46I'll create a producer for it. So we'll
- 54:48create a user service.
- 54:50Okay. Let me just copy this
- 54:52and let's call this
- 54:55a user service and it will not read from
- 54:58order events. It will read from user
- 55:00events.
- 55:01Okay.
- 55:03So here I can produce some messages.
- 55:05Maybe I say that user registered
- 55:08with
- 55:09ID 101 something like this.
- 55:12And again I can produce a message
- 55:16user registered with ID 102 and let me
- 55:21produce one more message user registered
- 55:25with
- 55:26ID
- 55:28103.
- 55:29Okay. Now you can see over here that I
- 55:32have not started any consumer yet. Okay.
- 55:35I have just simply started producing the
- 55:37messages. We have a topic and a
- 55:38producer, but we don't have a consumer
- 55:40as of now. Okay. So now let's create a
- 55:42consumer.
- 55:44Okay. So I'll just copy this command
- 55:48and I'll paste it over here. Okay. So
- 55:49this time maybe it could be a user
- 55:52analytics group. Okay. So there is a
- 55:54service user analytics service which
- 55:56does the analysis whenever a user
- 55:58registers. Okay. So it will consume the
- 56:00messages whenever a user is registering
- 56:02on the platform.
- 56:03Okay.
- 56:04So it's a user analytic group and it
- 56:06will read from this topic user events
- 56:10and
- 56:11maybe the consumer name is
- 56:14UA consumer one.
- 56:16Okay. So I'll just press enter.
- 56:19Now will this consumer read all these
- 56:21messages that we have already produced?
- 56:23Let's see.
- 56:25So far it hasn't and actually it will
- 56:27not. Okay. It will only start reading
- 56:29from the message which we produce now.
- 56:31Okay. Like if I produce user registered
- 56:34with ID 104
- 56:36then it might read this. Okay. It is
- 56:39reading only this, but not the messages
- 56:41which were produced earlier before we
- 56:42started the consumer.
- 56:44Okay. Now let me let me do one thing.
- 56:47Maybe I want maybe I want my consumer to
- 56:49read these messages as well. Okay. Maybe
- 56:51I wanted my consume my consumer to be
- 56:53configured in such a way that it could
- 56:54read those messages as well. Okay. So
- 56:57I'll just create the consumer again. Let
- 56:59me just stop this and I'll delete this
- 57:01group altogether.
- 57:02Okay.
- 57:04And we'll create it again.
- 57:05All right. So
- 57:07I'll use this command to delete.
- 57:09All right. this is a user analytics
- 57:12group.
- 57:14So I'll just write user
- 57:16analytics group
- 57:18and we'll delete this.
- 57:20Okay.
- 57:22So again, we don't have any consumer.
- 57:24We have this user service, this producer
- 57:26which has produced these four messages.
- 57:28And now I'll create this consumer again,
- 57:30but this time I'll configure it in such
- 57:33a way that it will read the messages
- 57:34from the beginning. Okay, so for that we
- 57:36use this from beginning command. Okay,
- 57:39so this is what I actually wanted to
- 57:40show you. I'll just copy this
- 57:42and I'll paste it over here. Okay, so
- 57:43let's call it a UA consumer one
- 57:46and let's call it
- 57:50user
- 57:51analytics
- 57:53group
- 57:54and let's make it read from user events.
- 57:58Okay, so this time we have added this
- 58:00property from beginning. This allows
- 58:02this consumer group to read the messages
- 58:04from the beginning, the ones which have
- 58:06been created from the starting even
- 58:08before this this consumer was
- 58:11up and running.
- 58:12Okay, so I'll press enter
- 58:14and as of now it has not read, but yes,
- 58:17now it reads all the messages.
- 58:19Okay, so this is a very important
- 58:22command which you should know because
- 58:23it's used quite a lot. And in fact,
- 58:26whenever you are creating any new
- 58:27consumer, you need to make sure that
- 58:29what exactly is your requirement,
- 58:30whether you want it to read the messages
- 58:32from the beginning or do you want it to
- 58:34read only the new messages?
- 58:36Okay.
- 58:37Now let me show you one more command.
- 58:38This is to delete a Kafka topic. So I'll
- 58:41just copy this command and I can just
- 58:43paste it over here. So again, we are
- 58:44running this script
- 58:46Kafka topics.sh and I think I forgot to
- 58:49add the bin folder.
- 58:50Just a second.
- 58:52I'll add the location bin slash Kafka
- 58:55topics.sh. Okay, we run the script. Then
- 58:58we say this command delete and here we
- 59:00write the topic name.
- 59:01Okay, so the topic name will be let me
- 59:04just add it over here.
- 59:09Okay, so let's say we want to delete the
- 59:11user events topic. So I can delete users
- 59:14hyphen events.
- 59:17Okay.
- 59:18And maybe I can list the topics. All
- 59:20right. We already saw a command for
- 59:22that. Yeah. Verify the topics.
- 59:26Okay.
- 59:27So we can see that
- 59:29we only have order events and okay, we
- 59:30still have user events.
- 59:33All right.
- 59:34Let me try deleting it again.
- 59:37Okay. User events. Maybe there was a
- 59:39spelling mistake. All right.
- 59:41Seems fine. Now let me list it again.
- 59:45Okay. Yeah. This time it's deleted. I'm
- 59:47not sure why it was not earlier. Maybe
- 59:49it was a spelling mistake. Yeah. And
- 59:51I'll also delete order events.
- 59:54Okay. And I'll list it again.
- 59:57Okay. Still order events is also here.
- 59:59Let me run it again.
- 1:00:02order hyphen events. What am I missing?
- 1:00:06Okay. Delete the topic. And I think
- 1:00:08there is space. Yeah. Fine.
- 1:00:13Okay. I think maybe it gets updated a
- 1:00:15little bit late. All right. It's fine.
- 1:00:17Okay. So we can delete the topics as
- 1:00:19well like this.
- 1:00:21Okay. So these were few of the commands
- 1:00:22that are also very helpful when you work
- 1:00:24with Kafka. And I've already shown you
- 1:00:26how consumers work, how producers work,
- 1:00:28how consumer groups work. So my
- 1:00:30recommendation is you also install Kafka
- 1:00:32in your system, play with it, create
- 1:00:34your own producers, consumers, see how
- 1:00:37partitions are working, in which
- 1:00:38partition the message is going, all
- 1:00:40these things.
- 1:00:41Okay.
- 1:00:41And I hope this video was helpful.
- 1:00:43Everything was clear. For more such
- 1:00:45content, please follow my channel, The
- 1:00:47Curious Coder. Java interview prep can
- 1:00:50get really confusing. There are just too
- 1:00:52many topics and figuring out what to
- 1:00:54actually prepare can be a lot of hassle.
- 1:00:56So I have selectively picked the
- 1:00:58questions and answers that are actually
- 1:00:59important for the interviews from Java
- 1:01:01basics to strings, exception handling,
- 1:01:04memory management, collections. I have
- 1:01:06compiled all the
About this transcript
This page contains the full transcript of Kafka Full Course | Apache Kafka Tutorial for Beginners with Hands-On by The Curious Coder, generated from the public captions YouTube serves with the video. The transcript has 10,775 words across 1,770 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.