YouTube2Text

Kafka Full Course | Apache Kafka Tutorial for Beginners with Hands-On — Transcript

by The Curious Coder · 10,775 words · 1,770 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Apache Kafka is widely used in backend
  2. 0:02systems. It handles real-time data
  3. 0:04between multiple services. But to
  4. 0:06understand how Kafka actually works, you
  5. 0:08first need to understand its complete
  6. 0:10architecture and the role of each
  7. 0:12component. So in this video, I'll break
  8. 0:15down the complete Kafka architecture
  9. 0:16step by step and we'll understand the
  10. 0:19role of each component. First of all,
  11. 0:21what is Kafka? Kafka is a distributed
  12. 0:24event streaming platform. If you search
  13. 0:26for Kafka, you'll find the same
  14. 0:28definition everywhere with these four
  15. 0:30words, distributed event streaming
  16. 0:32platform. In this video, I'll explain
  17. 0:35what each of these words actually means
  18. 0:37using a simple e-commerce application
  19. 0:39example. Let's start with event. An
  20. 0:41event is a record of something that has
  21. 0:43happened in a system. For example, a
  22. 0:45user placed an order. This is an event.
  23. 0:48Now suppose our e-commerce application
  24. 0:50has thousands of users. Orders are
  25. 0:52continuously being placed. So events are
  26. 0:55being generated continuously. Instead of
  27. 0:57waiting for all these events to
  28. 0:59accumulate, we can process them in real
  29. 1:01time. This continuous flow of events is
  30. 1:03called event streaming. Now whenever an
  31. 1:06event happens in our application, we
  32. 1:08might want other parts of the system to
  33. 1:10know about it. For example, when an
  34. 1:12order is placed, we might want the
  35. 1:14payment service to process the payment,
  36. 1:16the inventory service to update the
  37. 1:18inventory, and the notification service
  38. 1:20to send a confirmation to the user. So
  39. 1:23in a way, the order service needs to
  40. 1:25notify all the services that an order
  41. 1:27has been placed. In more technical
  42. 1:29terms, we say that this event needs to
  43. 1:32be produced by one application and
  44. 1:34consumed by other applications. Now we
  45. 1:36need some platform that can actually
  46. 1:38handle these events, something that
  47. 1:40allows our application to produce
  48. 1:41events, store events, and allow other
  49. 1:44applications to consume those events.
  50. 1:46Kafka provides all of this functionality
  51. 1:49and it allows these events to be
  52. 1:50processed continuously in real time.
  53. 1:53That's why we call Kafka an event
  54. 1:54streaming platform. It is also called
  55. 1:57distributed because we can run multiple
  56. 1:59Kafka together. Having multiple servers
  57. 2:02allows us to horizontally scale Kafka
  58. 2:04and also avoid having a single point of
  59. 2:06failure. These servers are called Kafka
  60. 2:09brokers. I'll explain this in more
  61. 2:11detail when we get to the Kafka
  62. 2:12architecture.
  63. 2:14Now that we understand what Kafka is,
  64. 2:16let's understand why do we actually need
  65. 2:18Kafka. Let's continue with the same
  66. 2:20e-commerce application. When a user
  67. 2:22places an order, the order service needs
  68. 2:24to notify the other services. Why can't
  69. 2:27it directly make API calls? Order
  70. 2:29service can call the payment service,
  71. 2:31then it can call the inventory service,
  72. 2:33and finally it can call the notification
  73. 2:35service. Yes, initially this might work
  74. 2:37perfectly fine, but as our application
  75. 2:39grows, we start running into problems.
  76. 2:42The first problem is performance and
  77. 2:44downtime. When the order service
  78. 2:46directly calls other services, the
  79. 2:48performance and availability of those
  80. 2:50services can directly affect the order
  81. 2:52service.
  82. 2:53For example, imagine the payment service
  83. 2:55is taking a long time to respond. The
  84. 2:57order service has already created the
  85. 2:59order, but now it has to wait for the
  86. 3:01payment service to complete its request.
  87. 3:04So even if the order service itself is
  88. 3:06fast, the overall request becomes slow
  89. 3:08because one of the downstream services
  90. 3:10is slow. And this can become even more
  91. 3:12problematic when a service is
  92. 3:14unavailable. For example, suppose the
  93. 3:16order service successfully creates the
  94. 3:18order and then tries to call the
  95. 3:19inventory service, but the inventory
  96. 3:21service is temporarily down. Now the
  97. 3:23order service has to deal with this
  98. 3:25failure. Should it retry the request?
  99. 3:28Maybe, but how many times? It can retry
  100. 3:30a few times, but what happens if the
  101. 3:32inventory service is still down?
  102. 3:34Eventually the request will fail.
  103. 3:36So in this case, the request isn't
  104. 3:38automatically saved somewhere so that
  105. 3:40the inventory service can process it
  106. 3:42later when it comes back up.
  107. 3:45Unless we build a separate mechanism to
  108. 3:47store and retry these failed requests,
  109. 3:49that information is essentially lost
  110. 3:51from the perspective of the inventory
  111. 3:53service.
  112. 3:54The second problem is tight coupling.
  113. 3:56The order service now needs to know
  114. 3:58about every service that needs to react
  115. 4:00to an order. Today it might be payment
  116. 4:02service, inventory service, and
  117. 4:04notification service. But tomorrow, we
  118. 4:06might add some analytics service or
  119. 4:08recommendation service. Now, every time
  120. 4:10we introduce a new service, we need to
  121. 4:12modify the order service because we have
  122. 4:15to integrate the API call.
  123. 4:17This is what tight coupling means.
  124. 4:19Instead, we should have a platform where
  125. 4:21the order service can simply publish the
  126. 4:23information that an order has been
  127. 4:25placed, and it shouldn't need to know
  128. 4:27which services are interested in this
  129. 4:29information. The payment service can
  130. 4:31consume it and process the payment. The
  131. 4:33inventory service can consume it and
  132. 4:34update the inventory, and the
  133. 4:36notification service can consume it and
  134. 4:38send the notification to the user.
  135. 4:41And tomorrow, if we introduce an
  136. 4:42analytics service, it can simply start
  137. 4:44consuming these events as well.
  138. 4:46We don't have to modify the order
  139. 4:48service at all. This makes our services
  140. 4:50much more loosely coupled. Kafka solves
  141. 4:53both these problems by acting as an
  142. 4:55intermediary between our services.
  143. 4:57Instead of the order service directly
  144. 4:59calling the payment, inventory, and
  145. 5:00notification services, it can send the
  146. 5:03information to Kafka. For example, when
  147. 5:05an order is placed, the order service
  148. 5:07can publish an event to Kafka that an
  149. 5:10order has been placed. Kafka stores this
  150. 5:12event. Now the order service doesn't
  151. 5:14have to wait for the payment service,
  152. 5:16inventory service, or notification
  153. 5:18service to process it. The order service
  154. 5:20can simply publish the event and
  155. 5:22continue with its own work. The
  156. 5:24different services can then consume this
  157. 5:26event from Kafka whenever they are
  158. 5:28ready. For example, the payment service
  159. 5:30can consume the event and process the
  160. 5:31payment. The inventory service can
  161. 5:33consume the same event and update the
  162. 5:35inventory, and the notification service
  163. 5:37can consume it and send the order
  164. 5:39confirmation. If the inventory service
  165. 5:41is slow, the order service doesn't have
  166. 5:44to wait for it. And if the inventory
  167. 5:45service is temporarily down, the event
  168. 5:48can remain stored in Kafka. When the
  169. 5:50inventory service comes back up, it can
  170. 5:52consume the event and process it.
  171. 5:54So the event is not simply lost because
  172. 5:57the downstream service was unavailable.
  173. 5:59And Kafka also helps us solve the tight
  174. 6:01coupling problem. The order service
  175. 6:03doesn't need to know which services are
  176. 6:05consuming the event. It only needs to
  177. 6:07know that it has to publish an order
  178. 6:09placed event to Kafka. If tomorrow we
  179. 6:11introduce an analytic service, we don't
  180. 6:13need to modify the order service. The
  181. 6:16analytic service can simply start
  182. 6:17consuming the same event from Kafka.
  183. 6:20So now instead of services directly
  184. 6:22communicating with each other, they
  185. 6:23communicate through Kafka. And this is
  186. 6:26the basic idea behind using Kafka in a
  187. 6:28distributed system. Now let's take a
  188. 6:30look at this diagram. This is the
  189. 6:32complete architecture of Apache Kafka in
  190. 6:34an e-commerce application. And honestly,
  191. 6:37when you look at it for the first time,
  192. 6:39it might seem a little overwhelming
  193. 6:41because there are so many components
  194. 6:42here, producers, consumers, brokers,
  195. 6:45topics, and many more. But don't worry,
  196. 6:47you don't need to understand everything
  197. 6:49at once. What I'll do in this video is
  198. 6:51I'll pick up each component one by one
  199. 6:54and we'll gradually build this entire
  200. 6:55architecture from scratch. Okay? So
  201. 6:58let's start with the producer. A
  202. 7:00producer is an application that sends
  203. 7:02events to Kafka. Imagine we have order
  204. 7:05service in our e-commerce application
  205. 7:07and a customer has just placed an order.
  206. 7:09Order service processes this order first
  207. 7:12and once it's done, other services also
  208. 7:14need to perform their respective tasks.
  209. 7:16For example, the payment service needs
  210. 7:18to process the payment, the inventory
  211. 7:20service needs to update the stock, and
  212. 7:22the notification service needs to notify
  213. 7:24the customer. So our order service needs
  214. 7:26to share the order information with
  215. 7:28these services. Hence the order service
  216. 7:30sends an event to Kafka containing the
  217. 7:32order information. And since it's
  218. 7:34sending events to Kafka, we call the
  219. 7:36order service a producer. Now what's a
  220. 7:39topic?
  221. 7:40A topic is a place where Kafka stores
  222. 7:42events of a particular type. So far,
  223. 7:44order service has sent an event to Kafka
  224. 7:46containing the order information. Kafka
  225. 7:49needs to store this event somewhere so
  226. 7:51that it can be consumed later on. For
  227. 7:53that, it uses topic. We can create a
  228. 7:56topic named order events. This topic
  229. 7:58will be used to store all the order
  230. 8:00related events. So whenever a new order
  231. 8:02is placed, the order service can publish
  232. 8:05an event to this topic. And any service
  233. 8:07that needs this order information can
  234. 8:09read the events from this topic and
  235. 8:11perform its respective tasks.
  236. 8:13Now let's take another example. Suppose
  237. 8:15we have a user service in our
  238. 8:17application which handles user
  239. 8:18registrations. Whenever a new user
  240. 8:21registers, the user service processes
  241. 8:23the registration and sends an event to
  242. 8:25Kafka containing the user information.
  243. 8:28Now this is a different type of event.
  244. 8:30It contains user-related information
  245. 8:32rather than order-related information.
  246. 8:34So instead of storing it in our order
  247. 8:36events topic, we can create another
  248. 8:39topic called user-hyphen-events.
  249. 8:41Now any service that might be interested
  250. 8:43in this user information can read the
  251. 8:45events from this topic. For example,
  252. 8:47analytics service might want to track
  253. 8:49the new user's registrations or
  254. 8:51marketing service might want to send any
  255. 8:53discount offers to newly registered
  256. 8:55users. So we have two topics now, order
  257. 8:58events and user events. The idea is
  258. 9:00simple. We can create different topics
  259. 9:02to organize different type of events,
  260. 9:04making it easier for the respective
  261. 9:06services to consume the events they are
  262. 9:08interested in. Now let's talk about
  263. 9:10partitions. A topic is divided into
  264. 9:13multiple partitions to distribute the
  265. 9:15events across them, allowing Kafka to
  266. 9:17process events in parallel.
  267. 9:19Let's understand this with our example.
  268. 9:21Imagine our e-commerce application is
  269. 9:23getting thousands of orders every
  270. 9:25second. It's becoming difficult for the
  271. 9:27topic to handle all these events. So
  272. 9:29Kafka allows us to divide a topic into
  273. 9:31multiple partitions. Let's say we divide
  274. 9:34our order events topic into three
  275. 9:35partitions, partition zero, one, and
  276. 9:37two. Our first order event arrives,
  277. 9:40Kafka stores it in partition zero. Then
  278. 9:42the second order event arrives, Kafka
  279. 9:44stores it in partition one. Next the
  280. 9:46third order event arrives, Kafka stores
  281. 9:48it in partition two. And then the fourth
  282. 9:50order event arrives, Kafka again stores
  283. 9:52it in partition zero. So this goes on in
  284. 9:55a round-robin manner.
  285. 9:57Now please note that round-robin is one
  286. 9:58of the partitioning strategy. There are
  287. 10:00other partitioning strategies as well
  288. 10:02which we'll explore in later videos. Now
  289. 10:04you might be wondering why are we doing
  290. 10:06this?
  291. 10:07So partitions gives us two advantages.
  292. 10:09First is that data is now distributed
  293. 10:12and second is that they enable parallel
  294. 10:14processing. But what processing? Topics
  295. 10:16were just storing data. Yes, but in
  296. 10:19future consumers are going to read this
  297. 10:21data. That's where we'll have parallel
  298. 10:23processing. We can define multiple
  299. 10:25consumers and they can read events from
  300. 10:27different partitions simultaneously.
  301. 10:29We'll explore how this works when we
  302. 10:31discuss consumer and consumer groups.
  303. 10:33Similarly, we can also divide the user
  304. 10:35events topic into multiple partitions
  305. 10:37depending on the user registration
  306. 10:39traffic.
  307. 10:40For our example, let's divide it into
  308. 10:42two partitions.
  309. 10:44Now let's talk about consumers and
  310. 10:45consumer groups. A consumer reads events
  311. 10:48from Kafka, while a consumer group is a
  312. 10:50group of consumers that process events
  313. 10:53in parallel.
  314. 10:54Let's continue with our example.
  315. 10:56Consider the payment service. Its job is
  316. 10:58to read these order events and process
  317. 11:00the payments. So we can define a
  318. 11:02consumer inside payment service that
  319. 11:05reads events from the order events topic
  320. 11:07and processes the payment. As we
  321. 11:09discussed earlier, our application is
  322. 11:11receiving thousands of orders every
  323. 11:13second. A single consumer might not be
  324. 11:15able to process all these events fast
  325. 11:17enough. So we can define three consumers
  326. 11:19all belonging to the payment service.
  327. 11:21Their responsibility is to read the
  328. 11:23order events in parallel and process the
  329. 11:25payments. Together these three consumers
  330. 11:28form a consumer group. Let's call it
  331. 11:30payment group.
  332. 11:31Now there's one common confusion I want
  333. 11:32to clear up here. A lot of people think
  334. 11:35that when we say three consumers, one
  335. 11:37consumer should handle payments, another
  336. 11:39should handle inventory, and the third
  337. 11:40should handle notifications. No, that's
  338. 11:43not how it works. All three consumers
  339. 11:45within this group belong to the payment
  340. 11:47service and perform the same task
  341. 11:49processing payments.
  342. 11:51Okay?
  343. 11:52Now Kafka distributes all the partitions
  344. 11:54among these consumers. Like our topic
  345. 11:56has three partitions, so each consumer
  346. 11:58can read from a different partition.
  347. 12:00This means all three consumers are
  348. 12:02reading all together different events
  349. 12:04and that too parallelly.
  350. 12:06Now whatever consumer goes down,
  351. 12:08Kafka will redistribute the partitions
  352. 12:10among the remaining consumers. One of
  353. 12:12the consumer will read from two
  354. 12:13partitions. Consumer two reads one event
  355. 12:15from partition one and then one event
  356. 12:17from partition two. Also, what happens
  357. 12:20if we create more consumers than the
  358. 12:21number of partitions? Then the extra
  359. 12:23consumers will remain idle because each
  360. 12:26partition can be assigned to only one
  361. 12:27consumer within a consumer group at a
  362. 12:29time.
  363. 12:30That's why the rule of thumb is number
  364. 12:32of partitions should be greater than
  365. 12:34equal to number of consumers in a
  366. 12:35consumer group.
  367. 12:37Okay.
  368. 12:38Now let's bring our inventory service
  369. 12:39into the picture. It also needs to read
  370. 12:42the same order events, but its job is to
  371. 12:44update the stock. So we'll define
  372. 12:46another consumer group called inventory
  373. 12:48group with three consumers. These
  374. 12:50consumers will also read same events
  375. 12:52from the same order events topic in
  376. 12:54parallel and update the inventory
  377. 12:56accordingly.
  378. 12:57Similarly, we can create another
  379. 12:58consumer group called notification group
  380. 13:01again with three consumers to handle
  381. 13:03customer notifications.
  382. 13:05So all three consumer groups are reading
  383. 13:07same events from the same topic, but
  384. 13:09each group processes those events to
  385. 13:11perform its own task.
  386. 13:13The payment group processes payments,
  387. 13:14the inventory group updates the stock
  388. 13:16and the notification group sends the
  389. 13:17notifications. And that's how multiple
  390. 13:20consumer groups can consume the same
  391. 13:22events from a single Kafka topic.
  392. 13:24Similarly, we can have a consumer group
  393. 13:26called user analytics group with two
  394. 13:29consumers that reads events from user
  395. 13:31events topic in parallel and process
  396. 13:33them for analytics. And another consumer
  397. 13:35group called marketing group which also
  398. 13:38reads events from user events and
  399. 13:40process them for marketing.
  400. 13:42Now what is offset? Whenever Kafka
  401. 13:44stores an event inside a partition, it
  402. 13:46assigns a unique number to that event
  403. 13:48within that partition. This number is
  404. 13:50called an offset. Let's understand this
  405. 13:52with an example. Suppose partition zero
  406. 13:55currently got three events. The first
  407. 13:57event has offset zero, the second has
  408. 13:59offset one, and the third has offset
  409. 14:01two. Similarly, partition one and two
  410. 14:03maintain their own offsets. Now why do
  411. 14:05we need these offsets? Remember,
  412. 14:08consumers read events from Kafka.
  413. 14:10Offsets help consumers keep track of
  414. 14:12which events they have already processed
  415. 14:14and where they need to continue reading
  416. 14:16from.
  417. 14:17For example, if a consumer has processed
  418. 14:18events up to offset two, it can track
  419. 14:20its progress and continue reading from
  420. 14:23the next offset.
  421. 14:24And that's it. An offset is simply a
  422. 14:26position that identifies an event within
  423. 14:28a particular partition.
  424. 14:30Now, what exactly is a broker?
  425. 14:32A broker is simply a server running
  426. 14:34Kafka.
  427. 14:35So far, our entire Kafka system is
  428. 14:37running on a single server. All our
  429. 14:39topics, partitions, and events are
  430. 14:41managed by a single server. Now, what if
  431. 14:43that server crashes?
  432. 14:45Our entire Kafka system could become
  433. 14:47unavailable and we could even lose
  434. 14:49access to the data stored on that
  435. 14:50server.
  436. 14:51That why Kafka is designed as a
  437. 14:53distributed system with multiple
  438. 14:55servers. These servers are called Kafka
  439. 14:58brokers.
  440. 14:59Now, how does it work? Let's say we have
  441. 15:01three Kafka brokers, broker one, two,
  442. 15:03and three.
  443. 15:04Now, remember our order events topic,
  444. 15:06which has three partitions, partition
  445. 15:07zero, one, and two. We can distribute
  446. 15:10these partitions across our three
  447. 15:11brokers.
  448. 15:12For example, partition zero can be
  449. 15:14stored on broker one, partition one on
  450. 15:16broker two, and partition two on broker
  451. 15:18three.
  452. 15:19This way, our data is distributed across
  453. 15:21multiple servers instead of being
  454. 15:23managed by a single server. This means
  455. 15:25that whenever a producer sends an event
  456. 15:27to partition zero, broker one handles
  457. 15:29that request. Similarly, when a consumer
  458. 15:32wants to read events from partition
  459. 15:33zero, it fetches that data from broker
  460. 15:36one.
  461. 15:36Now, the data is distributed, but
  462. 15:38there's still a problem. What if broker
  463. 15:40one crashes? Partition one and two are
  464. 15:42safe, but we still lost partition zero.
  465. 15:45No?
  466. 15:46So even though we have distributed our
  467. 15:48data across multiple servers, we still
  468. 15:50need a way to handle server failures.
  469. 15:52And that's where replication comes into
  470. 15:54the picture. Let's understand how
  471. 15:56replication works. Suppose you configure
  472. 15:58the replication factor of a topic to
  473. 16:00three.
  474. 16:01This means each partition will have
  475. 16:03three replicas. Partition zero will have
  476. 16:05three replicas, one on broker one, one
  477. 16:07on broker two, and one on broker three.
  478. 16:10Similarly, partition one will also have
  479. 16:11three replicas distributed across these
  480. 16:13brokers. And same goes for partition
  481. 16:15two. But now you might think we took
  482. 16:17every partition and assigned it to every
  483. 16:19broker. See, this is the important part.
  484. 16:22We might have made these replicas to
  485. 16:24avoid data loss, true. But the produce
  486. 16:26and consume requests are still
  487. 16:28distributed. Let me explain. Out of the
  488. 16:31three replicas of a partition, Kafka
  489. 16:33elects one replica as the leader and the
  490. 16:35remaining two replicas act as followers.
  491. 16:38For example, partition zero is leader in
  492. 16:40broker one and follower in broker two
  493. 16:42and three.
  494. 16:43Partition one is leader in broker two
  495. 16:45and follower in broker one and three.
  496. 16:48Partition two is leader in broker three
  497. 16:50and follower in broker one and two. So
  498. 16:52even though we have three copies of
  499. 16:54partition zero, the read and write
  500. 16:56request for that partition are handled
  501. 16:58by broker one only.
  502. 17:00Similarly, read and write request for
  503. 17:01partition one is handled by broker two
  504. 17:04and read and write request for partition
  505. 17:05two is handled by broker three. Now if
  506. 17:08broker one suddenly crashes, we don't
  507. 17:10necessarily lose partition zero's data
  508. 17:12and we don't have to stop sending
  509. 17:14requests to it either. But how?
  510. 17:16As soon as broker one crashes, partition
  511. 17:18zero's replica on broker two becomes the
  512. 17:21new leader. Partition one is already the
  513. 17:23leader on broker two. So now both
  514. 17:25partition zero and partition one will be
  515. 17:27handled by broker two. This means all
  516. 17:29the produce and consume request for both
  517. 17:32these partitions will now be handled by
  518. 17:34broker two. And that's how Kafka uses
  519. 17:36replication to handle broker failures
  520. 17:39and keep our system running.
  521. 17:40Now let's take our second topic, user
  522. 17:42events. Remember, we have divided this
  523. 17:44topic into two partitions, partition
  524. 17:46zero and one. And just like our order
  525. 17:48events topic, we can also distribute
  526. 17:51these partitions across our three
  527. 17:52brokers. For example, partition zero can
  528. 17:54be stored on broker one and partition
  529. 17:56one can be stored on broker two.
  530. 17:58Now you might be wondering, what about
  531. 18:00broker three? Well, that's completely
  532. 18:01fine. We don't necessarily need to use
  533. 18:03every broker to store the partitions of
  534. 18:05a particular topic. Again, we can keep a
  535. 18:08replication factor for this topic as
  536. 18:09well if we want.
  537. 18:11So this was all about brokers.
  538. 18:13Now all these brokers working together
  539. 18:15are called a Kafka cluster. Simple.
  540. 18:18The best way to learn Kafka is by
  541. 18:20working with it practically, rather than
  542. 18:22just understanding the theory. So in
  543. 18:24this video we'll learn how to set up
  544. 18:26Kafka in our system. The important part
  545. 18:28is that I'll be using Kraf for the
  546. 18:30setup. It's a modern way of using Kafka.
  547. 18:33Earlier we used to use ZooKeeper, but
  548. 18:35now it has changed. Okay. So in this
  549. 18:38video we'll first download Apache Kafka,
  550. 18:40then we'll explore the various scripts
  551. 18:41and configuration files that Kafka
  552. 18:43provides. I'll also briefly explain you
  553. 18:45the difference between ZooKeeper and
  554. 18:47Kraf. Then we'll run the Kafka server
  555. 18:50using some terminal commands and one by
  556. 18:52one I'll explain you what each command
  557. 18:54actually does. Then we'll create a
  558. 18:56topic, we'll start a producer and a
  559. 18:58consumer, and finally we'll produce some
  560. 19:00data to see how Kafka actually works in
  561. 19:03practice.
  562. 19:04All right. So let's get started.
  563. 19:06So we'll go to the browser and we'll
  564. 19:08search for download Apache Kafka. Here
  565. 19:11we'll have this link. We'll open this
  566. 19:12link and here we have different versions
  567. 19:15of Kafka. Okay, so I'm going to use this
  568. 19:17one, 4.3.1. I'll just click on this
  569. 19:19link, binary download. It gives me a TGZ
  570. 19:22file. Okay, so I'll just click on this
  571. 19:25and it will be downloaded. Okay, then
  572. 19:27I'll visit this folder. Now this is a
  573. 19:29TGZ file. I have to extract it.
  574. 19:31Okay, so I'll double click on this if
  575. 19:33I'm on Mac and it will be extracted. So
  576. 19:35here I got a folder for Kafka.
  577. 19:38Okay. Now inside this folder we have
  578. 19:40different other folders, like we have
  579. 19:42this bin, we have this config. So mostly
  580. 19:45all our scripts are written inside this
  581. 19:46bin folder. Scripts are used to perform
  582. 19:49Kafka operations, like if you want to
  583. 19:51start the Kafka server for that we have
  584. 19:53the script Kafka server start.sh.
  585. 19:56Similarly, we have this one to stop our
  586. 19:58Kafka server. Then we have other scripts
  587. 20:00as well, like if you want to run a
  588. 20:02consumer on our console, on the Kafka
  589. 20:04console, we have this script. Similarly,
  590. 20:06we have one for producer as well. Okay,
  591. 20:08so there are different scripts for
  592. 20:09different type of operations. Okay.
  593. 20:12Similarly, we have a config folder.
  594. 20:14Inside this folder we have various
  595. 20:16configuration files, like I'll just open
  596. 20:18this one, server.properties.
  597. 20:23Okay, so this is the file where we can
  598. 20:25configure our Kafka system. Okay we can
  599. 20:27give various configurations. So I'll
  600. 20:29just zoom it a little bit. Here we give
  601. 20:31the server basic configurations. Then
  602. 20:33here we have the socket server settings.
  603. 20:36For example here we have set up the
  604. 20:38listener. Okay so the listener is set up
  605. 20:40to 9092. This means the Kafka server
  606. 20:43runs on 9092 port. Okay so these type of
  607. 20:46things we can configure. Similarly here
  608. 20:48I have some log configurations. So here
  609. 20:51we can configure the log directory. Like
  610. 20:53here all the logs will be saved in this
  611. 20:55folder. Okay slash temp slash craft
  612. 20:57combined logs.
  613. 20:59Okay so this also you can configure and
  614. 21:01we have a few more things like here we
  615. 21:03have num dot partitions. Okay so
  616. 21:05whenever we create a topic if we don't
  617. 21:07specifically define any partitions this
  618. 21:10will be the default partitions. Okay so
  619. 21:12if if the number of partitions have not
  620. 21:14been specified then the partitions will
  621. 21:16be considered as one.
  622. 21:18Okay so this also we can configure over
  623. 21:19here. Okay so similarly we have a lot of
  624. 21:21properties that are configured over
  625. 21:23here. I'll not change any of these for
  626. 21:25now. I'll keep everything as default.
  627. 21:27Okay now one thing before moving ahead
  628. 21:29earlier Kafka was dependent on something
  629. 21:32called ZooKeeper.
  630. 21:33What is ZooKeeper? ZooKeeper is a
  631. 21:35service that Kafka used to maintain
  632. 21:37cluster metadata and support
  633. 21:39coordination among the brokers. In
  634. 21:41simple words it manages the Kafka
  635. 21:43cluster.
  636. 21:44See Kafka runs on multiple servers. Now
  637. 21:46which brokers are currently part of the
  638. 21:48cluster? Which partition will be the
  639. 21:50leader in a particular broker? What
  640. 21:52happens if a broker fails? Who will
  641. 21:54handle that particular partition? All
  642. 21:56these things were handled by ZooKeeper.
  643. 21:58Apart from this all the cluster metadata
  644. 22:01that is information about the topics,
  645. 22:02partitions, their replicas these were
  646. 22:05also managed by ZooKeeper only. But
  647. 22:07there was a problem with ZooKeeper.
  648. 22:09ZooKeeper worked perfectly but it was a
  649. 22:11separate service. So Kafka had to
  650. 22:13operate two separate systems Kafka
  651. 22:16itself and ZooKeeper for coordination
  652. 22:18and metadata management. This meant
  653. 22:21additional infra configuration,
  654. 22:22monitoring and maintenance. So Kafka's
  655. 22:25developers wanted to simplify the
  656. 22:27architecture. That's why Kraft is
  657. 22:29introduced. Now what is Kraft? Kraft
  658. 22:32stands for Kafka raft. It is Kafka's
  659. 22:34built-in metadata management
  660. 22:36architecture. Instead of depending on an
  661. 22:38external Zookeeper system, Kafka uses
  662. 22:40its own controllers to manage cluster
  663. 22:43metadata. So going forward, we are going
  664. 22:45to do the setup using Kraft instead of
  665. 22:47Zookeeper. Now let's start with the
  666. 22:49setup. I've opened terminal and first
  667. 22:52I'll go to the place where I have
  668. 22:54downloaded Kafka.
  669. 22:55Okay, so it's inside downloads, then
  670. 22:57this folder CD Kafka.
  671. 23:00Okay, so we are in this folder. Now here
  672. 23:02we have to run a few commands to do the
  673. 23:03setup.
  674. 23:04Okay, so these are the steps. I have
  675. 23:06written all the steps over here. Okay,
  676. 23:08so the first command is that we have to
  677. 23:10generate a cluster ID. Okay, so we are
  678. 23:12creating a Kafka cluster and we need to
  679. 23:14give a unique identifier to that
  680. 23:16cluster. For that, we create a cluster
  681. 23:18ID and save that cluster ID inside this
  682. 23:20variable.
  683. 23:22Okay, so this is what I'm going to do.
  684. 23:23I'll just copy this command
  685. 23:25and
  686. 23:26we are going to paste this in terminal
  687. 23:28over here. Okay, this is a one-time
  688. 23:30step. I'll press enter
  689. 23:33and a cluster ID has been created, a
  690. 23:34random UUID, and it has been saved
  691. 23:36inside this variable.
  692. 23:38Okay, now I'll just do the second step.
  693. 23:42It says format Kafka storage. All right.
  694. 23:45So as I told you earlier, now we are not
  695. 23:47using Zookeeper, now we are using Kraft.
  696. 23:50Okay, so we need a dedicated space to
  697. 23:52save our cluster metadata inside Kafka
  698. 23:54only. Okay, inside Kraft. So for that,
  699. 23:57we need to do this format Kafka storage.
  700. 23:59Basically, by formatting, what I mean is
  701. 24:01that we initialize the directories, we
  702. 24:03prepare some directories where we are
  703. 24:05going to save the metadata and we assign
  704. 24:07this storage to the cluster ID. Okay, so
  705. 24:10the cluster which we have created will
  706. 24:12assign a dedicated storage for that.
  707. 24:14Okay, so I'll just copy this command
  708. 24:18and I'll paste it over here. Okay, so
  709. 24:20you can see that what we do is inside
  710. 24:21this bin, inside this bin, we have the
  711. 24:23script Kafka storage.sh. We run this
  712. 24:26script and we are associating it with
  713. 24:28this cluster ID, which is present in
  714. 24:31this variable. Okay, Kafka cluster ID,
  715. 24:33which we already saved over here.
  716. 24:35Okay, so that's what we are actually
  717. 24:36doing, that we are running this script
  718. 24:38and we are
  719. 24:40we are basically this script initializes
  720. 24:41a storage and we are associating this
  721. 24:43storage with this cluster ID, which is
  722. 24:46present in this Kafka cluster ID.
  723. 24:48Okay, and the properties which we are
  724. 24:50considering for all our commands that
  725. 24:52will be this config server dot
  726. 24:53properties. Okay, the file which I
  727. 24:56showed you earlier. Okay, this is this
  728. 24:58file server dot properties was present
  729. 24:59in the config folder.
  730. 25:01Okay, so I'll press enter.
  731. 25:04And great, we have defined a directory
  732. 25:06where we are going to save all our
  733. 25:08cluster metadata and all the storage has
  734. 25:10been initialized. Okay, so both these
  735. 25:12steps were a one-time step. We don't
  736. 25:13have to do it again. Now we have a
  737. 25:15cluster ID and we have a storage. Now we
  738. 25:18are going to start Kafka. Okay, so
  739. 25:19earlier first we needed to start
  740. 25:21ZooKeeper as a different service, then
  741. 25:23only we could start Kafka. But now it's
  742. 25:25not necessary because we have we are
  743. 25:27using KRaft.
  744. 25:28Okay, so the third command will be to
  745. 25:30start the Kafka server. For this, I'll
  746. 25:32use this command.
  747. 25:33Okay, so it's very simple. Inside this
  748. 25:36bin, we have this script Kafka server
  749. 25:38start dot sh and as I told you, the
  750. 25:41server dot properties we are considering
  751. 25:43from this file. Okay, the configuration
  752. 25:45we are
  753. 25:46we are considering these configurations.
  754. 25:48Okay, so I'll just click enter.
  755. 25:52And great, our Kafka server has started.
  756. 25:54Okay, and if you'll search for it,
  757. 25:56you'll see that it's actually started on
  758. 25:58port 9092.
  759. 26:00All right. So let me start by creating a
  760. 26:02topic first. So for that, I'll be using
  761. 26:05this command. Okay, I'll just copy it
  762. 26:07and I'll paste it here in the terminal.
  763. 26:10Okay, so what we are doing here is first
  764. 26:12inside this bin folder, we have this
  765. 26:14script Kafka topics dot sh. Okay, we are
  766. 26:17running this script and I'm saying that
  767. 26:19we have to create a topic and here we
  768. 26:21provide the topic name. Okay, so I am
  769. 26:23creating a topic named order slash
  770. 26:25events. Okay, so this means that
  771. 26:27anything related to order can be
  772. 26:29published on this topic. Okay, that's
  773. 26:31what I'm creating this topic for. Okay,
  774. 26:33so whenever any order will be placed, a
  775. 26:35producer will produce a message on this
  776. 26:37topic.
  777. 26:38Okay. Now here I'm specifying the
  778. 26:40partitions for this topic. So I'm
  779. 26:42keeping it as three. Now why three?
  780. 26:44Because a single partition won't be able
  781. 26:46to handle much traffic. Okay, in future
  782. 26:48if traffic increases, so I want multiple
  783. 26:51partitions for that. So here I'm keeping
  784. 26:53it as three.
  785. 26:54Okay, and we are running this topic on
  786. 26:55localhost 9092.
  787. 26:57Okay, so I'll just press enter
  788. 26:59and our topic has been created.
  789. 27:01Okay, so I can just verify this. For
  790. 27:03that I have another command that
  791. 27:04verifies the topic. I'll just copy this
  792. 27:07command.
  793. 27:08Now again, what we are doing here is
  794. 27:10that
  795. 27:11we are running this script inside this
  796. 27:13bin folder only, kafka topics.sh and I
  797. 27:16have given this command slash list,
  798. 27:18okay, hyphen hyphen list. Okay, so this
  799. 27:20will list all the topics
  800. 27:22and as you can see as of now there are
  801. 27:23two topics. One is the order events
  802. 27:25which we created and there is a default
  803. 27:27topic that Kafka creates itself, which
  804. 27:29is this consumer underscore offsets.
  805. 27:32Okay, so here Kafka stores all the
  806. 27:33offsets. Okay, so this is created by
  807. 27:35Kafka and this one is created by us.
  808. 27:38Okay. Now one more thing which we can do
  809. 27:40is that we can inspect the number of
  810. 27:41partitions. So if you want to inspect a
  811. 27:44topic in a more detailed way, then we
  812. 27:46can just use this command.
  813. 27:48All right. So I'll again copy and paste
  814. 27:50it. Again, we are running the same
  815. 27:51script and I have given the command
  816. 27:53describe. This describes the topic order
  817. 27:56events. Okay, so I'll just press enter
  818. 27:59and here you can see that we have all
  819. 28:01the details of this topic.
  820. 28:03Okay, so like in this topic we have
  821. 28:04created three partitions. So the first
  822. 28:06partition is partition zero, then
  823. 28:08partition one, then partition two.
  824. 28:10Okay, and there are information about
  825. 28:12leader and replicas as well. Okay, so
  826. 28:14this is this comes into play when we
  827. 28:16have multiple Kafka servers. Okay, so
  828. 28:18right now we have only one server, that
  829. 28:19's why we'll just not look into these.
  830. 28:22Okay, so but now we know that there are
  831. 28:23three partitions and their names are
  832. 28:25partition zero, one and two.
  833. 28:27Okay, so let me just clear all this.
  834. 28:30And now let's start a producer.
  835. 28:32Okay.
  836. 28:33So for that I'll another command. I'll
  837. 28:35just copy this
  838. 28:38and let's paste it over here. Okay. So
  839. 28:40for that we have the script kafka
  840. 28:42console producer.sh. We'll run this
  841. 28:44script and here we have to mention the
  842. 28:47topic
  843. 28:48on which it will produce. Okay. So I'm
  844. 28:50saying that we are going to produce on
  845. 28:51this order events. Okay. So that's why
  846. 28:53here we have to mention that and we are
  847. 28:56running it on localhost 9092 and I have
  848. 28:58added another property that just gives
  849. 29:01some name to this producer. Okay. We are
  850. 29:02just giving some ID. So I'm calling it
  851. 29:05as order service. Okay. So whenever an
  852. 29:07order comes to this service, it will
  853. 29:09just produce an event to this topic
  854. 29:11order events. Okay. That's what we are
  855. 29:13trying to
  856. 29:14you know reproduce.
  857. 29:16Okay.
  858. 29:17So I'll just press enter and if you see
  859. 29:19this sign, it means now you can enter
  860. 29:21the message over here. Okay. Let's say I
  861. 29:23can say hello or not hello. Actually I
  862. 29:25can say that order placed
  863. 29:28with ID
  864. 29:29101. Okay. I can write some message
  865. 29:32something like this. Okay. But right now
  866. 29:33I'll not press enter. I'll not produce
  867. 29:35this message. First I want some
  868. 29:37consumers that are actually going to
  869. 29:39consume these messages.
  870. 29:41Okay. So consider it like this that we
  871. 29:43have an order service that produces the
  872. 29:44message on this topic order events. Now
  873. 29:47there could be some service which needs
  874. 29:49information about the order. Okay. Like
  875. 29:51there could be a payment service which
  876. 29:53wants to know that they have to make an
  877. 29:55payment for this order. Okay. So they
  878. 29:57will consume the message. So let me
  879. 29:59create a consumer group for that. I'll
  880. 30:00just open another tab and here we can
  881. 30:03start creating a consumer group.
  882. 30:05Okay. So we'll name this group as
  883. 30:07payment group and I'll be using this
  884. 30:09command for that.
  885. 30:11Okay.
  886. 30:13All right.
  887. 30:14So again let me explain inside the bin
  888. 30:17folder we have the script kafka console
  889. 30:19consumer.sh. We run this script. Here
  890. 30:22also we have to specify the topic from
  891. 30:24which it's going to consume the
  892. 30:25messages. Okay. So here we'll specify
  893. 30:27that it's going to read from order
  894. 30:29hyphen events
  895. 30:31and we are assigning a group to this
  896. 30:32consumer. So the group's name is payment
  897. 30:34group
  898. 30:35and again we are running on bootstrap
  899. 30:37server 9092.
  900. 30:38Then I've given a client ID to this. Now
  901. 30:40this is the first consumer which I'm
  902. 30:42creating inside this group. Okay, so in
  903. 30:45total we are going to create three
  904. 30:46consumers. You can see I have written
  905. 30:47three commands over here. Payment
  906. 30:49consumer two, payment consumer three and
  907. 30:51here is payment consumer one. Okay, so
  908. 30:53now again why are we creating three
  909. 30:54consumers? Because as I told you, when
  910. 30:56the traffic increases, a single
  911. 30:58partition won't be able to handle all
  912. 31:00the traffic. Similarly, a single
  913. 31:01consumer will also not be able to handle
  914. 31:03this much traffic.
  915. 31:05Okay, so we define three consumers and
  916. 31:06each consumer will read the event from
  917. 31:08one one topic, from one partition.
  918. 31:12Okay, so that's what I'm going to show
  919. 31:13you practically over here. Okay, so
  920. 31:15right now let's create one consumer and
  921. 31:16let's see what happens. Okay, so here I
  922. 31:18have added one more property over here
  923. 31:20that says print partition is equal to
  924. 31:22true. Okay, so when the messages will be
  925. 31:24consumed using this property, we'll be
  926. 31:27able to know that the messages are
  927. 31:28coming from which partition. Okay, it
  928. 31:30will print that. Okay, so I'll show you
  929. 31:32this. Let's just start this consumer
  930. 31:33first.
  931. 31:35Okay.
  932. 31:36All right. So the consumer has been
  933. 31:38started.
  934. 31:39Okay. So let me just produce a message.
  935. 31:42Let's say
  936. 31:44order
  937. 31:45placed
  938. 31:46with ID
  939. 31:48101. Okay, something like this.
  940. 31:51All right.
  941. 31:52So you will see that this order has been
  942. 31:54placed and this message has been
  943. 31:56consumed in this consumer
  944. 31:58payment consumer one.
  945. 32:00Okay, and the partition on which the
  946. 32:02order was sent was partition one.
  947. 32:04Okay, so Kafka randomly decides that I
  948. 32:07can place another order. Maybe order
  949. 32:09placed with ID 102.
  950. 32:13And this time also it went to partition
  951. 32:15one. Again, let me place another order.
  952. 32:17Order
  953. 32:19order placed with
  954. 32:22ID maybe 103 this time.
  955. 32:25And again it went to partition one. Now
  956. 32:26why is it going to the same partition?
  957. 32:28Why is it not going to partition zero or
  958. 32:30partition two? See, right now there is
  959. 32:32not much traffic. Okay, we are I'm just
  960. 32:34writing these orders one by one and
  961. 32:37Kafka is able to perform all this with a
  962. 32:38single partition only. Okay, so if I
  963. 32:41have to equally distributed, then for
  964. 32:43that I have to actually run Kafka in a
  965. 32:45different manner. I have to run producer
  966. 32:47in a different manner and I will use
  967. 32:49something called keys. Okay, so these
  968. 32:51are the keys. Let me show you this.
  969. 32:54Okay, so I'll just stop this producer.
  970. 32:57Okay, so how do we stop it? We use
  971. 32:59control C if we are on Mac. Okay,
  972. 33:01control C and the producer has been
  973. 33:03stopped. Okay, now I'll again, I'll
  974. 33:06again
  975. 33:07add this producer. I'll start this
  976. 33:09producer. This time I have added a
  977. 33:11property that says reader property parse
  978. 33:13key is equal to true. Okay, so what is a
  979. 33:16key? See, when we send a message to a
  980. 33:18topic, we can also send a key along with
  981. 33:20that. Now what what Kafka does is it
  982. 33:23hashes this key and based on that hash,
  983. 33:26it decides on which partition the
  984. 33:27message should go. Okay, so this helps
  985. 33:29us divide the messages in a in a more
  986. 33:32equal manner. Okay, it's not necessary
  987. 33:34that it will go one by one to each
  988. 33:35partition, but still it will be
  989. 33:37distributed in a much better fashion.
  990. 33:39Okay, so I'll parse a key as well and
  991. 33:41here I'm specifying a key separator
  992. 33:44where I'm saying that this is equal to
  993. 33:45colon. Okay, so what does this mean?
  994. 33:47That when I'll write the message over
  995. 33:49here, like I'll just press enter and
  996. 33:51you'll get to know. Like here I'll write
  997. 33:53a message. So first I'll say let's say I
  998. 33:54have I'll keep my order ID as the key.
  999. 33:57Okay, so I've written 101. Then I'll add
  1000. 33:59a colon. So whatever I'm writing before
  1001. 34:02this colon, that will be considered as
  1002. 34:04the key and whatever I'm writing over
  1003. 34:06here, like here now I can say
  1004. 34:08order placed with ID 101, something like
  1005. 34:12this. Okay, so this will be considered
  1006. 34:14as the message.
  1007. 34:16All right, so I'll not
  1008. 34:17I'll not press enter as of now. Let me
  1009. 34:20start three consumers and now we'll see
  1010. 34:22what happens. Okay, so let me just
  1011. 34:24remove this consumer as well. I'll again
  1012. 34:26print enter control C. So I have stopped
  1013. 34:29the consumer.
  1014. 34:31Let me clear this. Now one by one I'll
  1015. 34:33start three consumers this time.
  1016. 34:35Okay, so this is the consumer group one.
  1017. 34:39Let me just paste this.
  1018. 34:41Okay, this is consumer one.
  1019. 34:45Now in another tab in in this tab I'll
  1020. 34:48start a second consumer.
  1021. 34:50Okay, so let me just copy and paste
  1022. 34:53this. So here as well, it's the same
  1023. 34:55payment group. Inside this group, I'm
  1024. 34:57starting a second consumer, payment
  1025. 34:58consumer two.
  1026. 35:00Okay, I'll press enter.
  1027. 35:02Then another tab in this tab I'll open
  1028. 35:05consumer three.
  1029. 35:07Okay.
  1030. 35:09So this is the consumer three now.
  1031. 35:11All right, so we have we are creating
  1032. 35:13three consumers all inside a single
  1033. 35:15consumer group as of now and the
  1034. 35:16consumer group's name is payment group.
  1035. 35:19Okay, so let's just verify all this.
  1036. 35:20I'll I'll open one more tab and let's
  1037. 35:22verify all this over here. Okay, so here
  1038. 35:24we can list all the consumers in a
  1039. 35:26particular group. So I'll be using this
  1040. 35:28command for that.
  1041. 35:29Let's me just copy it and paste it.
  1042. 35:31Okay, so it says consumer groups.sh. We
  1043. 35:33are running this script and I'm just
  1044. 35:35describing the consumer group. And here
  1045. 35:37I specified the group name which is the
  1046. 35:39payment group. Okay, so I'll just press
  1047. 35:41enter.
  1048. 35:43All right.
  1049. 35:44So what what we can see over here is
  1050. 35:46that we have three consumers, consumer
  1051. 35:49one two one two and three.
  1052. 35:52Okay, so
  1053. 35:53that is what I have done. I have created
  1054. 35:54three consumers inside this payment
  1055. 35:56group.
  1056. 35:57Okay, and I can also verify one more
  1057. 35:58thing
  1058. 35:59that which consumer is going to read
  1059. 36:00from which partition. This also we can
  1060. 36:02check. Okay, so for that I'll be using
  1061. 36:04this command. Again, we are running the
  1062. 36:06script Kafka consumer groups.sh. I'm
  1063. 36:09saying describe. I have specified the
  1064. 36:11payment the group name and I have just
  1065. 36:13added this verbose as well. Okay, so now
  1066. 36:15when I press enter
  1067. 36:18great. So we have a lot of information
  1068. 36:19over here. Let me show you what it
  1069. 36:22exactly says. So it says that consumer
  1070. 36:25two will read from one partition.
  1071. 36:26Consumer one will also read from one
  1072. 36:28partition and consumer three will also
  1073. 36:29read from one one partition. So each
  1074. 36:31consumer will read from one partition.
  1075. 36:33Here is the number of partitions which
  1076. 36:35are specified. And here we have
  1077. 36:37specified from which partition it will
  1078. 36:39read. Like this is order events one.
  1079. 36:42Okay, so this this means that this
  1080. 36:44consumer two, this consumer two is
  1081. 36:46reading from partition one. And this
  1082. 36:48consumer one is going to read from
  1083. 36:49partition zero and this consumer three
  1084. 36:52is going to read from partition two.
  1085. 36:54Okay, so this Kafka decides
  1086. 36:55automatically. Earlier all this was
  1087. 36:57managed by Zookeeper, but now Kafka
  1088. 36:59decides it on its own.
  1089. 37:01Okay. So
  1090. 37:03we can remember this and let I'll just
  1091. 37:05clear this out for now.
  1092. 37:07Or let's keep it like this. Okay. So
  1093. 37:08here we have consumer three.
  1094. 37:11Here we have consumer two and here we
  1095. 37:13have consumer one. Okay, right now I
  1096. 37:14have not produced any message. So this
  1097. 37:16is my first message. What what Kafka is
  1098. 37:18going to do is that it will just use
  1099. 37:20this key 101 and using this key it will
  1100. 37:23create a hash and it will randomly send
  1101. 37:25it to any of the partition. Okay. And we
  1102. 37:27have already decided that which
  1103. 37:29partition from which partition which
  1104. 37:31consumer is going to read that has
  1105. 37:32already been decided.
  1106. 37:34Okay, so let's let's start producing
  1107. 37:36messages. Let's say I entered this.
  1108. 37:39Okay. So this went to consumer three.
  1109. 37:42Okay, you will see it went to consumer
  1110. 37:43three because it was on partition two.
  1111. 37:45And as you can see from partition two,
  1112. 37:48from this order events two, consumer
  1113. 37:50three is going to read. Okay, that's why
  1114. 37:52the message came to consumer three and
  1115. 37:54you will see this is consumer two.
  1116. 37:55Message is not here. This is consumer
  1117. 37:57one. Message is again not here. Okay, so
  1118. 37:59in a in one consumer group a single
  1119. 38:02message is read by one of the consumers
  1120. 38:05only.
  1121. 38:05Okay, this is how it it happens. Okay,
  1122. 38:07now let me change it. Let's say 105 this
  1123. 38:10time. Okay, so I say
  1124. 38:13order placed
  1125. 38:15with ID 105.
  1126. 38:18Okay. So by chance again it went to
  1127. 38:20partition two. That's why it's send over
  1128. 38:23here. This time let me change it a bit.
  1129. 38:25Let's say 110
  1130. 38:27order placed with ID 110.
  1131. 38:33Okay, so this time it went to partition
  1132. 38:34one. And partition one who is going to
  1133. 38:36read from partition one? This is
  1134. 38:38consumer two. Okay, let's verify it here
  1135. 38:40as well. From partition one consumer
  1136. 38:44two is going to read. Okay, this is the
  1137. 38:45consumer two.
  1138. 38:47Okay? So similarly, any message can go
  1139. 38:49to any of the partition and we have
  1140. 38:51already decided from which partition,
  1141. 38:52which particular consumer is going to
  1142. 38:54read the message. Okay, let's try one
  1143. 38:56two more as well. Let it be 109 maybe.
  1144. 39:00Okay?
  1145. 39:01Order placed with ID 109. Let's see
  1146. 39:05where it went. Okay, this time on the
  1147. 39:09partition zero and partition zero means
  1148. 39:11consumer one. Okay?
  1149. 39:13Partition zero here means consumer one.
  1150. 39:16All right. So this message which went
  1151. 39:18over here, this is not present in in
  1152. 39:20this as well as in in the in any of
  1153. 39:22other consumers, okay? So one message is
  1154. 39:25read by one of the consumers only in a
  1155. 39:26single consumer group.
  1156. 39:28All right.
  1157. 39:29Okay.
  1158. 39:30Now let me do one thing. Let's say I add
  1159. 39:33one more consumer.
  1160. 39:34Okay, so I'll just copy this command. We
  1161. 39:36have this command to create consumers.
  1162. 39:38Okay, I'll just copy this.
  1163. 39:40And let's say
  1164. 39:42I'll just enter a
  1165. 39:44I'll create another tab and here maybe I
  1166. 39:46create consumer four.
  1167. 39:49Okay?
  1168. 39:50So what's going to happen now? We have
  1169. 39:52four consumers but three partitions.
  1170. 39:54Okay? So let's just run this command
  1171. 39:57again
  1172. 39:58where we have allocated these partitions
  1173. 40:00to these consumers and let's see what
  1174. 40:01happens.
  1175. 40:02Okay? So I'm again describing the Kafka
  1176. 40:04consumer groups.sh and we have this
  1177. 40:07payment group and I'm using this verbose
  1178. 40:09command. Okay? So let see what's going
  1179. 40:11to happen.
  1180. 40:13Okay?
  1181. 40:13So you can see from from the partition
  1182. 40:16one, consumer two is going to read. From
  1183. 40:18partition zero, consumer one is going to
  1184. 40:19read. From partition two, consumer three
  1185. 40:22is going to read. But this consumer
  1186. 40:23four, this does not have any partition.
  1187. 40:26It is not assigned any partition. Okay,
  1188. 40:28so this is a very important interview
  1189. 40:30question. Interviewer asks you the
  1190. 40:32question in this way only that if you
  1191. 40:33have three partitions and four consumers
  1192. 40:35that what then what is going to happen
  1193. 40:37to the fourth consumer? So actually this
  1194. 40:39consumer is going to sit idle. There is
  1195. 40:41no point in creating this consumer
  1196. 40:43because it will not be assigned any
  1197. 40:45partition because one from one partition
  1198. 40:48on max to max one consumer can read.
  1199. 40:50Okay. So, there is no point in creating
  1200. 40:52the consumers greater than the number of
  1201. 40:54partitions.
  1202. 40:55Okay. Now, let me just delete this
  1203. 40:58consumer.
  1204. 41:00Okay. Or let me just stop this. This is
  1205. 41:02the consumer and I'll just stop this by
  1206. 41:04saying control C.
  1207. 41:06Okay. I'll just again run this command
  1208. 41:08and now you will see that again there
  1209. 41:09are only three consumers now.
  1210. 41:11Okay. So, this one is reading from
  1211. 41:13partition one. This is reading from
  1212. 41:14partition zero and this is reading from
  1213. 41:16partition two.
  1214. 41:17Okay. Now, what if what if I just let me
  1215. 41:20first close this tab. Yeah. So, this one
  1216. 41:22was for consumer three, right? Let's say
  1217. 41:25I just remove consumer three as well.
  1218. 41:27Okay. So, now we have two consumers but
  1219. 41:30three partitions. So, what is going to
  1220. 41:32happen now? Let's again run this
  1221. 41:33command. First let me clear all this.
  1222. 41:36Okay.
  1223. 41:37So, if I run it again.
  1224. 41:40All right. So, from the second partition
  1225. 41:43consumer two is going to read but from
  1226. 41:45zero and one consumer one is going to
  1227. 41:47read. Okay. So, in this case when when
  1228. 41:50the number of consumers are less than
  1229. 41:51the number of partitions then what
  1230. 41:53happens is that one consumer is going to
  1231. 41:55read from two partitions. Okay. So, in
  1232. 41:57this case like it has assigned it like
  1233. 41:59this that consumer one is going to read
  1234. 42:01both the messages from the consume from
  1235. 42:03partition zero as well as from partition
  1236. 42:04one and consumer two is going to read
  1237. 42:06only from partition two.
  1238. 42:08Okay. And if I'll remove one more then
  1239. 42:10that single consumer is going to read
  1240. 42:12from all the three partitions. Okay.
  1241. 42:13This is how it works. Okay. So, let me
  1242. 42:16just show it as well. So, here we had
  1243. 42:18the producer, right?
  1244. 42:20Let me create one more message. Let's
  1245. 42:21say it is
  1246. 42:23112
  1247. 42:25maybe. Okay. Order placed with ID 112.
  1248. 42:33Okay. So, it went over here. So, as you
  1249. 42:35can see earlier it was reading from
  1250. 42:36partition two partition one but now it's
  1251. 42:38reading from partition two as well.
  1252. 42:40Consumer two you can see over here that
  1253. 42:42consumer two is going to read from
  1254. 42:45partition two. Okay. Partition two has
  1255. 42:46been assigned to it.
  1256. 42:48And let say I I
  1257. 42:50create one more maybe 105 again or maybe
  1258. 42:53106 I'll use.
  1259. 42:56Order
  1260. 42:58placed
  1261. 43:00with ID 106.
  1262. 43:05All right, so it went over here.
  1263. 43:07So this is the partition zero and who is
  1264. 43:09going to read from partition zero?
  1265. 43:10Consumer one. Okay, who is going to read
  1266. 43:12from partition zero? This partition zero
  1267. 43:14consumer one is going to read from
  1268. 43:15partition. And you can see this number
  1269. 43:17also here it's two written. So what does
  1270. 43:19it mean? That this consumer this is
  1271. 43:20reading from one partition. This
  1272. 43:22consumer it is reading from two
  1273. 43:24partitions.
  1274. 43:25Okay, so this is how Kafka works. If the
  1275. 43:27number of consumers are less than the
  1276. 43:29number of partitions, then one consumer
  1277. 43:31can be assigned to multiple partitions
  1278. 43:33as well.
  1279. 43:34All right. Now so far we have created
  1280. 43:36one consumer group called the payment
  1281. 43:38group. Now the responsibility of payment
  1282. 43:40group is to process all the payments.
  1283. 43:42Okay, dedicatedly belongs to the payment
  1284. 43:44service. But let's say there are other
  1285. 43:46services as well. For example, there
  1286. 43:48might be an inventory service which also
  1287. 43:51wants to consume the same information.
  1288. 43:53Okay, because whenever an order is
  1289. 43:54placed, the inventory service also wants
  1290. 43:57to update their stock at their end.
  1291. 43:59Okay, so they will also consume the same
  1292. 44:01message. Similarly, there could be a
  1293. 44:02notification service as well. They also
  1294. 44:05want to consume the same messages
  1295. 44:07because they want to send some
  1296. 44:08notifications to the user whenever some
  1297. 44:10order is placed. Okay, so we can have
  1298. 44:13different groups for different types of
  1299. 44:15services. Okay, we already have a
  1300. 44:17payment group for the payment service.
  1301. 44:19Similarly, we can create another
  1302. 44:21consumer groups for different services.
  1303. 44:23So let me do that.
  1304. 44:25First, let me just
  1305. 44:27close all these
  1306. 44:29consumers that I've created so far.
  1307. 44:31Okay, we'll do it again. So this was the
  1308. 44:32producer. Okay, I'll just say control C
  1309. 44:36and let me clear this out
  1310. 44:38and let me just close all these tabs.
  1311. 44:40Okay, by closing them, I can simply stop
  1312. 44:43them. You could either press control C
  1313. 44:46or you could just close the tabs. It
  1314. 44:47works the same.
  1315. 44:49Okay, so here Kafka is running and here
  1316. 44:51we don't have anything as of now. Okay,
  1317. 44:53so I'll just start again. I'll first
  1318. 44:55create a producer.
  1319. 44:57Okay. So
  1320. 44:59this was the producer.
  1321. 45:03All right. I'll I'll just start the
  1322. 45:04producer again. Okay. I've created it
  1323. 45:05earlier as well, but now I'm starting it
  1324. 45:07again. Okay. So the producer has been
  1325. 45:09started. I'll open another tab. Here
  1326. 45:12I'll first create consumer group one.
  1327. 45:14Okay. I'll start it again.
  1328. 45:16So here we have all the commands.
  1329. 45:20Okay.
  1330. 45:22So this one is the payment group and
  1331. 45:24this is the payment consumer one. Okay.
  1332. 45:27I've started it. Then in the next tab,
  1333. 45:30I'll just say consumer two.
  1334. 45:33Okay.
  1335. 45:34And in the third tab, I'll say
  1336. 45:37consumer three.
  1337. 45:39Okay. So we have three consumers in the
  1338. 45:41consumer group, payment group.
  1339. 45:44Okay. Then in the next tab, I'll just
  1340. 45:46paste this only. This time I'll change
  1341. 45:48it to
  1342. 45:49inventory consumer. Okay. Inventory
  1343. 45:52consumer one and I'll even change the
  1344. 45:54group.
  1345. 45:55Okay. So this will be
  1346. 45:58inventory group.
  1347. 46:00Okay. And let me just copy this for
  1348. 46:01future.
  1349. 46:03All right.
  1350. 46:05Okay.
  1351. 46:05So again, we have this inventory group
  1352. 46:07and this will be inventory consumer two.
  1353. 46:10Then in the next tab, I'll just again
  1354. 46:12paste this and this will be inventory
  1355. 46:14consumer three.
  1356. 46:16Okay. So in the inventory group as well,
  1357. 46:18I have created three different
  1358. 46:20consumers.
  1359. 46:21All right. Now in the next tab, I'll
  1360. 46:23just change it to
  1361. 46:26notification consumer one.
  1362. 46:29Okay. And the group name also, this
  1363. 46:31should be
  1364. 46:34notification
  1365. 46:35group.
  1366. 46:36Okay. Let me just copy this.
  1367. 46:39All right.
  1368. 46:41Okay. So this is notification consumer
  1369. 46:42one.
  1370. 46:43Similarly, notification consumer two
  1371. 46:48and
  1372. 46:50notification consumer
  1373. 46:53three.
  1374. 46:54All right. So I've opened a lot of tabs
  1375. 46:56and I've created in total nine
  1376. 46:58consumers, three of each, three
  1377. 47:00belonging to one group, three belonging
  1378. 47:02to second group and the next three
  1379. 47:03belonging to the third group. Okay, so
  1380. 47:05let's just verify first that what all
  1381. 47:07have been created. Okay, so we have a
  1382. 47:10command that lists all the consumer
  1383. 47:12groups as well. Okay, so earlier we
  1384. 47:14listed all the consumers inside a single
  1385. 47:16group, but we have this command that
  1386. 47:18lists all the consumer groups as well.
  1387. 47:20Because I'm going to use that. First,
  1388. 47:22let's see which are our consumer groups.
  1389. 47:25Okay, so we have three different groups,
  1390. 47:27payment, notification and inventory.
  1391. 47:29Okay.
  1392. 47:30Let me just clear this.
  1393. 47:32Now let's see
  1394. 47:34what each group has, okay, and which
  1395. 47:36partition they are reading from.
  1396. 47:38All right. So these were the this was
  1397. 47:41the command to check the partitions.
  1398. 47:43All right. So first we are checking the
  1399. 47:45payment group.
  1400. 47:47Okay.
  1401. 47:48So these are the details for payment
  1402. 47:49group. These are different partitions
  1403. 47:51and these are the different consumers.
  1404. 47:54Now similarly I can check for
  1405. 47:58this will be inventory group.
  1406. 48:01Inventory group.
  1407. 48:03Okay, so again we have different
  1408. 48:05partitions and different consumers are
  1409. 48:06reading from these.
  1410. 48:08And I'll also paste
  1411. 48:10the command again and this time we'll
  1412. 48:12check for notification group.
  1413. 48:14Okay, notification group.
  1414. 48:18All right. Okay, so here as well these
  1415. 48:20partitions are assigned to different
  1416. 48:22consumers inside the notification
  1417. 48:24consumer group.
  1418. 48:26Okay, so I'll just keep this page open
  1419. 48:27so we can refer to these.
  1420. 48:29And I'll go back to the producer. So it
  1421. 48:32was somewhere here in the beginning.
  1422. 48:35Yeah, okay. So here we have the
  1423. 48:37producer.
  1424. 48:38All right. So this time I'll just say
  1425. 48:40101 maybe
  1426. 48:42order
  1427. 48:44placed
  1428. 48:45for [snorts] ID 101.
  1429. 48:48Okay.
  1430. 48:49So when I produce this message,
  1431. 48:51this key will be used, a hash value will
  1432. 48:53be created and it will be sent to some
  1433. 48:54partition.
  1434. 48:56Okay. So
  1435. 48:58this message has not been read here,
  1436. 49:01it's not consumed over here, it has not
  1437. 49:02been consumed over here as well. It has
  1438. 49:05been consumed over here in the payment
  1439. 49:07group in payment consumer three. Okay,
  1440. 49:09so why? Because it came to partition
  1441. 49:12two. Okay, so remember that the message
  1442. 49:14came to partition two and this payment
  1443. 49:17consumer two was reading from partition
  1444. 49:19two. Sorry, payment consumer three was
  1445. 49:21reading from partition two. That's why
  1446. 49:23we got the message over here.
  1447. 49:25Okay, now let's see the inventory group.
  1448. 49:27So in the consumer one there is no
  1449. 49:29message. Then we have
  1450. 49:31the consumer two again no message. In
  1451. 49:33the consumer three there is order placed
  1452. 49:35for ID 101. Okay, so you can see that
  1453. 49:38the same message is read by the payment
  1454. 49:40group as well as the inventory group.
  1455. 49:42Okay, and in fact, we'll just check the
  1456. 49:44notification group as well. Okay, so not
  1457. 49:46in the consumer one
  1458. 49:48and not in two. It is again in three.
  1459. 49:51Okay, so it's it's been read by the
  1460. 49:53notification consumer as well. So what
  1461. 49:56do we learn from this? That inside a
  1462. 49:58single group only one of the consumer
  1463. 50:01reads that message. But when we have
  1464. 50:03different consumer groups, all of them
  1465. 50:05read the same message. Okay, that's
  1466. 50:07their purpose. So you design them like
  1467. 50:09that that inside a single group if we
  1468. 50:11have multiple consumers that we have
  1469. 50:13created to handle high traffic. Okay,
  1470. 50:15because we can achieve parallelism. They
  1471. 50:17can
  1472. 50:18they can read these messages parallelly
  1473. 50:20different consumers when there will be a
  1474. 50:21lot of traffic.
  1475. 50:22Okay, so they can the the request get
  1476. 50:24distributed among the partitions and
  1477. 50:26these consumers can read from those
  1478. 50:28partitions simultaneously. But we create
  1479. 50:31different consumer groups
  1480. 50:33so that different services can perform
  1481. 50:35their own duty. Okay, so you should it
  1482. 50:37should always be clear to you that what
  1483. 50:39is the exact difference between a
  1484. 50:40consumer and a consumer group. Okay,
  1485. 50:42consumer groups are created for
  1486. 50:44different services to perform their
  1487. 50:46duties and consumers are added in a
  1488. 50:48consumer group to increase the
  1489. 50:50scalability and to increase the
  1490. 50:53parallelism.
  1491. 50:54Okay, so I hope this is clear. Now let's
  1492. 50:57verify once that which which topic was
  1493. 50:59reading from which partition. So
  1494. 51:01in the payment group, this the payment
  1495. 51:03group. The message came to partition
  1496. 51:05two, right? So, this was the partition
  1497. 51:07two and as you can see, consumer three
  1498. 51:09was reading from here. Similarly, in in
  1499. 51:12this as well, in inventory group as
  1500. 51:13well, if we see,
  1501. 51:15okay, this partition two was read by
  1502. 51:17consumer three and that's what happened
  1503. 51:18over there. And similarly here as well,
  1504. 51:21this partition two was read by consumer
  1505. 51:24three in notification group as well.
  1506. 51:27Okay, so this is how it works. Let me
  1507. 51:28just produce one more message. Let's
  1508. 51:30just verify it again. Okay, maybe 110
  1509. 51:34order placed for ID 110.
  1510. 51:40Okay, so this time it's partition one.
  1511. 51:42The message went to partition one and
  1512. 51:44it's being read by consumer two in the
  1513. 51:46payment group. Okay, payment consumer
  1514. 51:47two. Similarly here, it has it's being
  1515. 51:50read it's being read by inventory
  1516. 51:52consumer two. And here as well, I guess,
  1517. 51:57yeah, in the notification consumer two.
  1518. 52:00Okay. So this is how it works.
  1519. 52:03Now so far we have seen how different
  1520. 52:04consumers and consumer groups work. Now
  1521. 52:07let me show you a few more commands that
  1522. 52:09comes in handy when we are working with
  1523. 52:10Kafka. Okay, so we'll learn how to
  1524. 52:12delete a consumer group.
  1525. 52:14Okay, so we have this command for that.
  1526. 52:17All right. So
  1527. 52:18what I'm going to do that first before
  1528. 52:21running this command, I need to close
  1529. 52:22these consumers. Okay, so one thing
  1530. 52:24which you should understand is that
  1531. 52:26closing a consumer and deleting a
  1532. 52:28consumer group are two different things.
  1533. 52:30Okay, so like this consumer is open. So
  1534. 52:32it means that it it is able to read the
  1535. 52:34messages. So if I'll just do control C,
  1536. 52:37it means that now this consumer has
  1537. 52:38stopped. Okay. So similarly, I'll just
  1538. 52:41stop all of them. Okay, I can also close
  1539. 52:43the terminal as well. That also works.
  1540. 52:46Okay, so I'll terminate these processes
  1541. 52:47one by one.
  1542. 52:49All right.
  1543. 52:51Okay.
  1544. 52:53So we created nine consumers and I'll
  1545. 52:55just delete all of them. I'll just stop
  1546. 52:58all of them one by one.
  1547. 53:00All right. Okay, let me clear this as
  1548. 53:02well. Okay, so So have a producer over
  1549. 53:04here and we don' have any consumer up
  1550. 53:06and running right now, but you'll see
  1551. 53:08that if I just list all the consumers.
  1552. 53:11Okay. I'll just list them. You will see
  1553. 53:14that we still have these consumer
  1554. 53:16groups. Okay. Because I've not deleted
  1555. 53:18them. I have only stopped the consumers.
  1556. 53:21Okay. So now I'm going to delete these
  1557. 53:22consumer groups. For that we can just
  1558. 53:25use this command. We run this script,
  1559. 53:28this Kafka consumer groups.sh and I just
  1560. 53:31write delete then the group name over
  1561. 53:33here and it will just simply delete the
  1562. 53:36payment group.
  1563. 53:37Okay. Now if I list them again.
  1564. 53:41Okay. Now I only have two of them.
  1565. 53:44Okay. So similarly I can also delete
  1566. 53:47uh
  1567. 53:48the notification as well as the
  1568. 53:49inventory group. Let me show it to you.
  1569. 53:52This is the inventory group. I'll delete
  1570. 53:55this as well
  1571. 53:56and I'll also delete the
  1572. 53:59notification group.
  1573. 54:02All right.
  1574. 54:04Great. Okay. So now if I just list all
  1575. 54:08these consumers, you will see none of
  1576. 54:10them.
  1577. 54:12Okay. There are no consumers. Now let's
  1578. 54:15do one more thing. We have already
  1579. 54:17created this topic named order events.
  1580. 54:19Let's create another topic and maybe
  1581. 54:22call it user events.
  1582. 54:24Okay. So we had this order events to
  1583. 54:27process all the order related events.
  1584. 54:29Similarly we can have another topic user
  1585. 54:31events that processes all the user
  1586. 54:33related events. Okay. Like for example,
  1587. 54:35if a user registers on your platform, so
  1588. 54:37the user service can produce a message
  1589. 54:39on this topic.
  1590. 54:40And let's keep the partitions as two.
  1591. 54:43Okay.
  1592. 54:44So I've created this topic. Similarly
  1593. 54:46I'll create a producer for it. So we'll
  1594. 54:48create a user service.
  1595. 54:50Okay. Let me just copy this
  1596. 54:52and let's call this
  1597. 54:55a user service and it will not read from
  1598. 54:58order events. It will read from user
  1599. 55:00events.
  1600. 55:01Okay.
  1601. 55:03So here I can produce some messages.
  1602. 55:05Maybe I say that user registered
  1603. 55:08with
  1604. 55:09ID 101 something like this.
  1605. 55:12And again I can produce a message
  1606. 55:16user registered with ID 102 and let me
  1607. 55:21produce one more message user registered
  1608. 55:25with
  1609. 55:26ID
  1610. 55:28103.
  1611. 55:29Okay. Now you can see over here that I
  1612. 55:32have not started any consumer yet. Okay.
  1613. 55:35I have just simply started producing the
  1614. 55:37messages. We have a topic and a
  1615. 55:38producer, but we don't have a consumer
  1616. 55:40as of now. Okay. So now let's create a
  1617. 55:42consumer.
  1618. 55:44Okay. So I'll just copy this command
  1619. 55:48and I'll paste it over here. Okay. So
  1620. 55:49this time maybe it could be a user
  1621. 55:52analytics group. Okay. So there is a
  1622. 55:54service user analytics service which
  1623. 55:56does the analysis whenever a user
  1624. 55:58registers. Okay. So it will consume the
  1625. 56:00messages whenever a user is registering
  1626. 56:02on the platform.
  1627. 56:03Okay.
  1628. 56:04So it's a user analytic group and it
  1629. 56:06will read from this topic user events
  1630. 56:10and
  1631. 56:11maybe the consumer name is
  1632. 56:14UA consumer one.
  1633. 56:16Okay. So I'll just press enter.
  1634. 56:19Now will this consumer read all these
  1635. 56:21messages that we have already produced?
  1636. 56:23Let's see.
  1637. 56:25So far it hasn't and actually it will
  1638. 56:27not. Okay. It will only start reading
  1639. 56:29from the message which we produce now.
  1640. 56:31Okay. Like if I produce user registered
  1641. 56:34with ID 104
  1642. 56:36then it might read this. Okay. It is
  1643. 56:39reading only this, but not the messages
  1644. 56:41which were produced earlier before we
  1645. 56:42started the consumer.
  1646. 56:44Okay. Now let me let me do one thing.
  1647. 56:47Maybe I want maybe I want my consumer to
  1648. 56:49read these messages as well. Okay. Maybe
  1649. 56:51I wanted my consume my consumer to be
  1650. 56:53configured in such a way that it could
  1651. 56:54read those messages as well. Okay. So
  1652. 56:57I'll just create the consumer again. Let
  1653. 56:59me just stop this and I'll delete this
  1654. 57:01group altogether.
  1655. 57:02Okay.
  1656. 57:04And we'll create it again.
  1657. 57:05All right. So
  1658. 57:07I'll use this command to delete.
  1659. 57:09All right. this is a user analytics
  1660. 57:12group.
  1661. 57:14So I'll just write user
  1662. 57:16analytics group
  1663. 57:18and we'll delete this.
  1664. 57:20Okay.
  1665. 57:22So again, we don't have any consumer.
  1666. 57:24We have this user service, this producer
  1667. 57:26which has produced these four messages.
  1668. 57:28And now I'll create this consumer again,
  1669. 57:30but this time I'll configure it in such
  1670. 57:33a way that it will read the messages
  1671. 57:34from the beginning. Okay, so for that we
  1672. 57:36use this from beginning command. Okay,
  1673. 57:39so this is what I actually wanted to
  1674. 57:40show you. I'll just copy this
  1675. 57:42and I'll paste it over here. Okay, so
  1676. 57:43let's call it a UA consumer one
  1677. 57:46and let's call it
  1678. 57:50user
  1679. 57:51analytics
  1680. 57:53group
  1681. 57:54and let's make it read from user events.
  1682. 57:58Okay, so this time we have added this
  1683. 58:00property from beginning. This allows
  1684. 58:02this consumer group to read the messages
  1685. 58:04from the beginning, the ones which have
  1686. 58:06been created from the starting even
  1687. 58:08before this this consumer was
  1688. 58:11up and running.
  1689. 58:12Okay, so I'll press enter
  1690. 58:14and as of now it has not read, but yes,
  1691. 58:17now it reads all the messages.
  1692. 58:19Okay, so this is a very important
  1693. 58:22command which you should know because
  1694. 58:23it's used quite a lot. And in fact,
  1695. 58:26whenever you are creating any new
  1696. 58:27consumer, you need to make sure that
  1697. 58:29what exactly is your requirement,
  1698. 58:30whether you want it to read the messages
  1699. 58:32from the beginning or do you want it to
  1700. 58:34read only the new messages?
  1701. 58:36Okay.
  1702. 58:37Now let me show you one more command.
  1703. 58:38This is to delete a Kafka topic. So I'll
  1704. 58:41just copy this command and I can just
  1705. 58:43paste it over here. So again, we are
  1706. 58:44running this script
  1707. 58:46Kafka topics.sh and I think I forgot to
  1708. 58:49add the bin folder.
  1709. 58:50Just a second.
  1710. 58:52I'll add the location bin slash Kafka
  1711. 58:55topics.sh. Okay, we run the script. Then
  1712. 58:58we say this command delete and here we
  1713. 59:00write the topic name.
  1714. 59:01Okay, so the topic name will be let me
  1715. 59:04just add it over here.
  1716. 59:09Okay, so let's say we want to delete the
  1717. 59:11user events topic. So I can delete users
  1718. 59:14hyphen events.
  1719. 59:17Okay.
  1720. 59:18And maybe I can list the topics. All
  1721. 59:20right. We already saw a command for
  1722. 59:22that. Yeah. Verify the topics.
  1723. 59:26Okay.
  1724. 59:27So we can see that
  1725. 59:29we only have order events and okay, we
  1726. 59:30still have user events.
  1727. 59:33All right.
  1728. 59:34Let me try deleting it again.
  1729. 59:37Okay. User events. Maybe there was a
  1730. 59:39spelling mistake. All right.
  1731. 59:41Seems fine. Now let me list it again.
  1732. 59:45Okay. Yeah. This time it's deleted. I'm
  1733. 59:47not sure why it was not earlier. Maybe
  1734. 59:49it was a spelling mistake. Yeah. And
  1735. 59:51I'll also delete order events.
  1736. 59:54Okay. And I'll list it again.
  1737. 59:57Okay. Still order events is also here.
  1738. 59:59Let me run it again.
  1739. 1:00:02order hyphen events. What am I missing?
  1740. 1:00:06Okay. Delete the topic. And I think
  1741. 1:00:08there is space. Yeah. Fine.
  1742. 1:00:13Okay. I think maybe it gets updated a
  1743. 1:00:15little bit late. All right. It's fine.
  1744. 1:00:17Okay. So we can delete the topics as
  1745. 1:00:19well like this.
  1746. 1:00:21Okay. So these were few of the commands
  1747. 1:00:22that are also very helpful when you work
  1748. 1:00:24with Kafka. And I've already shown you
  1749. 1:00:26how consumers work, how producers work,
  1750. 1:00:28how consumer groups work. So my
  1751. 1:00:30recommendation is you also install Kafka
  1752. 1:00:32in your system, play with it, create
  1753. 1:00:34your own producers, consumers, see how
  1754. 1:00:37partitions are working, in which
  1755. 1:00:38partition the message is going, all
  1756. 1:00:40these things.
  1757. 1:00:41Okay.
  1758. 1:00:41And I hope this video was helpful.
  1759. 1:00:43Everything was clear. For more such
  1760. 1:00:45content, please follow my channel, The
  1761. 1:00:47Curious Coder. Java interview prep can
  1762. 1:00:50get really confusing. There are just too
  1763. 1:00:52many topics and figuring out what to
  1764. 1:00:54actually prepare can be a lot of hassle.
  1765. 1:00:56So I have selectively picked the
  1766. 1:00:58questions and answers that are actually
  1767. 1:00:59important for the interviews from Java
  1768. 1:01:01basics to strings, exception handling,
  1769. 1:01:04memory management, collections. I have
  1770. 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.