YouTube2Text

System Design One Shot Full Course — Transcript

by Telusko · 52,361 words · 7,560 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Welcome back aliens. My name is Vin
  2. 0:02Reddy, and in this video we'll talk
  3. 0:03about system design. I know you are
  4. 0:06waiting for a long time for a system
  5. 0:09design course on Telesco, and finally we
  6. 0:12are here. Now, in this course we are
  7. 0:14going to understand different aspect of
  8. 0:16system design. Now, why it is important?
  9. 0:18When you go for the interviews, most of
  10. 0:20the companies will ask you for system
  11. 0:22design knowledge, and the question will
  12. 0:24be based on that. Now, of course they
  13. 0:26will not be saying this is a system
  14. 0:27design round, but they will expect from
  15. 0:29you to understand different concepts
  16. 0:31here, and how will you solve a complex
  17. 0:33problem. And let me give you a statement
  18. 0:35which highlights this, and I have it
  19. 0:37here. Anyone can write a code that
  20. 0:40works. A system design is what makes it
  21. 0:44work for million people at once, and
  22. 0:48that's the power of system design. See,
  23. 0:50when you write a code, of course when
  24. 0:51you learn a language, you learn a
  25. 0:53framework, you can build application.
  26. 0:55That framework can I mean, the entire
  27. 0:56project will work. The moment you deploy
  28. 0:59it, the moment it is in production, and
  29. 1:00when you have users, not just 10 users,
  30. 1:03of course it will work for 10 users, and
  31. 1:05it should work for 10 users, but the
  32. 1:06moment you have 10 million users
  33. 1:09accessing that at the same time, will it
  34. 1:11work? That's the question. When you talk
  35. 1:13about all your favorite applications, it
  36. 1:15can be WhatsApp, YouTube, Instagram, or
  37. 1:18Amazon for that matter, there are not
  38. 1:20few users who are accessing that at one
  39. 1:23time. There are millions of users, but
  40. 1:25still if you can see, they work. So, how
  41. 1:29do we do it? And that's where system
  42. 1:31design plays a huge role. Now, to
  43. 1:33understand this, we have we will be
  44. 1:34going through these concepts. So, in
  45. 1:36this particular video, we are going to
  46. 1:39start with the basics of system design,
  47. 1:41what is system design, what are
  48. 1:42different components, data versus
  49. 1:45compute intensive, and functional and
  50. 1:47non-functional, and all this comes under
  51. 1:48foundation. Now, once that is done,
  52. 1:51we'll talk about how the communication
  53. 1:52happens between different systems. So,
  54. 1:54we'll focus on DNS, API, and of course
  55. 1:57REST, which you might be knowing if you
  56. 1:59have worked with Spring Boot or Fast or
  57. 2:03Django and any web framework. Then we'll
  58. 2:05move towards the data. Of course, when
  59. 2:07you have a lot of users, how will you
  60. 2:09store data? Not just their personal
  61. 2:11data, but their activities. Uh and in
  62. 2:13that we have different options like SQL
  63. 2:15databases, no SQL databases, when to use
  64. 2:17cache, and when to use SQL and no
  65. 2:20non-SQL. Then, let's say your users are
  66. 2:23growing day by day. How are you are
  67. 2:25going to manage your application? And
  68. 2:27that's where scaling and distribution
  69. 2:28comes into picture.
  70. 2:30In which we'll talk about load
  71. 2:31balancers, replication, partitioning,
  72. 2:33and the CAP theorem. That this will be
  73. 2:35an interesting the CAP theorem. And
  74. 2:37then, we'll talk about operations and
  75. 2:41reliability. And the most important part
  76. 2:43there becomes a fault-tolerant. What if
  77. 2:45something goes wrong? Does the
  78. 2:46application do have something to do
  79. 2:48that? In which we'll talk about message
  80. 2:50queues and monitoring as well. And of
  81. 2:52course, we'll cover different examples
  82. 2:54here to understand different concepts.
  83. 2:56And the entire section or this part will
  84. 2:59be taken by an amazing trainer, Akshay,
  85. 3:01who is very good in explaining stuff.
  86. 3:03And I hope you will enjoy the entire
  87. 3:05part. And do share your thoughts in the
  88. 3:08comment section. Don't keep it blank.
  89. 3:11You what you like, what you don't like.
  90. 3:12If you have any questions, do point it
  91. 3:14there. And we'll try to answer them. And
  92. 3:16if you enjoy this type of videos from
  93. 3:18us, let me know in the comments as well.
  94. 3:20That is our motivation. And I hope
  95. 3:22you're motivated. Let's get started.
  96. 3:24Akshay is all set.
  97. 3:26>> [music]
  98. 3:28>> Hello everyone. Welcome back to
  99. 3:29Teleschool. My name is Akshay Agarwal.
  100. 3:31And today, we are going to start the
  101. 3:33system design course. In this course, we
  102. 3:35are covering each and every topic which
  103. 3:37includes in system design. So, let's
  104. 3:39start with understanding why do we need
  105. 3:41system design at all? In systems, we
  106. 3:44have a lot of systems around us. If we
  107. 3:46talk about human body only, we have
  108. 3:49digestive system. And also, we have a
  109. 3:51system. Along with this we have a lot of
  110. 3:53systems, but these kind of systems are
  111. 3:55pre-built. It means we have not created
  112. 3:58them. We just discovered them with time
  113. 4:01and we got to know about various
  114. 4:02components which are required for us to
  115. 4:05breathe or to digest our food. So, in
  116. 4:08these systems as they are not built by
  117. 4:10us, we can't actually change anything.
  118. 4:12Maybe through surgery we can change some
  119. 4:14of the other component, but not much.
  120. 4:17But the kind of system we talk about in
  121. 4:19IT industry are the systems which are
  122. 4:21built by human or engineers like us
  123. 4:24only.
  124. 4:25So, in that there is a possibility that
  125. 4:27we can pick our systems based on our
  126. 4:29requirement and also based on our
  127. 4:31requirement we can pick the components
  128. 4:34which are required in our system and we
  129. 4:36can basically improve our system based
  130. 4:38on the request we are getting, based on
  131. 4:41the load our system is managing, based
  132. 4:43on the kind of traffic which we are
  133. 4:45getting in our system. So, in system
  134. 4:47design if we go through the words we
  135. 4:48have two words here, right? One is
  136. 4:50system and one is design. What is a
  137. 4:52system? If we go through the definition,
  138. 4:55system basically means there are a lot
  139. 4:57of components. There can be more than
  140. 4:59one components in that system. So, let
  141. 5:02me write components. And those
  142. 5:04components are working eventually to
  143. 5:06solve a particular use case or maybe
  144. 5:08multiple use cases, but the common
  145. 5:10purpose of the application remains same.
  146. 5:12So, that means if we write system is
  147. 5:15equal to components plus common goal,
  148. 5:18this sums up our definition of system.
  149. 5:20Now, when we talk about design, there
  150. 5:22are various components which we are
  151. 5:24going to learn and there are various
  152. 5:26common goals based on the requirement
  153. 5:27analysis of different applications which
  154. 5:29we are going to cover in this particular
  155. 5:31series. But in order to start, let's not
  156. 5:34dig too much into the technical details
  157. 5:35in this video. Let's start from a very
  158. 5:37basic. Let's say we have a requirement
  159. 5:40of creating our own application or our
  160. 5:43own bank. Let's say we have a bank named
  161. 5:46Alien Bank because we are in Telesco,
  162. 5:48why not? And in this bank, we are not
  163. 5:51dealing about each and every operation.
  164. 5:53Currently, we have a single operation,
  165. 5:55which is we have a cash counter, and we
  166. 5:58have a person sitting on a cash counter.
  167. 6:00Let's name it as cashier. Okay, so here
  168. 6:03we are ready with our cashier, who is on
  169. 6:05our cash counter, and the requirement or
  170. 6:07the flow is pretty simple. For the flow,
  171. 6:09let's understand the concept of our
  172. 6:11bank. So, we have multiple, let's say,
  173. 6:14customers are here, and each customer
  174. 6:17will come to our counter, either
  175. 6:19withdraw or deposit some amount, get the
  176. 6:21receipt from the cashier, and then move
  177. 6:23forward. So, let's write it down as
  178. 6:25customer flow cycle. So, in our customer
  179. 6:28flow cycle, there are four steps,
  180. 6:29basically. Customer first will visit the
  181. 6:31counter, will come to the counter, then
  182. 6:34the customer will either withdraw or
  183. 6:36deposit some amount, take the receipt
  184. 6:38from the cashier, and then the
  185. 6:39transaction is done. The purpose of our
  186. 6:41application is done. So, these are the
  187. 6:43core functionality of our system. It is
  188. 6:45very basic, very easy. You might have
  189. 6:47seen this already in some of the banks.
  190. 6:49So, we are building the same
  191. 6:50architecture right now. The architecture
  192. 6:52basically looks like this. Now, in order
  193. 6:55to understand system design, what it
  194. 6:57really is, and how we are going to
  195. 6:59improve the working of our Alien Bank,
  196. 7:01let's face some issues. We are going to
  197. 7:03face five issues, and based on the
  198. 7:05issues and solutions, we are going to
  199. 7:07understand how system design looks like.
  200. 7:10So, here comes our first issue, which is
  201. 7:12the process is really slow. By slow,
  202. 7:14what we meant is, let's say, our cashier
  203. 7:17takes 10 minutes per customer. So, that
  204. 7:20means we are serving only six customers
  205. 7:22in 1-hour span. So, this is a huge
  206. 7:25issue, because if the number of
  207. 7:26customers will increase to a limit, and
  208. 7:29also we have rush hour, in those cases,
  209. 7:31our system will break, and we will be
  210. 7:33not able to serve the customers rightly.
  211. 7:35So, in order to improve this, let's
  212. 7:37first focus on our cashier. So, let's
  213. 7:39understand our system from the cashier's
  214. 7:42point of view. So, basically, our
  215. 7:44cashier is taking time to understand the
  216. 7:47requirement. By requirement, I mean
  217. 7:49whether the customer needs to deposit
  218. 7:51the amount or customer needs to withdraw
  219. 7:52the amount. They have to make this
  220. 7:54interaction, which is time-consuming,
  221. 7:55right? Now, once the requirement is
  222. 7:57locked, whether the customer is going to
  223. 7:58deposit the amount or withdraw the
  224. 8:00amount, the cashier manually have to
  225. 8:02count the cash. And then our cashier
  226. 8:05will prepare a receipt, which will be
  227. 8:06again given to our customer. So, these
  228. 8:09are the time-consuming tasks for our
  229. 8:11cashier. How can we improve the cashier
  230. 8:13time? So, in order to improve the
  231. 8:15working of our cashier, we have three
  232. 8:17major issues. First is they are not able
  233. 8:20to understand the requirement. Next is
  234. 8:23they are counting the cash manually,
  235. 8:25which is taking time. And again, they
  236. 8:27have to prepare the receipt. So, these
  237. 8:29are the issues which our cashier is
  238. 8:31feeling, and we are trying to solve
  239. 8:33these issues. Maybe we can train our
  240. 8:36cashier in order to count the cash very
  241. 8:39rapidly. Okay. Also, our cashier is also
  242. 8:42responsible for maintaining the receipt.
  243. 8:44So, we will improve the typing speed of
  244. 8:46our cashier in order to make the receipt
  245. 8:48very fast. So, the second thing is train
  246. 8:51to type fast. So, with this, what we are
  247. 8:54doing, we are not adding any resources
  248. 8:56to our cashier. Simply, we are making
  249. 8:59our cashier super fast, right? We are
  250. 9:02improving the skills of our cashier,
  251. 9:03which is required for them to maintain
  252. 9:05this job. And let's say through this
  253. 9:07training, the cashier is really improved
  254. 9:09now.
  255. 9:10He is or he or she is now taking, let's
  256. 9:13say, 5 minutes in order to fulfill one
  257. 9:17customer's need. So, this is a huge
  258. 9:19improvement. By this, we are able to
  259. 9:21improve the process by 50%. So, let's
  260. 9:24say our second issue is that the
  261. 9:26customers which our cashier used to
  262. 9:28attend are increasing very fast, because
  263. 9:31this is our alien bank, and it is
  264. 9:32working very fastly, right? Growing very
  265. 9:34fastly. So, now the issue two is our
  266. 9:37customers count is increasing. This is
  267. 9:40our issue two. In this case, what we
  268. 9:42know so far? We know that our cashier we
  269. 9:45can't improve our cashier performance
  270. 9:46because it is now at its peak. So we
  271. 9:49have to basically handle some or the
  272. 9:51other components to our cashier in order
  273. 9:53to make the work very fluently. So maybe
  274. 9:56the desk on which our cashier is
  275. 9:58sitting, we will increase the size of
  276. 10:00desk. So next thing which we can do is
  277. 10:02we will provide the cash counting
  278. 10:04machine to our cashier so that the
  279. 10:05manual cash counting time will get
  280. 10:07reduced, right?
  281. 10:09So we'll provide cash counting machine,
  282. 10:12right? So with this also the time will
  283. 10:14get reduced and our cashier will be able
  284. 10:17to attend more customers. One more thing
  285. 10:19we can do that is instead of coming to
  286. 10:22counter and getting the requirement at
  287. 10:24the real time, we can give a form in
  288. 10:27advance to all customers so that they
  289. 10:30can fill it and our interaction time
  290. 10:32will get reduced, right? So our cashier
  291. 10:35will now don't have to communicate each
  292. 10:37and every request to our customers.
  293. 10:39Uh they can simply see the form and
  294. 10:41based on the form they will fill out the
  295. 10:44thing. And now our cashier have cash
  296. 10:47counting device also, so the process
  297. 10:49will be fast. Let's say through these
  298. 10:52processes, through enhancing our desk or
  299. 10:54cash counter, the timing got reduced
  300. 10:57from 5 minutes per customer to 3 minutes
  301. 11:01per customer. This is also a huge
  302. 11:03impactful thing. So now our cash counter
  303. 11:05is capable of attending more customers
  304. 11:08in lesser time. So that means even if
  305. 11:10the customer count is increasing, we
  306. 11:12will be able to process more customers
  307. 11:14and enroll more customers into our Alien
  308. 11:17bank. So this issue is also kind of
  309. 11:19solved. Now let's move to the third
  310. 11:21issue. The issue three is our customers
  311. 11:24are still waiting to get their
  312. 11:25application resolved, right? Currently,
  313. 11:28what do we have? We have our cashier who
  314. 11:30is upgraded to its maximum level. So, we
  315. 11:33can't upgrade the cashier more. Also,
  316. 11:36the desk on which it is sitting, the
  317. 11:38cash counter, is also upgraded to its
  318. 11:40maximum limit. So, we can't upgrade our
  319. 11:42cash counter more. So, these two things
  320. 11:45are pretty solid right now. We can't
  321. 11:47improve it now. Now, the thing is, so if
  322. 11:50we have 10 customers in place, that
  323. 11:52means our 10th customer will have to
  324. 11:54wait for 27 minutes in order to process
  325. 11:58the three-minute work. So, that is a
  326. 12:00huge drawback of our system right now.
  327. 12:02And why that drawback is coming? Because
  328. 12:05we have only single cash counter in our
  329. 12:07system. So, in order to make the process
  330. 12:09more feasible, now we have reached that
  331. 12:12point that we have to add more cash
  332. 12:14counters into our system.
  333. 12:16So, let's add them. So, now we have our
  334. 12:18cash counter one and cash counter two.
  335. 12:20Now, at this point, we have added one
  336. 12:22more counter, and things are pretty
  337. 12:24solid right now. So, our customers will
  338. 12:28eventually have two counters to go to,
  339. 12:30and both of those counters will be able
  340. 12:32to solve our issues. It feels good,
  341. 12:34right? Now, we have two counters to
  342. 12:35process. So, our customers will not have
  343. 12:37to wait for longer time. They can either
  344. 12:40go to counter one, or they can go to
  345. 12:41counter two. But, let's say if first
  346. 12:44customer is going to counter one, and
  347. 12:47the counter one is able to process the
  348. 12:49information, and they are maintaining
  349. 12:51their own data. And similarly, our
  350. 12:53second counter is also maintaining its
  351. 12:55own data. And they are not synced. So,
  352. 12:59this creates two issue, right? First
  353. 13:01issue is, if in our system, the counter
  354. 13:04two doesn't know anything about the
  355. 13:06customer, that will create an issue
  356. 13:07because they have a valid account in our
  357. 13:10bank, and they should be able to proceed
  358. 13:12their information through it. Now, the
  359. 13:14second issue is this only. That from the
  360. 13:18first account, they can also withdraw
  361. 13:20the amount, and they will not update the
  362. 13:23second counter. So, that means they can
  363. 13:25again withdraw the amount from cash
  364. 13:27counter two also. This will create an
  365. 13:29issue of data discrepancy. That means
  366. 13:31our data is not synced. So, the fourth
  367. 13:34issue here is we need to have a
  368. 13:35centralized database or central data
  369. 13:38from which the cash counter one and cash
  370. 13:40counter two will go and validate the
  371. 13:43data and go and retrieve the data. If
  372. 13:45the account is there for any customer
  373. 13:48and the amount is also there, so the
  374. 13:50customer one will also insert the cash
  375. 13:53into the same account. And if the
  376. 13:55counter two is getting withdrawal
  377. 13:56request, they will also get the amount
  378. 13:59out of the same account. So, in order to
  379. 14:01fix this, we need to have a centralized
  380. 14:03data. That means our counter will look
  381. 14:05like this. This is our counter, let's
  382. 14:07say two. This is our counter one. And
  383. 14:10they will eventually connect to a
  384. 14:12centralized database. And as it was
  385. 14:14before, our customers are standing in a
  386. 14:16queue waiting for their turn to come in
  387. 14:19order to get the request. Now, after
  388. 14:22solving all these case also, we have a
  389. 14:24new issue in hand. Which is, let's say
  390. 14:27the gate of our bank is here. This is
  391. 14:29our alien bank. So, since the gate is
  392. 14:32here, most of our customers are going to
  393. 14:35the cash counter one only, where cash
  394. 14:37counter two is sitting idle. Or maybe
  395. 14:40processing lesser request, far lesser
  396. 14:42request than the cash counter one. This
  397. 14:44will again end up adding the wait time
  398. 14:46to our customers. And again, we will be
  399. 14:49end up handling lesser customers than
  400. 14:51our potential. So, let's write that
  401. 14:53down. This is our issue five, which is
  402. 14:55underutilized counter. Now, in order to
  403. 14:57solve this issue, we need to have a
  404. 14:59middleman. Which will stand apart from
  405. 15:02our counters. Let's say this is our
  406. 15:04counter two. Again, this is our counter
  407. 15:06one. And here are our customers. And now
  408. 15:10our customers will not go to the counter
  409. 15:12directly. They will first go to this
  410. 15:14middleman. Then the middleman will check
  411. 15:17how much load each counter is handling.
  412. 15:20Suppose there are 10 customers at
  413. 15:22counter one and only five customers at
  414. 15:24counter two, then our middleman can
  415. 15:27decide that next customer should go to
  416. 15:30the counter two.
  417. 15:32And the count will increase to six,
  418. 15:33right? And the next customer will again
  419. 15:36go to counter two, and the next customer
  420. 15:39is again going to counter two only.
  421. 15:41When the limit reach, let's say we have
  422. 15:4311 customers here and 11 customers here.
  423. 15:47And if then any new customer arrive,
  424. 15:50they will again ask man where to go.
  425. 15:52Let's say on some fine day our counter
  426. 15:55one is not working. That means our
  427. 15:57middleman will know about this and will
  428. 15:59eventually send all the customers to
  429. 16:02counter two until the counter one is
  430. 16:04getting fixed. So, let's end this
  431. 16:06example here at this particular point
  432. 16:09and see what have we learned. Through
  433. 16:11this story, through this single story
  434. 16:13and five issues, I have tried to capture
  435. 16:15multiple concepts of system design. So,
  436. 16:17the first concept which we discussed is
  437. 16:20designing a system, that is for sure.
  438. 16:22Ultimately, what design we do have here?
  439. 16:25We have one cashier and the other
  440. 16:27cashier. Each cashier is having their
  441. 16:30own counter. So, this is our counter
  442. 16:32two, maybe this is our counter one. We
  443. 16:34have a number of customers waiting for
  444. 16:37us. Also, we have a middleman now. With
  445. 16:40that, we also have a common database,
  446. 16:43okay? So, this database is nothing but
  447. 16:46centralized database. The customers
  448. 16:48which are coming to our bank is nothing
  449. 16:51but requests. And if they are standing
  450. 16:54in a queue, we can say they are creating
  451. 16:56a traffic, okay? So, these are the
  452. 16:58traffic which we are going to handle.
  453. 17:00Now, all the requests or all the traffic
  454. 17:03is coming through a load balancer, the
  455. 17:05middleman. So, this is nothing but our
  456. 17:08load balancer. So, from the load
  457. 17:09balancers, all the requests are coming
  458. 17:11to either of the cash counter. So, we
  459. 17:14can say that our cash counter is nothing
  460. 17:16but servers. And the person sitting
  461. 17:19there doing the work is nothing but our
  462. 17:21application code. Now, let's see the
  463. 17:23system again through the system design
  464. 17:25point of view. So, the first issue which
  465. 17:27we have was our process is slow. So, in
  466. 17:30order to solve that, we improved our
  467. 17:33code quality, right? So, first thing
  468. 17:35which we do in our systems also, we
  469. 17:37improve our code quality. So, this is
  470. 17:39where our LLD will come and this is
  471. 17:42where our DSA will come. Suppose a
  472. 17:44particular loop is taking certain time,
  473. 17:47we can reduce that loop or we can change
  474. 17:49that loop to some for each function or
  475. 17:51some other way in order to process that
  476. 17:53information fast. So, while solving the
  477. 17:56issue two, we increase the size of our
  478. 17:58table, we added some functionality, we
  479. 18:00added this cash counter. That means we
  480. 18:03upgraded our server. So, in order to
  481. 18:05upgrade our server, this term is called
  482. 18:07vertical scaling. In vertical scaling,
  483. 18:10we don't add multiple servers, we
  484. 18:12basically update our current server
  485. 18:14only. And again, the current server
  486. 18:17updation have a particular limit. After
  487. 18:19that limit, you can't upgrade your
  488. 18:21system. So, then comes the issue third,
  489. 18:24which is our customers need to wait for
  490. 18:27the single server to respond. So, in
  491. 18:29this case, we added more servers. So, in
  492. 18:31this scaling, we added more server that
  493. 18:34is called horizontal scaling. So, after
  494. 18:36having multiple servers, we need to
  495. 18:38maintain the data at the common point.
  496. 18:40So, that's why we added databases. And
  497. 18:43in order to add a common database, we
  498. 18:45basically can move to distributed
  499. 18:47database architecture or can move to a
  500. 18:49centralized approach of handling that
  501. 18:51database. So, this is nothing but
  502. 18:54centralized database. So, after having
  503. 18:56centralized database, again the issue
  504. 18:58comes where we don't know how to handle
  505. 19:01multiple requests. That means our
  506. 19:03requests are again going to one counter
  507. 19:05only or one server only. Now that we
  508. 19:08know they are going to one server only
  509. 19:10and the other server is underutilized.
  510. 19:13So, what we did, we added a middleman.
  511. 19:15Our middleman is no one but our load
  512. 19:18balancer. Our load balancer basically do
  513. 19:20two things. They will check whether our
  514. 19:22servers are healthy or not and based on
  515. 19:25that, they will distribute the request
  516. 19:27equally into multiple servers. So, these
  517. 19:29are all the concepts which we discovered
  518. 19:32through this particular example. So, if
  519. 19:34we think about the applications we are
  520. 19:35using in our day-to-day life, we are
  521. 19:37using WhatsApp, we are using maybe
  522. 19:40Snapchat, we are using Instagram, we are
  523. 19:43using YouTube, we are using LinkedIn, we
  524. 19:46are using Netflix, Amazon, maybe
  525. 19:48Flipkart. All of the applications, if we
  526. 19:51think about it, are showing us the data
  527. 19:53either in the form of images or in
  528. 19:56videos, in audios or in text. So, we can
  529. 19:59say through a large lens, the data
  530. 20:02consist of these four kind of
  531. 20:04information. And all of the applications
  532. 20:06which we are using in our day-to-day
  533. 20:07life consist of these data's only. So,
  534. 20:10we can say that in the core of every
  535. 20:13system, we have data and we are then
  536. 20:15finding ways to make it available to the
  537. 20:18users in the way they are expecting it.
  538. 20:20So, in the center, as we discussed, we
  539. 20:22have data. Data can be of various form.
  540. 20:25It can be image, video, audio or text.
  541. 20:30Now, if we think about data, it is not
  542. 20:31like we are seeing the data once only.
  543. 20:34We have to see the data and also while
  544. 20:36storing also, we need to store the large
  545. 20:39amount of data. Now, in order to store
  546. 20:41the data, we do have databases. So,
  547. 20:43databases are basically our first
  548. 20:45component of system design. What is
  549. 20:48functionality of databases? Pretty
  550. 20:50simple. We need to store our data and
  551. 20:53whenever we want to retrieve some data,
  552. 20:54we need to get the data from our
  553. 20:56databases only. They are represented by
  554. 20:58this cylindrical figure. Databases can
  555. 21:01be of multiple ways. It can be
  556. 21:03categorized into no sequel or sequel.
  557. 21:06Now, there are various kind of databases
  558. 21:08in both of these categories. In sequel
  559. 21:11also, we have various databases from
  560. 21:13which we can select which database we
  561. 21:14are going to use. And similarly, in no
  562. 21:17sequel also, we have various kinds of
  563. 21:19database from which we can pick which
  564. 21:21database we need for our application
  565. 21:23right now. But, the core remains seems
  566. 21:26that we are saving our data in some or
  567. 21:29the other database. We will discuss
  568. 21:30about sequel and no sequel databases in
  569. 21:32the coming videos. That's why I'm not
  570. 21:34going to deep into this at this moment.
  571. 21:36Let's think about the scenario where you
  572. 21:39and every user on the planet knows the
  573. 21:41queries of database, knows how the
  574. 21:44tables are structured if we are talking
  575. 21:46about sequel, or how the documents or
  576. 21:48nodes are connected if we are talking
  577. 21:50about no sequel. If we talk about both
  578. 21:52of these scenarios, if user, the end
  579. 21:55user, already know the database query
  580. 21:58languages and know how to deal with
  581. 22:00different kind of data, in that case, we
  582. 22:02might not need any application if you
  583. 22:04think about it. We will simply go to our
  584. 22:06database, or the database will be
  585. 22:08accessible throughout every application.
  586. 22:11Suppose we are seeing the LinkedIn, but
  587. 22:13on LinkedIn we will see an interface on
  588. 22:15which we will write our query, and the
  589. 22:17data will be fetched to us. That will be
  590. 22:20an hectic task. But, if users know about
  591. 22:23that, and if users start exploring that,
  592. 22:26that will be easier, right? In that
  593. 22:27case, we don't need any application
  594. 22:29layer in between us, and in that case,
  595. 22:31we don't need any client-side
  596. 22:33application also between us. We simply
  597. 22:35will see a database, query it, and get
  598. 22:38the results, right? In that case, what
  599. 22:40will happen? Each query have to be
  600. 22:42managed through the users only. And
  601. 22:44these queries, when I talk about large
  602. 22:46systems, let's say we are talking about
  603. 22:48LinkedIn, there can be multiple tables
  604. 22:51through which our data is curating and
  605. 22:53coming to us. Every user can't do it,
  606. 22:56right? That's why we have different
  607. 22:58layer in front of our database, which is
  608. 23:00our application code. So, in application
  609. 23:02code, what we do is we hide the
  610. 23:04complexity of dealing with databases,
  611. 23:07different tables, different documents,
  612. 23:09and we give the endpoints to our
  613. 23:12application or to the world so that they
  614. 23:15can access the kind of information they
  615. 23:17are looking for. For that, we create
  616. 23:19APIs or we can say we create endpoints.
  617. 23:23There can be multiple endpoints based on
  618. 23:25different features, based on different
  619. 23:27operations. So, through endpoints, we
  620. 23:30communicate to our application, which
  621. 23:32will again communicate to our databases
  622. 23:35to get the data we are trying to get.
  623. 23:37There can be various operations, like we
  624. 23:39can insert some data, we can update some
  625. 23:41data, we can retrieve some data, we can
  626. 23:44delete some data based on our request.
  627. 23:47So, the thing becomes little easier to
  628. 23:49us. Now, we don't have to deal with all
  629. 23:52the database tables, but now we have
  630. 23:54assigned endpoints through which we can
  631. 23:56do the work which we want to do. And
  632. 23:58endpoints are pretty simple.
  633. 24:00So, in order to get some data, we have a
  634. 24:02get endpoint. Let's say https, and here
  635. 24:07comes our Teleschool. So, this URL or
  636. 24:10this endpoint can be used to fetch all
  637. 24:13the students which are there in our
  638. 24:15Teleschool environment. But then, what
  639. 24:17we are doing? By introducing application
  640. 24:19code, we are moving our logic and data
  641. 24:22separately. That means we need some kind
  642. 24:25of connections between these two. In
  643. 24:27which we can connect through IP
  644. 24:29addresses, we can connect through some
  645. 24:31HTTP protocols, or we can connect
  646. 24:33through some TCP protocol. There are
  647. 24:35many ways of connecting the database
  648. 24:38from the application code and getting
  649. 24:39the data which we want. So, in order to
  650. 24:41show the APIs or in order to interact
  651. 24:43with the APIs, we have to know what is
  652. 24:46JSON and how we are going to send the
  653. 24:49data to our APIs and how we are going to
  654. 24:51fetch the data from our API.
  655. 24:54In this level also, if we know how to
  656. 24:56handle JSON, if we know how to handle
  657. 24:59various operations through which
  658. 25:01endpoint, it will make our life little
  659. 25:03easier than accessing the databases,
  660. 25:05right? So, in that case, we don't need a
  661. 25:08front end if we know how to hit the
  662. 25:10endpoints through which we'll get the
  663. 25:12data. But, for our normal users, for
  664. 25:15other users of any applications, this is
  665. 25:17also a tedious task. So, in order to
  666. 25:20make this easier, we have a different
  667. 25:22layer between our system that is our
  668. 25:25client application. Client applications
  669. 25:27are nothing but the applications through
  670. 25:29which we are interacting to the
  671. 25:30database, through which we are getting
  672. 25:33those databases. So, our client
  673. 25:34applications, be it a mobile
  674. 25:37application, be it any web application,
  675. 25:39be it any ATM machine for that matter,
  676. 25:42they are connected with the application
  677. 25:44code, and further, the data is being
  678. 25:47consumed by us. So, again, in order to
  679. 25:49connect the application layer and the
  680. 25:51client code, we again need some kind of
  681. 25:54communication between them. We either
  682. 25:56can go to the same HTTP or TCP or maybe
  683. 26:00IP. Now, we have our application ready
  684. 26:02in our hand and we can use it. So, if I
  685. 26:04draw it like we used to draw it, this is
  686. 26:07represented by our client, either a
  687. 26:10desktop or a mobile device. Now, they
  688. 26:14are sending queries to our application
  689. 26:16code, and our application code is
  690. 26:18further getting the data from database.
  691. 26:21Now, for our client to communicate with
  692. 26:24our application code, we need to put our
  693. 26:26application code on any server. And now,
  694. 26:29if we think about it, our application
  695. 26:31code is already exposing the APIs to our
  696. 26:34client. That means, it can also expose
  697. 26:38the APIs, or any other application can
  698. 26:40also use our application codes through
  699. 26:44our APIs and do the work which is
  700. 26:46needed. So, it can be connected to any
  701. 26:49other server also, which will contain
  702. 26:51their application code. And this will
  703. 26:53communicate with our data through our
  704. 26:56application code. For every query which
  705. 26:58we are making, we are going to our
  706. 27:00database, and from that we are getting
  707. 27:02the data which we are trying to get. So,
  708. 27:04if we think about it, most of the
  709. 27:06communications will start from the
  710. 27:08client side, and they will come to our
  711. 27:10application code through which we will
  712. 27:12get the data from the database. This is
  713. 27:14pretty much the easy application or the
  714. 27:17applications which we create on our
  715. 27:18college projects look like. Either a
  716. 27:20to-do application or a simple chatting
  717. 27:22application will look like this. Now, in
  718. 27:24the current scenario, what we are doing
  719. 27:26with this kind of architecture is every
  720. 27:28client is going to ask application code
  721. 27:32for some or the other information, and
  722. 27:34to fetch the data, our application code
  723. 27:36needs to get the data from the database
  724. 27:38only. We don't have any other option. In
  725. 27:41order to make the files accessible to
  726. 27:43our server faster, because the
  727. 27:46database's calls are heavy, which will
  728. 27:48take a lot of time, let's say, if a
  729. 27:50database query is finding the columns
  730. 27:53from 12 tables, it is going to take
  731. 27:55time, right? Now, in order to make the
  732. 27:57frequently used data accessible faster,
  733. 28:01we have a different layer, which is
  734. 28:03called cache. When we are going to put
  735. 28:06any data into our cache, and when we are
  736. 28:09supposed to remove the data. There are
  737. 28:11different strategies we are going to
  738. 28:12cover that also in the upcoming videos,
  739. 28:15but for now, let's see, this is our
  740. 28:17cache, and all the frequently used data
  741. 28:20will be stored here. So, that we don't
  742. 28:22have to call to our database, and they
  743. 28:24can be easily accessible through our
  744. 28:26cache only, which will make our process
  745. 28:28slightly faster. So, this is our fourth
  746. 28:30component of our system, which is cache.
  747. 28:34Now, let's move forward. Let's say our
  748. 28:35application is big, is having multiple
  749. 28:38servers. In that case, we may have some
  750. 28:41client or this client, either the mobile
  751. 28:43application or the web view, and we are
  752. 28:46getting the data from multiple servers.
  753. 28:48Let's say this is server one or this is
  754. 28:51server two. Now, when we have two
  755. 28:53servers or multiple servers, our client
  756. 28:55have to decide where to go. From where
  757. 28:58do we fetch the information? Now, in
  758. 29:00order to take this control, we have a
  759. 29:02load balancer in the middle, which will
  760. 29:04take care of each and every request and
  761. 29:06which will take care of each and every
  762. 29:08connections of the server. So, our load
  763. 29:10balancers are responsible for handling
  764. 29:13two tasks. First, they will check
  765. 29:15whether the server is healthy or not.
  766. 29:17And second one, they will divide the
  767. 29:19request for each servers equally. This
  768. 29:22is what our fifth component looks like.
  769. 29:24Now, let's come to our next component,
  770. 29:26which is messaging queue. So, in order
  771. 29:29to understand message queue, let's first
  772. 29:31understand that in order to process one
  773. 29:33operation, there can be multiple task
  774. 29:35into that operation, right? Let's say we
  775. 29:38are ordering something. So, when we are
  776. 29:40ordering, we are going to do multiple
  777. 29:42tasks, right? We are first creating an
  778. 29:45inventory update. Next is we are going
  779. 29:47to process the order also. We are going
  780. 29:49to talk to the vendor who is going to
  781. 29:51take the delivery. So, we are going to
  782. 29:53handle delivery. And then, we are also
  783. 29:55trying to send you the information, the
  784. 29:57notifications that your order is placed
  785. 29:59successfully. Don't worry about it. So,
  786. 30:01that means we are going to handle the
  787. 30:02mail service or the SMS service also.
  788. 30:06Now, in order to handle multiple
  789. 30:08services like this, we can't wait for
  790. 30:10every application to respond back. There
  791. 30:12are two kind of applications basically.
  792. 30:15First is, let's say this is an
  793. 30:16application and this is another
  794. 30:17application. Let's say this is SMS
  795. 30:19application and this is our order
  796. 30:21application or microservice for that
  797. 30:24matter. Now, whenever an order is
  798. 30:26placed, it is going to speak to the SMS
  799. 30:28service or SMS application to send the
  800. 30:31message to the user that your order is
  801. 30:33placed successfully. Now, there can be
  802. 30:35two ways of handling this. First way is,
  803. 30:37whenever the SMS is sent, they will
  804. 30:40notify the order service or the order
  805. 30:43application back that we have sent your
  806. 30:44message done. Or there can be different
  807. 30:47way of handling it that from our order
  808. 30:49service, we simply say that okay, this
  809. 30:52is our order information. Please send
  810. 30:54the message to this number and we are
  811. 30:56done. We will not wait for the response.
  812. 30:59We will move forward with our
  813. 31:00application and with different orders.
  814. 31:03So, the kind of requests in which we are
  815. 31:05waiting for any confirmation or any
  816. 31:07feedback from the other service are
  817. 31:09called synchronous calls. And the kind
  818. 31:11of service in which we will simply put
  819. 31:13down the service and we are not waiting
  820. 31:15for any confirmation, we will move
  821. 31:16forward to do our task. Those are called
  822. 31:18asynchronous services. Don't worry about
  823. 31:21this if you don't understand it now. We
  824. 31:22are going to dissect each and every
  825. 31:24component in details in the further
  826. 31:26coming videos. So, for asynchronous kind
  827. 31:29of services, what we can do, we can
  828. 31:30simply push our notification or push our
  829. 31:32request to another service and move
  830. 31:35forward. So, in those kinds of services,
  831. 31:37we have a message queue or message
  832. 31:40broker in middle. What it will do, we
  833. 31:42are having our order service here. It
  834. 31:45will put multiple request or can put a
  835. 31:48request time to time into this message
  836. 31:51queue. And then all the requests which
  837. 31:54our order is processing through message
  838. 31:55queue, they will not take care of it
  839. 31:57anymore. Our message queues will take
  840. 32:00care of it. They will connect with our
  841. 32:02SMS service or SMS applications and send
  842. 32:06the request one by one. And they will
  843. 32:08take care of these all operations. So,
  844. 32:10our message queue is like a broker who
  845. 32:13will take the request from one
  846. 32:14application, store inside it. They will
  847. 32:17take care of these applications. They
  848. 32:19will take care of the retries or if any
  849. 32:22request is failing, they will take care
  850. 32:23of it. Maybe they will communicate to
  851. 32:26our order application in the end or in
  852. 32:28the end of each day, we will send some
  853. 32:31notification, some batch processes to
  854. 32:34our order application in order to show
  855. 32:37that which requests are fulfilled and
  856. 32:39which are not fulfilled. So, in short,
  857. 32:41our messages queues are doing two work
  858. 32:44here. They are storing each and every
  859. 32:46request which are coming from various
  860. 32:48other services or various other
  861. 32:50applications for that matter. They are
  862. 32:52storing it and they are taking care of
  863. 32:54those requests. They are sending those
  864. 32:57requests finally to SMS application or
  865. 32:59service and they are taking charge that
  866. 33:01okay, my first service or my first
  867. 33:04request is being processed. Nice. Now, I
  868. 33:07will send the next request. Now, I will
  869. 33:09send the next request and further on. If
  870. 33:11it is failed, there are various
  871. 33:12mechanisms how messages queues work and
  872. 33:15how they bisect and dissect each and
  873. 33:17every request and make sure that they
  874. 33:20will provide the latest status of each
  875. 33:22request. And in those kinds of
  876. 33:24environment where we have multiple
  877. 33:25microservices, in those cases message
  878. 33:28queues plays a vital role in between
  879. 33:30them. And with that, let's move to our
  880. 33:32final component which is monitoring and
  881. 33:35logs. In monitoring and logs, what do we
  882. 33:37do? As we know now, we have different
  883. 33:39components in our architecture, we have
  884. 33:42a client system here.
  885. 33:43We may have a mobile application again
  886. 33:46which is connecting through an API to
  887. 33:49our load balancer
  888. 33:50which is again further connected to
  889. 33:52either server one or server two.
  890. 33:55Our servers are the area in which we
  891. 33:58have our application code and then they
  892. 34:01may be connected to single database or
  893. 34:03multiple databases and those databases
  894. 34:06can also communicate with each other in
  895. 34:08order to maintain the data. And also,
  896. 34:10let's say our application is also using
  897. 34:12some third-party application for which
  898. 34:14we are using message queues. Our message
  899. 34:17queues are denoted by this. Now, this is
  900. 34:19our architecture. We have so many
  901. 34:21components.
  902. 34:22So, it is very important for us to
  903. 34:24monitor those components. To monitor
  904. 34:27whether our load balancer is working
  905. 34:29finely or not. To manage whether our
  906. 34:32application code does have any error or
  907. 34:34not. And if there is an error, we need
  908. 34:37to know how to solve it. We need to know
  909. 34:39the complete stack trace of that
  910. 34:41particular error. We need to know how to
  911. 34:43reproduce those errors on our local
  912. 34:45system in order to fix them. And also,
  913. 34:48while working with the applications, we
  914. 34:49know that for every each and now, we
  915. 34:52have multiple features which are being
  916. 34:54attached to our applications. So, we
  917. 34:56also need to monitor while adding one
  918. 34:58more feature to our application, whether
  919. 35:00any other feature is impacted or not, or
  920. 35:03it is failing or not. So, in order to do
  921. 35:06that, we have a lot of monitoring tools
  922. 35:08and also logs to check our system. There
  923. 35:11is a complete testing department in our
  924. 35:13team also, who takes care of these
  925. 35:15things. This is one of the vital role in
  926. 35:18our application. So, these are most of
  927. 35:20the common components which are used in
  928. 35:21system design. Whenever we think about
  929. 35:24any application or designing any
  930. 35:25application, we suddenly jump to the
  931. 35:27tools which are used in that
  932. 35:29application. We start creating diagrams
  933. 35:31that this is our client, this is our
  934. 35:33database, this is how our data is going
  935. 35:35to flow, this is how we add cache maybe.
  936. 35:38So, these all things we start doing
  937. 35:40without noticing a single thing, that
  938. 35:42is, how is our application going to
  939. 35:45work?
  940. 35:46What is this application? What are the
  941. 35:48limitations of this application? In
  942. 35:50system design, there is a smart way of
  943. 35:52doing that. First question you need to
  944. 35:54ask yourself before diving into the
  945. 35:57solutions is, what is this application?
  946. 36:00What is the type of this application? Is
  947. 36:03it a data intensive application or a
  948. 36:05compute intensive application? With this
  949. 36:08common differentiation, you will be able
  950. 36:10to save a lot of cost to your system
  951. 36:12design, as well as you will be able to
  952. 36:15handle the task properly into your
  953. 36:17system. Let's say, we have two
  954. 36:20applications. In both of these
  955. 36:22application, there are the same number
  956. 36:25of users. With that, the network on
  957. 36:27which both of the applications are
  958. 36:29working are also pretty much same. Now,
  959. 36:32let's put a scenario that our system or
  960. 36:34the application which we are working on
  961. 36:36is facing latency. Now, what is latency?
  962. 36:40Latency means our system is taking time
  963. 36:42to respond. There are various ways of
  964. 36:44handling this issue, of fixing the
  965. 36:47latency issue. We can add multiple
  966. 36:49databases, we can add different caching
  967. 36:51layers, we can update the caching
  968. 36:52mechanism, or we can update
  969. 36:55or the CPU powers. Now, on which vector
  970. 36:58we are going to do this changes defines
  971. 37:00how well we are going to solve this
  972. 37:02issue. So, now there are two type of
  973. 37:04applications as I discussed. One is data
  974. 37:06intensive application and one is compute
  975. 37:09intensive application. Let's understand
  976. 37:11them one by one. The basic difference on
  977. 37:14which data intensive application is
  978. 37:16different from a compute intensive
  979. 37:17application is that in data intensive
  980. 37:20application our core focus is on data.
  981. 37:23Now, for this data we need to have
  982. 37:25multiple operations, right? We need to
  983. 37:26store the data, plus we need to gather
  984. 37:29the data, we need to move the data time
  985. 37:31to time. That means we need to keep
  986. 37:33updating the data because new data is
  987. 37:35entering. And also, while new data is
  988. 37:38entering we can also, right, we need to
  989. 37:39add a lot of data.
  990. 37:41We can take example of any application
  991. 37:43which can be Instagram because on
  992. 37:45Instagram we see a lot of content and we
  993. 37:48post a lot of content. So, that is a
  994. 37:50data intensive application where the
  995. 37:52primary focus is on gathering the data,
  996. 37:55is on showing the data, is on the data
  997. 37:57you are posting. But, the focus is not
  998. 37:59on how the data will be calculated. The
  999. 38:01calculation part is pretty solid. We
  1000. 38:03just need to move the data from our
  1001. 38:05databases to the client applications and
  1002. 38:07we are sorted. So, in these kind of
  1003. 38:09systems the data mainly resides in the
  1004. 38:12database. So, first issue can be the
  1005. 38:16database response is low. Another is,
  1006. 38:18since we are using network calls here
  1007. 38:20and there, that means our network calls
  1008. 38:23are also bulky or we need to optimize
  1009. 38:25them. The third can be our server itself
  1010. 38:27that our server configuration is pretty
  1011. 38:29low, that's why we are not able to get
  1012. 38:31the data faster. So, all in all, in
  1013. 38:34data-intensive applications, we are not
  1014. 38:35blaming the client to upgrade their CPU,
  1015. 38:38we are blaming our circle, our database
  1016. 38:41calls, our network calls, our server
  1017. 38:43configuration in order to get the data
  1018. 38:45fast. Most of the applications in which
  1019. 38:48you work on are data-intensive
  1020. 38:50application. That's why we use terms
  1021. 38:52like cache enhancement, shredding, and
  1022. 38:55through all of those things we will be
  1023. 38:56able to solve this issue. Now, let me
  1024. 38:58also write some common examples of this.
  1025. 39:00So, the common examples here are
  1026. 39:02Instagram feed where we see a lot of
  1027. 39:04data as we discussed earlier. In
  1028. 39:06WhatsApp messages also, there is a high
  1029. 39:08amount of data because we are not only
  1030. 39:10taking care of one chat, we are taking
  1031. 39:12care of entire chat. The count of chats
  1032. 39:15or the count of messages WhatsApp deal
  1033. 39:17with is around 2 to 10 million messages
  1034. 39:20per day. So, this is also a heavy
  1035. 39:23data-intensive application. Now, there
  1036. 39:26are bank transactions, analytic
  1037. 39:28dashboard, and log process system as
  1038. 39:30well. In all these application, the
  1039. 39:32primary goal is to serve the data from
  1040. 39:35database to our client system faster.
  1041. 39:37There is not much calculation required
  1042. 39:40for them to show on the client machine.
  1043. 39:42Now, based on the data-intensive
  1044. 39:43application, what are the worries of a
  1045. 39:46data-intensive application? The first
  1046. 39:48worry is how fast can we read the data?
  1047. 39:51The second point is how safely are we
  1048. 39:54going to store the data? The third
  1049. 39:56point, as we all know, a lot of users
  1050. 39:58are using our application, so how many
  1051. 40:00users can use the same data
  1052. 40:02simultaneously? The next and one of the
  1053. 40:05most important worry of these kind of
  1054. 40:07application is what if any server dies?
  1055. 40:10What if any server breaks? What if any
  1056. 40:13network call breaks? What if any
  1057. 40:15database machine breaks down? So, in
  1058. 40:18those cases, how we are going to handle
  1059. 40:20the data? So, these are the most common
  1060. 40:22worries of the data intensive
  1061. 40:24applications. And now, in order to solve
  1062. 40:26them, we don't go to our client system.
  1063. 40:29We talk about the components on which
  1064. 40:32these processes lies. And those are our
  1065. 40:35databases, caching, replication, maybe
  1066. 40:38data sharding, or it can be solved
  1067. 40:41through data consistency. So, these are
  1068. 40:43all the components or the terms we use
  1069. 40:46in order to improve our data intensive
  1070. 40:48application. Don't worry about these
  1071. 40:50terms if you don't know them yet. We are
  1072. 40:52going to cover each and every component
  1073. 40:54in details in the coming videos. So, I
  1074. 40:56hope data intensive applications are
  1075. 40:58pretty clear up to now. But, for the
  1076. 41:00sake of example, we are also going to
  1077. 41:02take one more example. Now, think about
  1078. 41:04Instagram feed. We are not solving
  1079. 41:06Instagram feed issue through any
  1080. 41:09mathematical equations. We just have to
  1081. 41:11gather a lot of data and show you that
  1082. 41:13data. So, what we don't do here, we
  1083. 41:16don't do any complex maths. What we do
  1084. 41:18then? We normally fetch the posts. After
  1085. 41:21fetching the post, we sort them through
  1086. 41:24time or relevance. We show a lot of
  1087. 41:26images or videos. And according to those
  1088. 41:29content, we normally do some operations,
  1089. 41:33maybe like or commenting or sharing. So,
  1090. 41:36I'm writing that. So, this is all the
  1091. 41:38operations loosely we have on the
  1092. 41:41Instagram feed. Now, what is the common
  1093. 41:43challenge which Instagram is facing
  1094. 41:45daily? That is we we have a lot of
  1095. 41:48content. We have millions of users and
  1096. 41:51million of content which we have to
  1097. 41:53handle. So, let me write that down. In
  1098. 41:55other words, we can also say that
  1099. 41:57million of users are reading and writing
  1100. 42:00at the same time. So, with this
  1101. 42:01challenge, Instagram still needs to
  1102. 42:03scale, right? How does Instagram scale?
  1103. 42:06If we think about this, Instagram adds
  1104. 42:09multiple databases in order to store the
  1105. 42:11data. And also, Instagram uses
  1106. 42:13aggressive caching in order to store or
  1107. 42:16get the data fast. Now, we also know
  1108. 42:18that Instagram heavily depends on the
  1109. 42:20images and the video content form. So,
  1110. 42:23in order to handle them, we can't simply
  1111. 42:25store those kind of data into our
  1112. 42:27databases directly. For that, in order
  1113. 42:29to fetch them faster, we need to have
  1114. 42:32certain CDN in place. And in databases,
  1115. 42:35we will only store the location of that
  1116. 42:36particular file which we are going to
  1117. 42:38fetch through the CDN. Now, think about
  1118. 42:40this. In order to fix the latency issue
  1119. 42:43on Instagram or in order to help
  1120. 42:45Instagram scale, we hardly touch the CPU
  1121. 42:48or we hardly touch any GPU. We only
  1122. 42:51touch the data or the components which
  1123. 42:54are associated with them. Because the
  1124. 42:56issue here is not the CPU, but the data
  1125. 42:59movements between the components. Now,
  1126. 43:01this is all about data-intensive
  1127. 43:03applications. Let's now focus on the
  1128. 43:05compute-intensive applications. Now, the
  1129. 43:07compute-intensive application is exactly
  1130. 43:10opposite of a data-intensive
  1131. 43:12application. Here, the amount of data
  1132. 43:14which we are retrieving from databases
  1133. 43:16are pretty much low, but the
  1134. 43:18calculations we need to perform are
  1135. 43:20really heavy. So, instead of putting our
  1136. 43:22focus on database caching, replication,
  1137. 43:26sharding maybe, we move our focus from
  1138. 43:28there to CPU and GPUs. So, let me write
  1139. 43:31down the challenge now. So, we can
  1140. 43:33safely say that here our systems are
  1141. 43:36compute-bound or we can say CPU or
  1142. 43:39GPU-bound. Let me take some examples of
  1143. 43:41this. The most common example can be
  1144. 43:44image processing, video rendering. The
  1145. 43:46machine learning model training can also
  1146. 43:48be a good example of this. Also, we can
  1147. 43:51talk about simulation and cryptography.
  1148. 43:54Now, in all these examples, the data
  1149. 43:56which we are getting from the database
  1150. 43:58are in a smaller level only, but the
  1151. 44:01kind of processing we need to do in
  1152. 44:02order to show these data to user is
  1153. 44:05heavy. That's why they come under the
  1154. 44:07category of compute intensive
  1155. 44:09applications. Now, let's discuss about
  1156. 44:11the worries of a computer intensive
  1157. 44:13application. The first worry is pretty
  1158. 44:15simple, that is how fast can we do the
  1159. 44:18calculation or how fast can we compute?
  1160. 44:20The second worry can be can we do
  1161. 44:22something some processes in parallel in
  1162. 44:25order to show the data faster to the
  1163. 44:27user. You may have seen a lot of systems
  1164. 44:29in which in parallel the image is being
  1165. 44:32processed and you are able to move
  1166. 44:34further with the application. Let the
  1167. 44:36image process and the meanwhile you are
  1168. 44:38moving to the other features of the
  1169. 44:40application. The next issue can be how
  1170. 44:42can we reduce the computational cost?
  1171. 44:46And also as a system designer, we can
  1172. 44:48also think about can we use GPU instead
  1173. 44:51of CPU? So, these are the common worries
  1174. 44:54of a computer intensive application
  1175. 44:56where the data is not much, but the
  1176. 44:59processing is the king. Let me also take
  1177. 45:02a good example of a computer intensive
  1178. 45:04application. So, in order to create a
  1179. 45:07simulator machine, it is very important
  1180. 45:09for the simulator to work exactly how
  1181. 45:12the actual machine works. So, think
  1182. 45:15about a scenario that our country is
  1183. 45:17hiring a lot of pilots every year and we
  1184. 45:20can't spend too much on the actual
  1185. 45:23aircrafts when the pilot doesn't know
  1186. 45:25how to ride it or how to fly it.
  1187. 45:28In order to train them, we need to give
  1188. 45:31them simulators machines and on which
  1189. 45:34they train themself first and then sit
  1190. 45:36on the actual plane. So, now in
  1191. 45:38simulation machine, our common worries
  1192. 45:41or the task which we are going to have
  1193. 45:43is can we do anything in parallel again?
  1194. 45:46Can we use GPU instead of CPU? Again,
  1195. 45:49how can we reduce the cost of our
  1196. 45:50simulator? Also, we need to find a way
  1197. 45:53to gather a lot of good algorithms in
  1198. 45:56order to do the task frequently. Now, at
  1199. 45:59this point I think we are very clear on
  1200. 46:01how our data intensive applications are
  1201. 46:03different from computer intensive
  1202. 46:05applications and why we need to put our
  1203. 46:08focus on computation on different
  1204. 46:10algorithms on GPU or CPU in order to
  1205. 46:13make a computer intensive application
  1206. 46:15works because the data is very low and
  1207. 46:17in data intensive application if we
  1208. 46:19start focusing on GPU and CPU there will
  1209. 46:22not be any big changes because the issue
  1210. 46:25lies in the database network calls
  1211. 46:27caching algorithms or maybe server. So
  1212. 46:30in order to fix them we need to put our
  1213. 46:33effort into the right category into the
  1214. 46:35right components so that our application
  1215. 46:38works seamlessly. And why this is
  1216. 46:39important? Because if we put our efforts
  1217. 46:43into the wrong components our components
  1218. 46:45are not free they are going to cost us
  1219. 46:47money and if we put the money in the
  1220. 46:50wrong place and not getting any help in
  1221. 46:52order to solve our issue it is going to
  1222. 46:55be a bad system design for the entire
  1223. 46:57game. Now when we talk about data
  1224. 46:59intensive or computer intensive
  1225. 47:00applications we often think that these
  1226. 47:03two are very separate applications. But
  1227. 47:05in today's time we live in the
  1228. 47:07environment of multiple applications and
  1229. 47:10each application performs multiple
  1230. 47:12tasks. So that means our data
  1231. 47:14intensiveness or the computer
  1232. 47:16intensiveness of the application can
  1233. 47:18also depend on the feature we are
  1234. 47:20working in. For example we have YouTube
  1235. 47:22right? YouTube is data intensive for
  1236. 47:25serving the videos. There are a lot of
  1237. 47:28videos in YouTube environment and you
  1238. 47:30get the videos and we can say that it is
  1239. 47:33computer intensive when it comes to the
  1240. 47:35recommendation. Because in order to get
  1241. 47:38the recommendations we have to get what
  1242. 47:40you are watching right now. So I think
  1243. 47:42it is pretty sorted now that in order to
  1244. 47:44fix a particular issue you need to first
  1245. 47:47think whether the issue which we are
  1246. 47:49facing is computer intensive or data
  1247. 47:52intensive and then you get into the
  1248. 47:54components and fix that. There is a
  1249. 47:56trick to understand whether the feature
  1250. 47:58is data intensive or compute intensive.
  1251. 48:00That is, if the time is lost in data
  1252. 48:03movement, probably it is data intensive
  1253. 48:05application. Or if the time is lost in
  1254. 48:08the computation, it is probably a
  1255. 48:10compute intensive feature or the task.
  1256. 48:13This one distinctions save you a lot of
  1257. 48:15money, time, and complexity. In order to
  1258. 48:19discuss our requirements, we divide the
  1259. 48:20requirements into two categories. One is
  1260. 48:23functional requirement and the another
  1261. 48:25one is non-functional requirement. So,
  1262. 48:27let's go to our screen and let's discuss
  1263. 48:29this. Functional requirement says,
  1264. 48:31"Please define all the points which you
  1265. 48:33are building, all the features which you
  1266. 48:35are building, all the working which you
  1267. 48:37are building." That means, if we define
  1268. 48:40every feature that comes under the
  1269. 48:42functional requirement. So, let's
  1270. 48:44understand functional requirement by
  1271. 48:45taking an example of any e-commerce
  1272. 48:48application, let's say Amazon. So, what
  1273. 48:51can be the functional requirements of
  1274. 48:53Amazon application? That means, we are
  1275. 48:55defining each and every feature which we
  1276. 48:57see on Amazon. So, let's say on Amazon
  1277. 48:59application, a user can register
  1278. 49:02themself. User should also be able to
  1279. 49:04log in. So, let's write that. After
  1280. 49:07login, user should be able to see the
  1281. 49:08products. Also, user can search the
  1282. 49:11products or filter them. Let's write the
  1283. 49:13searching and filtering into two parts.
  1284. 49:15So, search the products and user
  1285. 49:18can also filter the products. After
  1286. 49:20filtering, searching, user should be
  1287. 49:22able to add any product to the cart.
  1288. 49:24After adding any product to the cart,
  1289. 49:26user should be able to get some
  1290. 49:28promotions or get some discounts, right?
  1291. 49:30So, they should be able to apply the
  1292. 49:32coupon codes to get the discount. And
  1293. 49:34once all this done, user should be able
  1294. 49:36to place the order. After placing the
  1295. 49:39order, user should be able to do the
  1296. 49:40payments, maybe. And once the item is
  1297. 49:43placed, once the order is placed, user
  1298. 49:45should be able to track that order. So,
  1299. 49:47all of these points are the functional
  1300. 49:49requirements of Amazon or any e-commerce
  1301. 49:52application for that matter. In which we
  1302. 49:55define all the functionalities, all the
  1303. 49:57actions which user can take, all the
  1304. 49:59working of the application is defined
  1305. 50:01under functional requirement. So, if we
  1306. 50:04have to write this, what is functional
  1307. 50:05requirement again? Functional
  1308. 50:07requirements means all the features
  1309. 50:09which you are going to have in your
  1310. 50:10application are the functional
  1311. 50:12requirements. Now, let's move forward to
  1312. 50:14the non-functional requirements. For
  1313. 50:17non-functional requirements also, let's
  1314. 50:18start with the example of Amazon only.
  1315. 50:21Let's say the first non-functional
  1316. 50:23requirement is that on our Amazon site,
  1317. 50:26we have
  1318. 50:27approximately 1 million users. So, that
  1319. 50:30means our system should be able to
  1320. 50:32handle 1 million daily active users.
  1321. 50:35Now, in order to give our users a good
  1322. 50:37experience, we can say that the response
  1323. 50:40time should be less. But, in
  1324. 50:42non-functional requirements, we have to
  1325. 50:43define how less.
  1326. 50:46So, let's say for Amazon application,
  1327. 50:48the response time should be less than
  1328. 50:51200 milliseconds for each request. Now,
  1329. 50:54our application is built and is running,
  1330. 50:56that means our application can't handle
  1331. 50:58failure. But, that is not possible,
  1332. 51:00right? So, we should define up to how
  1333. 51:03much is our availability of our system.
  1334. 51:05That means for how much time our system
  1335. 51:08will be available. That are defined
  1336. 51:10under SLAs and SLOs. So, we have to
  1337. 51:13define them. That for 1 year, our system
  1338. 51:16downtime should be this much amount
  1339. 51:18only. And for 99.9%
  1340. 51:21time, our system should be up. That is
  1341. 51:23defined under availability. After
  1342. 51:26availability, we don't want our data to
  1343. 51:28be flowing easily, so we want to encrypt
  1344. 51:30our data. And let's say on any sale day,
  1345. 51:34let's say Black Friday sale or Diwali
  1346. 51:36sale or Holi sale, in those times our
  1347. 51:39normal users can go up to 10x. So, our
  1348. 51:42system should be able to handle that
  1349. 51:44much traffic also. So, let's write that
  1350. 51:46down. With this, our system should be
  1351. 51:48also able to handle the faults. Because
  1352. 51:51there are going to be faults in the
  1353. 51:53system. No application is perfect so
  1354. 51:55far. Every system have their own faults,
  1355. 51:58have their own downtime. But we need to
  1356. 52:00see how can we track them and how can we
  1357. 52:03fix them as fast as possible. So, that
  1358. 52:05is we need to make our system fault
  1359. 52:08tolerant. So, with this example of
  1360. 52:10Amazon, we get to know that our
  1361. 52:12functional requirement are the features
  1362. 52:14of the system that we can say what our
  1363. 52:18system is going to do. And with
  1364. 52:20non-functional requirements, we define
  1365. 52:22how our system is going to perform. So,
  1366. 52:25these are the specific non-functional
  1367. 52:27requirements for Amazon. If we have to
  1368. 52:30write it in a generic way, there are
  1369. 52:32some generic terms which we can write.
  1370. 52:34So, we can talk about scalability. Let's
  1371. 52:37say our current configuration of server
  1372. 52:39is reaching to a point where it is not
  1373. 52:41able to handle the kind of traffic it is
  1374. 52:43getting. So, now in order to scale, will
  1375. 52:46you scale it horizontally or vertically?
  1376. 52:49That will be taken care under
  1377. 52:51non-functional requirements. Apart from
  1378. 52:53that, as we have discussed, availability
  1379. 52:55of every system will be defined under
  1380. 52:57the non-functional. As we have already
  1381. 52:58discussed, that means our system uptime
  1382. 53:02will be calculated here.
  1383. 53:04Which is defined under SLAs and SLOs.
  1384. 53:08So, in these agreements, it is already
  1385. 53:10defined that, let's say we are using
  1386. 53:12Amazon server in order to deploy our
  1387. 53:14application, then Amazon will give us
  1388. 53:17some contract that we will be down up to
  1389. 53:19this time. And apart from that, you will
  1390. 53:22not see any issues. Now, in those cases,
  1391. 53:24if we see any issues, let's say for a
  1392. 53:27day, Amazon says that we will be down
  1393. 53:30maybe for 5 minutes maximum. But then,
  1394. 53:32if Amazon fails to keep that promise or
  1395. 53:35that agreement, the companies which are
  1396. 53:37using the Amazon service can sue them.
  1397. 53:40Because the Amazon services have given
  1398. 53:42us the SLA's in writing that the
  1399. 53:45services will not be down after certain
  1400. 53:47point. So, availability plays a huge
  1401. 53:49concern there. Next, we have
  1402. 53:51reliability. In reliability, we can say
  1403. 53:54that our system should be reliable. That
  1404. 53:56means there should be no data loss in
  1405. 53:58our system. After reliability, comes the
  1406. 54:01performance. Under performance, we are
  1407. 54:03going to define how our system is going
  1408. 54:05to work, how much latency can we feel in
  1409. 54:08the system. What is our P95? What is our
  1410. 54:11P90? What is our P99? These all things
  1411. 54:15are defined under performance. So, all
  1412. 54:17the words which I have said in this
  1413. 54:19particular video, we are going to cover
  1414. 54:21each and every topic in details in the
  1415. 54:23coming videos. So, don't worry about
  1416. 54:25this if you don't get it now. After
  1417. 54:27performance, we can jump to security. In
  1418. 54:29security, we can have features like
  1419. 54:31authentication, authorization, and
  1420. 54:34encryption. With security, we are also
  1421. 54:37dealing with a lot of code. That means
  1422. 54:39we can say our code should be in perfect
  1423. 54:41modules so that each code or each chunk
  1424. 54:44of code can use different APIs or
  1425. 54:47different modules in order to work in
  1426. 54:48efficient way. After this, we have last
  1427. 54:51but not the least requirement, that is
  1428. 54:53observability. Even if our code is
  1429. 54:56pushed to production, there are a lot of
  1430. 54:57use cases which are handled after that.
  1431. 55:00Because we don't know the exact behavior
  1432. 55:02of users, that how they are going to use
  1433. 55:05it. So, we need to keep track of all the
  1434. 55:07monitoring tools, all the tools which
  1435. 55:09are going to track the user behavior on
  1436. 55:11our application. So that we can improve
  1437. 55:14our system and also we will be able to
  1438. 55:16track if there are any bugs and solve
  1439. 55:18them. So, let me write monitoring and
  1440. 55:20logging. So, this is all about
  1441. 55:22functional and non-functional
  1442. 55:24requirements. I hope you all get it. But
  1443. 55:27in order to completely get it, we all go
  1444. 55:29through a lot of applications on a
  1445. 55:31day-to-day basis, right? We may be
  1446. 55:32scrolling through Instagram, maybe
  1447. 55:34watching YouTube, maybe watching
  1448. 55:35Netflix, or Amazon Prime, or purchasing
  1449. 55:38anything from Flipkart, Amazon, or any
  1450. 55:40other application e-commerce platform,
  1451. 55:42or we may be scrolling through our
  1452. 55:44WhatsApp messages. Now, I want you,
  1453. 55:47while using your application, just sit
  1454. 55:49back for a few minutes and think about
  1455. 55:51all the functional features which you
  1456. 55:53are working, or all the functional
  1457. 55:55requirements which you are using, and
  1458. 55:58all the non-functional requirements
  1459. 55:59which the app must have configured or
  1460. 56:02thought of while creating that
  1461. 56:03applications. Also, comment down the
  1462. 56:05application on which you worked on, and
  1463. 56:08write down three to four functional and
  1464. 56:10non-functional requirements. That will
  1465. 56:12help you and me also in order to
  1466. 56:14understand that we are learning on a
  1467. 56:16similar pace.
  1468. 56:17How does our browser know which webpage
  1469. 56:20to hit? Do they know by domain name, or
  1470. 56:22do they get these things through IP
  1471. 56:24addresses?
  1472. 56:25Let's find out. So, whenever on our
  1473. 56:28browser or on our machine, whenever we
  1474. 56:31write any address, let's say
  1475. 56:33telesco.com, it is going to redirect us
  1476. 56:36to the telesco page, which is our
  1477. 56:39homepage. Maybe some banners here, maybe
  1478. 56:41some courses section here.
  1479. 56:44It is like this only. Now, how do our
  1480. 56:47browser figure out which page to show?
  1481. 56:50Because, as humans, we communicate
  1482. 56:52through names, but systems don't
  1483. 56:54communicate through names. In order to
  1484. 56:56establish the communication between one
  1485. 56:58system to another system, we need IP
  1486. 57:01addresses. So, that means there should
  1487. 57:03be some place in between or some system
  1488. 57:05in between our browser or our OS through
  1489. 57:09which we are going to get the correct IP
  1490. 57:11addresses of each and every site. So, we
  1491. 57:15will send telesco.com, and in return we
  1492. 57:18will get some IP addresses. So, this can
  1493. 57:20be the IP address of telesco. I'm not
  1494. 57:22exactly sure about the IP address, but
  1495. 57:24yeah. Like this, our DNS works.
  1496. 57:27So, DNS basically stands for domain name
  1497. 57:31system. Domain means any specific URL or
  1498. 57:34any specific domain. Domain is this
  1499. 57:36only, telesco.com. There can be multiple
  1500. 57:39domains, telesco.net, google.com,
  1501. 57:42facebook.com, whatsapp.com, .web I
  1502. 57:44think. But yeah, you get the point.
  1503. 57:46These all things are domain. And what is
  1504. 57:48subdomain? If I write any subdomain,
  1505. 57:51let's say docs.telesco.com
  1506. 57:55or any other subdomain, let's say
  1507. 57:57course. So, they both are subdomains of
  1508. 58:00telesco.com. Where telesco.com acts as
  1509. 58:03an umbrella and under which we can
  1510. 58:05create multiple subdomains if and when
  1511. 58:08required. So, these are the domain and
  1512. 58:10subdomains. But mind it, the machines,
  1513. 58:13the computers doesn't communicate with
  1514. 58:15each other through these domain names.
  1515. 58:17So, they need IP addresses. Let's figure
  1516. 58:20out ways on which our system can handle
  1517. 58:22the IP addresses. Let's say we are
  1518. 58:24building a browser. Why do we need DNS?
  1519. 58:27Why do we need another system
  1520. 58:30for maintaining the domains? We can
  1521. 58:32basically install or store all the
  1522. 58:35domain name list on our browser only.
  1523. 58:37Now, is this even possible? Let's say if
  1524. 58:41we have only two or three websites,
  1525. 58:43maybe 10 websites, we can store their
  1526. 58:45names, even 100 also. But the current
  1527. 58:48number of domains which exist in our
  1528. 58:50world is more than 350 million. And as a
  1529. 58:53browser, browser doesn't know which
  1530. 58:56sites I'm going to use, whether I'm
  1531. 58:57going to use Stack Overflow one day and
  1532. 58:59Gmail the other. So, we can't store all
  1533. 59:02the 350 plus domains into our browser.
  1534. 59:05That will crash our system. So, two
  1535. 59:07reasons because of which we can't store
  1536. 59:09the entire domain list on our browser
  1537. 59:12are first thing is it will get heavier.
  1538. 59:15And to store such amount of information,
  1539. 59:17we need a high storage browser. So, that
  1540. 59:20will also impact the performance of the
  1541. 59:21system. Now, the other point is what if
  1542. 59:25the IP changes after some time? What if,
  1543. 59:28let's say, I am running any site,
  1544. 59:30teleschool.com, I have purchased my
  1545. 59:33domain from maybe GoDaddy, and tomorrow
  1546. 59:35I decide to discontinue my service from
  1547. 59:37GoDaddy, and I'll move to Google
  1548. 59:40Domains. So, I'll purchase or renew the
  1549. 59:42domain from there. In that case, my IP
  1550. 59:46can be changed. And if it restores, I
  1551. 59:48need to reassure that in all the
  1552. 59:50browsers where teleschool.com is
  1553. 59:52running, I need to update them. I need
  1554. 59:55to send them some update. So, through
  1555. 59:57this we can estimate that it can't be
  1556. 59:58stored in one browser. And also, it
  1557. 1:00:01can't be stored in a single server,
  1558. 1:00:03also, because it will also get heavier.
  1559. 1:00:05There are more than 350 million domains.
  1560. 1:00:07That means, if we are going to store it
  1561. 1:00:09in a tabular manner, so the here will be
  1562. 1:00:11the domain name, and the corresponding
  1563. 1:00:14IP addresses accordingly. So, here we
  1564. 1:00:16will have any domain name such as
  1565. 1:00:18google.com, and here we will have the IP
  1566. 1:00:20address, 1.2.3.4,
  1567. 1:00:23for example.
  1568. 1:00:24So, by this, we will create the entire
  1569. 1:00:26list. This is not possible for a single
  1570. 1:00:28machine to handle, because if that
  1571. 1:00:30machine breaks, then the entire system,
  1572. 1:00:33the entire internet will break. Anyone
  1573. 1:00:35in the world will not be able to use any
  1574. 1:00:38internet services or any web pages for
  1575. 1:00:40that matter. No APIs will hit, because
  1576. 1:00:43it all depends on the DNS. So, one thing
  1577. 1:00:46is sure, we need a certain kind of
  1578. 1:00:47system in order to fulfill this
  1579. 1:00:49requirement. That's why we have one of
  1580. 1:00:51the most beautifully designed system,
  1581. 1:00:53DNS. Let's start from the high level.
  1582. 1:00:56So, this is our machine, which is going
  1583. 1:00:58to use some internet service provider,
  1584. 1:01:01and under this service provider, we have
  1585. 1:01:03something called as DNS resolver. So,
  1586. 1:01:05this DNS resolver is responsible for us
  1587. 1:01:08to getting the correct IP of any
  1588. 1:01:11websites or any domain which we are
  1589. 1:01:13trying to process. In order to
  1590. 1:01:15understand this clearly, let me take
  1591. 1:01:16this and put it in a side, so that I can
  1592. 1:01:19draw more things onto our screen. So,
  1593. 1:01:22the first call it is going to make is to
  1594. 1:01:26our root server. Now, there are 13 root
  1595. 1:01:29servers
  1596. 1:01:30in this entire world. That means there
  1597. 1:01:33are 13 companies who own 13 web servers
  1598. 1:01:37in this entire world. They are not
  1599. 1:01:39physically 13, but they are categorized
  1600. 1:01:41into 13. So, that means out of these 13,
  1601. 1:01:44we are going to hit one of them and get
  1602. 1:01:46the response. These root servers are
  1603. 1:01:48typically named from A to M like this.
  1604. 1:01:51Again, from this A to M, they are owned
  1605. 1:01:55from various companies. So, you can see
  1606. 1:01:57the complete list of the root servers
  1607. 1:01:58with the companies which own that root
  1608. 1:02:01servers on the screen. You don't have to
  1609. 1:02:03remember all of them. You just have to
  1610. 1:02:05see and understand that these are the
  1611. 1:02:07service providers of the root servers
  1612. 1:02:09from which we are going to get some
  1613. 1:02:11information in order to process our
  1614. 1:02:13domain. Now, let's come back to screen.
  1615. 1:02:16As discussed, we were searching for
  1616. 1:02:18telesco.com. Also, I can remove this
  1617. 1:02:21now. Now, when we are searching for
  1618. 1:02:23telesco.com, the root server doesn't
  1619. 1:02:25know anything about telesco.com, but it
  1620. 1:02:28knows about the TLDs which are top level
  1621. 1:02:31domains. What is top level domain? If I
  1622. 1:02:34talk about .com,
  1623. 1:02:37this is our top level domain. .com,
  1624. 1:02:40.net, .gov, .in, .uk, .us, .education
  1625. 1:02:46maybe .edu, we use them. These all
  1626. 1:02:49domains are the top level domains of any
  1627. 1:02:51website. So, that means if we need to
  1628. 1:02:53find out where telesco.com is, we first
  1629. 1:02:56need to know which server is handling
  1630. 1:02:58all the .coms. So, in order to do that,
  1631. 1:03:01our root server will give the response
  1632. 1:03:04that okay, you can go to one of the TLDs
  1633. 1:03:07which is handling the top level domain
  1634. 1:03:10for .com and the root server will return
  1635. 1:03:12the IP address of that. Maybe this is
  1636. 1:03:14the IP address and from now, we know
  1637. 1:03:17that we are going to hit any TLD,
  1638. 1:03:20top-level domain for a .com. Now, from
  1639. 1:03:23this TLD, we are again not going to get
  1640. 1:03:26the complete IP address of the site. We
  1641. 1:03:29are going to get the proper server or
  1642. 1:03:32the name server or the authoritative
  1643. 1:03:34servers which are managing the all the
  1644. 1:03:36IP addresses for our domains. Let's say
  1645. 1:03:39this is the IP address of our
  1646. 1:03:41authoritative name server. And from
  1647. 1:03:43this, we are going to make another call
  1648. 1:03:45which are our authoritative name
  1649. 1:03:47servers. And from here, we are going to
  1650. 1:03:50actually get where our telesco.com
  1651. 1:03:53resides, the IP address, which was
  1652. 1:03:5519283841.
  1653. 1:03:57And once this is captured through our
  1654. 1:04:00browser, the IP, then it is going to
  1655. 1:04:03make the call to telesco.com server and
  1656. 1:04:06it is going to get the web page which is
  1657. 1:04:08desired for. I get it this can be a
  1658. 1:04:10little confusing at start, so let me
  1659. 1:04:11reiterate it for once.
  1660. 1:04:13So, whenever we click on any site,
  1661. 1:04:16whenever we want to find any site, let
  1662. 1:04:18it be telesco.com, it is first going to
  1663. 1:04:21our service provider, internet service
  1664. 1:04:24provider. Also, you can see uh
  1665. 1:04:27it can go through router and router can
  1666. 1:04:29also have DNS resolver. So, from
  1667. 1:04:32internet service provider which have DNS
  1668. 1:04:34resolver, this component is going to
  1669. 1:04:37resolve all our queries for us and
  1670. 1:04:39ultimately will give us a perfectly fine
  1671. 1:04:42working IP address for telesco.com or
  1672. 1:04:45any other site for that matter. So, the
  1673. 1:04:48first step is it will go to the root
  1674. 1:04:50servers. What is root servers? There are
  1675. 1:04:5313 companies who own these root servers
  1676. 1:04:57who doesn't know the IP address of your
  1677. 1:04:59site directly, but knows which TLD to
  1678. 1:05:02follow. So, from root server, it is
  1679. 1:05:05going to check what is your
  1680. 1:05:08TLD, top-level domain. And in this case,
  1681. 1:05:11it is .com. Now, as it is dot com it is
  1682. 1:05:14going to get the IP address of the TLD
  1683. 1:05:17which is handling the dot com domain. So
  1684. 1:05:20after this first call now we are going
  1685. 1:05:22to call the TLD which we get from the
  1686. 1:05:24root server and from this also we are
  1687. 1:05:27not going to get the complete IP address
  1688. 1:05:29of telesco. We are going to get on which
  1689. 1:05:32name server we are going to hit to get
  1690. 1:05:34the complete IP address of telesco.com.
  1691. 1:05:37So from here we are going to get the
  1692. 1:05:39authoritative name servers IP and
  1693. 1:05:42accordingly we are going to hit our
  1694. 1:05:44third and final call to our
  1695. 1:05:46authoritative name servers. Our
  1696. 1:05:48authoritative name servers are mostly
  1697. 1:05:51can be configured in GoDaddy or
  1698. 1:05:53Hostinger or any other service providers
  1699. 1:05:56in your current application. And from
  1700. 1:05:58these authoritative name servers we are
  1701. 1:06:01going to get the IP address of
  1702. 1:06:03telesco.com. Now in order to click one
  1703. 1:06:06side we have to do these three
  1704. 1:06:08operations. This can be heavy, right? So
  1705. 1:06:11this is only done first time and the
  1706. 1:06:13next time when you click on anything it
  1707. 1:06:16basically takes the value from cache.
  1708. 1:06:18Now cache can be stored in three
  1709. 1:06:20locations here or more than three if you
  1710. 1:06:22think about it, but three are definite
  1711. 1:06:25locations. The first is the DNS resolver
  1712. 1:06:28can have the cache to have all the
  1713. 1:06:31common IP addresses which we are going
  1714. 1:06:33to take or the all the common domains
  1715. 1:06:35which we are trying to hit. It can store
  1716. 1:06:38it here. Also on our system we have two
  1717. 1:06:41things. One is OS and one is browser. In
  1718. 1:06:44both of these areas we can basically add
  1719. 1:06:46that domain and the IP address in order
  1720. 1:06:49to get the details fast. So browser also
  1721. 1:06:52maintain that cache OS level also we can
  1722. 1:06:55basically maintain the cache and on our
  1723. 1:06:58DNS resolver site also a cache is
  1724. 1:07:00maintained in order to get the details
  1725. 1:07:03fast. Now when we are at this topic let
  1726. 1:07:05me tell you about one more thing, that
  1727. 1:07:07whenever we create domain name systems
  1728. 1:07:09like this dogs or subdomains, these all
  1729. 1:07:13entirely creates a zone
  1730. 1:07:15in the authoritative name server. That
  1731. 1:07:18means telesco.com including all the
  1732. 1:07:21subdomains. So, one zone can contain
  1733. 1:07:23multiple domain list, which will include
  1734. 1:07:26only the domains specifically to that
  1735. 1:07:29particular main domain. Okay? And
  1736. 1:07:32through this it is easier to get to the
  1737. 1:07:34main domain. So, in this case, whenever
  1738. 1:07:37we are hitting the root server, we are
  1739. 1:07:39getting the IP of TLD, again hitting the
  1740. 1:07:41TLD, getting the IP of authoritative
  1741. 1:07:44name server. And in authoritative name
  1742. 1:07:46servers, we can have different zones
  1743. 1:07:49attached to different domains.
  1744. 1:07:52And in this zone, if it is telesco zone,
  1745. 1:07:55in that case, all the domains which are
  1746. 1:07:57associated with telesco.com will be
  1747. 1:07:59here. So, if instead of telesco.com, if
  1748. 1:08:02we are hitting courses.telesco.com,
  1749. 1:08:04and from this, the authoritative name
  1750. 1:08:07server will get that this is our telesco
  1751. 1:08:09zone. And in telesco zone, we will get
  1752. 1:08:12the courses.telesco.com
  1753. 1:08:14IP address also. So, yeah, this is it
  1754. 1:08:17for DNS for now. I hope you understand
  1755. 1:08:19it. If you get it, let me know in the
  1756. 1:08:21comments. If you have any queries also,
  1757. 1:08:23let me know in the comments. Have you
  1758. 1:08:25ever wondered, if you see Zomato, if you
  1759. 1:08:27see Uber, if you see Ola, if you see
  1760. 1:08:30Rapido, and all these applications, also
  1761. 1:08:32Swiggy, they all have an inbuilt map.
  1762. 1:08:36But, did they create their own map in
  1763. 1:08:38their applications, or do they use the
  1764. 1:08:41existing Google Maps in order to do
  1765. 1:08:43that? Now, that is possible only through
  1766. 1:08:46APIs. So, Google Maps present some APIs
  1767. 1:08:50to other applications which they can
  1768. 1:08:52use. Now, in order to understand APIs in
  1769. 1:08:54detail, let's dig into this video and go
  1770. 1:08:57to our screen. If we think about API,
  1771. 1:09:00the API basically stands for application
  1772. 1:09:03programming interface. Now, what does
  1773. 1:09:06this mean? Suppose this is our
  1774. 1:09:08application.
  1775. 1:09:09Any application will work. Let's say
  1776. 1:09:11this is a movie application.
  1777. 1:09:13In which we have all the movies, plus we
  1778. 1:09:16have all the ratings, all the reviews of
  1779. 1:09:19the movie. Like IMDb or something, okay?
  1780. 1:09:22Like Rotten Tomatoes or something. So,
  1781. 1:09:24we are already capturing all the details
  1782. 1:09:26which includes a movie like latest
  1783. 1:09:28movie, latest hits of 2000, 2010, 2020,
  1784. 1:09:32everything. Now, we already have this
  1785. 1:09:34amount of data which is stored in our
  1786. 1:09:36database and our application is already
  1787. 1:09:39working on that. Let's say now in any
  1788. 1:09:43other corner of the world, someone is
  1789. 1:09:45trying to create one application based
  1790. 1:09:47on the data which is already captured in
  1791. 1:09:50this app. Now, they have two options.
  1792. 1:09:53Either they can create their own engine
  1793. 1:09:55like this, their own application where
  1794. 1:09:57they will again curate all the ratings,
  1795. 1:09:59curate all the reviews, and curate all
  1796. 1:10:01the data. But now, where are they going
  1797. 1:10:03to curate this from?
  1798. 1:10:05For this, every application leaves some
  1799. 1:10:08end points which can be used by another
  1800. 1:10:10application. Now, if
  1801. 1:10:13for our application or for this
  1802. 1:10:15developer application, if they want to
  1803. 1:10:17use rating of some movie application,
  1804. 1:10:19they can directly call the APIs of this
  1805. 1:10:22application and through which they will
  1806. 1:10:24get the proper information which is
  1807. 1:10:25required for them. Now, like this, this
  1808. 1:10:28developer or this application don't have
  1809. 1:10:31to create their own application for the
  1810. 1:10:33task which has already been done.
  1811. 1:10:36So, they can use the application data
  1812. 1:10:38directly. But again, can't we directly
  1813. 1:10:41give access to our database? This can be
  1814. 1:10:44dangerous, right? Because then this
  1815. 1:10:46application again have to handle a lot
  1816. 1:10:48of scenarios based on the database
  1817. 1:10:50queries, plus they can also delete some
  1818. 1:10:53important data in this database which
  1819. 1:10:55will affect the other applications which
  1820. 1:10:58are using the same content. Also, there
  1821. 1:11:00are some added functionalities. Let's
  1822. 1:11:02say we are giving an option to write a
  1823. 1:11:03review again. So, in this we have
  1824. 1:11:06already handled that case through some
  1825. 1:11:08or the other API. So, instead of
  1826. 1:11:11creating a new endpoint in this
  1827. 1:11:13application, we can directly access the
  1828. 1:11:16endpoint of the other application, which
  1829. 1:11:18is going to take our review also.
  1830. 1:11:21So, in this case, this application saves
  1831. 1:11:23a lot of time because they are using
  1832. 1:11:26another application, the APIs of another
  1833. 1:11:28application through which they are going
  1834. 1:11:30to do the same work. Now, in this case,
  1835. 1:11:33if this application wants to add some
  1836. 1:11:35data into their database also, they can
  1837. 1:11:37easily do this. So, this is one use case
  1838. 1:11:40of creating an API or using APIs of
  1839. 1:11:42other application. Also, there can be a
  1840. 1:11:45case where you're creating an
  1841. 1:11:47application and in the backend you are
  1842. 1:11:50exposing the APIs which can be used by
  1843. 1:11:53your front end. So, front end can be a
  1844. 1:11:55web application or even a mobile
  1845. 1:11:57application. Can be both also. Let's say
  1846. 1:12:00we are creating some backend and from
  1847. 1:12:02for that application we are also
  1848. 1:12:03creating front end maybe using React or
  1849. 1:12:05something and backend using Swift or
  1850. 1:12:08something. In both of these cases, these
  1851. 1:12:11languages, the React and Swift, both are
  1852. 1:12:14capable of handling APIs. So, they don't
  1853. 1:12:17care about which language did the
  1854. 1:12:19backend use in order to provide them the
  1855. 1:12:22API. They will directly call the APIs
  1856. 1:12:25and use the information which they are
  1857. 1:12:26trying to use. So, this is another
  1858. 1:12:28scenario where there are no two
  1859. 1:12:30applications, there is only single
  1860. 1:12:32application which is divided into front
  1861. 1:12:34end and backend part and using the data
  1862. 1:12:37which backend is providing. Again, in
  1863. 1:12:39this case also, you can think that they
  1864. 1:12:42will use the database directly, but it
  1865. 1:12:44is restricted and prohibited for the
  1866. 1:12:46security of data which we have already
  1867. 1:12:48discussed in the first case. So, with
  1868. 1:12:50this we understand the core
  1869. 1:12:52functionality or usage of the APIs.
  1870. 1:12:54Number one is if you want to give access
  1871. 1:12:57to some other application, and number
  1872. 1:12:59two is if you want to give access of the
  1873. 1:13:02endpoints or the APIs to our front end.
  1874. 1:13:05Now, this both can also happen at the
  1875. 1:13:07same time because it is up to us how do
  1876. 1:13:10we want to expose our APIs. Some
  1877. 1:13:12applications or some backend basically
  1878. 1:13:15restrict the APIs to be used through
  1879. 1:13:17their front end applications only. And
  1880. 1:13:19if you want to make them open, you can
  1881. 1:13:21make them open so that other application
  1882. 1:13:23can also use the same API. Now, let's
  1883. 1:13:25move forward to the type of APIs. The
  1884. 1:13:28first type of API is called as REST API.
  1885. 1:13:31If you are a developer, you already are
  1886. 1:13:33using this. Also, if you are creating
  1887. 1:13:35projects, you might already have created
  1888. 1:13:37them or use them in your application.
  1889. 1:13:40Now, REST basically stands for
  1890. 1:13:43representational state transfer. REST is
  1891. 1:13:46one of those APIs which you will see in
  1892. 1:13:49most of the application nowadays because
  1893. 1:13:51of its easiness, its structure, and its
  1894. 1:13:53ability to easily maintain a large
  1895. 1:13:55amount of data with low volume.
  1896. 1:13:58So, that's why we use REST. Mostly, in
  1897. 1:14:01REST we use JSON,
  1898. 1:14:04which is JavaScript Object Notation,
  1899. 1:14:06which is a way or a format through which
  1900. 1:14:08we will send or retrieve the data. It is
  1901. 1:14:11a easy to use format, that's why we are
  1902. 1:14:13sticking with this for this today's
  1903. 1:14:16video, and we are going to cover this in
  1904. 1:14:17details in a minute. Also, we have some
  1905. 1:14:20other types, that is SOAP. SOAP
  1906. 1:14:24basically stands for Simple Object
  1907. 1:14:26Access Protocol. This is also one way of
  1908. 1:14:28handling the data across applications,
  1909. 1:14:31across different platforms, and SOAP
  1910. 1:14:33basically use XML formats in order to
  1911. 1:14:36transfer the data.
  1912. 1:14:38XMLs are replaced by JSON because XMLs
  1913. 1:14:42were really bulky in nature because in
  1914. 1:14:44order to create every object or every
  1915. 1:14:47array, we have to mention the attributes
  1916. 1:14:50again and again, which is solved by
  1917. 1:14:52JSON. So, that's why we prefer JSON over
  1918. 1:14:56XML any day in today's time. But, the
  1919. 1:14:59SOAP or the XML was famous back then.
  1920. 1:15:02So, that means there are a lot of
  1921. 1:15:04applications which are still using XMLs.
  1922. 1:15:07Why? Because there are legacy
  1923. 1:15:08applications. They are built way earlier
  1924. 1:15:11when JSON was introduced and still are
  1925. 1:15:14using XML. So, in legacy system, if the
  1926. 1:15:17APIs are created and exposed using XML,
  1927. 1:15:21that means
  1928. 1:15:23in order to make our system work, we
  1929. 1:15:25also need to know about XMLs so that we
  1930. 1:15:28can integrate those APIs into our
  1931. 1:15:30system. Also, you can use some
  1932. 1:15:33middleware application where the data
  1933. 1:15:36will be taken in the form of XML and
  1934. 1:15:38will be converted into JSON and your
  1935. 1:15:42application can use that data
  1936. 1:15:43afterwards. So, that's why if you are
  1937. 1:15:46working with a legacy system, it is
  1938. 1:15:47preferred to understand the XMLs also so
  1939. 1:15:50that you can convert them in JSON or you
  1940. 1:15:53can work them without converting them as
  1941. 1:15:55well. So, after SOAP, let's jump to the
  1942. 1:15:57third one, which is GraphQL. So, GraphQL
  1943. 1:16:01is also used widely across many
  1944. 1:16:03applications, many platforms. So, the
  1945. 1:16:05basic thing in GraphQL is GraphQL
  1946. 1:16:08basically says that I'll provide a
  1947. 1:16:11single endpoint, only a single endpoint,
  1948. 1:16:14and based on that, you can send
  1949. 1:16:16whichever query you want. So, in FE,
  1950. 1:16:19from FE side, we will not hit multiple
  1951. 1:16:22endpoints, but we will send a request as
  1952. 1:16:25a query. That's why the GraphQL stands
  1953. 1:16:28for graph query language. So, GraphQL
  1954. 1:16:31also have a query language that is
  1955. 1:16:33similar to SQL. In SQL also, we have
  1956. 1:16:35multiple queries from which we can do
  1957. 1:16:37our things.
  1958. 1:16:38In this also, only one point is exposed
  1959. 1:16:41as I told earlier and using that only
  1960. 1:16:44the front end now can send multiple
  1961. 1:16:47queries and access that particular
  1962. 1:16:49endpoint and get the data which they
  1963. 1:16:51want. So, this is the core concept of
  1964. 1:16:53GraphQL. So, as I said, GraphQL is also
  1965. 1:16:56present in our today's time also. So,
  1966. 1:16:58there can be a chance that you learning
  1967. 1:17:00GraphQL and working on it. The other
  1968. 1:17:03type of API is gRPC. Here, the RPC
  1969. 1:17:07stands for remote procedural calls and
  1970. 1:17:09for G some may say that G stands for
  1971. 1:17:12Google because it is created by Google,
  1972. 1:17:14but it is gRCP
  1973. 1:17:18remote procedural calls only. So,
  1974. 1:17:21instead of using XML and JSON, we have a
  1975. 1:17:23new format here which is called protocol
  1976. 1:17:25buffers. It is very much efficient than
  1977. 1:17:28JSON and XML combined. So, that means
  1978. 1:17:31the size of protocol buffers are usually
  1979. 1:17:33very small than XML or JSON. So, that
  1980. 1:17:36will enable the data to transfer faster.
  1981. 1:17:39So, because of this only Google created
  1982. 1:17:41this and uses this.
  1983. 1:17:43So, this is small in size and faster to
  1984. 1:17:47transfer. In today's applications where
  1985. 1:17:50we have microservices, right? So, in
  1986. 1:17:52microservices, this is our entire
  1987. 1:17:55application and let's say our
  1988. 1:17:57applications is divided into multiple
  1989. 1:17:59applications which is combining the data
  1990. 1:18:03of their endpoints through each other.
  1991. 1:18:05Let's say these are the connections and
  1992. 1:18:08through this they are going to provide
  1993. 1:18:10the actual API to the outer world. Now,
  1994. 1:18:12for this communication between these
  1995. 1:18:14microservices, we often use gRPC. Why?
  1996. 1:18:18Because internally at least the
  1997. 1:18:21communication should be faster to
  1998. 1:18:22provide the relevant information in a
  1999. 1:18:25low latency. This will also help us
  2000. 1:18:28having multiple microservices and having
  2001. 1:18:30various kind of communications between
  2002. 1:18:32them before giving the actual data to
  2003. 1:18:34the other application or the front end.
  2004. 1:18:37Now, let's jump to the last one in our
  2005. 1:18:39list today, which is WebSockets.
  2006. 1:18:42WebSockets are also used in today's time
  2007. 1:18:44and today's application for various
  2008. 1:18:46purposes. For every notifications, if we
  2009. 1:18:49see, WebSockets are used. In order to
  2010. 1:18:51maintain a chatting stream, WebSockets
  2011. 1:18:54are used. Wherever you need some
  2012. 1:18:56real-time connections, WebSockets are
  2013. 1:18:58used. So, its architecture is slightly
  2014. 1:19:01different from the other services. In
  2015. 1:19:03other services, front end or the other
  2016. 1:19:05application will place some request to
  2017. 1:19:08the application and then they will
  2018. 1:19:10provide the response. But, in
  2019. 1:19:12WebSockets, whenever front end creates a
  2020. 1:19:15request, a channel is created. Now, once
  2021. 1:19:18the channel is established, then the
  2022. 1:19:20conversation can be lead through back
  2023. 1:19:22end also. Whereas, in other forms, only
  2024. 1:19:25front end or the other application,
  2025. 1:19:27which is trying to access the
  2026. 1:19:28information, they send the request and
  2027. 1:19:30get the request. But, in this case,
  2028. 1:19:32since the back end also can initiate the
  2029. 1:19:35conversation, that means it can also
  2030. 1:19:37send some or the other information by
  2031. 1:19:40itself. Those information can be the
  2032. 1:19:43chat or the notifications, which is very
  2033. 1:19:46helpful for front end to get. Because,
  2034. 1:19:48if this channel is not there, the front
  2035. 1:19:50end have to constantly ask the back end
  2036. 1:19:53whether there are some new notifications
  2037. 1:19:55or not or whether the chat is there or
  2038. 1:19:57not. But, once the channel is maintained
  2039. 1:19:59through WebSockets, it is easy to
  2040. 1:20:01navigate message from back end to front
  2041. 1:20:04end and front end to back end as well.
  2042. 1:20:06With this also, if you have played
  2043. 1:20:07quizzes on our Teleschool platform only,
  2044. 1:20:10Teleschool channel, in which you may see
  2045. 1:20:12an application which Naveen sir presents
  2046. 1:20:14in order to share the quiz with you all,
  2047. 1:20:16you use that application in order to
  2048. 1:20:18give the responses. Now, those responses
  2049. 1:20:21or the marks are calculated based on the
  2050. 1:20:23response time and the correctness of the
  2051. 1:20:25answer. Now, those connections are also
  2052. 1:20:28maintained through WebSockets. That's
  2053. 1:20:30why you see many notifications or some
  2054. 1:20:32GIF or something something. So, this is
  2055. 1:20:34about web socket and with this I would
  2056. 1:20:36like to conclude this session because it
  2057. 1:20:38is going to be a long session and it is
  2058. 1:20:40better to understand these things in
  2059. 1:20:42parts. So, these are the most used five
  2060. 1:20:46type of APIs which we have. Now, in
  2061. 1:20:49order to understand them briefly in the
  2062. 1:20:51next video, I'm going to talk about
  2063. 1:20:53RESTful APIs in which we will discuss
  2064. 1:20:55the various aspects or the parts of the
  2065. 1:20:58APIs
  2066. 1:21:00and also see what are the
  2067. 1:21:01recommendations and usage of those APIs.
  2068. 1:21:04Now that we already know about RESTful
  2069. 1:21:06APIs and all the other different type of
  2070. 1:21:08APIs, it's time to deep dive into one of
  2071. 1:21:10them. And for today's video, I have
  2072. 1:21:12selected REST APIs to deep dive into.
  2073. 1:21:15Why? Because it is the most used APIs
  2074. 1:21:18across the world. It doesn't matter if
  2075. 1:21:20you are starting a career or you are
  2076. 1:21:22already seasoned, you have to work with
  2077. 1:21:24RESTful APIs for sure. So, let's move to
  2078. 1:21:26the screen now. RESTful APIs are also
  2079. 1:21:29known as REST APIs. As we know, it
  2080. 1:21:31stands for Representational State
  2081. 1:21:34Transfer. What does it mean? It means
  2082. 1:21:36that REST is also one format through
  2083. 1:21:38which we will transfer our data from one
  2084. 1:21:40end to another end. What are these ends?
  2085. 1:21:43We have discussed this already. Either
  2086. 1:21:45we are giving some data from front end
  2087. 1:21:47to back end or vice versa. It is also
  2088. 1:21:49possible that one application needs to
  2089. 1:21:52get some data from some other
  2090. 1:21:53application. So, in that case also, APIs
  2091. 1:21:56are used and REST specifically is used
  2092. 1:21:59to transfer some data from one end to
  2093. 1:22:01another end. So, basically, it is a way
  2094. 1:22:04of how our applications are going to
  2095. 1:22:06communicate with each other. Now, before
  2096. 1:22:08starting, let me tell you that in REST,
  2097. 1:22:10we normally use JSON. JSON is a standard
  2098. 1:22:14way of sharing the data.
  2099. 1:22:16In JSON, we can have array, we can have
  2100. 1:22:19object and with these two combinations,
  2101. 1:22:22we can share our data in the form of key
  2102. 1:22:24and value. So, let me create a simple
  2103. 1:22:26JSON. Let's say this is a start of an
  2104. 1:22:28object and here we have a key, let's say
  2105. 1:22:32name. For value, I'm writing my name.
  2106. 1:22:34With comma, we separate different
  2107. 1:22:36values. So, after comma, I have to write
  2108. 1:22:38some other key. So, let it be age. And
  2109. 1:22:41with age, I can write any numeric value
  2110. 1:22:43as well. So, as we can see, this is
  2111. 1:22:45string value and this is a numeric
  2112. 1:22:47value. There are more values who are
  2113. 1:22:50accepted as a form of value, which is We
  2114. 1:22:53have already seen string and then
  2115. 1:22:55number. We can also use boolean, even a
  2116. 1:22:57nested object, arrays, and a null. Null
  2117. 1:23:01can also be used because if we don't
  2118. 1:23:03have any value for that particular field
  2119. 1:23:04directly, it is easy to say it is a null
  2120. 1:23:07value. And the application which is
  2121. 1:23:09using that value can take care of it.
  2122. 1:23:11So, this is all about the JSON which we
  2123. 1:23:13need to know. We need to know that it is
  2124. 1:23:14in the form of key-value pair and what
  2125. 1:23:17are the values which we can use. Now,
  2126. 1:23:19let's move forward to the next part,
  2127. 1:23:21which is endpoint. In order to
  2128. 1:23:23understand endpoint, we have to get two
  2129. 1:23:25things. One is method and the other
  2130. 1:23:28thing is path. So, in order to access
  2131. 1:23:30any data, we have to have some URL on
  2132. 1:23:34which we will hit with that particular
  2133. 1:23:36method and based on the method selection
  2134. 1:23:38and the path, we are going to either get
  2135. 1:23:41or pass some data to our server. Let me
  2136. 1:23:43start this with an example. Suppose we
  2137. 1:23:45are using HTTP as uh any site, my site
  2138. 1:23:50.com. So, this is our main site and
  2139. 1:23:52after that, we can put a slash and use a
  2140. 1:23:55different URL. Let's say we are
  2141. 1:23:57separating all the APIs in the API
  2142. 1:24:00subdomain. So, APIs is there and then we
  2143. 1:24:03can also segregate the APIs using
  2144. 1:24:06version. So, this is version one and
  2145. 1:24:08here we are going to write resource.
  2146. 1:24:10Now, resource can be anything. If I talk
  2147. 1:24:12about any e-commerce application, it can
  2148. 1:24:14be a product, it can be coupon, can be
  2149. 1:24:17user, can be anything, basically. A
  2150. 1:24:19resource is an entity through which we
  2151. 1:24:21are navigating and getting and passing
  2152. 1:24:23the data to the database. So, this is
  2153. 1:24:25the URL. The URL is called the path
  2154. 1:24:28through which we are going to either get
  2155. 1:24:30or pass some data. And with this, we
  2156. 1:24:33need to have a method. So, for method,
  2157. 1:24:36we can have a get basically. So, get
  2158. 1:24:38method with this path combinedly creates
  2159. 1:24:41an endpoint and through which now we can
  2160. 1:24:43get some data. We expect this path to
  2161. 1:24:46retrieve some data because we are using
  2162. 1:24:49get. Now, there can be multiple ways of
  2163. 1:24:52creating this URL. So, normally, we hide
  2164. 1:24:55the domain part of it. We don't use them
  2165. 1:24:58in the explanations. We only use the
  2166. 1:25:01resource part. Also, we can remove the
  2167. 1:25:03version also. With that, API can also be
  2168. 1:25:05removed in order to understand the
  2169. 1:25:07resource part directly.
  2170. 1:25:09So, from now on, if I create any path,
  2171. 1:25:12let's say users, it is expected to have
  2172. 1:25:15all this domain name API versioning if
  2173. 1:25:18it is there beforehand. So, I'm not
  2174. 1:25:20writing that again and again. Cool. So,
  2175. 1:25:22with that, if I write users here and use
  2176. 1:25:25get method, now I'm expecting some
  2177. 1:25:28response from our server. I'm expecting
  2178. 1:25:31either a list or a response from our
  2179. 1:25:33server. We'll come back to responses in
  2180. 1:25:35a while, but this is how we can get some
  2181. 1:25:37user information from our server. Now,
  2182. 1:25:40let's move to the second part. In this,
  2183. 1:25:42we don't want all the users, we want a
  2184. 1:25:44specific users. So, to find a specific
  2185. 1:25:46user, we have to provide them an ID.
  2186. 1:25:48Normally, we provide them in the URL
  2187. 1:25:50itself. So, it is get users and here we
  2188. 1:25:54can include the ID. So, in real time, it
  2189. 1:25:57can be used as users/1.
  2190. 1:26:00So, if we write one, the user with the
  2191. 1:26:02ID one will be retrieved. Now, with
  2192. 1:26:05this, we can also add any user. So, the
  2193. 1:26:07third part is
  2194. 1:26:09adding a user. So, again, the URL will
  2195. 1:26:12be same users and now we are adding the
  2196. 1:26:14user, so we are using the method post
  2197. 1:26:17here. So, in this with post, we are
  2198. 1:26:20going to give some body, and in body, we
  2199. 1:26:23will have some JSON which will store the
  2200. 1:26:25user information which we are trying to
  2201. 1:26:27create. And based on our request, our
  2202. 1:26:29user will be created in our database.
  2203. 1:26:31Now, once we have all the user details
  2204. 1:26:33in our table, we may choose to update
  2205. 1:26:35any user information, correct? So, for
  2206. 1:26:38that, we use another method which is
  2207. 1:26:41put. Again, the URL will be same, users.
  2208. 1:26:45And with this users, we will send the ID
  2209. 1:26:47of the user of which information we are
  2210. 1:26:50trying to update. So, with put, we also
  2211. 1:26:53have another method which is patch,
  2212. 1:26:55which can also be used in a similar way
  2213. 1:26:57to update the user information. Now,
  2214. 1:27:00what is the difference between put and
  2215. 1:27:02patch? It is simply that if we use put,
  2216. 1:27:05our entire user object will be replaced
  2217. 1:27:08with the new data. So, this is going to
  2218. 1:27:10replace the user data. And if we don't
  2219. 1:27:13want to update all the fields, we want
  2220. 1:27:16to update a specific field or particular
  2221. 1:27:17field, suppose username, name, age, or
  2222. 1:27:20anything, so we can use patch. It can be
  2223. 1:27:23used to partially update the user
  2224. 1:27:25record. Let's understand this with an
  2225. 1:27:27example. Let's say we have any user
  2226. 1:27:30information which have ID and have a
  2227. 1:27:33name and have a username, maybe age.
  2228. 1:27:36This is all. So, these are all the
  2229. 1:27:38values which are
  2230. 1:27:40user currently have. Let's we put some
  2231. 1:27:42values. So, if our user table or user
  2232. 1:27:45entity have these all four values, if we
  2233. 1:27:49use put to update a particular change
  2234. 1:27:51only, let's say I'm going to change my
  2235. 1:27:55username from Akshay to AK something.
  2236. 1:28:00So, now this change will only be done if
  2237. 1:28:02we again send the entire body into the
  2238. 1:28:05put request. If we only send username
  2239. 1:28:09like this, then all the other values of
  2240. 1:28:12our entity will be updated with the
  2241. 1:28:13default value. So, if we send this kind
  2242. 1:28:16of data in a put request, it is going to
  2243. 1:28:18replace our entire entity. So, that
  2244. 1:28:20means the name, the age will be replaced
  2245. 1:28:24by the default or null value. So, it is
  2246. 1:28:26not recommended to use put entirely if
  2247. 1:28:29you want to update your entity
  2248. 1:28:30partially. In those cases, always try to
  2249. 1:28:33use patch. In patch, we're going to
  2250. 1:28:36again update the particular information.
  2251. 1:28:38So, this is perfectly valid for a patch
  2252. 1:28:41request. So, with this, let's move to
  2253. 1:28:43the next part. The next kind of method
  2254. 1:28:46which we use is delete. In delete also,
  2255. 1:28:48normally we pass the ID like this. So,
  2256. 1:28:52if we write users and with {slash} we
  2257. 1:28:55write one, so that means the user with
  2258. 1:28:58the ID one will be deleted. Now, this is
  2259. 1:29:00the usual stuff. You will get this
  2260. 1:29:02information anywhere. Now, let me talk
  2261. 1:29:04about something which is nested data.
  2262. 1:29:07Let's understand nested data with an
  2263. 1:29:08example. Suppose you're creating a blog
  2264. 1:29:11site. So, in blog site, we normally have
  2265. 1:29:14our users who are going to read our
  2266. 1:29:16blogs. Then, we have blog, the actual
  2267. 1:29:18content on the site. And on blog, the
  2268. 1:29:21user can comment. So, we have clearly
  2269. 1:29:23three entities: users, blogs, and then
  2270. 1:29:27comments. So, these three entities are
  2271. 1:29:29interrelated. So, a user is going to
  2272. 1:29:32give comment on a particular blog. Users
  2273. 1:29:36can't comment if there is no blog,
  2274. 1:29:38correct? So, this is the way. Now, in
  2275. 1:29:41order to create APIs for this, we can
  2276. 1:29:44create a users API for all the users'
  2277. 1:29:47activity. So, all the users' activity
  2278. 1:29:49means we can use it for get, put, push,
  2279. 1:29:54delete, and patch also. Now, this is
  2280. 1:29:56simple because a single entity is there,
  2281. 1:29:58users, and all the operations are on
  2282. 1:30:01that users only. Now, let's say we want
  2283. 1:30:04to create an API on which user can add a
  2284. 1:30:09comment. Okay? So, for that, what we can
  2285. 1:30:11do, we can either go from the blogs
  2286. 1:30:14perspective. So, every blog have a
  2287. 1:30:16particular comment section, right? So,
  2288. 1:30:18blogs, then here we can write ID. So,
  2289. 1:30:21this ID is basically belong to the blog,
  2290. 1:30:23and with blog, we can write comments.
  2291. 1:30:26And if we write get here, what it is
  2292. 1:30:28going to do, it is going to give us all
  2293. 1:30:30the comments which are associated with
  2294. 1:30:32the blogs with ID this. Correct? So,
  2295. 1:30:36this is a nested relationship where
  2296. 1:30:38we're trying to access comments, but we
  2297. 1:30:41are going via blogs. We can also do like
  2298. 1:30:44this. Suppose we are on the user's page,
  2299. 1:30:46and we want to see all the comments that
  2300. 1:30:48user have created. So, we can again
  2301. 1:30:50create a get query
  2302. 1:30:52via users, where this is the user ID
  2303. 1:30:55here, and we can see comments. So, we
  2304. 1:30:58are dealing with two different entities
  2305. 1:31:01by creating the relationships between
  2306. 1:31:02them. So, by this, we are going to get
  2307. 1:31:05all the comments which are created by
  2308. 1:31:07the user with this ID. So, we can go to
  2309. 1:31:10comments via blogs, as well, and via
  2310. 1:31:12users, as well. So, this is nested.
  2311. 1:31:15Again, if you want to update any
  2312. 1:31:17comment, delete any comment, we can use
  2313. 1:31:20simply the comments path
  2314. 1:31:22where the user can enter the ID here of
  2315. 1:31:25the comment.
  2316. 1:31:26And they can use it to update or delete
  2317. 1:31:30that particular comment. So, here you
  2318. 1:31:32can also ask me that why are we using
  2319. 1:31:34this kind of nesting in URL? Why are we
  2320. 1:31:36not sending the data in the request? So,
  2321. 1:31:39that is also possible. We can do like
  2322. 1:31:41this. We can
  2323. 1:31:42go to this comments, and we can pass the
  2324. 1:31:45request body, in which we'll pass
  2325. 1:31:48the post ID to get all the comments
  2326. 1:31:50associated with this post. This is also
  2327. 1:31:52possible, but this is not recommended.
  2328. 1:31:54As there is a direct relationship
  2329. 1:31:56between two entities, we don't normally
  2330. 1:31:58go for this body approach. We go for the
  2331. 1:32:01URL approach because it is easy to
  2332. 1:32:04understand also. So, when do we go for
  2333. 1:32:06the body approach? So, the body approach
  2334. 1:32:09is used when we are trying to filter any
  2335. 1:32:12data. So, we understood two concepts
  2336. 1:32:14here. First is the nesting data versus
  2337. 1:32:17filtering. So, when to use which one? If
  2338. 1:32:20you have a clear relationship between
  2339. 1:32:22two entities and it is clean, so you can
  2340. 1:32:25go for nesting. And also in filtering,
  2341. 1:32:28apart from sending the data like this in
  2342. 1:32:31post ID, you can also use
  2343. 1:32:33the URLs like this, comments, and then
  2344. 1:32:36in query parameter, you use post ID.
  2345. 1:32:39Now, this is again not recommended
  2346. 1:32:41because there is a direct relationship
  2347. 1:32:43between comments and post. So, it should
  2348. 1:32:45be a nested type of URL. So, if there
  2349. 1:32:48are clear relationship, we can use
  2350. 1:32:50nesting. And if the relationship is
  2351. 1:32:52complex, we can go for filtering. Also,
  2352. 1:32:56the filtering will be used whenever we
  2353. 1:32:58are trying to do pagination or we are
  2354. 1:33:00trying to filter particular records
  2355. 1:33:03based on multiple ways like we can go
  2356. 1:33:05for any e-commerce page where we are
  2357. 1:33:07seeing different products and we can
  2358. 1:33:09filter our product based on our color,
  2359. 1:33:11price range, and all these things. So,
  2360. 1:33:14those things normally go for query
  2361. 1:33:16parameters as a URL, right? And we don't
  2362. 1:33:19go for nesting there because it will be
  2363. 1:33:21complex to handle a long path with
  2364. 1:33:22nesting data. So, the crux is if the
  2365. 1:33:25relationship is pretty straightforward
  2366. 1:33:27and we will go with respect to the other
  2367. 1:33:29entity, we'll go for nesting kind of
  2368. 1:33:31URLs. And if there are complex
  2369. 1:33:34relationships or the filtering is used,
  2370. 1:33:37we'll go for the query parameters. Now,
  2371. 1:33:41let's move to the next part. How do we
  2372. 1:33:43pass the actual data to the endpoint?
  2373. 1:33:45So, there are three ways. Either we can
  2374. 1:33:47use path, so we can integrate the IDs in
  2375. 1:33:50the path, or we can use some query
  2376. 1:33:53parameter, or we can use body. I have
  2377. 1:33:56given you glimpse of these all three in
  2378. 1:33:58the previous section, but now let's talk
  2379. 1:34:00about this specifically. So for path, it
  2380. 1:34:03is very specific that we will only pass
  2381. 1:34:06the recommended IDs or maybe slugs or
  2382. 1:34:09maybe that particular unique identifier
  2383. 1:34:12only. So this is unique identifier. It
  2384. 1:34:16can be ID or slug. Slug is basically a
  2385. 1:34:19short form of that particular post. If
  2386. 1:34:22you are using any blog site, so let's
  2387. 1:34:24say the blog site the blog is about what
  2388. 1:34:27is Java and in slug you can see
  2389. 1:34:30something like blog /what is Java like
  2390. 1:34:34this. So this is our slug. Again, this
  2391. 1:34:37slug will be unique throughout our
  2392. 1:34:39blogs. So the name of the URL I have
  2393. 1:34:42also written blogs, it is wrong.
  2394. 1:34:44Normally we address the URL in the form
  2395. 1:34:47of plurals. So it is going to access in
  2396. 1:34:50multiple areas, multiple scenarios. So
  2397. 1:34:53normally we don't go for blog, we go for
  2398. 1:34:56blogs like this. Cool. Now for query
  2399. 1:34:59parameters, whenever we are filtering or
  2400. 1:35:02maybe sorting, we can go for query
  2401. 1:35:04parameter. Suppose we are again
  2402. 1:35:06scrolling through our blogs and in blogs
  2403. 1:35:09we can write like sort ASC or we can
  2404. 1:35:13assign some numbers also. So ascending
  2405. 1:35:15means sort ascending maybe by date. So
  2406. 1:35:18whichever date they are created, they
  2407. 1:35:20will be sorted by that. Or maybe we can
  2408. 1:35:22use DESC for decreasing order. So that
  2409. 1:35:26the latest post will be pop up first.
  2410. 1:35:28With this again we can use the query
  2411. 1:35:30parameter for filtering. So maybe we can
  2412. 1:35:33use blogs again here and with that we'll
  2413. 1:35:36pass query parameter. So this is Q for
  2414. 1:35:39maybe query search or search step. So
  2415. 1:35:42anyone with is searching for Java and it
  2416. 1:35:45will give us a list of blogs for Java or
  2417. 1:35:49which have Java in their name or their
  2418. 1:35:51content or their category. So, this is
  2419. 1:35:53all about query parameters. Now,
  2420. 1:35:56whenever we pass any information in
  2421. 1:35:58query parameter and path, those
  2422. 1:36:00informations are going to be exposed,
  2423. 1:36:02right? So, that means we can't pass any
  2424. 1:36:05sensitive information into query
  2425. 1:36:07parameters or path. So, for that we have
  2426. 1:36:10body. So, let's say we have a login
  2427. 1:36:12path, and now we can pass username and
  2428. 1:36:15password in the body, which is not
  2429. 1:36:17exposed at all.
  2430. 1:36:18So, it is safe. So, the rules are pretty
  2431. 1:36:21clear when we use path, query params,
  2432. 1:36:24and body. Now, with this let's see a
  2433. 1:36:25full request in action. The first thing
  2434. 1:36:28is we will have our method. So, this is
  2435. 1:36:30maybe a post method, and we are using
  2436. 1:36:33some API of version three, maybe.
  2437. 1:36:37For version three, we are using users,
  2438. 1:36:40and then we can also pass the metadata.
  2439. 1:36:42Suppose content type we can pass, which
  2440. 1:36:46is application/json
  2441. 1:36:49here, because we are passing the
  2442. 1:36:50information in the form of JSON. We can
  2443. 1:36:52also use XML, or if you have latest
  2444. 1:36:55information, we also have a new format
  2445. 1:36:58in town, which is
  2446. 1:36:59tone. So, we can also use various kind
  2447. 1:37:02of information, but because currently we
  2448. 1:37:04are using JSON, so we can pass it like
  2449. 1:37:06this, and with this we have our body. In
  2450. 1:37:09body, we can pass anything, maybe some
  2451. 1:37:11data we are trying to add, and then
  2452. 1:37:13maybe username. So, like this we can
  2453. 1:37:16pass the entire request or the data to
  2454. 1:37:19our server. So, this is what? This is
  2455. 1:37:21our method. This is the path. This again
  2456. 1:37:25is metadata, and this is our body. Now,
  2457. 1:37:28once we have understood the request
  2458. 1:37:30correctly, it is time to understand the
  2459. 1:37:32responses. So, in order to understand
  2460. 1:37:34the response, first we need to
  2461. 1:37:36understand the response code. So, there
  2462. 1:37:38are specific codes assigned to a
  2463. 1:37:41specific type of response, which if the
  2464. 1:37:43user gives will clearly understand what
  2465. 1:37:45they want to tell us. So, let me start
  2466. 1:37:48with those status codes and then we will
  2467. 1:37:50move forward. So, these are all the
  2468. 1:37:52response codes which we have to know.
  2469. 1:37:54200 stands for okay. So, this means
  2470. 1:37:57everything is working as expected. Don't
  2471. 1:37:59worry about it. Then we have 201. 201 is
  2472. 1:38:03basically used when there is a new
  2473. 1:38:05entity created in the database. So, this
  2474. 1:38:07is created. Then we have 204. 204 is
  2475. 1:38:11again saying that everything is working
  2476. 1:38:13fine, but I don't have anything to
  2477. 1:38:15provide to you. Let's say we are
  2478. 1:38:17deleting any entity, so it is
  2479. 1:38:19successfully deleted, but they don't
  2480. 1:38:21have any response body to give to us.
  2481. 1:38:23So, that means there is no content to
  2482. 1:38:25give to us, but the request is working
  2483. 1:38:28fine. Then we have 301. 301 is basically
  2484. 1:38:32used for redirects and 301 indicates
  2485. 1:38:35that it is a permanent redirect. And if
  2486. 1:38:38the redirect is not permanent, then we
  2487. 1:38:40use 302. That is temporary redirect.
  2488. 1:38:44Then we will come to the 400 series. So,
  2489. 1:38:47all the 400 series basically says that
  2490. 1:38:49there is some issue with the request we
  2491. 1:38:52are having. So, the 400 stands for bad
  2492. 1:38:56request. That means some or the other
  2493. 1:38:58data is not correctly mentioned in the
  2494. 1:39:01body. Either they have some validation
  2495. 1:39:03issue, either either they are missing
  2496. 1:39:05when they are required. Either they have
  2497. 1:39:07some other issues, but request which we
  2498. 1:39:10are getting, the body doesn't have the
  2499. 1:39:13correct information. So, in that case,
  2500. 1:39:15we normally give 400 bad request as a
  2501. 1:39:18response. Then we have 401. 401 is given
  2502. 1:39:22when someone is trying to access some
  2503. 1:39:24information for which they are not
  2504. 1:39:26basically authorized. So, 401 is
  2505. 1:39:30unauthorized. With that we have 403,
  2506. 1:39:33which is used for forbidden. Suppose on
  2507. 1:39:35an LMS, you have not enrolled for a
  2508. 1:39:37course and you want to access a
  2509. 1:39:39particular video or lesson part of that
  2510. 1:39:41course which is not publicly accessible.
  2511. 1:39:44So, in that case, you will receive 403
  2512. 1:39:46forbidden because you are not allowed to
  2513. 1:39:48see that particular resource. With this,
  2514. 1:39:51we have 404 page not found. This is
  2515. 1:39:53basically used whenever the URL is
  2516. 1:39:56incorrect or trying to access some or
  2517. 1:39:58the other resource which is not there
  2518. 1:40:00into our system. And at last, we have
  2519. 1:40:03500. So, 500 is the response which is
  2520. 1:40:07given to a front end when there is
  2521. 1:40:09something wrong with the back end. So,
  2522. 1:40:12this is related to back end. Some or the
  2523. 1:40:14other error is there in the back end.
  2524. 1:40:16Maybe some syntax error, maybe some
  2525. 1:40:18other error, some logic is missing, some
  2526. 1:40:21data is missing, something is wrong with
  2527. 1:40:23back end. So, this is internal server
  2528. 1:40:26error. There are many errors which we
  2529. 1:40:28don't want users to see. So, those all
  2530. 1:40:32are handled by 500 because we don't want
  2531. 1:40:35the user to understand what is the issue
  2532. 1:40:37we are facing with our database
  2533. 1:40:38connection or anything. So, we will
  2534. 1:40:40create logs into our system and through
  2535. 1:40:43which we'll resolve the 500 error
  2536. 1:40:45issues. So, these are all the most
  2537. 1:40:47commonly used response codes which we
  2538. 1:40:49have to understand. So, now whenever we
  2539. 1:40:51are creating any APIs, if the response
  2540. 1:40:54is completely fine, we will provide 200.
  2541. 1:40:57If we are creating any entity, we will
  2542. 1:40:59provide 201. If we are deleting or
  2543. 1:41:02performing some operations where we
  2544. 1:41:04don't have to give any content or any
  2545. 1:41:06data or any body in the response, we
  2546. 1:41:09will use 204. Then, if the permanent
  2547. 1:41:13redirection is there, we will use 301.
  2548. 1:41:15If the redirection is for temporary
  2549. 1:41:17basis, we will use 302. For any bad
  2550. 1:41:21request in the body, we will use 400.
  2551. 1:41:24For authorization, we will use 401.
  2552. 1:41:28For forbidden or the content which
  2553. 1:41:30requires some special access, we will
  2554. 1:41:33give 403. 404 we will give when they are
  2555. 1:41:37trying to access any resource which is
  2556. 1:41:39not there and 500 we will give when
  2557. 1:41:42there is any issue in the internal
  2558. 1:41:44server. So with this, let's move to the
  2559. 1:41:46next part which is response body. So
  2560. 1:41:49normally in response body, what is
  2561. 1:41:51allowed first we start with that. So we
  2562. 1:41:54can return an array, right? Also, we can
  2563. 1:41:57return an object. With this also we can
  2564. 1:42:00return data as nested objects. Now in
  2565. 1:42:04most of the applications whenever you
  2566. 1:42:06are going through any YouTube tutorial
  2567. 1:42:08or any course also sometimes when you
  2568. 1:42:10are dealing with multiple responses,
  2569. 1:42:13let's say the responses for this is
  2570. 1:42:16normally an array, right? But it is not
  2571. 1:42:19recommended to handle the response as
  2572. 1:42:22array. It should always be wrapped in
  2573. 1:42:25the form of an object. And if you want,
  2574. 1:42:27you can use any other key for this users
  2575. 1:42:31and in the value you will give these
  2576. 1:42:34responses. Now these can also be string
  2577. 1:42:38or an object. Normally an object only of
  2578. 1:42:40all the users, but it should be handled
  2579. 1:42:42like this. So this is the correct or I
  2580. 1:42:45would say recommended way. So every
  2581. 1:42:47response body should be an object. And
  2582. 1:42:50even if we're trying to give an array,
  2583. 1:42:53we have to wrap it up into an object and
  2584. 1:42:56give us in the key value form. So this
  2585. 1:42:58is a good practice and through this
  2586. 1:42:59we'll achieve a more long-lasting code.
  2587. 1:43:02Why? Let's say today we are passing the
  2588. 1:43:05user details only and tomorrow we
  2589. 1:43:07understand that okay, we have to add
  2590. 1:43:09more fields. So we don't have to change
  2591. 1:43:12much on the front end side or the
  2592. 1:43:14receiving side, but we will simply
  2593. 1:43:17update our response, change the object
  2594. 1:43:20here
  2595. 1:43:21and maybe add some other field user
  2596. 1:43:24count and pass that information maybe
  2597. 1:43:2729. And with this we can end our object
  2598. 1:43:30here. So we can pass as many
  2599. 1:43:32informations as we want, but if we have
  2600. 1:43:35used a single array
  2601. 1:43:37as a response, then we have to struggle
  2602. 1:43:39with it and we have to change the entire
  2603. 1:43:41architecture of our response. So, it is
  2604. 1:43:44always recommended to pass the response
  2605. 1:43:47always in the form of an object. So, let
  2606. 1:43:49me write returning as array not
  2607. 1:43:52recommended. Let me know in the comments
  2608. 1:43:54if you already knew about this or you
  2609. 1:43:56are hearing this for the first time. So,
  2610. 1:43:58with this, let me create a map of
  2611. 1:44:00request type and responses. So, let's
  2612. 1:44:03say here
  2613. 1:44:04we have request. Here are the responses
  2614. 1:44:07which we are going to receive. So, if
  2615. 1:44:09the request type is get and we are
  2616. 1:44:12trying to access many records, so maybe
  2617. 1:44:14we are trying to access users, it is
  2618. 1:44:18going to give an array of records,
  2619. 1:44:19right? So, it is normally going to give
  2620. 1:44:23200 as a response code and going to give
  2621. 1:44:26an array. Now, we understand that we
  2622. 1:44:28have to not give array directly, so it
  2623. 1:44:30will be wrapped in the form of an
  2624. 1:44:32object. Now, the next request is also a
  2625. 1:44:36get request where we want to access a
  2626. 1:44:39particular object. Let's say one. Now,
  2627. 1:44:43you can think that we will give the user
  2628. 1:44:45information directly, but again, it is
  2629. 1:44:48not recommended to give that directly.
  2630. 1:44:50We will use the nested object for the
  2631. 1:44:52same reason. So, in this object, we will
  2632. 1:44:54use the user as key and pass on the user
  2633. 1:44:58information into this object. So, this
  2634. 1:45:01is the actual user object, but we are
  2635. 1:45:04wrapping it into an another object
  2636. 1:45:06because it will give us flexibility to
  2637. 1:45:09pass some more information if we want
  2638. 1:45:11to. So, let me clear this up for now.
  2639. 1:45:14So, normally, we are going to get 200
  2640. 1:45:16response with this kind of nested
  2641. 1:45:19objects. Also, it is possible that this
  2642. 1:45:22ID is not there in our DB. In that case,
  2643. 1:45:26we can also give 404 response. Now, if
  2644. 1:45:29we are adding anything, we will use post
  2645. 1:45:31with the same URL, and normally it is
  2646. 1:45:34going to give 201 response, which means
  2647. 1:45:38the entity is created. And also, we can
  2648. 1:45:41use 200 response if you want. But, if we
  2649. 1:45:44are using 201, normally we give the ID
  2650. 1:45:48or the new object as response. Now, here
  2651. 1:45:53with post request, we are also giving
  2652. 1:45:55the body, so it can also have 400
  2653. 1:45:58issues, right? 400 bad request issue.
  2654. 1:46:01And also, if the format doesn't sync in,
  2655. 1:46:03it can also give 500 issue. If the data
  2656. 1:46:06is coming properly and there is some
  2657. 1:46:08issue in the back end, it can also give
  2658. 1:46:10500 easily. So, these are most common
  2659. 1:46:13response codes which you can have while
  2660. 1:46:16working with post. Now, let's talk about
  2661. 1:46:18delete. In delete also, we will pass
  2662. 1:46:21this
  2663. 1:46:22URL with some ID, and this can also give
  2664. 1:46:26200 as a response. And also, if it is
  2665. 1:46:29deleted correctly and we don't want to
  2666. 1:46:31give anybody in the response, we can go
  2667. 1:46:34for 204. If this entity is not present,
  2668. 1:46:37maybe the ID they are using is one and
  2669. 1:46:40we don't have any record with the ID
  2670. 1:46:41one, we can have 404 or we can have 500
  2671. 1:46:46if there are any dependencies
  2672. 1:46:48and which is not handled correctly by
  2673. 1:46:50our back end. With this, you can also
  2674. 1:46:52see 401 here if you are unau- prized
  2675. 1:46:55person to delete that particular record
  2676. 1:46:57or you are not allowed to delete that
  2677. 1:46:59particular entity, you can also get 401.
  2678. 1:47:02So, these are the most common request
  2679. 1:47:04responses, and with that we have also
  2680. 1:47:07discussed what can be there in the
  2681. 1:47:08response body. So, with this we have
  2682. 1:47:10covered the entire request section and
  2683. 1:47:13the response part of the REST APIs, and
  2684. 1:47:16I hope the information which you get
  2685. 1:47:18from this video is going to help you in
  2686. 1:47:20your entire development career. Whenever
  2687. 1:47:22you are dealing with REST APIs. In order
  2688. 1:47:25to build any application, we have client
  2689. 1:47:27on this side, right? Who is going to
  2690. 1:47:30interact with either a web application
  2691. 1:47:32or a mobile device to interact with our
  2692. 1:47:35application. Now, with this we will have
  2693. 1:47:38our server in this side. So, this is
  2694. 1:47:40maybe our server. And if we keep the
  2695. 1:47:42model like this only, that means every
  2696. 1:47:46time we restart our server, all the data
  2697. 1:47:49which user is trying to access will be
  2698. 1:47:51lost. Because when server restarts, it
  2699. 1:47:54clears all the data. But, the thing is
  2700. 1:47:57we need to store that data somewhere,
  2701. 1:47:59right? So, for that we use database in
  2702. 1:48:02order to store the information which the
  2703. 1:48:04user may require next time when they log
  2704. 1:48:07in or see our application. Now, one
  2705. 1:48:09thing is very clear that most of the
  2706. 1:48:11application can't live without
  2707. 1:48:13databases. Now, in order to understand
  2708. 1:48:16how do we store data, we have two
  2709. 1:48:18approaches. First is the SQL approach,
  2710. 1:48:22and the other one is no SQL approach.
  2711. 1:48:25So, SQL basically consists of RDBMS,
  2712. 1:48:29which is relational database model, in
  2713. 1:48:31which we will have tables, rows,
  2714. 1:48:33columns, and we will connect those
  2715. 1:48:35tables based on different joins and
  2716. 1:48:38everything. And in no SQL, we have other
  2717. 1:48:41methods. See, saying SQL and no SQL is
  2718. 1:48:44like saying Java and no Java. That means
  2719. 1:48:47there are various options in no SQL. We
  2720. 1:48:49can have our data as in the form of key
  2721. 1:48:52value. Also, we can have column. We can
  2722. 1:48:55also have the data in the form of nodes,
  2723. 1:48:57maybe. And we can store them in the form
  2724. 1:49:00of graphs. Also, we can store the data
  2725. 1:49:03in the form of document. And here, if we
  2726. 1:49:05talk about SQL, we have our tables in
  2727. 1:49:09place, who is going to hold our data in
  2728. 1:49:11the form of entities. And there will be
  2729. 1:49:13different relationships between
  2730. 1:49:15different tables. Also, in order to do
  2731. 1:49:18in SQL manner, we have various softwares
  2732. 1:49:20like post dress or we can go for MySQL.
  2733. 1:49:24Right? Like this, we can have multiple
  2734. 1:49:26options here. For here also, we have
  2735. 1:49:28multiple options like Cassandra or we
  2736. 1:49:31can go for MongoDB. And based on this
  2737. 1:49:34selection, where we are going to do it
  2738. 1:49:36SQL way or no SQL way, we are going to
  2739. 1:49:38build the entire foundation of our work.
  2740. 1:49:41Like how our data, our important data is
  2741. 1:49:44going to store in our database. So, in
  2742. 1:49:47today's video, let's focus on SQL. And
  2743. 1:49:50in next video, we will focus the ways of
  2744. 1:49:52no SQL. So, let's start. So, SQL
  2745. 1:49:56basically stands for structured query
  2746. 1:49:58language. And SQL comes under the
  2747. 1:50:01category of relational DB. Now, in
  2748. 1:50:03relational DB, we are going to have
  2749. 1:50:05multiple entities and we are going to
  2750. 1:50:07establish different type of
  2751. 1:50:08relationships between those entities, if
  2752. 1:50:10required. So, we are going to store our
  2753. 1:50:13data in the form of tables.
  2754. 1:50:16So, we can loosely say that every table
  2755. 1:50:19is loosely an entity.
  2756. 1:50:22It can either be a user entity or it can
  2757. 1:50:25be comments or it can be post, if we are
  2758. 1:50:28building anything like blog site or
  2759. 1:50:30something or it can be videos, if we are
  2760. 1:50:32building YouTube. So, like this, we
  2761. 1:50:34segregate our different entities and
  2762. 1:50:37then we will build the relationship
  2763. 1:50:38between them. Now, if I draw one of
  2764. 1:50:40those tables, let's say we are building
  2765. 1:50:42a user table. A user table can have
  2766. 1:50:45multiple fields. That is equivalent to a
  2767. 1:50:48user entity can have multiple
  2768. 1:50:49properties. For example, a user can have
  2769. 1:50:52their unique ID. A user can also have
  2770. 1:50:55their first name
  2771. 1:50:57and L name and maybe we also have their
  2772. 1:51:00phone number. So, this is how a table is
  2773. 1:51:02formed, where we have multiple
  2774. 1:51:04attributes of a table. These attributes
  2775. 1:51:07are stored in columns and when we have a
  2776. 1:51:10complete record, let's say in this
  2777. 1:51:12table, we can add a new record where I
  2778. 1:51:15can add any record, let's say the ID is
  2779. 1:51:18one, the name is Akshay, last name is
  2780. 1:51:21Agarwal, and phone number is, let's say,
  2781. 1:51:24987654
  2782. 1:51:27321. So, this is my number, this is a
  2783. 1:51:30complete record. Now, this record is
  2784. 1:51:33called as a row.
  2785. 1:51:35So, by getting any row from our table,
  2786. 1:51:37we can get the entire information of
  2787. 1:51:39that particular entity or of that
  2788. 1:51:42particular user. Normally, for the name
  2789. 1:51:44of entities, we don't go for singular
  2790. 1:51:46words because it will contain multiple
  2791. 1:51:48records. So, we will go for plural
  2792. 1:51:51things, users. So, this is the standard,
  2793. 1:51:54this is a constraint, this is not a
  2794. 1:51:56particular rule, but it is safer to go
  2795. 1:51:58into plural things. So, for comments, we
  2796. 1:52:02can go for comments, for post, we will
  2797. 1:52:04go for posts, for videos, also, we will
  2798. 1:52:06go for videos. So, all these things are
  2799. 1:52:09in plural. Why? Because a single table
  2800. 1:52:12will contain multiple records. It will
  2801. 1:52:14be easier for us to fetch the data with
  2802. 1:52:17that plural name because then we will
  2803. 1:52:19know that multiple entries are there and
  2804. 1:52:21it is a convention to use plural things.
  2805. 1:52:23So, with this, let's move to our next
  2806. 1:52:25topic, which is constraints. If we are
  2807. 1:52:27building any application, we can have
  2808. 1:52:29multiple users who are coming with
  2809. 1:52:31different backgrounds, different
  2810. 1:52:32geography, and everything. So, it is our
  2811. 1:52:35responsibility to make them enter the
  2812. 1:52:38data correctly. We will use validations
  2813. 1:52:40in form, and in database, we can handle
  2814. 1:52:43them with the use of constraints.
  2815. 1:52:45Because every wrong data and every
  2816. 1:52:46faulty data is of no use. So, for that,
  2817. 1:52:49let's dive into the constraints. So, the
  2818. 1:52:51first constraint is unique. Unique
  2819. 1:52:54basically stands for a unique value
  2820. 1:52:57throughout the table. For example, in
  2821. 1:53:00the user field, let's create the user
  2822. 1:53:02table again. So, this was our user
  2823. 1:53:04table, let's add one more column or one
  2824. 1:53:07more attribute, which is username. Now,
  2825. 1:53:09we don't want two users to share the
  2826. 1:53:11same username. This is a common thing
  2827. 1:53:13which we can see on Instagram also. If
  2828. 1:53:16you're trying to create a new username
  2829. 1:53:18and if that username is already taken,
  2830. 1:53:20you may get some suggestions with some
  2831. 1:53:22extra characters or you can change the
  2832. 1:53:24name entirely. Similarly, for Gmail
  2833. 1:53:26also, if the username is taken, they
  2834. 1:53:28suggest you some more usernames with the
  2835. 1:53:30extra characters. So, that means we can
  2836. 1:53:32say that the username is unique. Every
  2837. 1:53:35user will have their unique usernames.
  2838. 1:53:37So, in order to maintain that username
  2839. 1:53:39uniqueness, what we will do, either
  2840. 1:53:41while creating the table or while adding
  2841. 1:53:44the username field like we did, what we
  2842. 1:53:46do, we add the constraint unique to that
  2843. 1:53:49particular username. So, that when any
  2844. 1:53:52user comes to this entry and if they
  2845. 1:53:55write ABC, no other user can write ABC.
  2846. 1:53:59If they want to use the same username
  2847. 1:54:01again, they will not be added into our
  2848. 1:54:03databases. Why? Because this username
  2849. 1:54:06field is unique. The next constraint is
  2850. 1:54:10not null. So, not null basically
  2851. 1:54:11represent a value which can't be null.
  2852. 1:54:14Null is the default value for any
  2853. 1:54:16entity, right? So, whenever we want a
  2854. 1:54:19field to have a particular value either
  2855. 1:54:21given by a system or server or given by
  2856. 1:54:25our user, we want to have that value.
  2857. 1:54:27So, we can't have null in that. If we
  2858. 1:54:30see in users table, the first name can
  2859. 1:54:33easily come under the not null category.
  2860. 1:54:35So, we don't want our first name to be
  2861. 1:54:37null because we are going to address our
  2862. 1:54:39user with their first name. So, this is
  2863. 1:54:41a required field. The next constraint is
  2864. 1:54:43primary key. So, our primary key acts as
  2865. 1:54:46a unique identifier for that particular
  2866. 1:54:48entity. In our whole user table, there
  2867. 1:54:51is no record which can share that
  2868. 1:54:53particular value. So, if we find any
  2869. 1:54:56record with ID, let's say ID is equal to
  2870. 1:54:58three, so only one record will come out
  2871. 1:55:01of this. Okay? It can't give two records
  2872. 1:55:04because the ID cannot be shared between
  2873. 1:55:06two records. So, it will give a unique
  2874. 1:55:09record out of our users table. So, our
  2875. 1:55:11primary key basically helps us to track
  2876. 1:55:13down to that particular record. And
  2877. 1:55:15also, it helps us when we deal with
  2878. 1:55:18multiple tables because there we might
  2879. 1:55:21need some ID to put into some other
  2880. 1:55:23table as a foreign key. So, in that
  2881. 1:55:25concept also, primary key is used. With
  2882. 1:55:28this, we can have a fourth option here,
  2883. 1:55:31which is check. Check is basically used
  2884. 1:55:33in order to maintain the authenticity of
  2885. 1:55:36any record. Let's say we are using a
  2886. 1:55:38password. So, while creating a password
  2887. 1:55:41or while having a password, we might
  2888. 1:55:43want our user to use certain constraints
  2889. 1:55:46like the length should be greater than
  2890. 1:55:48eight or maybe it should have any
  2891. 1:55:51numeric values or it can have one or two
  2892. 1:55:54special characters also. So, all these
  2893. 1:55:56rules which we apply are applied in the
  2894. 1:55:59DB for that particular column through
  2895. 1:56:02the check constraint. Also, the other
  2896. 1:56:04example of this can be a mobile number
  2897. 1:56:07or a phone number. So, a phone number
  2898. 1:56:10will only contain numeric values. It
  2899. 1:56:12can't have any characters apart from
  2900. 1:56:14that. So, this is also a check which we
  2901. 1:56:17can apply for our phone number field.
  2902. 1:56:20Also, the checks can be applied to any
  2903. 1:56:22field. Let's say for first name, we
  2904. 1:56:24can't have any name whose length is less
  2905. 1:56:27than maybe two because A B C D, this
  2906. 1:56:30can't be stored as a name. The shortest
  2907. 1:56:32name I know is Om, right? It is also two
  2908. 1:56:35characters long. So, we want to have
  2909. 1:56:38that check. So, all the checks are
  2910. 1:56:39covered with the check constraint. Now,
  2911. 1:56:42with this,
  2912. 1:56:43let's move to the other constraint,
  2913. 1:56:45which is foreign key. So, let's
  2914. 1:56:47understand foreign key through an
  2915. 1:56:48example. Let's say we are creating a
  2916. 1:56:50blog site where we have users table
  2917. 1:56:52again. So, the users table is basically
  2918. 1:56:55the authors table. Or let me rename it
  2919. 1:56:58to author then.
  2920. 1:56:59So this contains the list of all the
  2921. 1:57:01authors who are going to publish some or
  2922. 1:57:03the other blogs. So there we have this
  2923. 1:57:06ID and then from this ID, let's say we
  2924. 1:57:09only have two fields, first name
  2925. 1:57:11and last name. We can have multiple
  2926. 1:57:14fields if you want. For the sake of this
  2927. 1:57:16example, I'm creating this smaller. So
  2928. 1:57:19let's say we have one and the author is
  2929. 1:57:21Akshat Agarwal.
  2930. 1:57:23Let's say I have two authors. Okay. Now
  2931. 1:57:26these two authors are not going to write
  2932. 1:57:29only single post. They are going to
  2933. 1:57:31write multiple posts. So we will have to
  2934. 1:57:33create a separate table for posts and in
  2935. 1:57:36that post we will have some ID. I'll
  2936. 1:57:39have data or maybe content is the better
  2937. 1:57:42word. So I'll change the name data to
  2938. 1:57:44content. And that content is provided by
  2939. 1:57:48some or the other author. So here we
  2940. 1:57:50will have author ID also. So now let's
  2941. 1:57:52say our post, first post, have some data
  2942. 1:57:55about HTML and who wrote that post?
  2943. 1:57:59Maybe Akshat. So for Akshat, I'm not
  2944. 1:58:01going to write the entire details about
  2945. 1:58:04the author again into this table. So for
  2946. 1:58:06that, what I will use, I'll use the
  2947. 1:58:08common identifier which is our ID. So
  2948. 1:58:11our ID is our primary key here and which
  2949. 1:58:14will be used as the foreign key here. So
  2950. 1:58:16this is our foreign key and we will
  2951. 1:58:18simply write one. With this particular
  2952. 1:58:20information, we are going to get that
  2953. 1:58:22okay, this author ID is one that means
  2954. 1:58:25it is written by Akshat. Similarly, if
  2955. 1:58:28we have multiple fields like this, let's
  2956. 1:58:30say post two is on AI maybe and it is
  2957. 1:58:34created by Gaurav.
  2958. 1:58:36So we don't have to write the entire
  2959. 1:58:38fields or entire information about
  2960. 1:58:40Gaurav. Only the primary key will do.
  2961. 1:58:44So from this two, we are going to again
  2962. 1:58:46recognize that it is written by our
  2963. 1:58:48second author who is Gaurav. So this is
  2964. 1:58:50the role of our foreign key to establish
  2965. 1:58:53the connections between two entities.
  2966. 1:58:55With this, we also have a next
  2967. 1:58:58constraint, which is default. So,
  2968. 1:59:00default basically stands for the area or
  2969. 1:59:02the field where we can't have null. So,
  2970. 1:59:05null is not an option. And we don't want
  2971. 1:59:08our user also to have that entry as a
  2972. 1:59:10mandatory field. We can fill in for
  2973. 1:59:13them. Let's say we are building anything
  2974. 1:59:15for subscription. And if user have not
  2975. 1:59:18paid anything to get a subscription,
  2976. 1:59:20they might be having free as the value
  2977. 1:59:23because they are currently using our
  2978. 1:59:25free subscription. So, this is one case.
  2979. 1:59:27The other case can be, let's say you are
  2980. 1:59:30into our learn.telesco.com, right? So,
  2981. 1:59:33this is our LMS and we are creating
  2982. 1:59:34them. So, in this, every new user or the
  2983. 1:59:38more number of users are going to be
  2984. 1:59:40students only. And only few users are
  2985. 1:59:43going to be managers or trainers or
  2986. 1:59:45maybe admin. So, those are few
  2987. 1:59:48categories and most of them are going to
  2988. 1:59:51be students. So, the default value of
  2989. 1:59:54role for every user here is student.
  2990. 1:59:58Student/learner.
  2991. 2:00:00So, either student or learner, we can
  2992. 2:00:02have as a default role. And with time,
  2993. 2:00:04let's say one or the other student or
  2994. 2:00:06learner got hired into Telesco only to
  2995. 2:00:08maintain the notes or maybe to teach,
  2996. 2:00:11then we will update their role to
  2997. 2:00:13trainer. This is how the system works
  2998. 2:00:16with default. So, these are all the
  2999. 2:00:18constraints which we have in our
  3000. 2:00:19databases. Now comes the next part of
  3001. 2:00:22our relational databases. One of the
  3002. 2:00:24most important part is join. Join is
  3003. 2:00:27basically used to associate two tables.
  3004. 2:00:30That means to manage relationships
  3005. 2:00:32between multiple tables. And why do we
  3006. 2:00:34do that? To basically maintain
  3007. 2:00:36relationships between different
  3008. 2:00:37entities. So, let's see how many joins
  3009. 2:00:40options do we have. The first and the
  3010. 2:00:42most common join option is one to many.
  3011. 2:00:46So, we have a users table. We have ID
  3012. 2:00:49here, and we have F name only.
  3013. 2:00:52Again, I'm going to use the same entry,
  3014. 2:00:54one and two. And here, I can write any
  3015. 2:00:56name. So, here are the two names, Akshay
  3016. 2:00:59and Gaurav. Two entries basically for
  3017. 2:01:01this users table. And again, we are
  3018. 2:01:03creating a blog site. So, that means
  3019. 2:01:05both Akshay can write multiple blogs,
  3020. 2:01:08and Gaurav can also write multiple
  3021. 2:01:10blogs. So, we will create another table
  3022. 2:01:13to maintain our blogs, which will have
  3023. 2:01:16ID, the content, and the author ID. So,
  3024. 2:01:19maybe the first blog, which is on HTML
  3025. 2:01:23again, is written by Akshay. The second
  3026. 2:01:25blog is on AI, which is written by
  3027. 2:01:28Gaurav. And then, there is third blog,
  3028. 2:01:31which is on system design, which is
  3029. 2:01:33created by Akshay. So, here we have one
  3030. 2:01:36to many relationships.
  3031. 2:01:38Because for a single record here, we
  3032. 2:01:40have multiple options or multiple rows
  3033. 2:01:43associated with that particular row. So,
  3034. 2:01:45this is one to many relationship. With
  3035. 2:01:47this, if we reverse this, we can also
  3036. 2:01:49have many to one relationship. So, what
  3037. 2:01:52we will do? Here, we were talking about
  3038. 2:01:54users to blogs relationship. And here,
  3039. 2:01:57we are talking about blogs relationship
  3040. 2:01:59with users. So, here we can safely say
  3041. 2:02:02that multiple blogs are associated with
  3042. 2:02:05the same user. So, here we can have
  3043. 2:02:07multiple blogs associated with the
  3044. 2:02:10single user. So, this is many to one
  3045. 2:02:12relationship. Now, let's move forward
  3046. 2:02:15for our third relationship, which is
  3047. 2:02:17many to many. So, in order to understand
  3048. 2:02:19many to many, let's again go to our LMS.
  3049. 2:02:22So, in LMS, we can have students.
  3050. 2:02:25And we have courses here, right?
  3051. 2:02:27Students also, we have ID maybe, and
  3052. 2:02:30only F name is here right now.
  3053. 2:02:33And in course, we have ID, and then we
  3054. 2:02:36can have the course name here. Now,
  3055. 2:02:38let's fill in some entries here. So,
  3056. 2:02:41here we have students as Akshaya and
  3057. 2:02:43Gaurav. And our courses are Master Java,
  3058. 2:02:45Master AI. Now, Akshaya as a student can
  3059. 2:02:49enroll into multiple courses. So, we can
  3060. 2:02:51sense that from students, we can have
  3061. 2:02:54multiple relationship for courses. And
  3062. 2:02:58also, one course can have multiple
  3063. 2:03:01students. And it is also possible that
  3064. 2:03:03in Master Java, we have both Akshaya and
  3065. 2:03:06Gaurav. So, that means our students
  3066. 2:03:09can enroll into same course. So,
  3067. 2:03:12whenever we see a situation like this
  3068. 2:03:15where we can have many students or many
  3069. 2:03:17records from single table associated
  3070. 2:03:19with the single record of another table,
  3071. 2:03:22and vice versa is also true, we are
  3072. 2:03:24going to get a many-to-many
  3073. 2:03:26relationship. So, in order to maintain
  3074. 2:03:27multiple relationships, we have to have
  3075. 2:03:30a
  3076. 2:03:31junction table in between. So, this is
  3077. 2:03:33our students-courses
  3078. 2:03:35table. And here, we will have a unique
  3079. 2:03:38ID of this particular record. Maybe this
  3080. 2:03:41is required, maybe this is not, based on
  3081. 2:03:44your queries and use cases. Here, we
  3082. 2:03:47will have students ID. And here, we will
  3083. 2:03:50have course ID. And now, while entering
  3084. 2:03:52the data, we can simply say the record
  3085. 2:03:54one, we have Akshaya enrolled in Master
  3086. 2:03:57Java.
  3087. 2:03:58Now, record number two, we have Akshaya
  3088. 2:04:01again enrolled in Master AI. Then, for
  3089. 2:04:04the third record, we have Gaurav
  3090. 2:04:06enrolled in Master Java. And the fourth
  3091. 2:04:08record, Gaurav enrolled in Master AI as
  3092. 2:04:11well. So, with this junction table, we
  3093. 2:04:13are going to figure out how many
  3094. 2:04:15students are enrolled in how many
  3095. 2:04:17courses, and how many courses have how
  3096. 2:04:21many students. I hope this makes sense.
  3097. 2:04:23This is a little confusing if you are
  3098. 2:04:25starting, but this is how we maintain
  3099. 2:04:27many-to-many relationships. With this,
  3100. 2:04:29we can have one more relationship, which
  3101. 2:04:31is one-to-one. Suppose we are building a
  3102. 2:04:35platform like YouTube or blog site which
  3103. 2:04:38also have podcast, which also have maybe
  3104. 2:04:42video section. So, we have three kind of
  3105. 2:04:45content, right? The first content is
  3106. 2:04:47text for blogs. The second content is
  3107. 2:04:51audio, and the third kind of content can
  3108. 2:04:53be video.
  3109. 2:04:54Now, the processing of all these
  3110. 2:04:56contents are different. We can handle
  3111. 2:04:58these multiple options by two ways.
  3112. 2:05:01Either create a common table
  3113. 2:05:04named as contents. So, in content now we
  3114. 2:05:07have three types. For video, we are
  3115. 2:05:09going to store MP4. For audio, we're
  3116. 2:05:12going to store MP3 maybe. And for text,
  3117. 2:05:15we're going to use the text which is
  3118. 2:05:16there in database. We can use varchar,
  3119. 2:05:19long text, short text, maybe text. Now,
  3120. 2:05:21there are three kind of content. We can
  3121. 2:05:23basically divide them by using a field
  3122. 2:05:26which is type. So, in type we will have
  3123. 2:05:29options like text, audio, or video. So,
  3124. 2:05:32here the first record can be on again
  3125. 2:05:35HTML, which is a blog type text type.
  3126. 2:05:38So, we will type one here or text
  3127. 2:05:40directly here. For the next content, it
  3128. 2:05:42can be anything. Let's say the content
  3129. 2:05:45is on improve focus, and in order to
  3130. 2:05:47make that content, we don't have a
  3131. 2:05:49video. So, we have something like audio
  3132. 2:05:52here. And for the third thing, we have
  3133. 2:05:54any video, let's say
  3134. 2:05:56system design.
  3135. 2:05:58And for this video, we have the type as
  3136. 2:06:00video. Now, if you have worked into
  3137. 2:06:03systems with these type of content, the
  3138. 2:06:05processing of each type is very
  3139. 2:06:07different. And if we curate all these
  3140. 2:06:09data into a single table, it is going to
  3141. 2:06:12be a really heavy one. Now, what are the
  3142. 2:06:14drawbacks of having a heavy table? That
  3143. 2:06:16is, whenever we are trying to filter, we
  3144. 2:06:19have to basically filter through each
  3145. 2:06:21and every content type and each and
  3146. 2:06:23every record as well. So, instead of
  3147. 2:06:26having all these kind of data into
  3148. 2:06:28single table, what we can do is we can
  3149. 2:06:30segregate the data already into multiple
  3150. 2:06:33tables and have those IDs as a foreign
  3151. 2:06:35key.
  3152. 2:06:36So, the other approach is we will create
  3153. 2:06:39a simple contents table where we will
  3154. 2:06:42have ID here again and maybe the content
  3155. 2:06:46name.
  3156. 2:06:47And then we will not have the entire
  3157. 2:06:49video or audio. What we will have, we
  3158. 2:06:52will have the ID of that particular
  3159. 2:06:54content. So, that means we are going to
  3160. 2:06:55create multiple tables, one for videos
  3161. 2:06:59and one for audios and one for maybe
  3162. 2:07:02blogs. They all will have a common ID.
  3163. 2:07:06With common ID, I mean their primary key
  3164. 2:07:09and we will use the ID from videos, from
  3165. 2:07:11audios and from blogs into this
  3166. 2:07:14particular table. So, we will not have
  3167. 2:07:16all the data here. Here we will only
  3168. 2:07:19have content ID maybe. And with this, we
  3169. 2:07:22can also have content type. So, this
  3170. 2:07:24type will determine where we need to
  3171. 2:07:26follow. So, this content table will only
  3172. 2:07:29contain all the fields which user can
  3173. 2:07:31search. They can search for name, they
  3174. 2:07:33can search for type, they can search for
  3175. 2:07:35maybe some keywords, some slugs. So, all
  3176. 2:07:38these details will be there in our
  3177. 2:07:40content field. And the actual data, the
  3178. 2:07:43actual content will not be in the
  3179. 2:07:44contents field but in their respective
  3180. 2:07:47tables. So, all the blogs will contain
  3181. 2:07:50all the blogs, all the audios will
  3182. 2:07:52contain all the audio files, all the
  3183. 2:07:54videos will contain all the video files.
  3184. 2:07:56So, now if we have one and type as
  3185. 2:08:00video, we know that one is going to
  3186. 2:08:03fetch some data
  3187. 2:08:05from this videos table where the ID is
  3188. 2:08:08equal to one.
  3189. 2:08:09So, this is one to one relationship.
  3190. 2:08:13Where we know that we are using a
  3191. 2:08:15different entity to our entity and the
  3192. 2:08:19relation between us is one to one.
  3193. 2:08:21Whenever we write one here,
  3194. 2:08:23we are not going to fetch any other data
  3195. 2:08:26and also we are not going to use the one
  3196. 2:08:29again in this table. So, this is
  3197. 2:08:31one-to-one relationship. So, we have
  3198. 2:08:32understood that in relational DB, we
  3199. 2:08:35have entities, right? And we join
  3200. 2:08:38multiple entities through relations.
  3201. 2:08:41That means every entity will have
  3202. 2:08:42particular set of properties, and then
  3203. 2:08:45they will be joined, and we will get the
  3204. 2:08:47data accordingly. Let's understand the
  3205. 2:08:49same thing with the example of posts and
  3206. 2:08:51comments. So, suppose we are building a
  3207. 2:08:54blog site, and in blog site, we have
  3208. 2:08:56some post, and then we have some
  3209. 2:08:58comments. The post table will have some
  3210. 2:09:01ID, unique identifier, then some content
  3211. 2:09:05associated with the post. Now, content
  3212. 2:09:07can be anything, right? It can be
  3213. 2:09:08images, it can again be text, or
  3214. 2:09:13sometimes it can be video, also. So,
  3215. 2:09:15these all are the category of post. Now,
  3216. 2:09:18we have different type of posts, but we
  3217. 2:09:21have to somehow pass those IDs and
  3218. 2:09:23manage three tables for image, text, and
  3219. 2:09:28videos. We can manage them in the same
  3220. 2:09:30table, as well, but that will increase
  3221. 2:09:32our latency, because different kind of
  3222. 2:09:34formats are curated into a single table.
  3223. 2:09:37That will increase the size, right? With
  3224. 2:09:39content, we can also have content type,
  3225. 2:09:42through which we will basically apply
  3226. 2:09:43some filters, if we want to. Now, if we
  3227. 2:09:45talk about it, we already have four
  3228. 2:09:48tables. One for images, one for videos,
  3229. 2:09:51one for text, and one for content. Now,
  3230. 2:09:54we will have comments, as well. So, in
  3231. 2:09:57order to have comments, we will have
  3232. 2:09:58some comment ID, and then we will have
  3233. 2:10:00comment. Now, comment can also be a sub
  3234. 2:10:03comment of a parent comment. So, that
  3235. 2:10:05means we can have a parent kind of
  3236. 2:10:08comment ID, as well. Now, in order to
  3237. 2:10:10handle comments, we have to handle
  3238. 2:10:12users, also. Now, in order to handle
  3239. 2:10:14user, we need one more table. We will
  3240. 2:10:16have ID, maybe name here. So, our number
  3241. 2:10:19again got increased to six. Now, in with
  3242. 2:10:22this PID, we will also have user ID who
  3243. 2:10:25is commenting on our post. So, in order
  3244. 2:10:28to maintain a simple post and comment
  3245. 2:10:31section, we will have to have
  3246. 2:10:34four to five to six tables, right? And
  3247. 2:10:37then we have to maintain the
  3248. 2:10:38relationships as well. So, that means
  3249. 2:10:40even for a small blog site, we will have
  3250. 2:10:43to maintain so much data. And when the
  3251. 2:10:45size of the blogs increase, with that I
  3252. 2:10:48mean this will get hard to scale. Now,
  3253. 2:10:51why I'm saying it is hard to scale?
  3254. 2:10:53First, understand how can we scale any
  3255. 2:10:55database? We have two options only,
  3256. 2:10:57right? First is by scaling them
  3257. 2:11:00vertically, and the second option is by
  3258. 2:11:02scaling them horizontally. In vertical
  3259. 2:11:05scaling, what do we do? We add the
  3260. 2:11:07capacity to a single server. So, that
  3261. 2:11:10means if we are using 4 GB data, we will
  3262. 2:11:12add more space into our server to have
  3263. 2:11:15more data. We can only expand this kind
  3264. 2:11:19of vertical scaling to a extent. After
  3265. 2:11:22that, we will have to use horizontal
  3266. 2:11:25scaling. That means having different
  3267. 2:11:26servers across different places. So, in
  3268. 2:11:29this case, we will have to maintain
  3269. 2:11:31relationships between the different
  3270. 2:11:33entities
  3271. 2:11:35in all of these databases.
  3272. 2:11:37And this will be a tedious task. So, in
  3273. 2:11:40order to avoid this, we can use NoSQL.
  3274. 2:11:43So, we can say the first advantage of
  3275. 2:11:45using NoSQL databases is it is easy to
  3276. 2:11:48scale both vertically and horizontally.
  3277. 2:11:52Because we will have to maintain a
  3278. 2:11:54single document, and we can replicate it
  3279. 2:11:57on different servers or even split them
  3280. 2:11:59into different partitions and pass it to
  3281. 2:12:02different servers. We will understand
  3282. 2:12:03about that also in a bit. Now, what is
  3283. 2:12:06the next issue? The next issue is
  3284. 2:12:09if we are maintaining a table, we can't
  3285. 2:12:13change the table again and again. We
  3286. 2:12:15have some fixed columns, right? We have
  3287. 2:12:18some fixed attributes or columns. We
  3288. 2:12:21can't add or remove different column
  3289. 2:12:23types in order to maintain different
  3290. 2:12:25kind of data. Now, let's say we are
  3291. 2:12:28going to handle the blog site, but this
  3292. 2:12:30time we are going for NoSQL database. In
  3293. 2:12:33this, what we can do now, we can create
  3294. 2:12:35again and content entity and we can
  3295. 2:12:39basically use any kind of content which
  3296. 2:12:41we want. So, we'll create the first
  3297. 2:12:43object and maybe we'll write something.
  3298. 2:12:46Let's say user ID is one, then we will
  3299. 2:12:48write the content. Here, the content is
  3300. 2:12:51an image, so let me pass some URL. https
  3301. 2:12:55maybe something something.jpg.
  3302. 2:12:58Now, when we are adding next content,
  3303. 2:13:00let's we can add it directly into the
  3304. 2:13:02same content document. So, what we'll
  3305. 2:13:05do, we'll again use some user ID. Maybe
  3306. 2:13:08this time it is two and in form of
  3307. 2:13:10content, now we have some textual data
  3308. 2:13:13and we can have some text. Also, if you
  3309. 2:13:15want to use it in a more refined way, if
  3310. 2:13:18you want to add some rows, we can use
  3311. 2:13:20that, right? We can use user ID as
  3312. 2:13:23three, then maybe heading as anything,
  3313. 2:13:27let's say Java, and then maybe we can
  3314. 2:13:29have some description. This is
  3315. 2:13:32description. And like this, we can have
  3316. 2:13:35as many fields as we want. So, that
  3317. 2:13:37means in a single entity content, we can
  3318. 2:13:40have different fields and also we can
  3319. 2:13:43have different number of fields in the
  3320. 2:13:45same document. And if we go about
  3321. 2:13:48relational database, we can't do that
  3322. 2:13:50there, right? So, in these kind of
  3323. 2:13:52scenarios where the document structure
  3324. 2:13:53is not fixed, this is basically called
  3325. 2:13:56schema-less, we need to go for NoSQL
  3326. 2:13:59options because in SQL we don't have
  3327. 2:14:01this option. So, that comes to our
  3328. 2:14:03second advantage of using NoSQL, that is
  3329. 2:14:06we can handle schema-less objects as
  3330. 2:14:09well. So, now let's discuss about third
  3331. 2:14:11point. So, in NoSQL up to now we have
  3332. 2:14:14seen it is easy to scale, then it is
  3333. 2:14:17schema-less and what makes NoSQL more
  3334. 2:14:21feasible is when we don't have to handle
  3335. 2:14:24a lot of linking between different
  3336. 2:14:26entities. That is we don't have to
  3337. 2:14:28maintain a lot of relationships between
  3338. 2:14:30different entities and also every entity
  3339. 2:14:33in a NoSQL database stands on its own.
  3340. 2:14:36Think of this as a key-value pairs.
  3341. 2:14:38Let's say a course ID. ID is the key
  3342. 2:14:42here and in the course content we can
  3343. 2:14:45have anything. We can have only name or
  3344. 2:14:49we can have entire object. Or in third
  3345. 2:14:51place we can have entire course with
  3346. 2:14:55lesson. Of course there are going to be
  3347. 2:14:57nested lessons in a course structure.
  3348. 2:14:59Again with lessons in fourth place we
  3349. 2:15:02can have course which will contain some
  3350. 2:15:04lessons as a nested object and those
  3351. 2:15:06lessons can also contain some comments.
  3352. 2:15:09Now all these things are possible in
  3353. 2:15:11NoSQL which were not there in SQL. If we
  3354. 2:15:14want to have similar structure in SQL we
  3355. 2:15:16have to maintain different entities and
  3356. 2:15:18we have to maintain different
  3357. 2:15:19relationships between them. Here we
  3358. 2:15:21don't have that struggle. So that makes
  3359. 2:15:23our NoSQL work easy. So this is why we
  3360. 2:15:26use NoSQL over SQL and with time it is
  3361. 2:15:29gaining popularity. For example in
  3362. 2:15:31Netflix we use Apache Cassandra in order
  3363. 2:15:34to maintain user activities. In Amazon
  3364. 2:15:36they use DynamoDB in order to scale
  3365. 2:15:38their application. In Facebook or Meta
  3366. 2:15:41they use HBase in order to maintain user
  3367. 2:15:44messaging and large-scale storage. In
  3368. 2:15:46Uber they use MongoDB in order to handle
  3369. 2:15:49flexible and real-time data. And with
  3370. 2:15:51that Twitter or X use Redis for caching
  3371. 2:15:55and timeline purposes. So the conclusion
  3372. 2:15:58is that NoSQL is also being used in
  3373. 2:16:00variety of systems nowadays. Let's
  3374. 2:16:03distinguish it for a bit now. So when we
  3375. 2:16:06should go for SQL and when we should go
  3376. 2:16:09for NoSQL. Whenever we are handling
  3377. 2:16:11payments, we don't have so much variety
  3378. 2:16:14there. So, payments can be easily
  3379. 2:16:16managed through SQL. So, with payments,
  3380. 2:16:19we can also think about transactions or
  3381. 2:16:22any other feature which needs
  3382. 2:16:24consistency where the data is not
  3383. 2:16:26changing, the schema is not changing,
  3384. 2:16:27and also we need to handle those
  3385. 2:16:30relationships perfectly. In all those
  3386. 2:16:32areas where we need consistency, we can
  3387. 2:16:35easily pick SQL. Now, when do we pick
  3388. 2:16:38NoSQL? When we are talking about scaling
  3389. 2:16:41and also we can talk about speed because
  3390. 2:16:44as in NoSQL, we have a single document
  3391. 2:16:47to search from whether in SQL, we have
  3392. 2:16:49different entities or tables who are
  3393. 2:16:51joined through one or more
  3394. 2:16:52relationships. So, we have to traverse
  3395. 2:16:54multiple tables in a SQL database and in
  3396. 2:16:57NoSQL, we don't have to do that. So, by
  3397. 2:16:59that nature, the speed of NoSQL database
  3398. 2:17:02is faster than the speed of SQL one. And
  3399. 2:17:05also, one of the core reasons to use
  3400. 2:17:07NoSQL is
  3401. 2:17:08unstructured data. So, in other words,
  3402. 2:17:11whenever the consistency is on more
  3403. 2:17:13priority than availability, we can
  3404. 2:17:16easily pick SQL. And when availability
  3405. 2:17:19and scalability is on more priority than
  3406. 2:17:22strict consistency, we can choose NoSQL.
  3407. 2:17:25With this, I hope you all get why we use
  3408. 2:17:28NoSQL databases also with SQL and in
  3409. 2:17:31what areas will we pick SQL over NoSQL
  3410. 2:17:34and vice versa. Now that we have
  3411. 2:17:36understood what NoSQL database is and
  3412. 2:17:38why do we use that, let's see some
  3413. 2:17:40examples and application areas of NoSQL
  3414. 2:17:43database. Now, the thing is in SQL, we
  3415. 2:17:46have only considered all the databases
  3416. 2:17:49which will have tables, similar
  3417. 2:17:51relationships, and in NoSQL, there can
  3418. 2:17:54be various ways in which we can store
  3419. 2:17:56data because it is like saying Java and
  3420. 2:17:58NoJava. In Java, we have Java and in no
  3421. 2:18:02Java, we can have many applications,
  3422. 2:18:04right? So, like that, let's start by our
  3423. 2:18:07first example, which is key-value pair.
  3424. 2:18:10Now, if you are a developer or
  3425. 2:18:12developing front-end, back-end, or
  3426. 2:18:14anything, you must have used cache. We
  3427. 2:18:17use the format of key and value whenever
  3428. 2:18:19we are trying to insert some value in
  3429. 2:18:20cache or maybe cookies or in data
  3430. 2:18:23storage. We use this kind of data. So,
  3431. 2:18:26if we talk about key, the only feature
  3432. 2:18:29which a key should have is that a key
  3433. 2:18:31should be unique. And for value, we can
  3434. 2:18:34use any kind of value. We can use
  3435. 2:18:36basically JSON or a string or maybe a
  3436. 2:18:41number. Maybe if you want to add blob,
  3437. 2:18:44we can use that. We can also store it as
  3438. 2:18:48byte array or any array for that matter.
  3439. 2:18:51We can also obviously have objects as
  3440. 2:18:53well and we can also have the
  3441. 2:18:55combinations of them. Like a list of
  3442. 2:18:57object can be stored in an array or an
  3443. 2:19:00object can also have nested objects. And
  3444. 2:19:02with that, obviously, it will be having
  3445. 2:19:04some strings, numbers, blob, byte array,
  3446. 2:19:07or any of that values. Now, the two
  3447. 2:19:10important points here are we don't have
  3448. 2:19:12to follow any structure because the
  3449. 2:19:15NoSQL database follows schema-less
  3450. 2:19:17structure. So, we don't have to follow
  3451. 2:19:21any structure. With that, we also don't
  3452. 2:19:24have to maintain any relationships.
  3453. 2:19:26Suppose, if you want to add post and
  3454. 2:19:28comments, we can simply use any key.
  3455. 2:19:31Let's say post is the key here, post one
  3456. 2:19:33maybe. And in order to have that object,
  3457. 2:19:36we can have any ID, post ID, or any
  3458. 2:19:40content. So, these all things are key
  3459. 2:19:42basically of this object. Post ID can be
  3460. 2:19:45a number. Post content can be anything.
  3461. 2:19:47And with that, we can have comments and
  3462. 2:19:51we can have array of objects in
  3463. 2:19:54comments. We can have multiple comments
  3464. 2:19:56field also.
  3465. 2:19:57Let's say it can have a comment ID and
  3466. 2:20:00the actual content of the comment. Like
  3467. 2:20:03this, it can have multiple objects and
  3468. 2:20:05this all can be done without any
  3469. 2:20:07relationships. So, this is a good
  3470. 2:20:09example of key-value pair, which we
  3471. 2:20:10normally use in our cache, cookies, or
  3472. 2:20:13any other places where we don't need to
  3473. 2:20:15maintain any structure. Now, let's move
  3474. 2:20:17to the second one, columnar DB. So, the
  3475. 2:20:19thing is whenever we are having any
  3476. 2:20:21table, let's say we have student table,
  3477. 2:20:23which have typical ID, a unique
  3478. 2:20:25identifier, the name, and then the
  3479. 2:20:28marks. Maybe total marks of all the
  3480. 2:20:30students. So, ID is one, name is Akshat,
  3481. 2:20:35and marks is maybe 90. With that, let's
  3482. 2:20:37have one more record. The ID is two,
  3483. 2:20:40name is Gaurav, and the marks maybe is
  3484. 2:20:4495. Now, what we have to do is we have
  3485. 2:20:46to get the average marks of class. So,
  3486. 2:20:50the students is basically maintaining a
  3487. 2:20:52class, we are assuming that, and all the
  3488. 2:20:54marks, if we sum them, we will get the
  3489. 2:20:56total marks, and if we divide them with
  3490. 2:20:58the total number of students, we will
  3491. 2:20:59get the aggregated marks. Now, in order
  3492. 2:21:01to do that, if we do it in a SQL manner,
  3493. 2:21:04the SQL manner basically reads the data
  3494. 2:21:07from left to right. So, even if we don't
  3495. 2:21:10require any name in our data, we have to
  3496. 2:21:13still read our table from left to right
  3497. 2:21:16for each and every row. This is a heavy
  3498. 2:21:18task, right? Because we don't need those
  3499. 2:21:20values. Here, we have only three
  3500. 2:21:22columns. What if we have 17 columns?
  3501. 2:21:25Then also we have to read all the rows
  3502. 2:21:26from left to right, and which is not
  3503. 2:21:28mandatory.
  3504. 2:21:29So, in order to fix this issue, we have
  3505. 2:21:31columnar DB. What columnar DB does is it
  3506. 2:21:35gives us the flexibility to read the
  3507. 2:21:37data column-wise. So, instead of reading
  3508. 2:21:40it row-wise, now we can read the data
  3509. 2:21:42column-wise. That means if you want to
  3510. 2:21:45deal with only marks, we can read only
  3511. 2:21:48marks data column-wise. And then we can
  3512. 2:21:51aggregate all the marks. And if we want
  3513. 2:21:53some other column, those can be also
  3514. 2:21:55read by column-wise. And how we can link
  3515. 2:21:59them? We can link them with the common
  3516. 2:22:00IDs. So, the common IDs will be there
  3517. 2:22:03whenever we are reading column-wise, and
  3518. 2:22:05then it will be easier and faster
  3519. 2:22:08through our unique identifier that is ID
  3520. 2:22:10here. Where we are reading the data, but
  3521. 2:22:13column-wise instead of row-wise. The
  3522. 2:22:15common examples of columnar DB are
  3523. 2:22:18Google BigQuery, Amazon Redshift, and
  3524. 2:22:20Snowflake.
  3525. 2:22:22So, all in all, if we have to read a lot
  3526. 2:22:24of data, we can go for columnar DB. So,
  3527. 2:22:26it is good for analysis. And imagine
  3528. 2:22:30writing the data into columnar DB, that
  3529. 2:22:32is going to take again a lot of time
  3530. 2:22:34because we have to write also
  3531. 2:22:35column-wise. So, that means we have to
  3532. 2:22:37first maintain an ID, and then the name
  3533. 2:22:40will be written, and then the marks will
  3534. 2:22:42be written, and then the other values if
  3535. 2:22:44we have them. Those will be also
  3536. 2:22:46written. So, in order to read the value,
  3537. 2:22:49it is useful. And when it comes to
  3538. 2:22:51write, it is going to take more time
  3539. 2:22:54than a SQL database. The third kind of
  3540. 2:22:56NoSQL database is graph DB. Imagine, we
  3541. 2:23:00have all seen those movies where
  3542. 2:23:02detectives basically pinpoint many
  3543. 2:23:04photos and join them with some thread or
  3544. 2:23:07something, right? So, like this only, we
  3545. 2:23:10have graph DB where we have different
  3546. 2:23:13nodes, and they are joined by the
  3547. 2:23:15threads which are called edge. So, every
  3548. 2:23:18edge basically represent a common
  3549. 2:23:20relationships between two nodes. Two
  3550. 2:23:22nodes are basically the entities, and
  3551. 2:23:25edge signifies the relationship between
  3552. 2:23:28them. Now, what is different in graph DB
  3553. 2:23:31is both nodes and edges can have
  3554. 2:23:35properties. Suppose we are creating any
  3555. 2:23:38node which is student, maybe, and which
  3556. 2:23:41contain all the student information.
  3557. 2:23:43Now, we have another node for course,
  3558. 2:23:45maybe, and we can name the relationship
  3559. 2:23:48anything. Maybe it is enrolled. Now, the
  3560. 2:23:50enrolled can also have properties in it.
  3561. 2:23:53It can contain year, it can contain
  3562. 2:23:55marks. It can contain year of passing
  3563. 2:23:58maybe. And all of these things the edge
  3564. 2:24:01or the relationship can also have. So,
  3565. 2:24:03this is what makes our system unique.
  3566. 2:24:05Graph DBs are often used by data
  3567. 2:24:07scientists to find patterns between
  3568. 2:24:10different scenarios. It works basically
  3569. 2:24:12in the same level which we see in any
  3570. 2:24:14detective movie as well. So, if you want
  3571. 2:24:16to work with graph DB, we can learn
  3572. 2:24:19Gremlin or we can go for
  3573. 2:24:22SparkQL. Also, we can use Cypher in
  3574. 2:24:25order to work with graph DB. As I said,
  3575. 2:24:28it is used in huge companies whenever we
  3576. 2:24:30are dealing with a huge amount of data
  3577. 2:24:32in order to face some patterns or
  3578. 2:24:34basically analyze the customer behavior
  3579. 2:24:37on our site. So, as we can see the two
  3580. 2:24:39entities are going to have some
  3581. 2:24:41properties and also the relationship
  3582. 2:24:44between them will also have some
  3583. 2:24:46properties. Also, two entities can have
  3584. 2:24:49different relationships. And also it can
  3585. 2:24:51be linked via different entities as
  3586. 2:24:53well. So, student basically studies in
  3587. 2:24:56this college. We can write all the
  3588. 2:24:58parameters of the studies edge also. And
  3589. 2:25:01college provides the course. So, in this
  3590. 2:25:05provide relationship also we can have
  3591. 2:25:07multiple properties. So, which makes our
  3592. 2:25:10graph DB complex. And also since we are
  3593. 2:25:13dealing with multiple entities with
  3594. 2:25:15properties and also multiple
  3595. 2:25:16relationship with different properties,
  3596. 2:25:19it is going to be a bit slow. With this,
  3597. 2:25:21let's move to our final and the most
  3598. 2:25:23famous NoSQL database which is document
  3599. 2:25:28DB. So, in document DB we will have some
  3600. 2:25:31document and in that document we can
  3601. 2:25:34write JSON and can store data with any
  3602. 2:25:37length. We don't have to follow any
  3603. 2:25:38structure, we don't have to follow any
  3604. 2:25:40schema. So, that is flexible in nature.
  3605. 2:25:43Also, it is schema-less. So, similarly,
  3606. 2:25:46document DB also don't have to follow
  3607. 2:25:48any relationship. At this point, if we
  3608. 2:25:50think about this, the graph QL is kind
  3609. 2:25:52of an exception on the borderline there
  3610. 2:25:54because we do have relationships between
  3611. 2:25:57them and with relationships, we also
  3612. 2:25:59have properties of those relationships,
  3613. 2:26:01which makes it entirely unique and
  3614. 2:26:03different from no sequel and sequel
  3615. 2:26:05both. But on a large level, it comes
  3616. 2:26:07under the category of no sequel only.
  3617. 2:26:09The common examples of document DB is
  3618. 2:26:11CouchDB and also MongoDB is widely used.
  3619. 2:26:15So, that is also an example of our
  3620. 2:26:17document DB only. Our document DB is
  3621. 2:26:20highly used in logging the systems
  3622. 2:26:22because we need flexible log structures.
  3623. 2:26:25So, it is highly used in logging,
  3624. 2:26:27logging the error, maybe logging the
  3625. 2:26:28warn messages, logging any kind of
  3626. 2:26:30stuff. Also, it can be used to handle
  3627. 2:26:33profile section because the profile
  3628. 2:26:35section can also contain different
  3629. 2:26:37values for different users. Suppose some
  3630. 2:26:39have given all the values, some have
  3631. 2:26:41given less values. So, that can be
  3632. 2:26:42easily maintained through profile. And
  3633. 2:26:45with that, if we talk about content like
  3634. 2:26:47content on any application, let's say
  3635. 2:26:50Instagram, we have different kind of
  3636. 2:26:52content. We can have image, we can have
  3637. 2:26:55video, or we can have some text. If we
  3638. 2:26:58talk about Facebook here. Now, these
  3639. 2:27:01different kind of posts or content don't
  3640. 2:27:04have to go to different tables. In
  3641. 2:27:06document DB, we can basically create a
  3642. 2:27:08single document and curate all of these
  3643. 2:27:11content into the same document,
  3644. 2:27:13obviously with different attributes and
  3645. 2:27:15values. So, again, as it is a no sequel
  3646. 2:27:18DB, it is going to be complex and the
  3647. 2:27:20retrieval of any information will be
  3648. 2:27:22faster. Easily, any no sequel databases
  3649. 2:27:25can be used for any kind of analysis.
  3650. 2:27:28So, this is all about the types of no
  3651. 2:27:29sequel databases. After understanding
  3652. 2:27:31about databases, one thing is very sure
  3653. 2:27:34that each and every important
  3654. 2:27:35information needs to be stored in
  3655. 2:27:37databases and will be retrieved from
  3656. 2:27:39there itself. That means if we have any
  3657. 2:27:42user here who is trying to access, let's
  3658. 2:27:45say telesco.com only from their mobile
  3659. 2:27:47application, the request eventually go
  3660. 2:27:50to our backend and from backend if we
  3661. 2:27:54are showing any information, it will be
  3662. 2:27:56retrieved from the database and will be
  3663. 2:27:58shown to our user. Now, the thing is for
  3664. 2:28:01every information we need to hit our
  3665. 2:28:03database. So,
  3666. 2:28:05even if we have a homepage which have,
  3667. 2:28:08let's say, three most popular courses
  3668. 2:28:12aligned like this. This is our course
  3669. 2:28:13one, course two, and course three. Maybe
  3670. 2:28:17the details of each course are visible.
  3671. 2:28:20One, a thumbnail is visible. Then, with
  3672. 2:28:22that the name is visible. Maybe the
  3673. 2:28:25price is also visible. And with price,
  3674. 2:28:29the discounted price is also visible.
  3675. 2:28:32Also, let's say we are showing some of
  3676. 2:28:34the important sections. So, yeah. These
  3677. 2:28:38are all the details of every course
  3678. 2:28:40which is visible on our homepage. Now,
  3679. 2:28:43this maybe is coming from any DNS
  3680. 2:28:46because that's where we are saving any
  3681. 2:28:48static files, remember? The name will be
  3682. 2:28:51stored in a table. Let's say the table
  3683. 2:28:53name is course or courses. For price, we
  3684. 2:28:57may have the prices in the courses table
  3685. 2:28:59itself, but according to area, according
  3686. 2:29:02to the currency, the price keeps on
  3687. 2:29:04changing. So, we will create a separate
  3688. 2:29:07tables for prices. So, the price is
  3689. 2:29:10coming from price. Then, the discount
  3690. 2:29:12price also based on the locality. So,
  3691. 2:29:15these both details are coming from the
  3692. 2:29:16same place, prices table. And then, the
  3693. 2:29:19important section. Important section may
  3694. 2:29:21have some criteria or some course
  3695. 2:29:23content which we need to show. So, this
  3696. 2:29:26maybe it is coming from courses section
  3697. 2:29:28only, but courses section normally have
  3698. 2:29:30the course main course detail and then
  3699. 2:29:33accordingly we can have different
  3700. 2:29:35sections. So this is maybe courses table
  3701. 2:29:37and we can create some other table which
  3702. 2:29:40will have all the information for
  3703. 2:29:42courses. We can name it as course info.
  3704. 2:29:45So here also the things are coming from
  3705. 2:29:48course info table.
  3706. 2:29:50So that means in order to show these
  3707. 2:29:52three courses we are hitting 1 2 3 and 4
  3708. 2:29:57resources, three tables and one DNS. And
  3709. 2:30:00that means that for every user who is
  3710. 2:30:03trying to access our site telesco.com,
  3711. 2:30:05it is going to hit all of these four
  3712. 2:30:07resources again and again. So if the
  3713. 2:30:10count increases to 10,000 maybe
  3714. 2:30:14daily active users that means we are
  3715. 2:30:17going to hit the database and the DNS
  3716. 2:30:2010,000 times for each and every user.
  3717. 2:30:23Also don't forget that the courses are
  3718. 2:30:25three.
  3719. 2:30:26Maybe in future we will increase the
  3720. 2:30:28number to six. So that means 10,000
  3721. 2:30:31requests multiplied by six
  3722. 2:30:34that is 60,000 requests. In short, for
  3723. 2:30:37every view we are hitting six courses
  3724. 2:30:41and from those six courses we are
  3725. 2:30:43hitting these four sections. So for
  3726. 2:30:46every user we are hitting 24 requests.
  3727. 2:30:49This is totally going to break our
  3728. 2:30:51system because 24 requests means it will
  3729. 2:30:54add latency to our home page. And in
  3730. 2:30:57these fast times where nobody wants to
  3731. 2:30:59wait for any resources, these latency
  3732. 2:31:02are eventually going to affect our
  3733. 2:31:04business.
  3734. 2:31:06Also the more the latency the poor the
  3735. 2:31:08performance. So this is clearly not a
  3736. 2:31:11good sign. We can't keep hitting our
  3737. 2:31:13databases again and again in order to
  3738. 2:31:15fetch the same information for each
  3739. 2:31:17user. We need some other mechanism. So
  3740. 2:31:19this is where cache comes into the
  3741. 2:31:22picture.
  3742. 2:31:23Now let's understand what cache is.
  3743. 2:31:26Cache is nothing but a memory storage,
  3744. 2:31:30which is away from databases and it can
  3745. 2:31:33either be in our backend or into our
  3746. 2:31:35front end in order to load the resources
  3747. 2:31:37faster. Now, how does it work? Let's
  3748. 2:31:39take the same example. Our user is
  3749. 2:31:41trying to access telesco.com and is
  3750. 2:31:45hitting backend
  3751. 2:31:46to get the information and backend
  3752. 2:31:48eventually is hitting database to get
  3753. 2:31:50the information. Now, once we understand
  3754. 2:31:53that these six courses are going to be
  3755. 2:31:55static on our home page, right? So, that
  3756. 2:31:58means we don't want to fetch this
  3757. 2:31:59information again and again from our
  3758. 2:32:01databases. So, what will we do?
  3759. 2:32:04We will eventually get that particular
  3760. 2:32:07information and put it onto our backend
  3761. 2:32:11either or we can also put the same
  3762. 2:32:13information on our frontend storage as
  3763. 2:32:16well. So, when the user again comes on
  3764. 2:32:18telesco.com, we don't have to hit the
  3765. 2:32:20entire database again and again. We have
  3766. 2:32:23that information handy on our backend or
  3767. 2:32:26frontend or in both the areas. So,
  3768. 2:32:29whenever we hit our home page again, we
  3769. 2:32:31can get those information directly
  3770. 2:32:33through our cache, which is stored
  3771. 2:32:36either on our frontend or on our
  3772. 2:32:38backend. Here, we are saying that our
  3773. 2:32:40user is accessing mobile device. Maybe
  3774. 2:32:43it is accessing our site through any web
  3775. 2:32:45application.
  3776. 2:32:46So, there also we can handle cache,
  3777. 2:32:48okay? These caches, which are on client
  3778. 2:32:51side, are normally referred as client
  3779. 2:32:53side cache. And the data, which we
  3780. 2:32:56stored in backend systems, are referred
  3781. 2:32:58as server side cache. In databases also,
  3782. 2:33:02there is some mechanisms where they can
  3783. 2:33:05also store the information ahead of
  3784. 2:33:07time, that is known as well. And we can
  3785. 2:33:10get the most requested information from
  3786. 2:33:12this data only. We don't need to hit our
  3787. 2:33:16database tables. Also, we can have cache
  3788. 2:33:19in DB as well, which is known as
  3789. 2:33:20database cache. Up to this point, we
  3790. 2:33:23have understand why do we need cache
  3791. 2:33:25from a very high level point. Now, let's
  3792. 2:33:27understand how can we implement the
  3793. 2:33:29caches in the real system. In order to
  3794. 2:33:31understand them, first we have to make
  3795. 2:33:33sure that we know some of the terms.
  3796. 2:33:35The first term which we need to know is
  3797. 2:33:37cache hit. Now, what is cache hit? Cache
  3798. 2:33:40hit basically means let's say we have
  3799. 2:33:43any request which is coming from our
  3800. 2:33:46front end to back end. Let's say we have
  3801. 2:33:48cache installed here and here is our
  3802. 2:33:51final DB. Now, whenever we are hitting
  3803. 2:33:54the request and we found the data in the
  3804. 2:33:56cache, that is known as a cache hit
  3805. 2:33:59scenario where the data is present in
  3806. 2:34:01the cache and we don't need to hit our
  3807. 2:34:03database. But, if the data which we are
  3808. 2:34:06trying to get is not in cache and we
  3809. 2:34:08have to hit our database, that is known
  3810. 2:34:10as cache miss. So, what is cache miss
  3811. 2:34:13here? Cache miss is again we are trying
  3812. 2:34:15to access some data
  3813. 2:34:17from our back end and we don't find the
  3814. 2:34:20data in our cache. Instead, we have to
  3815. 2:34:23get to our database to fetch that
  3816. 2:34:25particular data. So, from where we are
  3817. 2:34:27getting the data in cache miss? We are
  3818. 2:34:29getting it from database. And from where
  3819. 2:34:32we are getting the data in cache hit
  3820. 2:34:34situation? We are getting it from the
  3821. 2:34:36cache itself. So, whenever we hit a
  3822. 2:34:38cache hit kind of situation, our
  3823. 2:34:40responses will be faster. And similarly,
  3824. 2:34:42in cache miss, our responses are going
  3825. 2:34:45to be slower. With this, the next term
  3826. 2:34:47which we need to know is TTL.
  3827. 2:34:50TTL basically means time to live. Time
  3828. 2:34:53to live basically specifies for how long
  3829. 2:34:56the data is going to be in the cache. In
  3830. 2:34:58other words, we can also say after how
  3831. 2:35:01much time our data is going to expire
  3832. 2:35:03from cache. Up to now, I hope we have
  3833. 2:35:06understood what cache really is.
  3834. 2:35:08So, how do we store data in cache? We
  3835. 2:35:11store the data in cache in normally a
  3836. 2:35:14key value form. And we get these data by
  3837. 2:35:17using the keys, obviously, and by value,
  3838. 2:35:20we also have TTL with them.
  3839. 2:35:22In order to check whether the time is
  3840. 2:35:25expired for that particular key-value
  3841. 2:35:27pair or not. If it is expired, this
  3842. 2:35:29key-value pair will be eliminated from
  3843. 2:35:32our cache so that it makes room for
  3844. 2:35:34having more data. So, at this point, we
  3845. 2:35:36understand that the cache works faster
  3846. 2:35:39than our databases. Then, why don't we
  3847. 2:35:41get all the data from our databases and
  3848. 2:35:44put it in cache so that every request
  3849. 2:35:46which we make will be faster. If you
  3850. 2:35:48understand software development, you
  3851. 2:35:50understand that I'm asking a silly
  3852. 2:35:52question. But, if you are new, just hear
  3853. 2:35:54me out. Now, this is those kind of
  3854. 2:35:56question where the answer lies in the
  3855. 2:35:58question itself. We are saying that
  3856. 2:36:01cache is faster. Now, let's understand
  3857. 2:36:03why it is faster. Because we are only
  3858. 2:36:06collecting the information which is
  3859. 2:36:09frequently used. Okay, so that's why the
  3860. 2:36:12size of our cache is very smaller than
  3861. 2:36:16our DB. Because DB have all the
  3862. 2:36:19information which our site needs. So,
  3863. 2:36:21that means the size of cache is limited.
  3864. 2:36:24And we have to keep refreshing the data
  3865. 2:36:26in cache so that it performs well.
  3866. 2:36:30Why do we need to refresh? Let's say
  3867. 2:36:32today we have some campaign of maybe
  3868. 2:36:34selling our most sellable course, maybe
  3869. 2:36:37Java we are selling on the let's go. We
  3870. 2:36:39are saying mastering Java. So, you can
  3871. 2:36:42master Java. This is our most prominent
  3872. 2:36:44course today. But, tomorrow we change
  3873. 2:36:46our focus from Java to maybe AI. Let's
  3874. 2:36:49say most students are not interested to
  3875. 2:36:52learn Java at that time. But, are
  3876. 2:36:54interested to learn AI. At that time, we
  3877. 2:36:57have to replace the content of Java to
  3878. 2:37:00AI.
  3879. 2:37:01So that we can get this information
  3880. 2:37:03faster. Why it is faster? Because it is
  3881. 2:37:06easy to find something in a limited
  3882. 2:37:09space than in the entire database. That
  3883. 2:37:12is why cache is faster. And the second
  3884. 2:37:14point is if let's say somehow we managed
  3885. 2:37:17to create a large size of cache itself
  3886. 2:37:20and we managed to save our entire
  3887. 2:37:22database into cache, in that case, every
  3888. 2:37:25search query or everything which we are
  3889. 2:37:26trying to find is going to take a longer
  3890. 2:37:28time into our cache itself. So, it
  3891. 2:37:31defeats the whole purpose of having a
  3892. 2:37:33cache. That is to reduce the latency and
  3893. 2:37:36to get the performance better. So, with
  3894. 2:37:38this two points are very clear. First is
  3895. 2:37:41the size of our cache needs to be way
  3896. 2:37:44smaller than the size of our DB so that
  3897. 2:37:47we can get the responses very faster.
  3898. 2:37:50And now that we have size constraint, we
  3899. 2:37:52need to keep updating the cache data in
  3900. 2:37:55order to get the values which are
  3901. 2:37:57relevant to our users. Now, I think we
  3902. 2:37:59are very sure what cache is, why the
  3903. 2:38:02size is limited, why do we need to
  3904. 2:38:04refresh the content again and again.
  3905. 2:38:06Now, it's time to go a bit deeper. Let's
  3906. 2:38:09go into the strategies through which we
  3907. 2:38:11can update the cache data. The first
  3908. 2:38:14strategy is read through cache. As the
  3909. 2:38:18name suggests, read through cache
  3910. 2:38:20strategy is designed to handle the reads
  3911. 2:38:23from the cache only, not the rights. So,
  3912. 2:38:26let me create a diagram. Let's say this
  3913. 2:38:27is our server. Here we have our back end
  3914. 2:38:30and our cache and with this we have our
  3915. 2:38:33actual database here. So, read through
  3916. 2:38:35the query suggests that every time we
  3917. 2:38:38have a request of getting any
  3918. 2:38:40information, we are only going to read
  3919. 2:38:44through cache. We are not going to hit
  3920. 2:38:46database. We are only going to read the
  3921. 2:38:49information from cache. And if and only
  3922. 2:38:53if the data is not present in our cache,
  3923. 2:38:55then only we will need to hit our
  3924. 2:38:57database.
  3925. 2:38:59Again, for right, we are not going to
  3926. 2:39:01write any value in our cache. We are
  3927. 2:39:03directly going to write our data into
  3928. 2:39:06our database. So, in order to read, we
  3929. 2:39:08are going to hit our cache, and in order
  3930. 2:39:11to write, we are going to hit our
  3931. 2:39:12databases. Here, clearly, we can have
  3932. 2:39:15two cases. The first case is
  3933. 2:39:18cache hit. In case of cache hit, that
  3934. 2:39:20means we are trying to get some
  3935. 2:39:22information, the back end is going to
  3936. 2:39:24hit our cache, and cache have that
  3937. 2:39:26value, right? Cache returns that value
  3938. 2:39:29to our back end system. So, this is
  3939. 2:39:31good. This means that we are going to
  3940. 2:39:33have a faster retrieval. Then, we have
  3941. 2:39:36our case two for cache miss. What
  3942. 2:39:39happens here is our back end is again
  3943. 2:39:42going to ask our cache whether they have
  3944. 2:39:44the information or not. And in this
  3945. 2:39:46case, as this is cache miss, that means
  3946. 2:39:49we don't have that information into our
  3947. 2:39:50cache. So, here, cache itself is going
  3948. 2:39:53to ask to our database
  3949. 2:39:56and fetch that particular data. So,
  3950. 2:39:58database is going to give that
  3951. 2:40:00information to our cache, and then cache
  3952. 2:40:02will return the data again to BE. So,
  3953. 2:40:05clearly, it adds a next level step to
  3954. 2:40:08get the data from database, which is
  3955. 2:40:10going to make this request call slower.
  3956. 2:40:13So, that means whenever we are reading
  3957. 2:40:15any data, we are not going to go to
  3958. 2:40:18database directly. Our cache will be
  3959. 2:40:20connecting to database and getting the
  3960. 2:40:22value which is required for our back
  3961. 2:40:24end. And from cache, we are going to
  3962. 2:40:27give that value again to back end in
  3963. 2:40:29order to process it. So, here, if you
  3964. 2:40:31see what we are doing, we are not adding
  3965. 2:40:33any recently added data directly to our
  3966. 2:40:35cache. We are adding data directly to
  3967. 2:40:38our databases, and only the data which
  3968. 2:40:40is getting retrieved from our users are
  3969. 2:40:43going to be in our cache. Let's say,
  3970. 2:40:45from our back end, we are making an
  3971. 2:40:47order placement, and that data we are
  3972. 2:40:50not seeing that again. So, that means
  3973. 2:40:52that data is never going to come to our
  3974. 2:40:54cache because no one is accessing that.
  3975. 2:40:57Only the data which user is accessing
  3976. 2:40:59are going to come to our cache, which
  3977. 2:41:02makes our cache always ready with the
  3978. 2:41:04information which user wants. So, this
  3979. 2:41:06is all about RTC. Let's move to the
  3980. 2:41:08second strategy, which is WTC. WTC
  3981. 2:41:12stands for write through cache. As in
  3982. 2:41:16RTC, the strategy is only for reading,
  3983. 2:41:18here this strategy is only for writing.
  3984. 2:41:21So, that means we have again our client
  3985. 2:41:24application. Here we have our backend
  3986. 2:41:26and our cache with our database. Now,
  3987. 2:41:29whenever we are going to write any data,
  3988. 2:41:31we're going to write it into cache and
  3989. 2:41:34then eventually from cache, the data is
  3990. 2:41:36going to be there into our databases.
  3991. 2:41:39So, this means whenever we are writing
  3992. 2:41:41any data, we're going to write it into
  3993. 2:41:43our cache first and from cache, the data
  3994. 2:41:46is going to be written into databases
  3995. 2:41:47finally. So, by doing this, our cache
  3996. 2:41:50data is always going to be updated with
  3997. 2:41:53the latest value. Where it can be used?
  3998. 2:41:55It can be used if we talk about stock
  3999. 2:41:57values, where we always want to see the
  4000. 2:42:00latest data. And with this, it can be
  4001. 2:42:02used to any other application where we
  4002. 2:42:03want to see the latest data first. With
  4003. 2:42:06this, let's move to the third strategy,
  4004. 2:42:08which is WAC. WAC basically stands for
  4005. 2:42:12write around cache. So, up to now, we
  4006. 2:42:15have only talked about either read or
  4007. 2:42:17write. With WAC, we're going to talk
  4008. 2:42:20about read and write both into one
  4009. 2:42:22strategy. So, read around cache. So,
  4010. 2:42:26when it comes to write, we are going to
  4011. 2:42:28write the data from any query from our
  4012. 2:42:30client, it is going to call our backend
  4013. 2:42:32services and from backend, we are
  4014. 2:42:34directly going to write the data into
  4015. 2:42:37our databases. No cache is in between.
  4016. 2:42:40So, that means all the writes will be
  4017. 2:42:42directly dumped into our database. And
  4018. 2:42:45we haven't updated any cache here. So,
  4019. 2:42:48this is about write. And with read, what
  4020. 2:42:50we are doing, whenever we are reading
  4021. 2:42:52any data, we are going to read it always
  4022. 2:42:55from cache. If the data is there in our
  4023. 2:42:58cache, that is a cache hit situation,
  4024. 2:43:02then we're going to get this information
  4025. 2:43:04directly. So, this is good. If we get
  4026. 2:43:06any cache miss kind of situation, where
  4027. 2:43:09the data is not there in our cache, the
  4028. 2:43:11cache is going to call database and
  4029. 2:43:13update the value in the cache first and
  4030. 2:43:16then will give the value to our backend.
  4031. 2:43:19So, this is how WAC works. You may think
  4032. 2:43:22that it is a combination of RTC and WDC,
  4033. 2:43:25which is correct. So, in RTC and WDC, we
  4034. 2:43:28are only talking about read and write
  4035. 2:43:31respectively. But, in WAC, we are
  4036. 2:43:33talking about the entire flow of data.
  4037. 2:43:36So, it is heavily used in applications
  4038. 2:43:39like Twitter. Now, it is called X. So,
  4039. 2:43:42here the scenario is we have a lot of
  4040. 2:43:45users who are going to create tweets.
  4041. 2:43:49But, we can't directly create those
  4042. 2:43:51tweets and save them in our cache,
  4043. 2:43:52right? And whenever the users are trying
  4044. 2:43:56to access any tweet, at this point it is
  4045. 2:43:58decided to keep this tweet in cache
  4046. 2:44:01memory because now it can be used by
  4047. 2:44:04some other users as well. So, we don't
  4048. 2:44:07decide the importance of any tweet at
  4049. 2:44:10the time of its creation. We decide the
  4050. 2:44:13importance at the time of reading those
  4051. 2:44:14tweets, right? So, with WAC, we also
  4052. 2:44:17have one more strategy, which is WBC.
  4053. 2:44:21WBC basically stands for write back
  4054. 2:44:25cache, which basically means whenever we
  4055. 2:44:27are trying to write any data, it is
  4056. 2:44:30going to be written into our cache and
  4057. 2:44:32at this point our write operation is
  4058. 2:44:34completed. We are going to write this
  4059. 2:44:36data into our database, but this is an
  4060. 2:44:39async process. So, that means while
  4061. 2:44:42writing our data, we don't care about
  4062. 2:44:45consistency much, but we care about
  4063. 2:44:47speed. We want to get the update faster.
  4064. 2:44:51So, whenever we write any data into our
  4065. 2:44:52cache, it is going to give us the okay
  4066. 2:44:56response, maybe.
  4067. 2:44:57And after that, the data will be dropped
  4068. 2:45:00into our databases eventually. Now, as
  4069. 2:45:03this is async process, it is very
  4070. 2:45:06possible that we may get some or other
  4071. 2:45:09error here.
  4072. 2:45:11So, we have to handle this kind of
  4073. 2:45:12scenario or strategy very carefully. And
  4074. 2:45:15now, let's talk about read. While
  4075. 2:45:17reading the data, again, we are going to
  4076. 2:45:19read the entire data from our cache
  4077. 2:45:21itself, right? That is the whole point
  4078. 2:45:23of having a cache. So, as most of our
  4079. 2:45:26data is directly written into cache, it
  4080. 2:45:29is most likely to have the most updated
  4081. 2:45:31values. So, normally, it is going to
  4082. 2:45:33give cache hit. And if we get cache miss
  4083. 2:45:37kind of situation, so we are again going
  4084. 2:45:39to call our database, update our cache,
  4085. 2:45:42and from cache, we are again going to
  4086. 2:45:45read the value from our application. So,
  4087. 2:45:47as you can see the WAC and WBC are
  4088. 2:45:50pretty common, but here we are using
  4089. 2:45:53async to write the data. That means we
  4090. 2:45:56are trading off between speed and
  4091. 2:46:00consistency. So, even if our data is not
  4092. 2:46:03consistent, we are making sure that the
  4093. 2:46:05speed of application remains fast. Now,
  4094. 2:46:08WBCs are basically used where we have a
  4095. 2:46:11high write traffic. The most common
  4096. 2:46:14example of this can be any food delivery
  4097. 2:46:17application. Let's say Swiggy or Zomato.
  4098. 2:46:20So, in Zomato or Swiggy kind of
  4099. 2:46:22application, what normally happens is at
  4100. 2:46:25a particular time, we may be handling
  4101. 2:46:27multiple orders with multiple status.
  4102. 2:46:30And the status keeps on changing. So,
  4103. 2:46:32instead of saving all the data changes
  4104. 2:46:35into our database, we choose to save
  4105. 2:46:38them into cache. Because we know that
  4106. 2:46:41the status of this particular order will
  4107. 2:46:43change very fast. So, we use WAL here
  4108. 2:46:47because we need to update the
  4109. 2:46:49information fast and also the user who
  4110. 2:46:52have created the order are also going to
  4111. 2:46:55see the latest information through this
  4112. 2:46:56cache only. And in the back process or
  4113. 2:46:59in the backend or async manner, we are
  4114. 2:47:02going to keep updating our databases.
  4115. 2:47:04So, this way it is going to handle
  4116. 2:47:06million of request and status updates at
  4117. 2:47:09a particular time. So, up to now we have
  4118. 2:47:11understood that how do we handle the
  4119. 2:47:13data into our cache? But, we have only
  4120. 2:47:16added the data. We haven't deleted the
  4121. 2:47:18data. So, now let's understand the cache
  4122. 2:47:21eviction policies through which we are
  4123. 2:47:23going to remove the data which is not
  4124. 2:47:25used by our cache. Here I'm writing
  4125. 2:47:28policies here. We can easily use
  4126. 2:47:30strategies. I just wanted to sound
  4127. 2:47:32little different. So, that's why I added
  4128. 2:47:33policies.
  4129. 2:47:35Um just for fun, you may say. So, the
  4130. 2:47:37first policy basically is LRU. LRU
  4131. 2:47:42basically stands for least recent used.
  4132. 2:47:47So, as the name suggest, least recent
  4133. 2:47:50used.
  4134. 2:47:51That means the data which is not used
  4135. 2:47:54recently will be evicted from our cache.
  4136. 2:47:57Suppose Apple launches a new iPhone
  4137. 2:48:00which is 17 Pro
  4138. 2:48:03Max updated something something very
  4139. 2:48:07acronym synonyms of Pro Max Ultra
  4140. 2:48:09something. And now everyone is keep
  4141. 2:48:12looking for 17 Pro. No one is in today's
  4142. 2:48:15time looking for 11. So, that means 11
  4143. 2:48:19is a stale data which was used earlier
  4144. 2:48:22by our cache maybe. But, in today's time
  4145. 2:48:25we don't need to store that information
  4146. 2:48:27into our cache because a very few clicks
  4147. 2:48:30were there who were still searching for
  4148. 2:48:3211. Now, with this let's talk about the
  4149. 2:48:35second policy which is MRU, the exact
  4150. 2:48:39opposite of LRU.
  4151. 2:48:40There we were saying least here we will
  4152. 2:48:43say most recent used. So here we are
  4153. 2:48:46going to delete the data which is most
  4154. 2:48:48recently used. This sound a bit strange,
  4155. 2:48:51right? Why do we delete something which
  4156. 2:48:52is most recently used?
  4157. 2:48:54But if you think about it, let's say you
  4158. 2:48:57are surfing through an e-commerce
  4159. 2:48:58application and you already apply a
  4160. 2:49:00particular coupon. So in that case we
  4161. 2:49:03don't need that particular coupon into
  4162. 2:49:05our cache, right? So we will directly
  4163. 2:49:07remove that. Also, if you think about
  4164. 2:49:10it, we have streaming applications. For
  4165. 2:49:12example, let's say this YouTube only. So
  4166. 2:49:14in YouTube's the streaming normally
  4167. 2:49:17happens in sections. When you are
  4168. 2:49:19watching any video, let's say you have
  4169. 2:49:21watched the video till here. At this
  4170. 2:49:23point this data gets loaded.
  4171. 2:49:26And you may see a white line moving
  4172. 2:49:29forward which denotes that the data up
  4173. 2:49:32to that point is loaded right now. Let's
  4174. 2:49:35say currently we are at this point and
  4175. 2:49:37this data is already loaded. Now at this
  4176. 2:49:39point we don't need the previous video
  4177. 2:49:41information. This can easily be deleted
  4178. 2:49:44from our cache at least.
  4179. 2:49:47Because in this case the chances of me
  4180. 2:49:50going back into the video is very slow.
  4181. 2:49:52So that's where we remove the most
  4182. 2:49:54recently used data. So I hope you are
  4183. 2:49:57getting all the points. The next part,
  4184. 2:49:59the next eviction policy is LFU. LFU
  4185. 2:50:03basically stands for least frequently
  4186. 2:50:07used. So LFU basically means the data
  4187. 2:50:09which is not frequently used will be
  4188. 2:50:12evicted from our cache. What does that
  4189. 2:50:14mean? Let's say today I opened any e-com
  4190. 2:50:18application and search for plant. Also,
  4191. 2:50:21I am a gadget guy. I basically purchase
  4192. 2:50:24a lot of gadgets. So suppose I have a
  4193. 2:50:27history of seeing any secondary screen
  4194. 2:50:31also. With that I also purchase a lot of
  4195. 2:50:33clothes. So clothes section is also
  4196. 2:50:36there. Now, between these three, I keep
  4197. 2:50:39on seeing about the secondary screen and
  4198. 2:50:41I keep on seeing the clothes in one or
  4199. 2:50:44the other manner, but the plant search I
  4200. 2:50:47only did once. Maybe I saw some YouTube
  4201. 2:50:50video or liked some plant into some
  4202. 2:50:53Instagram story of others. Only at that
  4203. 2:50:55time I thought of purchasing a plant and
  4204. 2:50:58after that I didn't think about it
  4205. 2:51:00again.
  4206. 2:51:01So, that means the plant is the least
  4207. 2:51:04frequently used terms for my history of
  4208. 2:51:07that e-commerce applications. So, now
  4209. 2:51:09whenever we are going to evict any data,
  4210. 2:51:11it is going to be this. So, this is
  4211. 2:51:13another strategy where we remove the
  4212. 2:51:16least frequently used data from our
  4213. 2:51:19cache. After LFU, we have two standard
  4214. 2:51:22ways of handling cache data or removing
  4215. 2:51:25the cache data. The first strategy is
  4216. 2:51:27FIFO. We all know about it. FIFO
  4217. 2:51:30basically stands for
  4218. 2:51:32first in, first out. Suppose we have
  4219. 2:51:34assigned 180 MB to our cache and we
  4220. 2:51:37don't want to exceed this limit.
  4221. 2:51:40Now, whenever we are trying to add any
  4222. 2:51:42value and and if we have reached our
  4223. 2:51:44limit, first data which we are going to
  4224. 2:51:47remove is the first data which we
  4225. 2:51:49entered into the cache without thinking
  4226. 2:51:51about anything, without thinking about
  4227. 2:51:52frequency, without thinking about how
  4228. 2:51:55recently they are being used. We will
  4229. 2:51:57simply remove the first added data from
  4230. 2:52:00our cache. And with this we have the
  4231. 2:52:02last eviction policy which is LIFO. LIFO
  4232. 2:52:05basically stands for last in, first out.
  4233. 2:52:08You may think of a stack kind of
  4234. 2:52:10situation here, right? Where the last
  4235. 2:52:12object which is inserted will be evicted
  4236. 2:52:15first.
  4237. 2:52:16So, this is how LIFO works. So, in this
  4238. 2:52:19what we will do, again we have 180 MB
  4239. 2:52:21memory and here we I have written bytes.
  4240. 2:52:24Here it is bits. Don't worry about that.
  4241. 2:52:26It doesn't matter. Now, in that 180 MB
  4242. 2:52:30memory, I don't want to store any other
  4243. 2:52:32information. So, what information will I
  4244. 2:52:35remove? I'll remove the most recent
  4245. 2:52:37information which I added. Okay? So,
  4246. 2:52:40maybe this information which is added
  4247. 2:52:42first
  4248. 2:52:44will be there for the longest time, and
  4249. 2:52:47we will keep on removing this or this or
  4250. 2:52:49this information in order to have more
  4251. 2:52:52space for the added information. So,
  4252. 2:52:54this is about LIFO and FIFO. I have not
  4253. 2:52:57added any application areas for LIFO and
  4254. 2:52:59FIFO both. I want you to explore this a
  4255. 2:53:02bit and tell me in the comments. And
  4256. 2:53:05with this, we have understood about
  4257. 2:53:06caches, their eviction policies, and how
  4258. 2:53:09do we add the data into our cache. I
  4259. 2:53:12hope you all get it. Up to now, we have
  4260. 2:53:14understood how our request basically
  4261. 2:53:16flows through different components of
  4262. 2:53:18our system and ultimately provide some
  4263. 2:53:20information to our user. Right? We have
  4264. 2:53:23seen who our customer is. They're going
  4265. 2:53:26to interact with either some web
  4266. 2:53:28application or maybe some mobile
  4267. 2:53:30application.
  4268. 2:53:31From where the request will flow to a
  4269. 2:53:34server where our back-end code is, and
  4270. 2:53:36through the server, we will hit some or
  4271. 2:53:39the other database in order to retrieve
  4272. 2:53:41that information and provide it back to
  4273. 2:53:43the user. With this, we have also
  4274. 2:53:45discussed about DNS through which our
  4275. 2:53:48clients are going to get the address of
  4276. 2:53:50our server. And then, the cycle repeats.
  4277. 2:53:53So, this is all we know up to now. This
  4278. 2:53:55all looks very good, works very
  4279. 2:53:57efficiently up to a single point. Why?
  4280. 2:54:00Because here we are using only single
  4281. 2:54:03server, and a single server can scale
  4282. 2:54:05only up to certain limits.
  4283. 2:54:07Suppose we are already using 128 GB of
  4284. 2:54:12our server, and somehow our processes
  4285. 2:54:15are consuming this much resources. That
  4286. 2:54:17means we are getting more requests than
  4287. 2:54:19we were expecting. Maybe our app is
  4288. 2:54:22tracking attraction now from across the
  4289. 2:54:24globe. And at that time, we have no
  4290. 2:54:26choice but to scale our system. In order
  4291. 2:54:29to scale the system, this is the first
  4292. 2:54:31step which you can think of.
  4293. 2:54:33So, instead of having a single server,
  4294. 2:54:35we need to have multiple servers. Let's
  4295. 2:54:39name them server one and server two.
  4296. 2:54:42So, we have two servers. That means our
  4297. 2:54:44client need to give the call to either
  4298. 2:54:47server one or server two. But, how our
  4299. 2:54:50client is going to decide where to give
  4300. 2:54:52information to? This is where we need to
  4301. 2:54:54discuss one more component which is load
  4302. 2:54:56balancer. Now, let's make the diagram
  4303. 2:54:58again with load balancer. Now, what
  4304. 2:55:00happens? Our client or the user is going
  4305. 2:55:04to access the same information from any
  4306. 2:55:06mobile application or website, and they
  4307. 2:55:09are going to hit DNS in order to get the
  4308. 2:55:13IP address of server. Up to now, things
  4309. 2:55:16are not changing. Everything is pretty
  4310. 2:55:17straightforward. But, now our DNS is not
  4311. 2:55:21going to give the IP address of the
  4312. 2:55:22actual server, but it is going to give
  4313. 2:55:24the IP address of load balancer. And
  4314. 2:55:27then, the load balancer is connected to
  4315. 2:55:30multiple servers. Suppose this is server
  4316. 2:55:32one,
  4317. 2:55:33this is server two, this is server
  4318. 2:55:35three. And then, the load balancer
  4319. 2:55:37basically decides to which server our
  4320. 2:55:40request should go to, whether it should
  4321. 2:55:41go to S1, S2, or S3. And if it goes to
  4322. 2:55:46S1, S2, and S3, they are basically going
  4323. 2:55:48to connect to a database. Maybe they are
  4324. 2:55:50connected to same database, or there can
  4325. 2:55:53be a possibility that S3 is managing the
  4326. 2:55:56data in some other database, and through
  4327. 2:55:58migration, replication, or partitioning,
  4328. 2:56:00we are handling the data between those
  4329. 2:56:02databases as well. So, this is what the
  4330. 2:56:05actual picture of a microservice
  4331. 2:56:07applications looks like. Where the load
  4332. 2:56:10balancer is going to play a huge role in
  4333. 2:56:13order to pass those requests from client
  4334. 2:56:16to server. Now, if we talk about load
  4335. 2:56:18balancer in brief, our load balancer
  4336. 2:56:21mainly is going to perform two tasks.
  4337. 2:56:23The first task is how do it give
  4338. 2:56:26responses to the perfect server from
  4339. 2:56:28which it can get the response faster,
  4340. 2:56:31right? So, the first thing is choosing
  4341. 2:56:34the server. Also, if we are giving the
  4342. 2:56:36responsibility of choosing the server to
  4343. 2:56:38load balancer, that means load balancer
  4344. 2:56:40should know how our servers are
  4345. 2:56:42performing in the current situation. So
  4346. 2:56:44that no server will be overheated and no
  4347. 2:56:47server will be underutilized. So, the
  4348. 2:56:50second thing
  4349. 2:56:51which load balancers are going to do is
  4350. 2:56:54maintaining the health checks of our
  4351. 2:56:57server. So that whenever any new request
  4352. 2:56:59comes to load balancer, they are the one
  4353. 2:57:02who is going to decide whichever server
  4354. 2:57:04is working in a fine condition plus can
  4355. 2:57:07handle that request pretty well.
  4356. 2:57:09So now, let's discuss these both points
  4357. 2:57:11one by one. So, in order to choose a
  4358. 2:57:14server, we need some algorithms, right?
  4359. 2:57:16So, there are various algorithms and
  4360. 2:57:18hybrids also from which our load server
  4361. 2:57:21decides where the request should go. So,
  4362. 2:57:25let's discuss those algorithms. The
  4363. 2:57:27first algorithm here is round robin.
  4364. 2:57:30Round robin is the most simplistic
  4365. 2:57:32approach in order to pick the web server
  4366. 2:57:34for the next request. So, in round
  4367. 2:57:36robin, what do we do? Suppose this is
  4368. 2:57:38our load balancer and we have our server
  4369. 2:57:41one here, server two here, and server
  4370. 2:57:43three here. Load balancer is now going
  4371. 2:57:46to pick server one for the first
  4372. 2:57:49request. For second request, it is going
  4373. 2:57:51to pick server two. For third request,
  4374. 2:57:53it is going to pick server three. And
  4375. 2:57:55where will the fourth request go? It
  4376. 2:57:57will again go to server one. Then five
  4377. 2:58:00for server two and six for server three.
  4378. 2:58:02And this is how every request will keep
  4379. 2:58:05generating and going to the next server
  4380. 2:58:07which is in the list. So, there is no
  4381. 2:58:09much calculation needed here. We only
  4382. 2:58:11need to know that we are following the
  4383. 2:58:13sequence of S1, S2, and S3. And if the
  4384. 2:58:16current request is being processed by
  4385. 2:58:18S2, the next request is going to be on
  4386. 2:58:21S3. This is how round robin works. So,
  4387. 2:58:24that means round robin send request one
  4388. 2:58:27by one, right? Apart from this, it
  4389. 2:58:30doesn't check anything, which makes the
  4390. 2:58:32round robin very easy to implement. That
  4391. 2:58:35means if you are using round robin
  4392. 2:58:37algorithm, each server are going to
  4393. 2:58:40handle almost equal number of requests.
  4394. 2:58:42So, we can say the distribution of
  4395. 2:58:45requests are equal. And as in this
  4396. 2:58:48algorithm, we are not managing any state
  4397. 2:58:50of any server, basically, we'll manage
  4398. 2:58:53only a single counter from which we are
  4399. 2:58:56going to decide where the next request
  4400. 2:58:58is going to be. And also, we need to
  4401. 2:59:00manage where the current request is. So,
  4402. 2:59:04with simple implementation, we can
  4403. 2:59:05easily set up round robin into a load
  4404. 2:59:08balancer. But, there are some demerits
  4405. 2:59:10to it. What if server one, server two,
  4406. 2:59:13server three doesn't have equal
  4407. 2:59:15configurations? What if the RAM
  4408. 2:59:17configuration between them is different?
  4409. 2:59:20What if the storage is different? What
  4410. 2:59:23if the CPU power is different? Let's say
  4411. 2:59:26for S1, RAM is 4GB.
  4412. 2:59:29For S2, it is 8GB.
  4413. 2:59:32For S3, it is 16GB.
  4414. 2:59:35Again, the storage, let's say for server
  4415. 2:59:38one, we are using maybe 100GB storage.
  4416. 2:59:42For server two, we are using 200GB
  4417. 2:59:44storage. And for server three, we are
  4418. 2:59:46using 500GB of storage. Also, the CPU,
  4419. 2:59:50let's say it is 16 core, it is 32, it is
  4420. 2:59:5364 core. So, the configurations are
  4421. 2:59:56entirely different. And what we are
  4422. 2:59:57doing in round robin, we are not
  4423. 2:59:59checking any of these matrixes, but we
  4424. 3:00:02are simply following the next counter.
  4425. 3:00:04So, that means even if with uneven
  4426. 3:00:07configurations,
  4427. 3:00:08the load on server one, server two, and
  4428. 3:00:11server three are going to be same. And
  4429. 3:00:13this is unfair because the server three
  4430. 3:00:16may do all the task very frequently and
  4431. 3:00:18can sit idle. Whereas server one will
  4432. 3:00:21struggle to complete the task and it
  4433. 3:00:24will be overloaded, maybe heat up, maybe
  4434. 3:00:26stop working. So, this is the demerits
  4435. 3:00:29of using round robin. Now, let's jump to
  4436. 3:00:31the second algorithm, which is
  4437. 3:00:33geo-based, which basically states that
  4438. 3:00:37suppose this is our load balancer and we
  4439. 3:00:39have our server one again and server two
  4440. 3:00:42again. This is in India and this is in
  4441. 3:00:46US. So, if the majority of users are
  4442. 3:00:50from India and some users are from US,
  4443. 3:00:53that means they need to hit their
  4444. 3:00:55respective servers in order to process
  4445. 3:00:57the information. That way,
  4446. 3:01:00they will get the response faster
  4447. 3:01:03because if I'm sitting in India and I'm
  4448. 3:01:05requesting any information from the
  4449. 3:01:06Indian server, it will be faster to
  4450. 3:01:09process those information in the Indian
  4451. 3:01:10server as well with respect to
  4452. 3:01:12processing the same information through
  4453. 3:01:14US servers because the connections are
  4454. 3:01:16going to take time and which will
  4455. 3:01:18increase the latency of the application,
  4456. 3:01:20which the user doesn't want, right? So,
  4457. 3:01:22this is what our second algorithm
  4458. 3:01:24implies. It is based on geo addresses of
  4459. 3:01:28the particular user. So, here also we
  4460. 3:01:30are not maintaining any matrices for
  4461. 3:01:32server specifications. We are only
  4462. 3:01:34maintaining two matrices. The number one
  4463. 3:01:37matrix is where the server is and the
  4464. 3:01:40second matrix is from where are we
  4465. 3:01:42getting the request or we can say where
  4466. 3:01:45the user is. Now, this algorithm is used
  4467. 3:01:48by many applications across the world
  4468. 3:01:50whichever have the global appearance.
  4469. 3:01:53Whenever we are trying to hit google.com
  4470. 3:01:55or facebook.com or any other global
  4471. 3:01:57applications, our request basically
  4472. 3:02:00moves to the server whichever is closest
  4473. 3:02:03to us. Maybe we are residing in this
  4474. 3:02:05area. Here we have some server and also
  4475. 3:02:08here we have some server. Maybe this is
  4476. 3:02:10US server, this is Indian server.
  4477. 3:02:13So, our server needs to be situated near
  4478. 3:02:16us so that we get the response faster
  4479. 3:02:19and all the requests are maintained
  4480. 3:02:21through the same server. So, whenever
  4481. 3:02:23I'm trying to access any information,
  4482. 3:02:25they are going to be addressed through
  4483. 3:02:27the same server. So, that means our
  4484. 3:02:29session and other informations are also
  4485. 3:02:32will be in sync for this particular
  4486. 3:02:34user. In this algorithm, there are some
  4487. 3:02:37disadvantages as well, right? Because
  4488. 3:02:39now our load server needs to manage more
  4489. 3:02:42information.
  4490. 3:02:43Now, it needs to know from where the
  4491. 3:02:46requests are coming and it needs to know
  4492. 3:02:48where each and every server is located.
  4493. 3:02:51So, this is a lot of information to be
  4494. 3:02:54seen by load balancer. And now as our
  4495. 3:02:56servers are distributed across the
  4496. 3:02:58globe, we also need to maintain
  4497. 3:03:01databases accordingly. So, every region
  4498. 3:03:04may have their own databases for faster
  4499. 3:03:06retrieval. So, that will include the
  4500. 3:03:08computation power of replication and
  4501. 3:03:10partitioning, which we will be covering
  4502. 3:03:12in further series. Also, let's say
  4503. 3:03:16if we are using Indian server or this
  4504. 3:03:18server particularly, maybe there are two
  4505. 3:03:20servers in India. This is India B
  4506. 3:03:22server, this is India A server. For some
  4507. 3:03:25reason, this server breaks down.
  4508. 3:03:28Then we need to reroute our request to
  4509. 3:03:30this particular server, which is again
  4510. 3:03:33needs all the information which we are
  4511. 3:03:34accessing through this server. That
  4512. 3:03:36means the data or the application on
  4513. 3:03:38which we are working on should transfer
  4514. 3:03:40the data immediately to this server as
  4515. 3:03:43well. Even if the server A doesn't
  4516. 3:03:45crash, then we need all the information
  4517. 3:03:47which is being tracked by server A. But
  4518. 3:03:50this is a necessity in geo-based
  4519. 3:03:52location algorithm. Now, the third point
  4520. 3:03:54here is if any user is using VPN or
  4521. 3:03:58other proxy servers to change their
  4522. 3:04:00location, that means VPN or proxies can
  4523. 3:04:03mislead the traffic. With this, let's
  4524. 3:04:05move to our next algorithm, which is
  4525. 3:04:07least connection algorithm. So, in this,
  4526. 3:04:10what happens is we have our load
  4527. 3:04:12balancer in place.
  4528. 3:04:14And suppose we have two servers, server
  4529. 3:04:16one and server two. And server one and
  4530. 3:04:19server two at current times are managing
  4531. 3:04:22multiple requests, right? Suppose S1 is
  4532. 3:04:26maintaining
  4533. 3:04:2730 requests or 30 connections at this
  4534. 3:04:29particular time and S2 is maintaining 50
  4535. 3:04:32connections.
  4536. 3:04:33Now, whenever we get a new request, it
  4537. 3:04:36is going to be addressed by S1. Why?
  4538. 3:04:39Because S1 currently is having least
  4539. 3:04:41connections.
  4540. 3:04:43So, this is how our least connection
  4541. 3:04:45algorithm works. Whichever server have
  4542. 3:04:48the least connections, our next request
  4543. 3:04:50is going to that particular server. In
  4544. 3:04:52this algorithm also, we are not
  4545. 3:04:54considering any of the matrices of
  4546. 3:04:57server. We are also not maintaining any
  4547. 3:04:59of the requests or response time. We are
  4548. 3:05:02only maintaining the number of
  4549. 3:05:04connections. Let's say if these 30
  4550. 3:05:07requests are heavy request and these 50
  4551. 3:05:10are lightweight, then also the next
  4552. 3:05:13request is going to be taken care by S1,
  4553. 3:05:16which is wrong and it is going to take
  4554. 3:05:18time, but this is how our least
  4555. 3:05:20connections work. Then why do we use
  4556. 3:05:22this? Basically, the least connection
  4557. 3:05:24algos works very well when there are
  4558. 3:05:26sticky sessions. Now, what is sticky
  4559. 3:05:29session? Where we have to maintain a
  4560. 3:05:32proper channel for a particular
  4561. 3:05:33communication. Let's say we are using
  4562. 3:05:35web sockets for maintaining any kind of
  4563. 3:05:38application. Let's say we are
  4564. 3:05:39maintaining a chat application or a
  4565. 3:05:41video streaming application or maybe we
  4566. 3:05:44are handling some gaming kind of setup.
  4567. 3:05:47In all of these cases, we needs the
  4568. 3:05:49request and response to be faster, so we
  4569. 3:05:52will pass our request to those servers
  4570. 3:05:55who are having least connections
  4571. 3:05:57currently so that we can get the
  4572. 3:06:00response faster. Now, if we talk to
  4573. 3:06:03disadvantages, the first disadvantage is
  4574. 3:06:05this only heavy and lightweight. Maybe
  4575. 3:06:07these calls are for AI calls which are
  4576. 3:06:09going to take time and these calls are
  4577. 3:06:12basically chat calls or maybe some web
  4578. 3:06:14CTR calls or maybe some REST API calls
  4579. 3:06:17which are lightweighted normally. So,
  4580. 3:06:19they are working faster then also we
  4581. 3:06:22will send the next request to 30 until
  4582. 3:06:24the number of connections drops from 50
  4583. 3:06:26to maybe 29. Then only we will consider
  4584. 3:06:29S2. So, this is how we are going to
  4585. 3:06:31work. Now, if we talk about
  4586. 3:06:33disadvantages of least connections, the
  4587. 3:06:35first we already discussed. The second
  4588. 3:06:38disadvantage is that our load balancer
  4589. 3:06:40basically needs to maintain the number
  4590. 3:06:42of connections for each web server. So,
  4591. 3:06:45if S1 is maintaining 50 connections at
  4592. 3:06:48that time and it give response to one of
  4593. 3:06:50them, that means currently it is
  4594. 3:06:52maintaining 49 connections. It needs to
  4595. 3:06:55be updated to load balancer. So, our
  4596. 3:06:58node balancer will keep updating the
  4597. 3:07:01number of connections again and again
  4598. 3:07:03which is of course a tedious task. The
  4599. 3:07:05third disadvantage is we are only
  4600. 3:07:07considering least number of connections.
  4601. 3:07:10What if our server one is of very low
  4602. 3:07:13configuration than server two
  4603. 3:07:15and it is maintaining eight connections
  4604. 3:07:17at that time or request at that time and
  4605. 3:07:20this is maintaining 16.
  4606. 3:07:22It is easy for S2 to maintain 16
  4607. 3:07:24connections or maybe 100 connections and
  4608. 3:07:26it is hard to maintain 10 connections
  4609. 3:07:28for S1. Then also we will send the next
  4610. 3:07:31request to S1 because we are not
  4611. 3:07:33checking anything apart from number of
  4612. 3:07:35connections. So, this is how our least
  4613. 3:07:37connection algorithm is going to work.
  4614. 3:07:40After least connections come least time.
  4615. 3:07:43So, it is our fourth algorithm least
  4616. 3:07:46time algo. Suppose load balancer is
  4617. 3:07:50maintaining two web servers S1
  4618. 3:07:53and S2. The response time of S1 is
  4619. 3:07:57nearly about 50 ms. The average response
  4620. 3:08:01time, basically. It is handling some
  4621. 3:08:03thousand requests, and for server two,
  4622. 3:08:05it is currently 10 ms. So, in this
  4623. 3:08:08scenario, the next request is going to
  4624. 3:08:10be handled by server two. Because the
  4625. 3:08:13average response time of server two is
  4626. 3:08:15lesser than the average response time of
  4627. 3:08:18server one. Now, if we talk about
  4628. 3:08:20response time, if any server is having
  4629. 3:08:22less response time, that means the
  4630. 3:08:24configurations of those servers are
  4631. 3:08:27high. Right? That's why the response
  4632. 3:08:29time is low. So, this algorithm
  4633. 3:08:32basically takes care of the actual
  4634. 3:08:33matrices of the server by taking the
  4635. 3:08:35response time into measure. Now, the
  4636. 3:08:37least time algorithm is used in those
  4637. 3:08:39applications where we can't handle more
  4638. 3:08:42latency. That can be our real-time
  4639. 3:08:44applications, like the trading app or
  4640. 3:08:47even our search engines. In both of
  4641. 3:08:50these areas, we want the responses to be
  4642. 3:08:52very fast, and that's why we use least
  4643. 3:08:54time algorithms there. And now, if we
  4644. 3:08:56talk about least time algo's
  4645. 3:08:58disadvantage, that is our load balancer
  4646. 3:09:02needs to calculate the average response
  4647. 3:09:04time. So, with each request they are
  4648. 3:09:06sending to server, also the average
  4649. 3:09:09response time will be calculated. And
  4650. 3:09:11based on this, our load balancer needs
  4651. 3:09:14to manage where the next request is
  4652. 3:09:16going to be. So, calculating these
  4653. 3:09:18things can be really complex because if
  4654. 3:09:20we are calculating average time, that
  4655. 3:09:22means we need to know number of maybe
  4656. 3:09:24requests, and also the response time of
  4657. 3:09:28each and every response. Then only our
  4658. 3:09:32load balancer is going to calculate the
  4659. 3:09:33average response time. So, this is for
  4660. 3:09:36sure complex. Now, in this scenario, if
  4661. 3:09:39our web servers maintain any kind of
  4662. 3:09:41spike, it is going to affect the
  4663. 3:09:43calculations of our load balancer. So,
  4664. 3:09:45we need to be very careful there. Now,
  4665. 3:09:47let's talk about the fifth algorithm,
  4666. 3:09:49which is IP hash. Here, as the name
  4667. 3:09:52suggests, the IP of every request is
  4668. 3:09:56going to be processed by our load
  4669. 3:09:58balancer and will be given a unique
  4670. 3:10:00number.
  4671. 3:10:02And according to those numbers, our
  4672. 3:10:04servers are going to decide. Let's say
  4673. 3:10:06our server one is maintaining the hash
  4674. 3:10:08values from one to 10. So, that means
  4675. 3:10:10all the IPs which results between this
  4676. 3:10:13range are going to be addressed by S1
  4677. 3:10:15and maybe from 11 to 20 are addressed by
  4678. 3:10:18S2.
  4679. 3:10:19So, this is how IP hash algorithm works.
  4680. 3:10:23Every request is associated with some of
  4681. 3:10:25the other IP. They are going to process
  4682. 3:10:27through a hash function, and suppose the
  4683. 3:10:29result is five, then it is going to be
  4684. 3:10:32addressed by S1 because S1 is
  4685. 3:10:34maintaining one to 10. As we all know,
  4686. 3:10:36the IP of any user normally doesn't
  4687. 3:10:39change. That means this user for at
  4688. 3:10:41least one session is going to be
  4689. 3:10:43addressed through the same server. And
  4690. 3:10:45it will help. Basically, if we are
  4691. 3:10:47maintaining any session data, it is
  4692. 3:10:49going to help because we don't need to
  4693. 3:10:50replicate this data to other servers. We
  4694. 3:10:52are only handling these requests through
  4695. 3:10:55the same server. So, it is very easy.
  4696. 3:10:57Now, if the connection breaks in between
  4697. 3:11:00the session, then we are in problem. In
  4698. 3:11:03that case, this next request will go to
  4699. 3:11:06S2, and we need all the session data
  4700. 3:11:08here. And if that data is not
  4701. 3:11:10maintained, our user may need to address
  4702. 3:11:13or add the same data again into server
  4703. 3:11:15two, which is a loss case, which is not
  4704. 3:11:17recommended at all. So, for this to work
  4705. 3:11:19efficiently, our server basically needs
  4706. 3:11:21to be healthy enough to maintain a
  4707. 3:11:23proper session. Plus, even if the
  4708. 3:11:26session breaks due to any reason, due to
  4709. 3:11:28any server crash, it is very important
  4710. 3:11:31to replicate those data to other server
  4711. 3:11:33as well. The major disadvantage of this
  4712. 3:11:36is, as we are basically focused on IP
  4713. 3:11:39functions and hash functions, so scaling
  4714. 3:11:42is challenging here. Let's say we are
  4715. 3:11:43adding S3 here, then we again needs to
  4716. 3:11:47reconfigure our hash function. So, that
  4717. 3:11:50it consider S3 as well. And also, we
  4718. 3:11:52need to reassign these values also.
  4719. 3:11:55Maybe next time it is only handling 1 to
  4720. 3:11:588, and maybe it is handling
  4721. 3:12:00uh
  4722. 3:12:019 to 12 maybe, and this one is handling
  4723. 3:12:0412 to 20. So, whenever any server is
  4724. 3:12:07down or we are adding any new server,
  4725. 3:12:10basically the function will get affected
  4726. 3:12:12and it will reassign all the values to
  4727. 3:12:14the entire server. This is hard to scale
  4728. 3:12:16in this manner. And that's why it is not
  4729. 3:12:19recommended for most of the
  4730. 3:12:21microservices architecture we have
  4731. 3:12:23today. It is basically used where the
  4732. 3:12:25data was used in the old kind of
  4733. 3:12:27applications where the applications were
  4734. 3:12:30monolithic and mostly were legacy apps.
  4735. 3:12:34Like in today's scenario, if you want to
  4736. 3:12:35maintain a session of a particular user,
  4737. 3:12:37we don't only stick for that particular
  4738. 3:12:39session. We use many other tools like
  4739. 3:12:42JWT token we use, right? So, this is how
  4740. 3:12:45we are going to maintain an entire
  4741. 3:12:47connection through a server and a
  4742. 3:12:48client. We don't only rely on giving the
  4743. 3:12:51request to the same server again and
  4744. 3:12:53again. Even if the server changes, it is
  4745. 3:12:55going to recognize us through our JWT
  4746. 3:12:57token. And this is going to work
  4747. 3:12:59excellently with our distributed
  4748. 3:13:01systems,
  4749. 3:13:02microservices, and also if we use Redis.
  4750. 3:13:06In all these scenarios, we now don't
  4751. 3:13:08need IP hash functions. We don't need
  4752. 3:13:11our request to be handled through same
  4753. 3:13:13server. Even if the server changes, our
  4754. 3:13:16request or our session should be
  4755. 3:13:18consistent. Also, there is another
  4756. 3:13:20algorithm which is extension of round
  4757. 3:13:22robin, which is called weighted round
  4758. 3:13:24robin. So, in which the round robin, as
  4759. 3:13:27it was discussed, is working same, but
  4760. 3:13:30we added weight. So, that means
  4761. 3:13:33let's say this is S1, this is S2, and
  4762. 3:13:36this is S3. Let's say the RAM capacity
  4763. 3:13:39of S1 is 8 GB, for S2 it is 16, and for
  4764. 3:13:43S3 it is 32 GB. Now, what we are doing,
  4765. 3:13:47we are adding a weight to our request.
  4766. 3:13:50So, more the configuration,
  4767. 3:13:52more is going to be the weight. So, if
  4768. 3:13:55our load balancer basically keeps track
  4769. 3:13:58of this, so it knows that the S3 may
  4770. 3:14:01have weight three, S2 may have weight
  4771. 3:14:03two, and S1 may have weight one. So,
  4772. 3:14:06whenever a new request arrives at load
  4773. 3:14:09balancer, it is going to be addressed by
  4774. 3:14:12the one which have the most weight. So,
  4775. 3:14:15here the weight of S3 is maximum, so it
  4776. 3:14:17is going to be addressed by our S3
  4777. 3:14:19server. And then, based on the weight
  4778. 3:14:21and round robin fashion, our request are
  4779. 3:14:24going to be answered. So, with weighted
  4780. 3:14:26round robin algorithm also added in the
  4781. 3:14:28list, we have now covered all the
  4782. 3:14:29algorithms, major algorithms basically,
  4783. 3:14:32which we needs to know when we talk
  4784. 3:14:34about load balancers. Also, in real
  4785. 3:14:37base, no one algorithm basically works.
  4786. 3:14:39We always work with hybrid approach.
  4787. 3:14:42And load balancer basically balances
  4788. 3:14:45multiple algorithms. It is very possible
  4789. 3:14:48that we are using maybe IP hash with the
  4790. 3:14:51combination of round robin. And also,
  4791. 3:14:54other combinations are also possible.
  4792. 3:14:55Maybe we are combining two, maybe we are
  4793. 3:14:58combining all six at the same time. Like
  4794. 3:15:00here, we were talking about least time
  4795. 3:15:03algorithm and least connection
  4796. 3:15:05algorithm. We can combine these two
  4797. 3:15:07algorithms in order to get a better
  4798. 3:15:08algorithm. So, in order to fulfill our
  4799. 3:15:11requirement, we normally don't rely on a
  4800. 3:15:13single algorithm, we rely on multiple
  4801. 3:15:16hybrid algorithms. So, after discussing
  4802. 3:15:19all the algorithms, we know now how the
  4803. 3:15:21load balancer basically chooses a server
  4804. 3:15:23for the next request. The second thing
  4805. 3:15:25which load balancer does is doing the
  4806. 3:15:28health checks. Let's understand how load
  4807. 3:15:30balancer basically manages the health
  4808. 3:15:32checks. Here the thing is we have two
  4809. 3:15:34approaches how a load balancer can check
  4810. 3:15:37the health checks of entire servers.
  4811. 3:15:39Let's say we have a load balancer here
  4812. 3:15:41again and we have server one
  4813. 3:15:45and server two. The first way how we can
  4814. 3:15:47check any performance is by simply
  4815. 3:15:49seeing how server one and server two are
  4816. 3:15:52performing. We are not adding any new
  4817. 3:15:54request in server one or server two. We
  4818. 3:15:56are just observing how they are
  4819. 3:15:58responding to the actual requests. This
  4820. 3:16:01is the passive way of checking the
  4821. 3:16:02health. By this, we don't add any new
  4822. 3:16:05request to the servers. We don't
  4823. 3:16:07basically increase the load of any
  4824. 3:16:09server, but we simply observe. By we, I
  4825. 3:16:13mean load balancer. The load balancer
  4826. 3:16:15simply observe how the server one is
  4827. 3:16:18responding to request and maintain that
  4828. 3:16:21track. And let's say at a particular
  4829. 3:16:23point, if server one breaks or stops
  4830. 3:16:25responding, at that time the load
  4831. 3:16:27balancer knows about this by again
  4832. 3:16:30tracking the actual request. It doesn't
  4833. 3:16:33add any new request to server one. So,
  4834. 3:16:35this is what the passive way is. Now,
  4835. 3:16:38there is another way which is active
  4836. 3:16:40way. In active way, what a load balancer
  4837. 3:16:42does is it adds additional request to
  4838. 3:16:45our servers. And based on the responses
  4839. 3:16:48on those requests, it checks the actual
  4840. 3:16:51health of the server. So, these are the
  4841. 3:16:53two ways of checking the health of
  4842. 3:16:56servers. One by simply observing the
  4843. 3:16:59request and second by actively adding
  4844. 3:17:01the request to the servers. It is very
  4845. 3:17:03important to check the health of every
  4846. 3:17:06server. Why? Because it gives us the
  4847. 3:17:08pattern to take decisions. Like when to
  4848. 3:17:11scale or when to scale down. Both of
  4849. 3:17:14these cases are very usual by working on
  4850. 3:17:16any application on a day-to-day basis.
  4851. 3:17:18Let's suppose if we are maintaining our
  4852. 3:17:20application only for India region, in
  4853. 3:17:23that case also we know that in day time
  4854. 3:17:25the requests are going to be heavy.
  4855. 3:17:28Maybe in evening time it will be
  4856. 3:17:29heavier. And in night time it is going
  4857. 3:17:31to be little or lesser in count. So, at
  4858. 3:17:35that time we can maybe scale down our
  4859. 3:17:37server in the night time in order to
  4860. 3:17:39save some cost. So, let's see what are
  4861. 3:17:41the other parameters of health checks.
  4862. 3:17:44The first parameter of health check is
  4863. 3:17:47interval. This means how often do we
  4864. 3:17:49check our server? For example, it can be
  4865. 3:17:52maybe 5 seconds. So, if the case is
  4866. 3:17:54active, every 5 seconds I'll monitor all
  4867. 3:17:57the requests I've sent to the server and
  4868. 3:18:00see how the performance is going. And
  4869. 3:18:02also, in the passive case we'll see the
  4870. 3:18:05responses and calculate the matrices
  4871. 3:18:07every 5 seconds. The second params can
  4872. 3:18:10be time out. So, time out basically
  4873. 3:18:12means for how long do we wait to get the
  4874. 3:18:15response? Suppose there is any server
  4875. 3:18:18who normally takes from maybe 50
  4876. 3:18:20milliseconds
  4877. 3:18:21to the maximum of maybe 5 seconds
  4878. 3:18:25in order to process any request from a
  4879. 3:18:28long time. And now suddenly it is taking
  4880. 3:18:3110 seconds. So, this should be an
  4881. 3:18:33alarming case and we should immediately
  4882. 3:18:35check what went wrong. Okay, with that
  4883. 3:18:37particular web server. What is that
  4884. 3:18:39particular request which is taking 10
  4885. 3:18:41seconds? Is there any issue with the
  4886. 3:18:43code? Is there any issue with the
  4887. 3:18:44request? Does Do we change any
  4888. 3:18:47configuration? Or does our server is
  4889. 3:18:49managing some other task because of
  4890. 3:18:51which our this request is being
  4891. 3:18:53affected? We need to know the answers of
  4892. 3:18:56all of these questions in order to make
  4893. 3:18:57a better decision. We also need to know
  4894. 3:18:59about the threshold. Threshold can be
  4895. 3:19:02like healthy and unhealthy both, right?
  4896. 3:19:04In unhealthy, we basically take care of
  4897. 3:19:06the failed calls. So, we will basically
  4898. 3:19:09maybe set an alarm after 20 failed
  4899. 3:19:13calls. Okay? We can configure them based
  4900. 3:19:16on a particular region, based on a
  4901. 3:19:18particular area of our code, maybe a
  4902. 3:19:21route, particular subdomain of a route,
  4903. 3:19:24or maybe randomly. At a particular time,
  4904. 3:19:26if 20 calls fail, that is an alarming
  4905. 3:19:29situation for us. In normal scenario,
  4906. 3:19:32many application may give a faulty
  4907. 3:19:34response based on various factors. It
  4908. 3:19:36happens on a day-to-day basis, but after
  4909. 3:19:38that n threshold, after that count of
  4910. 3:19:41threshold, we should start investigating
  4911. 3:19:44the cause. And similarly, we have our
  4912. 3:19:47healthy threshold, where we should put
  4913. 3:19:49our marker maybe after every some calls.
  4914. 3:19:53So, maybe after every 100 calls, 100
  4915. 3:19:56positive calls, we need to check is
  4916. 3:19:58everything fine with those calls. The
  4917. 3:20:00response was expected, and the system is
  4918. 3:20:02working the way it should work. So,
  4919. 3:20:04these are the three health parameters of
  4920. 3:20:07our web servers which our load balancer
  4921. 3:20:09needs to check end over time in order to
  4922. 3:20:11make sure that our web servers are
  4923. 3:20:13working fine. And with this, we now
  4924. 3:20:15understand the two major factors of load
  4925. 3:20:17balancer, how it gives request to the
  4926. 3:20:20other server, and plus, how do it check
  4927. 3:20:23the health of every server. Now that we
  4928. 3:20:25have understood load balancer, it is
  4929. 3:20:27very important to know that in terms of
  4930. 3:20:29scaling, we also need to scale our
  4931. 3:20:31databases, right? So, in databases, what
  4932. 3:20:34normally happens is, suppose we have 100
  4933. 3:20:36GB of data, and we have a storage of
  4934. 3:20:38maybe 200 GB. This is still fine, right?
  4935. 3:20:41Maybe we have 1 TB of data, maybe 100 TB
  4936. 3:20:44of data, it doesn't matter. The thing
  4937. 3:20:46which matter is this data will
  4938. 3:20:48eventually get full.
  4939. 3:20:50And there will come a stage where you
  4940. 3:20:53can't expand your databases vertically.
  4941. 3:20:56You have to add more databases into our
  4942. 3:20:59system
  4943. 3:21:00whenever it comes to scaling. Also, now
  4944. 3:21:03that we know about load balancers, we
  4945. 3:21:05know that our server
  4946. 3:21:06can be placed into different regions of
  4947. 3:21:09our world, right? This can be placed
  4948. 3:21:10into US region, this can be placed into
  4949. 3:21:13India region only. And what we were
  4950. 3:21:15doing is we were adding both of these
  4951. 3:21:17data into single database. This is
  4952. 3:21:19effective if the user count is one, the
  4953. 3:21:21request we are getting are minimal. But
  4954. 3:21:24if the user increases, the traffic
  4955. 3:21:26increases, the data increases, it will
  4956. 3:21:28be hard to cater both of these servers
  4957. 3:21:30from the single database.
  4958. 3:21:32This is where we need to segregate this
  4959. 3:21:34data into multiple databases. One may be
  4960. 3:21:38catering Indian server and the other
  4961. 3:21:40might be catering the US server. Now,
  4962. 3:21:42this way we segregated our data into two
  4963. 3:21:44databases so that the requests can be
  4964. 3:21:46faster and also with that the data will
  4965. 3:21:49be segregated in such a manner that it
  4966. 3:21:51will be easy to maintain. Now, this can
  4967. 3:21:53be one of the case where we don't need
  4968. 3:21:56to have same data into two databases. In
  4969. 3:21:59this case, what we are doing, we are
  4970. 3:22:00assuming that the data in this data
  4971. 3:22:02table will be different from the data
  4972. 3:22:05into this database. Normally, this is
  4973. 3:22:07not the case, right? Normally, what can
  4974. 3:22:09happen is suppose we have multiple
  4975. 3:22:12servers for Indian region only. So, this
  4976. 3:22:14is also Indian region, this is also
  4977. 3:22:16Indian region. And we can have multiple
  4978. 3:22:18databases to cater the same server. Why?
  4979. 3:22:21Because if we have a single database and
  4980. 3:22:23if that fails, we don't have anywhere to
  4981. 3:22:25go and recover the data from. That's why
  4982. 3:22:27we manage multiple databases in order to
  4983. 3:22:30recover the data, maybe maintain the
  4984. 3:22:32data, and also it increase the
  4985. 3:22:34efficiency of our system by reducing the
  4986. 3:22:36latency. And also we may have some
  4987. 3:22:38portion of data here and some portion of
  4988. 3:22:40data here.
  4989. 3:22:41Both of these things are possible. These
  4990. 3:22:44things are basically
  4991. 3:22:46replication and partitioning. In
  4992. 3:22:49replication, what we do, we basically
  4993. 3:22:51copy the data from one database to other
  4994. 3:22:54database so that even if anything
  4995. 3:22:56happens to database one, we are able to
  4996. 3:22:59retrieve the data from database two.
  4997. 3:23:00Also, if database one is handling so
  4998. 3:23:03much calls, we can redirect those
  4999. 3:23:05requests to database two as well. And in
  5000. 3:23:07partitioning, what we do is we basically
  5001. 3:23:09divide the data into single database and
  5002. 3:23:12put one part here into other database
  5003. 3:23:15and the other part anywhere else. So
  5004. 3:23:17that if they are getting different
  5005. 3:23:19requests, it will be easier to cater
  5006. 3:23:21each and every request with their
  5007. 3:23:22specific database. We are going to cover
  5008. 3:23:24both of these topics, but for now let's
  5009. 3:23:26talk about replication. So we have
  5010. 3:23:28understood that replication basically
  5011. 3:23:30means keeping the copy of data. And why
  5012. 3:23:33are we doing this? Why are we making
  5013. 3:23:35copies? The first point is we are
  5014. 3:23:37avoiding or improving our single point
  5015. 3:23:40of failure issue. We don't want our
  5016. 3:23:41database to fail and our system to
  5017. 3:23:43collapse. We want some other database at
  5018. 3:23:45that time so that our system will be
  5019. 3:23:47working. The other reason of this is
  5020. 3:23:50availability. By having data into
  5021. 3:23:52multiple databases, our availability as
  5022. 3:23:54a system can increase. We can have
  5023. 3:23:56different data centers and it will
  5024. 3:23:58increase our performance. Also, as we
  5025. 3:24:01discussed, if we talk about different
  5026. 3:24:02localities or different regions of area,
  5027. 3:24:05we are increasing the performance by
  5028. 3:24:07adding particular database into that
  5029. 3:24:09site. So that will keep the data close
  5030. 3:24:11to our server and will be retrieved
  5031. 3:24:13faster. And also with all this, we are
  5032. 3:24:15eventually increasing our read
  5033. 3:24:17throughput. So suppose with a single
  5034. 3:24:20database, we were catering maybe 10,000
  5035. 3:24:23requests per second. Now with two
  5036. 3:24:25databases of same storage and data, we
  5037. 3:24:27can cater two times of the previous one.
  5038. 3:24:29So this is how the three throughput will
  5039. 3:24:31also increase. So after understanding
  5040. 3:24:33what replication is and why do we use
  5041. 3:24:36it, let's talk about how can we achieve
  5042. 3:24:38replication. So we are going to talk
  5043. 3:24:40about three algorithms. The first
  5044. 3:24:42algorithm is single leader replication.
  5045. 3:24:46That means there will be a database
  5046. 3:24:49which will be our leader and from the
  5047. 3:24:52leader only we will replicate the data
  5048. 3:24:54to other follower nodes or follower data
  5049. 3:24:56centers.
  5050. 3:24:57Suppose there is any user who is maybe
  5051. 3:25:00updating their profile picture. And when
  5052. 3:25:03they are updating their profile picture,
  5053. 3:25:04the request from our client basically
  5054. 3:25:07moves to a server and from server we are
  5055. 3:25:10only sending that request to our leader
  5056. 3:25:12only. We don't directly communicate to
  5057. 3:25:15our followers because it is the leader
  5058. 3:25:18who is going to then replicate our data
  5059. 3:25:19to followers. So every request will go
  5060. 3:25:22to our leader. To be specific, every
  5061. 3:25:24write request will go to our leader and
  5062. 3:25:26then the data will be replicated to our
  5063. 3:25:28followers. This is how a single leader
  5064. 3:25:31approach works. Let's understand this
  5065. 3:25:33through an another diagram. This is our
  5066. 3:25:35user. Maybe this is our leader and we
  5067. 3:25:39have our F1 follower database and
  5068. 3:25:42follower two. Assume this as a timeline,
  5069. 3:25:45okay? Now what we are going to do,
  5070. 3:25:46suppose at this point the user is
  5071. 3:25:49requesting to update the profile
  5072. 3:25:51picture. And this request will
  5073. 3:25:53eventually go to our leader node and
  5074. 3:25:56once the request is taken care by
  5075. 3:25:58leader,
  5076. 3:25:59they will send the same request to our
  5077. 3:26:01follower nodes as well.
  5078. 3:26:03Follower node one, maybe it is taking
  5079. 3:26:05longer time because it is far. So at
  5080. 3:26:07this point the request went to follower
  5081. 3:26:10one and follower two. At this point two
  5082. 3:26:12things can happen.
  5083. 3:26:14First thing is if the calls from leader
  5084. 3:26:16node to follower node are async, in that
  5085. 3:26:20case leader will do its work and update
  5086. 3:26:22the user that the task is done. And in
  5087. 3:26:25the back processes, the follower nodes
  5088. 3:26:27can continue updating the profile
  5089. 3:26:29picture and will eventually update our
  5090. 3:26:32leader that the task is done. This type
  5091. 3:26:34of request where leader updates the user
  5092. 3:26:37directly after updating the data without
  5093. 3:26:39getting the acknowledgement from these
  5094. 3:26:40follower nodes is called async
  5095. 3:26:43operation. Async basically means once
  5096. 3:26:46the request is catered by a leader, it
  5097. 3:26:48will directly update our user. And in
  5098. 3:26:51the back end, it will also share the
  5099. 3:26:53same request for follower one and
  5100. 3:26:55follower two node to perform the same
  5101. 3:26:57task and update our leader in the back
  5102. 3:26:59end. In this case, even if our F1 fails
  5103. 3:27:02to update, F2 fails to update, or
  5104. 3:27:04anything went wrong with these other
  5105. 3:27:05nodes, the leader have already given the
  5106. 3:27:07confirmation to our user. So, the task
  5107. 3:27:10is done. And eventually, when the nodes
  5108. 3:27:12are recovered, these operations will be
  5109. 3:27:15performed again on F1 and F2 to keep the
  5110. 3:27:18sync alive. After async, let's also talk
  5111. 3:27:20about sync approach, where the user
  5112. 3:27:22updates the profile picture.
  5113. 3:27:24The request basically carried by our
  5114. 3:27:26leader,
  5115. 3:27:27and it sends the update request to our
  5116. 3:27:30follower nodes as well,
  5117. 3:27:31F1 and F2. And the leader in this case
  5118. 3:27:35will not give any confirmation until it
  5119. 3:27:37get the okay response from both of our
  5120. 3:27:41followers. So, this gives okay,
  5121. 3:27:44and again, this gives okay. Once it get
  5122. 3:27:47confirmation from both of these follower
  5123. 3:27:49nodes, then only our database is going
  5124. 3:27:52to update our server, and eventually,
  5125. 3:27:54our user that the task is done. So, this
  5126. 3:27:56kind of approach is called sync
  5127. 3:27:58approach, where the leader will provide
  5128. 3:28:00the same operations to be performed by
  5129. 3:28:02F1 and F2. It will wait for the
  5130. 3:28:05responses, and once the response is
  5131. 3:28:08taken, then only it will update the
  5132. 3:28:10user. So, this is going to take a lot of
  5133. 3:28:12time, because our databases can be
  5134. 3:28:14placed into multiple places. There can
  5135. 3:28:17be two follower nodes, as we have
  5136. 3:28:18mentioned here, or more. So, in that
  5137. 3:28:20case, our time will increase, and
  5138. 3:28:22eventually, it will update the user
  5139. 3:28:25after getting the confirmation from each
  5140. 3:28:27and every follower. Now, let's talk
  5141. 3:28:29about advantages and disadvantages of
  5142. 3:28:31both of these approaches. If we talk
  5143. 3:28:33about synchronized approach, sync
  5144. 3:28:35approach, the advantage is all of the
  5145. 3:28:38followers will always have the updated
  5146. 3:28:39values. The other advantage of sync can
  5147. 3:28:42be, even if our leader fails, it will be
  5148. 3:28:45easier to select another leader, because
  5149. 3:28:47every node we know have the current
  5150. 3:28:50updated data. So, it will be easy to
  5151. 3:28:52migrate our leader to any of the
  5152. 3:28:54follower nodes, maybe follower one,
  5153. 3:28:56maybe follower two. The disadvantages
  5154. 3:28:59will be it is going to take a longer
  5155. 3:29:01time and also after getting any request,
  5156. 3:29:04our leader is waiting for the responses
  5157. 3:29:07from other follower nodes. It is going
  5158. 3:29:09to block that particular resource and
  5159. 3:29:11other operations on that particular
  5160. 3:29:13resource is also be blocked because of
  5161. 3:29:15our asset principles, right? And because
  5162. 3:29:18of these two disadvantages, we can say
  5163. 3:29:21that the sync operation here while
  5164. 3:29:23replication is an imprac- tical
  5165. 3:29:25approach. Any leader can't wait for all
  5166. 3:29:27the followers to update them first and
  5167. 3:29:30then they will update the user.
  5168. 3:29:32So, we always prefer async approach
  5169. 3:29:34here. In async approach, as we can see
  5170. 3:29:36clearly, no resources are blocked,
  5171. 3:29:39right? And the responses will also be
  5172. 3:29:41faster. And if we talk about
  5173. 3:29:43disadvantages,
  5174. 3:29:44we are basically trading off our
  5175. 3:29:47consistency. That means our data will be
  5176. 3:29:50mismatched. Suppose if a user is
  5177. 3:29:53updating any data to leader node and
  5178. 3:29:55leader provides the response to user,
  5179. 3:29:57our user will assume that the task is
  5180. 3:29:59done. So, even if they read from any
  5181. 3:30:02other follower nodes, the data should be
  5182. 3:30:04reflected. And in the async approach, it
  5183. 3:30:07is very possible that if user is trying
  5184. 3:30:09to access the data and it get connected
  5185. 3:30:11to any follower nodes, it will see the
  5186. 3:30:14stale data, which is eventually going to
  5187. 3:30:16update by our leader, but currently they
  5188. 3:30:18are seeing the stale data. So, this is
  5189. 3:30:20the issue in our async approach. Now,
  5190. 3:30:23before moving to the other algorithms,
  5191. 3:30:25let's first talk about how can we add a
  5192. 3:30:27new follower?
  5193. 3:30:28So, let's say we already have a system
  5194. 3:30:31where we have a leader and we have maybe
  5195. 3:30:33follower one and follower two already in
  5196. 3:30:36our system. Now, we need to add our
  5197. 3:30:38follower three. How can we add it? Now,
  5198. 3:30:41in order to add a new node into our
  5199. 3:30:43system, we need to perform some
  5200. 3:30:45operations. Our follower three will also
  5201. 3:30:48need to have all the data which our
  5202. 3:30:50leader have.
  5203. 3:30:52So, for that, what we do, we create
  5204. 3:30:54snapshot of the data from leader or
  5205. 3:30:57maybe we can create it from follower as
  5206. 3:30:59well. But, we need to make sure that if
  5207. 3:31:02we are picking follower two in order to
  5208. 3:31:04do the replication, we will again need
  5209. 3:31:06to use F2 only to update the data. Let's
  5210. 3:31:09say the current time is 12:55 p.m. and
  5211. 3:31:13we are taking the snapshot of F1 or F2
  5212. 3:31:16at this point. So, that means whatever
  5213. 3:31:19the state of our F2 is at this 12:55
  5214. 3:31:23p.m., we are only capturing the current
  5215. 3:31:25state of that particular database and we
  5216. 3:31:29are that dumping it into new node, which
  5217. 3:31:31is F3. Now, this process is going to
  5218. 3:31:35take time for sure. So, this is our
  5219. 3:31:37snapshot one, which is at 2:55. So, this
  5220. 3:31:41will contain the entire state of our F2
  5221. 3:31:45node. Now, what will happen again is
  5222. 3:31:47suppose the replication process took 2
  5223. 3:31:50hours or 3 hours, maybe, because the
  5224. 3:31:53data is huge and everything.
  5225. 3:31:55Now, we need to know what changes did
  5226. 3:31:57the F2 went through in this 2 and 3
  5227. 3:32:00hours. So, in that case, what we will
  5228. 3:32:02do, we will again take the snapshot, but
  5229. 3:32:04this time we don't have to capture the
  5230. 3:32:06entire state. We only need to capture
  5231. 3:32:08the difference between the snapshot and
  5232. 3:32:11the current time. So, we will create
  5233. 3:32:13another snapshot, which will include the
  5234. 3:32:15changes from 12:55 to current time. And
  5235. 3:32:19finally, we are going to dump this data
  5236. 3:32:21also in F3. Once we get to know that the
  5237. 3:32:24F3 is completely in sync with F2 and F2
  5238. 3:32:27is already in sync with leader, we will
  5239. 3:32:30make the connections of leader to our F3
  5240. 3:32:33also. And with this, we will have a new
  5241. 3:32:35follower node into our system with all
  5242. 3:32:38the updated data. After this, what
  5243. 3:32:40happens if the leader only fails? Let's
  5244. 3:32:42say we are having this topology again,
  5245. 3:32:45leader, F1, and F2.
  5246. 3:32:48Again, we have some databases linking.
  5247. 3:32:51The replication process is working fine,
  5248. 3:32:53then suddenly our leader fails.
  5249. 3:32:56In this case, we need to elect a new
  5250. 3:32:58leader. Now, how will we elect?
  5251. 3:33:00Basically, we will take care of two
  5252. 3:33:02factors. The first factor is which of
  5253. 3:33:06these F1 and F2 have the latest
  5254. 3:33:08information. So, we can take care of
  5255. 3:33:10that by seeing the timestamp of the
  5256. 3:33:12latest update. So, whichever we will
  5257. 3:33:14see, we will pick from F1 and F2, and we
  5258. 3:33:18will see which one is
  5259. 3:33:20updated recently. And the second step is
  5260. 3:33:23we need to update F1 to become a leader,
  5261. 3:33:26and our F2 to see F1 as a leader now.
  5262. 3:33:30So, this is the process of electing a
  5263. 3:33:32new leader when the leader goes down.
  5264. 3:33:34With this, you may have also heard these
  5265. 3:33:36terms like FS image or maybe added logs.
  5266. 3:33:40These things are highly used in the
  5267. 3:33:42replication process. FS image basically
  5268. 3:33:44con- tains the full metadata snapshot,
  5269. 3:33:47and added logs is used to update the
  5270. 3:33:49current changes. So, these both are also
  5271. 3:33:52used while doing the replication. And
  5272. 3:33:55while choosing the new leader or adding
  5273. 3:33:57a new follower, these two things are
  5274. 3:33:59also helpful. Now that we understand how
  5275. 3:34:02to add new leader, how to add new
  5276. 3:34:03follower, how to take care of things,
  5277. 3:34:06let's move to our second algorithm,
  5278. 3:34:08which is multi-leader algorithm. So, as
  5279. 3:34:10the name suggests, it is having multiple
  5280. 3:34:12leaders instead of one. So, that means
  5281. 3:34:14there is a possibility that every data
  5282. 3:34:16center, be it DS1 and DS2, will have
  5283. 3:34:20their bunch of leaders and followers.
  5284. 3:34:23So, maybe for DS1, we have this leader,
  5285. 3:34:26and we have follower like F1 here. Also,
  5286. 3:34:29here we have another leader and
  5287. 3:34:32follower. So, for DS2, the leader is
  5288. 3:34:35L2, and for DS1, the leader is L. Now,
  5289. 3:34:38what is going to happen? Let's say the
  5290. 3:34:40user is maintaining their records
  5291. 3:34:43through DS1. So, every record which DS1
  5292. 3:34:46handles will eventually first go to
  5293. 3:34:48their leader, and then the leader will
  5294. 3:34:50update their nodes. With this, now
  5295. 3:34:53leader also needs to update the other
  5296. 3:34:55leader into other data centers. And then
  5297. 3:34:58this leader is going to update these
  5298. 3:35:00nodes. This is how the process flows.
  5299. 3:35:02And similarly, if any request is being
  5300. 3:35:04made to this data center two, our leader
  5301. 3:35:07two is going to update their followers,
  5302. 3:35:10and also will send some query to other
  5303. 3:35:12leader to update the leader as well as
  5304. 3:35:15their followers. So, this is how a
  5305. 3:35:17multi-leader replication works, where
  5306. 3:35:19the leader will not communicate to each
  5307. 3:35:22and every nodes, but it will communicate
  5308. 3:35:24to leader nodes, and maybe their
  5309. 3:35:26follower nodes. So, this is going to
  5310. 3:35:29increase our performance. Also, we have
  5311. 3:35:32multiple leaders, that means even if a
  5312. 3:35:34leader node breaks, we can use the other
  5313. 3:35:37leaders to update the other nodes. So,
  5314. 3:35:39we have more tolerance to threats. Now,
  5315. 3:35:42since we have two leaders, we can also
  5316. 3:35:43perform collaborative operations, like
  5317. 3:35:46they can be working on the same file.
  5318. 3:35:49Now, we can work on the same Google
  5319. 3:35:51file, same Google sheet, those kind of
  5320. 3:35:53operation also we can perform, because
  5321. 3:35:55one request will not block that
  5322. 3:35:57resource, and the two requests can
  5323. 3:35:59eventually work simultaneously. Now,
  5324. 3:36:00when we are saying that both of these
  5325. 3:36:03resources are going to perform actions
  5326. 3:36:04on a similar table or single data, that
  5327. 3:36:07is going to increase our chances of
  5328. 3:36:09conflicts. Let me explain it through
  5329. 3:36:11another diagram. Let's say this is user
  5330. 3:36:13one, this is leader one. Here we have
  5331. 3:36:16leader two, and then we have another
  5332. 3:36:18user. Let me create the timelines again.
  5333. 3:36:20Let's say, user one and user two both
  5334. 3:36:22working on the same file, which is named
  5335. 3:36:25as A. Now, user one wants to rename that
  5336. 3:36:28file to B, and user two want to rename
  5337. 3:36:32that file to maybe C. Now, these both
  5338. 3:36:35will eventually hit their request to
  5339. 3:36:38their particular leaders. And based on
  5340. 3:36:40the async approaches we have seen, our
  5341. 3:36:42leaders can eventually give the okay
  5342. 3:36:45response to our users, right? That okay,
  5343. 3:36:47it is renamed. And here also, it can
  5344. 3:36:50give the response that okay, it is
  5345. 3:36:52renamed. So, these two users will see
  5346. 3:36:55two different data. This is not
  5347. 3:36:56possible, right? And also, our leader is
  5348. 3:36:59going to communicate with each other.
  5349. 3:37:00So, this will send the request to update
  5350. 3:37:03the data with C. And also, this leader
  5351. 3:37:06will also send the same request to
  5352. 3:37:08update it to B. Now, this is the
  5353. 3:37:11conflict, and how do we resolve it?
  5354. 3:37:12Basically, there are three ways of
  5355. 3:37:14resolving it. The first way of resolving
  5356. 3:37:17is the last right win. So, that is this
  5357. 3:37:20right is performed, let's say, at this
  5358. 3:37:23point, and this right is performed at
  5359. 3:37:25this point. So, as this operation is
  5360. 3:37:28performed later, so that means our L2
  5361. 3:37:31needs to update our file to C. So,
  5362. 3:37:35eventually, the file name will be C.
  5363. 3:37:37This is our first approach. The second
  5364. 3:37:39approach here is while working with
  5365. 3:37:41different leaders, we assign some IDs to
  5366. 3:37:43each and every leader. Suppose, leader
  5367. 3:37:46one have ID 10, and leader two have ID
  5368. 3:37:4911. Now, based on the ID, we can decide
  5369. 3:37:52which leader is more powerful, and which
  5370. 3:37:55leader we have to follow. Ultimately, if
  5371. 3:37:58leader two is of higher ID, that means
  5372. 3:38:00we have to follow leader two. And now,
  5373. 3:38:02it doesn't matter if the last right was
  5374. 3:38:04performed by L1 or L2. It is going to
  5375. 3:38:07update it with the value of L2 only. So,
  5376. 3:38:10here, instead of the last right, we are
  5377. 3:38:12going to focus on the ID. So, we can say
  5378. 3:38:15replica with higher ID wins. Replica is
  5379. 3:38:19nothing but a replication. Like, every
  5380. 3:38:22node which have the replica of content
  5381. 3:38:24can be called replica. So, we can write
  5382. 3:38:26leader also here. Okay, no issues. The
  5383. 3:38:29third approach basically is the approach
  5384. 3:38:31which we also see sometimes on Git when
  5385. 3:38:33we try to solve merge requests. So, the
  5386. 3:38:35same approach is used by the replication
  5387. 3:38:37process as well. So, whenever there are
  5388. 3:38:39conflicting situations, our leaders or
  5389. 3:38:42any other nodes are not going to solve
  5390. 3:38:44it. In this case, we will prompt our
  5391. 3:38:46user directly that the file name has
  5392. 3:38:49been changed to C and the last change
  5393. 3:38:52which you have made was B. So, do you
  5394. 3:38:54want to keep the current changes or do
  5395. 3:38:56you want to update it to C? It is up to
  5396. 3:38:59user. So, in this case, we need to add
  5397. 3:39:01the application logic to basically pass
  5398. 3:39:04our conflict to the users and eventually
  5399. 3:39:06user will update it.
  5400. 3:39:09After this, the third algorithm is
  5401. 3:39:11leaderless replication. So, in this
  5402. 3:39:14replication, we don't assign a specific
  5403. 3:39:16leader to any data center or to any
  5404. 3:39:19other node. We don't have any kind of
  5405. 3:39:21leader in this leaderless replication.
  5406. 3:39:24As the name suggests correctly, so here,
  5407. 3:39:26what do we do? Here, let's say we have
  5408. 3:39:29any client there. So, he is our user and
  5409. 3:39:32now we have some databases. Let's say
  5410. 3:39:34this is our node one, this is our node
  5411. 3:39:36two, this is our node three. So, these
  5412. 3:39:39are the three databases onto which our
  5413. 3:39:42data needs to be replicated. So, this
  5414. 3:39:44time whenever our user request for any
  5415. 3:39:47update, let's say again this user is
  5416. 3:39:49updating the profile picture, but this
  5417. 3:39:52time the request basically goes to all
  5418. 3:39:54of the nodes and eventually all will
  5419. 3:39:57update the data and provides a response
  5420. 3:40:01to the user. Here, the thing is in other
  5421. 3:40:03applications where we had a leader, we
  5422. 3:40:06can basically rely on that leader
  5423. 3:40:08replica and based on the response which
  5424. 3:40:10we get from leader, we can eventually
  5425. 3:40:11decide that whether the operation is
  5426. 3:40:13performed correctly or not. But here, we
  5427. 3:40:15don't have any leader. So, what can we
  5428. 3:40:17do? Do we wait for all the replicas to
  5429. 3:40:20answer us and at this point we update
  5430. 3:40:22our user or there is some other method?
  5431. 3:40:25Let's talk about that in a bit, but
  5432. 3:40:26before that, let's jot down some points.
  5433. 3:40:29So, the first point here is we don't
  5434. 3:40:31have any leader here. So, here we are
  5435. 3:40:34bound to send our queries to more than
  5436. 3:40:37one replicas, right? More than one
  5437. 3:40:40nodes. So, every query, when I say every
  5438. 3:40:43query, I mean both read as well as write
  5439. 3:40:46queries, will go to multiple nodes and
  5440. 3:40:49then collaboratively we'll get the
  5441. 3:40:51response. When I say read also, that
  5442. 3:40:54means let's say in this database only,
  5443. 3:40:57if there is some other user who is also
  5444. 3:40:58trying to access some values, and while
  5445. 3:41:01accessing some values, the user can use
  5446. 3:41:04this replica at this point or this
  5447. 3:41:06replica at this point or this replica at
  5448. 3:41:08this point. In all of these three
  5449. 3:41:11scenarios, the user is going to get
  5450. 3:41:13different values. So, here the user will
  5451. 3:41:16get updated profile, and from here they
  5452. 3:41:18will get the stale data. So, based on
  5453. 3:41:20multiple entries, we have to decide
  5454. 3:41:22whether to give this user updated data
  5455. 3:41:24or the stale data. So, this is our
  5456. 3:41:26struggle with this kind of replication.
  5457. 3:41:28But, apart from the struggles which we
  5458. 3:41:29see here, most of the applications in
  5459. 3:41:32databases use this setup. It is used by
  5460. 3:41:34various applications like Amazon Dynamo,
  5461. 3:41:37Riak, Cassandra, VM. All of these use
  5462. 3:41:40leaderless application. So, now the only
  5463. 3:41:42issue is should we wait for each and
  5464. 3:41:44every node to give us confirmation so
  5465. 3:41:46that we can proceed to tell any user any
  5466. 3:41:49response or is there any other way? So,
  5467. 3:41:52as you may have guessed, definitely we
  5468. 3:41:53can't wait for this long. There is a
  5469. 3:41:56way. That way is basically termed as
  5470. 3:41:58quorum. So, in quorum, what do we do?
  5471. 3:42:00Suppose we have total number of nodes as
  5472. 3:42:02three. We denote them by N, and for
  5473. 3:42:06reading, we have to use more than half
  5474. 3:42:09of the total nodes. So, it should be
  5475. 3:42:11more than N by 2. Again, for writing, we
  5476. 3:42:14should get the confirmation from more
  5477. 3:42:16than the half nodes. This way we
  5478. 3:42:18maintain the quorum and we will get the
  5479. 3:42:21responses which we want for. So, in the
  5480. 3:42:23same example, if I redraw it, suppose
  5481. 3:42:26the user is again going to update any
  5482. 3:42:28information, so updating profile picture
  5483. 3:42:31again maybe, and the request went like
  5484. 3:42:33this for first node, for second node
  5485. 3:42:35maybe the network is faster here, and
  5486. 3:42:38for the third node. Now, this is the
  5487. 3:42:40time each node took in order to perform
  5488. 3:42:43the operation. So, here we don't have to
  5489. 3:42:45wait for this response because we
  5490. 3:42:48already got the responses from more than
  5491. 3:42:50the half of the nodes. So, we can easily
  5492. 3:42:53say that profile picture is updated. So,
  5493. 3:42:55here we can confirm that the operation
  5494. 3:42:57of updating profile picture is done.
  5495. 3:43:00Again, if a new user appears here, and
  5496. 3:43:03the user is trying to access the value,
  5497. 3:43:05so it can basically sends a read
  5498. 3:43:07operation from these all nodes. Maybe at
  5499. 3:43:10this point, maybe at this point. Suppose
  5500. 3:43:13this point gives a result here, then
  5501. 3:43:15this point also gives a result here. So,
  5502. 3:43:17in these two responses, the user is
  5503. 3:43:19getting different values because this is
  5504. 3:43:21giving them stale data, and this is
  5505. 3:43:23giving them updated data because the
  5506. 3:43:25update is already done here, right? So,
  5507. 3:43:27in this case, there are various other
  5508. 3:43:29mechanisms also. You can go for
  5509. 3:43:30timestamp, or they can go for any other
  5510. 3:43:33mechanisms. But, let's say they don't go
  5511. 3:43:35for any other mechanism. There is one
  5512. 3:43:37more mechanism here where that is if the
  5513. 3:43:39requests are equal, and we are still
  5514. 3:43:41getting the different data, we will
  5515. 3:43:43consider the third request and response,
  5516. 3:43:46and then again calculate the results.
  5517. 3:43:48So, here we are getting updated from two
  5518. 3:43:51of the nodes, and we are getting stale
  5519. 3:43:53value from one of them. So, we will move
  5520. 3:43:56ahead with the updated value, and this
  5521. 3:43:58user will get the updated profile
  5522. 3:44:01picture. So, there can be various ways
  5523. 3:44:03of solving this conflicts, but the thing
  5524. 3:44:05is ultimately the user don't have to
  5525. 3:44:08wait for each and every response. This
  5526. 3:44:10is a special case where half of the
  5527. 3:44:12nodes are saying that things are done,
  5528. 3:44:14and half are saying things are not done.
  5529. 3:44:15We don't have three servers also. We
  5530. 3:44:17have more than 10 or 12 servers in a
  5531. 3:44:20large system. So, this case becomes a
  5532. 3:44:22rare case. So, yeah, this is all about
  5533. 3:44:24the replication which we needs to know
  5534. 3:44:26at this point. So, after replication,
  5535. 3:44:28the other way of segregating our data
  5536. 3:44:29into multiple nodes in order to scale
  5537. 3:44:32them properly is partition. Now, before
  5538. 3:44:35moving to partition, first see what can
  5539. 3:44:37be the issue with replication.
  5540. 3:44:39Replication basically suggests that we
  5541. 3:44:41will copy entire data to other node.
  5542. 3:44:43What if the data is so huge that we
  5543. 3:44:45can't have them in one node? With this,
  5544. 3:44:47the second concern is what if the data
  5545. 3:44:50is so huge that even with secondary
  5546. 3:44:52indexes, our time of searching any
  5547. 3:44:55element is getting worse and worse. So,
  5548. 3:44:58with these two concerns, we can't really
  5549. 3:45:00have all of the data in one database and
  5550. 3:45:03same data in other database. What can we
  5551. 3:45:06do then? We can easily or through some
  5552. 3:45:09logic break the system. We will do the
  5553. 3:45:12partitioning of data and maybe place
  5554. 3:45:14some amount of data here and other
  5555. 3:45:16amount of data in some other node. And
  5556. 3:45:19then, we can replicate this partition
  5557. 3:45:21data. Why? Because this data is
  5558. 3:45:23comparatively smaller and it will be
  5559. 3:45:26easier to manage. Let's say, in a
  5560. 3:45:28particular node, we are only saying that
  5561. 3:45:31we will store up to 10,000 records. And
  5562. 3:45:35then, once we hit this point, we will
  5563. 3:45:37again create a new partition and will
  5564. 3:45:40add the new records there. Or maybe
  5565. 3:45:42divide these records into 5,000. So,
  5566. 3:45:45these all things can be done only when
  5567. 3:45:47we have a new way of partitioning the
  5568. 3:45:50data. So, this is what partition really
  5569. 3:45:52is. While partitioning the data, there
  5570. 3:45:54is only a single rule. That is, if we
  5571. 3:45:58are doing the partitions
  5572. 3:46:00like this, then if we combine them, it
  5573. 3:46:03should give us the entire data set. This
  5574. 3:46:06is the only condition which is required
  5575. 3:46:08for partition. And And makes sense, too.
  5576. 3:46:11We don't want to lose any data, right?
  5577. 3:46:13So, this is our first condition and the
  5578. 3:46:15second condition is that let's say we
  5579. 3:46:18have again two partitions. Now, we want
  5580. 3:46:20our data to be equally distributed among
  5581. 3:46:23them. We don't want any nodes to get
  5582. 3:46:25let's say consumed by 90% of data and
  5583. 3:46:28here we only have 10% of data because
  5584. 3:46:31this basically beats the reasoning of
  5585. 3:46:33partitioning. We are doing partitioning
  5586. 3:46:34so that every node will be able to
  5587. 3:46:37access some data and will be able to
  5588. 3:46:39work frequently. So, basically we are
  5589. 3:46:41saying that the data should be evenly
  5590. 3:46:44distributed. So, these are the two major
  5591. 3:46:47conditions of partitioning. Apart from
  5592. 3:46:50this, everything works fine. So, the
  5593. 3:46:51first condition makes sense. It will
  5594. 3:46:53take care of itself and in order to
  5595. 3:46:55fulfill second condition, we have to
  5596. 3:46:56basically maintain some system where we
  5597. 3:46:59will protect the database to get
  5598. 3:47:01overloaded or to get underutilized like
  5599. 3:47:04this. So, now let's talk about those
  5600. 3:47:06solutions. So, the first thing here is
  5601. 3:47:09we will partition using key. Let's say
  5602. 3:47:12we have some table, maybe user table. We
  5603. 3:47:14are handling these entities and then we
  5604. 3:47:17have some ID here, user one or user two
  5605. 3:47:21and the series goes like this. So, what
  5606. 3:47:23we will do? We will basically in the
  5607. 3:47:25partitioning based on the keys will
  5608. 3:47:28insert the values. Let's say this user
  5609. 3:47:30entity can hold up to 10,000
  5610. 3:47:34or maybe 100,000 values. So, what we
  5611. 3:47:36will do? We will divide these 100,000 to
  5612. 3:47:3950,000 here and 50,000 here. So, that
  5613. 3:47:41means from user one to user 50,000,
  5614. 3:47:44these data will go to the node one or we
  5615. 3:47:47can say partition one and the users from
  5616. 3:47:51user ID maybe 50,000 one to user ID
  5617. 3:47:55100,000 will go into partition two.
  5618. 3:47:59So, this is how we will segregate our
  5619. 3:48:01data so that it will be easier to
  5620. 3:48:03retrieve the data afterwards as well.
  5621. 3:48:05This may looks fair on the surface
  5622. 3:48:07value, but it is quite messy. What if
  5623. 3:48:10these users are from different regions?
  5624. 3:48:13What if half of them from 1 to 50,000
  5625. 3:48:17are from India and half of them are from
  5626. 3:48:19US? Maybe here also they are distributed
  5627. 3:48:22like this only. So, half of them are
  5628. 3:48:24from India and half of them are from US.
  5629. 3:48:27So, in that case, both of these nodes
  5630. 3:48:29will be busy. This is a good scenario
  5631. 3:48:31because both of the nodes will be used
  5632. 3:48:33and will be busy in order to provide the
  5633. 3:48:35information. And also it is possible
  5634. 3:48:37that the node one is only taking care of
  5635. 3:48:40India request
  5636. 3:48:42and the US request are being taken care
  5637. 3:48:44by partition two or node two. So, in
  5638. 3:48:47this case what happens? Let's say the
  5639. 3:48:49application is very popular in US, but
  5640. 3:48:52is not that popular in India. So, maybe
  5641. 3:48:55P1 is getting maybe 100 requests per
  5642. 3:48:58second and on the other hand our
  5643. 3:49:00partition two is getting maybe 10,000
  5644. 3:49:03requests per second. Now, earlier this
  5645. 3:49:06was looking good on the surface level,
  5646. 3:49:08but now this is messy because one
  5647. 3:49:10partition here is clearly becoming the
  5648. 3:49:13hotspot. Now, what is hotspot? A hotspot
  5649. 3:49:16is that partition which is getting more
  5650. 3:49:18requests compared to other ones. So, it
  5651. 3:49:22is our duty to maintain this hotspot as
  5652. 3:49:24well. We don't want any node to become
  5653. 3:49:27hotspot because eventually a hotspot can
  5654. 3:49:30leads to the node failure. And if that
  5655. 3:49:33node fails, we have to basically
  5656. 3:49:35distribute the data into multiple nodes
  5657. 3:49:37then and then do the configuration again
  5658. 3:49:39or maybe change this node or maybe
  5659. 3:49:42transfer this data to other node. All
  5660. 3:49:44the things we have to manage, but that
  5661. 3:49:46thing is surely not recommended. So, we
  5662. 3:49:48avoid any node from becoming a hotspot.
  5663. 3:49:52I hope this is making sense to you. So,
  5664. 3:49:54one way of handling the hotspot is let's
  5665. 3:49:58say for every request, we also attach
  5666. 3:50:01timestamp and if most of these requests
  5667. 3:50:04are going to node one and very less are
  5668. 3:50:07going to node two, that means the data
  5669. 3:50:10distribution which we did is not good.
  5670. 3:50:13So, we have to rearrange our data with
  5671. 3:50:16some other methods. So, let's see what
  5672. 3:50:18the other points are. The second
  5673. 3:50:20approach here can be partitioning the
  5674. 3:50:23data with hash value of key. What we
  5675. 3:50:27will do here is, let's say we have user
  5676. 3:50:29one, user two, maybe user three as the
  5677. 3:50:33ID or unique identifier of the data. And
  5678. 3:50:36now we have to put that data into
  5679. 3:50:38different partitions. We will not here
  5680. 3:50:40choose ID, but we will use any hash
  5681. 3:50:43function to basically transfer the ID to
  5682. 3:50:46a unique number. Maybe this is one, this
  5683. 3:50:48is two. And user three belongs to maybe
  5684. 3:50:51one again because of the hash function
  5685. 3:50:53which we are using. So, based on this,
  5686. 3:50:55one and one will go to partition one and
  5687. 3:50:59two will go here. Or maybe we are
  5688. 3:51:01catering some range here, so maybe one
  5689. 3:51:03to 10 can go into one direction and 10
  5690. 3:51:06to 15 can go into other direction. So,
  5691. 3:51:08this is also one way of doing the
  5692. 3:51:10partition. The challenge here is still
  5693. 3:51:12same. If the request bombarded at P1, we
  5694. 3:51:16don't have any other way, but we have to
  5695. 3:51:18rearrange like we discussed in the
  5696. 3:51:20previous example. So, based on our
  5697. 3:51:22discussion so far, the hotspot is
  5698. 3:51:24becoming a real issue. Before solving it
  5699. 3:51:27for partitions, let's first see how do
  5700. 3:51:29we solve this issue in a local DB. Let's
  5701. 3:51:32say we have a table for products. And in
  5702. 3:51:35products, we have various attributes.
  5703. 3:51:37Let's say we have some name and color or
  5704. 3:51:40maybe price. We can have more filter
  5705. 3:51:43values, right? And like this, we can
  5706. 3:51:45have as many attributes as we want. So,
  5707. 3:51:47name, color, price, material. All these
  5708. 3:51:49attributes basically are used in order
  5709. 3:51:52to filter these products. Name can be
  5710. 3:51:54used in query parameters. Other than
  5711. 3:51:57that, color, price, and range can be
  5712. 3:52:00used in filter easily. So, what we will
  5713. 3:52:03do in normal scenario? We will create
  5714. 3:52:05indexes based on material, based on
  5715. 3:52:08color, maybe based on price as well. I'm
  5716. 3:52:11not sure about it because price normally
  5717. 3:52:13goes into a range, but we can easily
  5718. 3:52:16make a secondary index out of this and
  5719. 3:52:19by that, whenever any user basically
  5720. 3:52:22tries to search that particular product
  5721. 3:52:24with material, maybe material is wood.
  5722. 3:52:26So, what will happen? We will not go to
  5723. 3:52:28our table or this huge table directly.
  5724. 3:52:31Where we will redirect this query to, it
  5725. 3:52:33is going to redirect it to our second
  5726. 3:52:35query, which is maintained for material.
  5727. 3:52:38And if that data or that value is here,
  5728. 3:52:42we will then redirect those records
  5729. 3:52:44particularly to our display. So, this is
  5730. 3:52:47how we manage the queries to find out if
  5731. 3:52:49we have the data or not, and if we have
  5732. 3:52:51the data, where is that data
  5733. 3:52:53particularly. This is how we solve it in
  5734. 3:52:55local DB or a single instance. Now, we
  5735. 3:52:58are going to use the same way into
  5736. 3:53:00partitioning as well. So, what we are
  5737. 3:53:02going to do? We are going to do the
  5738. 3:53:04partitioning by the secondary index.
  5739. 3:53:07What do we mean by this is, we are going
  5740. 3:53:09to maintain secondary indexes in each
  5741. 3:53:13partition. So, that means all the
  5742. 3:53:15secondary indexes will be present in
  5743. 3:53:18each partition respectively. This way is
  5744. 3:53:21also used by Cassandra and also by
  5745. 3:53:24Elastic Search. What we are going to do
  5746. 3:53:26here is, let's say this is our partition
  5747. 3:53:28one, which have some data. Maybe this
  5748. 3:53:30partition have car data from ID, let's
  5749. 3:53:34say 200 to 400. And we have partition
  5750. 3:53:37two, where also we have some car data
  5751. 3:53:40from ID, let's say 500 to 700. Here, we
  5752. 3:53:45are maintaining some secondary indexes.
  5753. 3:53:47Maybe we are maintaining color as a
  5754. 3:53:49secondary index. And for color, we know
  5755. 3:53:51that the blue color is for car maybe
  5756. 3:53:54with ID 209 and 305. Similarly, for red,
  5757. 3:53:59we can say the ID is 350
  5758. 3:54:03or 359, anything, right?
  5759. 3:54:07For P2 also, we are maintaining it like
  5760. 3:54:08this only. Again, for color, we know
  5761. 3:54:11that the blue color cars are maybe ID
  5762. 3:54:14509
  5763. 3:54:15and 609 and red cars are 610 690. So,
  5764. 3:54:21these are the indexing which is done in
  5765. 3:54:23both of these partitions. So, whenever a
  5766. 3:54:25query comes for a blue car, we don't
  5767. 3:54:28have to scan to all the records, but we
  5768. 3:54:31can directly see the blue car here
  5769. 3:54:33belongs to 209 and 305. And here, we
  5770. 3:54:37have blue car for 509 and 609 only.
  5771. 3:54:40Let's say a new request come for silver
  5772. 3:54:43car. So, we don't have to basically
  5773. 3:54:46indulge into all the records, but by
  5774. 3:54:48seeing secondary indexes on color only,
  5775. 3:54:51we will get that this partition doesn't
  5776. 3:54:53have any car which is of silver color.
  5777. 3:54:55So, we don't scan this partition
  5778. 3:54:57entirely and this request can now go to
  5779. 3:55:00some other partition. So, this is how we
  5780. 3:55:02are maintaining the request so that our
  5781. 3:55:05data doesn't get overloaded with a lot
  5782. 3:55:07of request, but based on the request, if
  5783. 3:55:10they are present in our secondary
  5784. 3:55:11indexes, then only we will entertain
  5785. 3:55:13them and show them the records or fetch
  5786. 3:55:15some records, otherwise, we will
  5787. 3:55:17directly skip them. Now, this kind of
  5788. 3:55:19setup also have its challenges, right?
  5789. 3:55:21Because partitioning is done, but
  5790. 3:55:24secondary indexes are also present in
  5791. 3:55:27each partition. That means even if the
  5792. 3:55:30partition doesn't have any value, we
  5793. 3:55:33still have to send a request to that
  5794. 3:55:35particular partition. This is again a
  5795. 3:55:37drawback of the system which can make
  5796. 3:55:39the system heavy, which can send the
  5797. 3:55:41request to many partitions where the
  5798. 3:55:44data is not there and we are still
  5799. 3:55:45getting the request. So, this is also
  5800. 3:55:48wrong. So, that's why we have a fourth
  5801. 3:55:50and final way of solving this, which is
  5802. 3:55:53by having common global index. What we
  5803. 3:55:57will do here is instead of having
  5804. 3:55:59secondary indexes in each and every
  5805. 3:56:01partition, we will choose a partition or
  5806. 3:56:04any data center where we will maintain
  5807. 3:56:07the secondary indexes. So, if we are
  5808. 3:56:10saying that we want the car with color
  5809. 3:56:13blue, we should know that blue car
  5810. 3:56:15colors, according to the previous
  5811. 3:56:17examples, are only 209305
  5812. 3:56:20and here we have 509 and 609. And
  5813. 3:56:23basically, this belongs to our P1 and
  5814. 3:56:26this belongs to our P2.
  5815. 3:56:28So, we should know this first-handedly.
  5816. 3:56:31So, that whenever anyone is requesting
  5817. 3:56:33to get any information about blue color
  5818. 3:56:35car, we will directly redirect it to P1
  5819. 3:56:39and P2. And suppose we have some other
  5820. 3:56:41partitions, P3, P4, and others, we don't
  5821. 3:56:46send the request there. So, that these
  5822. 3:56:48partitions can take care of other
  5823. 3:56:50requests. So, this is our fourth way of
  5824. 3:56:53maintaining the global index. So, with
  5825. 3:56:56this, definitely, we are going to
  5826. 3:56:57improve our reading, but the writing
  5827. 3:57:00becomes a challenge. Why? Because even
  5828. 3:57:03if we are working with partition 79,
  5829. 3:57:06where we are adding some values, and
  5830. 3:57:08again have to update that, "Okay, we
  5831. 3:57:10have some new car, maybe 1011, which is
  5832. 3:57:13of blue color." So, like this, it is
  5833. 3:57:15handled. So, these are the ways of
  5834. 3:57:18handling partitioning into our system.
  5835. 3:57:20After understanding about partitioning,
  5836. 3:57:22replication, and load balancing, we now
  5837. 3:57:25know that we need to segregate our data
  5838. 3:57:27into multiple nodes in order to scale
  5839. 3:57:30them. We can't scale them into a single
  5840. 3:57:33database after an extent. So, after
  5841. 3:57:35understanding this, let's see what CAP
  5842. 3:57:38theorem basically says.
  5843. 3:57:40CAP theorem is one of those theorems
  5844. 3:57:42which we have to know whenever you think
  5845. 3:57:45about trade-offs between capacity and
  5846. 3:57:47availability. So, first see what CAP
  5847. 3:57:49theorem really stands for. In CAP
  5848. 3:57:52theorem, there are three terms
  5849. 3:57:54basically. C stands for consistency,
  5850. 3:57:57whereas A stands for availability, and P
  5851. 3:57:59stands for partition tolerance.
  5852. 3:58:02So, CAP theorem basically states that
  5853. 3:58:04you can only provide two of these three
  5854. 3:58:07variables, which means if we talk about
  5855. 3:58:10consistency, what does it mean?
  5856. 3:58:12Consistency basically means providing
  5857. 3:58:14latest information to every user.
  5858. 3:58:17So, consistency stands for latest
  5859. 3:58:19information. Now, if we are providing
  5860. 3:58:21latest information, let's see what it
  5861. 3:58:23really means. Let's say this is our
  5862. 3:58:25partition one,
  5863. 3:58:26this is our partition two. Some data is
  5864. 3:58:29updated on partition one,
  5865. 3:58:32and someone is reading that data
  5866. 3:58:34immediately after the update. So, this
  5867. 3:58:37update from partition one and partition
  5868. 3:58:40two will also take some time. And at
  5869. 3:58:42that time, we basically show something
  5870. 3:58:45like system is loading or data is
  5871. 3:58:47loading kind of thing to user until the
  5872. 3:58:50change is completed on P2. Then only we
  5873. 3:58:53can show the latest data. So, this means
  5874. 3:58:55that we will trade off our availability
  5875. 3:58:58of that particular information for
  5876. 3:59:00consistency. The second thing is
  5877. 3:59:02availability. Availability basically
  5878. 3:59:04states that we will always respond to
  5879. 3:59:08every request. Now, what does this mean?
  5880. 3:59:10In the same scenario, if it is taking
  5881. 3:59:13time to upload a data from B1 to B2, and
  5882. 3:59:16while updating someone request in
  5883. 3:59:18between, so they will not see loading
  5884. 3:59:20icon. Instead, they will see the stale
  5885. 3:59:23data. So, this is what availability
  5886. 3:59:25means. Now, the third term, which is
  5887. 3:59:27partitioning. Partitioning, we all know
  5888. 3:59:29now we need to divide our data into
  5889. 3:59:32multiple nodes, right? Now, with
  5890. 3:59:34partition, we can also see tolerance
  5891. 3:59:36here. What does it mean? Let's say there
  5892. 3:59:38are some nodes attached or linked to
  5893. 3:59:41each other. Now, this is partition
  5894. 3:59:44tolerant when the network of this node
  5895. 3:59:47is completely broken. So, this can't be
  5896. 3:59:50updated from any other method. We can
  5897. 3:59:52either wait to basically fix this
  5898. 3:59:54partition connection and update the
  5899. 3:59:56value or we can keep it working to
  5900. 3:59:58provide the information. But, then the
  5901. 4:00:01information we are going to provide is
  5902. 4:00:03going to be stale data. Why? Because the
  5903. 4:00:05connections between this node and the
  5904. 4:00:07other nodes are broken. Now, let's talk
  5905. 4:00:09about the combinations one by one. So,
  5906. 4:00:11first combination is CA,
  5907. 4:00:14which means we are saying that our data
  5908. 4:00:16will be consistent and it will be
  5909. 4:00:18available also. In this case, we can't
  5910. 4:00:21provide the partitioning part. Because
  5911. 4:00:23if we are saying that our data is
  5912. 4:00:24consistent and available, that means we
  5913. 4:00:27are maintaining a single database. We
  5914. 4:00:29can't basically have two data centers or
  5915. 4:00:32two nodes and think about consistency
  5916. 4:00:35and availability at the same time.
  5917. 4:00:36Suppose we have updated any data here,
  5918. 4:00:38it is going to take at least some
  5919. 4:00:40fraction of time to update the data to
  5920. 4:00:42other node. So, in that meantime, if
  5921. 4:00:45someone request about that particular
  5922. 4:00:47data, we can't decide basically whether
  5923. 4:00:49to show that data or node. Because if we
  5924. 4:00:52show that data, that means we are
  5925. 4:00:53compromising our consistency there. And
  5926. 4:00:56if we don't show that data and wait for
  5927. 4:00:58the update, that means we are
  5928. 4:00:59compromising with the availability. So,
  5929. 4:01:01this combination is only possible when
  5930. 4:01:05we are working with a single database or
  5931. 4:01:06a single node. We can't do partitioning
  5932. 4:01:09here. With this, let's move to our
  5933. 4:01:10second combination,
  5934. 4:01:12which is CP. CP means we are choosing
  5935. 4:01:15consistency over availability. So, here
  5936. 4:01:18we are saying consistency and
  5937. 4:01:20partitioning will be there, but we'll be
  5938. 4:01:22trade-off availability. Now, we have
  5939. 4:01:24understood this term, let me take the
  5940. 4:01:26example again. These are our two nodes
  5941. 4:01:28and the partition happens. Basically,
  5942. 4:01:31the connection between these nodes
  5943. 4:01:32break. Now, this doesn't have the
  5944. 4:01:35updated value. Now, when we are choosing
  5945. 4:01:37consistency, we have to let go of the
  5946. 4:01:39availability. So, we are saying no
  5947. 4:01:42request will be processed by this node.
  5948. 4:01:44Until when? Until it gets updated with
  5949. 4:01:47the latest data. So, this is what
  5950. 4:01:48happens when we are working in a
  5951. 4:01:50partitioned environment and choose
  5952. 4:01:51consistency over availability. That
  5953. 4:01:53means the requests which are calling
  5954. 4:01:55this node will be redirected to some
  5955. 4:01:58other nodes which have the latest or
  5956. 4:02:00consistent data. With this, let's see
  5957. 4:02:02the third combination as well, which is
  5958. 4:02:05AP. In AP, we are providing availability
  5959. 4:02:08to every request, right? So, we are
  5960. 4:02:10providing availability with
  5961. 4:02:12partitioning. So, what is going to
  5962. 4:02:14happen here? When there are two nodes
  5963. 4:02:17and the partition tolerance happen, that
  5964. 4:02:19means this node doesn't have updated no
  5965. 4:02:22updates. At that time also, because we
  5966. 4:02:25have chosen availability over
  5967. 4:02:26consistency, we will keep addressing the
  5968. 4:02:28request by showing them the stale data.
  5969. 4:02:31And that means we are compromising with
  5970. 4:02:33consistency. So, why are we learning
  5971. 4:02:35this CAP theorem? The CAP theorem
  5972. 4:02:37basically states that you have to do
  5973. 4:02:39some trade-offs in order to keep your
  5974. 4:02:41application working. Suppose we are
  5975. 4:02:44working on Instagram. In Instagram, the
  5976. 4:02:46availability matters over consistency.
  5977. 4:02:48Suppose you have updated a video or a
  5978. 4:02:50photo, it doesn't matter for your
  5979. 4:02:53followers to see that photo immediately.
  5980. 4:02:56They can wait for 1 or 2 seconds
  5981. 4:02:57basically. So, in Instagram, we will be
  5982. 4:03:00following AP. But in any application,
  5983. 4:03:03let's say banking application, where
  5984. 4:03:05each transaction matters to us, where
  5985. 4:03:07each change matters to us. So, if I have
  5986. 4:03:10sent 100 rupees to any other account,
  5987. 4:03:13the person will have to wait in order to
  5988. 4:03:16process that transaction, and after that
  5989. 4:03:18only they will be able to see. So, here
  5990. 4:03:21CP matters. And for CA, CA will be there
  5991. 4:03:25if we have a single data. That means for
  5992. 4:03:27every small application, CA can work.
  5993. 4:03:30But when the system scales, we don't
  5994. 4:03:32have an option to pick CA. That's when
  5995. 4:03:34we have to choose between consistency
  5996. 4:03:36and availability. So, this is what CAP
  5997. 4:03:38theorem is. I hope you all understand
  5998. 4:03:40it. Now, let's understand one of the
  5999. 4:03:42most important component of our system
  6000. 4:03:44design, which is message queue. Now,
  6001. 4:03:46before going into depth of message
  6002. 4:03:48queue, let's first understand the type
  6003. 4:03:50of request we deal with. We normally
  6004. 4:03:52deal with two type of request. Or we can
  6005. 4:03:55also say requests or maybe processes.
  6006. 4:03:58Some requests are sync. That means
  6007. 4:04:01whenever the request is received, we
  6008. 4:04:03have to immediately provide them some
  6009. 4:04:05output or response for the system to
  6010. 4:04:08keep working. On the other hand, we have
  6011. 4:04:10async calls, requests, or processes in
  6012. 4:04:13which we get the request, but there is
  6013. 4:04:15no hurry for our responses. Also, it is
  6014. 4:04:19also possible that we don't give any
  6015. 4:04:21response at all. So, so that means our
  6016. 4:04:24system only
  6017. 4:04:26takes the request and basically fulfill
  6018. 4:04:29the requirement in the backend
  6019. 4:04:30processes. So, in case of sync type of
  6020. 4:04:33calls, requests, or processes, we can't
  6021. 4:04:35do anything. Our system needs to be
  6022. 4:04:38concentrated on these requests and have
  6023. 4:04:41to provide the responses as soon as
  6024. 4:04:43possible, which we have discussed
  6025. 4:04:45throughout these courses. Now comes the
  6026. 4:04:47second part, which are async requests.
  6027. 4:04:49So, let's understand async process or
  6028. 4:04:51request with some example. Let's say we
  6029. 4:04:54are an e-commerce application. We have
  6030. 4:04:57some store, maybe some product is there,
  6031. 4:05:00and finally someone purchased this
  6032. 4:05:01particular product. So, at the time of
  6033. 4:05:04purchase, what are the operations we
  6034. 4:05:06need to perform? First is, we need to
  6035. 4:05:08send some email to the user that order
  6036. 4:05:11is confirmed and here are your details.
  6037. 4:05:13We have to also update the inventory so
  6038. 4:05:16that if, let's say, only one product is
  6039. 4:05:19left, that product should immediately be
  6040. 4:05:21out of stock so that there will be no
  6041. 4:05:23confusion for any other record. Other
  6042. 4:05:25than this, we also have to inform our
  6043. 4:05:27delivery partner, right? So, that they
  6044. 4:05:29will start packaging and moving the
  6045. 4:05:31order further. Now, out of these three
  6046. 4:05:33processes or request, which do you think
  6047. 4:05:36is the most immediate action? So, let's
  6048. 4:05:39see. Updating inventory is one of the
  6049. 4:05:42crucial task which have to be performed
  6050. 4:05:44immediately. We can't wait for it,
  6051. 4:05:45right? Otherwise, it can create
  6052. 4:05:47blunders. So, this is definitely a
  6053. 4:05:50synchronous task. But, other than that,
  6054. 4:05:53we can use some third-party email
  6055. 4:05:55service to process the email, right? We
  6056. 4:05:58don't have to send the emails
  6057. 4:05:59immediately. Even if the email goes,
  6058. 4:06:02let's say, 1 minute late, that is also
  6059. 4:06:04fine, and our system will be keep
  6060. 4:06:06working.
  6061. 4:06:07Similarly, for delivery, we can say that
  6062. 4:06:09delivery also is an async operation
  6063. 4:06:12because we are not in any hurry. Also,
  6064. 4:06:14our system doesn't need to take care of
  6065. 4:06:16delivery, as well. So, we will simply
  6066. 4:06:18pass this information to any delivery
  6067. 4:06:20service, and our current system can
  6068. 4:06:22basically focus on some other task. So,
  6069. 4:06:25this is how a distributed environment
  6070. 4:06:27works. Basically, we have multiple
  6071. 4:06:30services who communicate with each other
  6072. 4:06:33in order to fulfill the requirement. So,
  6073. 4:06:35if the services are different, and we
  6074. 4:06:38have to use, let's say, email, with
  6075. 4:06:39email, we can also use
  6076. 4:06:41SMS service, or maybe WhatsApp service.
  6077. 4:06:45We can use them, and these all are async
  6078. 4:06:48kind of calls. We don't have to stop our
  6079. 4:06:51system in order to process this request.
  6080. 4:06:54So, I hope up to now we have understood
  6081. 4:06:56about async and sync kind of request,
  6082. 4:06:59and in async, why we can use some other
  6083. 4:07:01services, and for sync, we have to wait
  6084. 4:07:04for the response in order to show some
  6085. 4:07:06immediate data, like out of service
  6086. 4:07:08product. Now, let's talk about some of
  6087. 4:07:10the examples for sync. Every financial
  6088. 4:07:13application can be an example here.
  6089. 4:07:15Wherever we are concerned about
  6090. 4:07:17security, we can't rely on async kind of
  6091. 4:07:20operation. We have to basically provide
  6092. 4:07:23the communication immediately. The third
  6093. 4:07:25area can be dependent. Wherever there is
  6094. 4:07:28a dependency on one or the other task to
  6095. 4:07:31be performed after a particular task in
  6096. 4:07:33a synchronous manner, we have to
  6097. 4:07:35complete this task in a synchronous
  6098. 4:07:37manner.
  6099. 4:07:38So that the dependent processes can also
  6100. 4:07:41work. For example, we can't process any
  6101. 4:07:43emails for order details before
  6102. 4:07:46processing the order. Once the order is
  6103. 4:07:48processed and confirmed, then only we
  6104. 4:07:51can put down the emails or other
  6105. 4:07:53notifications. So after this, let's talk
  6106. 4:07:55about async also. Async request
  6107. 4:07:58basically works wherever we need to
  6108. 4:08:00simply fire our request and our system
  6109. 4:08:03can then forget about that. So this is
  6110. 4:08:06the golden rule to understand what
  6111. 4:08:08system is async or what request is async
  6112. 4:08:11versus sync. Whenever we get the feeling
  6113. 4:08:14or we get the components where we can
  6114. 4:08:16simply fire a request and then our
  6115. 4:08:18system can forget about that, we can
  6116. 4:08:21easily move to async calls, which will
  6117. 4:08:23not only speed our systems but also free
  6118. 4:08:26our system in order to work on different
  6119. 4:08:28tasks. So some examples on free and
  6120. 4:08:31forget can be broadcasting. Broadcasting
  6121. 4:08:33can be of anything like email
  6122. 4:08:35broadcasting, even the matches, the live
  6123. 4:08:37matches which we see online on any
  6124. 4:08:39application, there also we can feel some
  6125. 4:08:42latency. The latency can be from 0
  6126. 4:08:44seconds to 7 or 8 or 10 seconds also
  6127. 4:08:47based on the network connections you
  6128. 4:08:49have. So broadcasting we have discussed,
  6129. 4:08:51email we can write here. Also we have
  6130. 4:08:53seen
  6131. 4:08:54SMS
  6132. 4:08:56or WhatsApp. Basically any communication
  6133. 4:08:58can be held asynchronously. Also the OTP
  6134. 4:09:01applications. In all the OTP
  6135. 4:09:04application, there is a timer and that
  6136. 4:09:06timer is mostly more than 30 seconds.
  6137. 4:09:09Why? Because in 30 seconds our system
  6138. 4:09:12can handle this request in an async
  6139. 4:09:14manner. So, now we have understood sync
  6140. 4:09:16and async, let's talk about message
  6141. 4:09:18queues. In order to understand message
  6142. 4:09:20queue, let's take the same example. So,
  6143. 4:09:22this is our e-commerce application and
  6144. 4:09:25we are using the service for emails.
  6145. 4:09:28Now, on our e-commerce application, we
  6146. 4:09:30are getting some order and based on that
  6147. 4:09:33order, we have to send emails.
  6148. 4:09:35But, in between, we will implant message
  6149. 4:09:38queue. In diagrams, message queue is
  6150. 4:09:41denoted like this because it is
  6151. 4:09:42basically a queue
  6152. 4:09:44and each of these order request or email
  6153. 4:09:47request can be hold in the message queue
  6154. 4:09:49and then through our message queues, it
  6155. 4:09:52can go directly to our email services.
  6156. 4:09:55So, what our application needs to do, it
  6157. 4:09:58will simply dump all the email request
  6158. 4:10:01to our message queue and from there, the
  6159. 4:10:03message queue will take care of those
  6160. 4:10:04requests. It is like a buffer or we can
  6161. 4:10:07say broker in between two apps or
  6162. 4:10:09services. Now, whichever party or
  6163. 4:10:12application is putting the request into
  6164. 4:10:15message queues are known as producers
  6165. 4:10:18and whichever service is taking the
  6166. 4:10:21request from message queues are called
  6167. 4:10:23consumers. With this, producers can also
  6168. 4:10:26be called publishers and consumers can
  6169. 4:10:29also be called subscribers because
  6170. 4:10:32publishers are publishing the message to
  6171. 4:10:34message queue and then subscribers are
  6172. 4:10:37the message who are subscribed to take
  6173. 4:10:39those request from our message queue.
  6174. 4:10:42So, this is how a message queue
  6175. 4:10:43basically works. You may think now that
  6176. 4:10:46why are we using message queue? Can't we
  6177. 4:10:47directly call the consumer or the email
  6178. 4:10:50service from our application? Yes, we
  6179. 4:10:52can call, but that will keep our system
  6180. 4:10:55busy. We don't want to keep our system
  6181. 4:10:57busy and we also want that whatever
  6182. 4:10:59request we are sending should be taken
  6183. 4:11:01care by. So, these two things can only
  6184. 4:11:04be done by a message queue. Now, if we
  6185. 4:11:07talk about qualities of message queue,
  6186. 4:11:09before writing the qualities, I just
  6187. 4:11:11want to say that if you still don't get
  6188. 4:11:13what message queues are, just bear with
  6189. 4:11:15me for some minutes and you will be
  6190. 4:11:16absolutely get why do we use message
  6191. 4:11:19queues. So, the first quality here is
  6192. 4:11:21that our message queues can basically
  6193. 4:11:24hold the request. With holding the
  6194. 4:11:26request, our message queues will have to
  6195. 4:11:29communicate those requests to our
  6196. 4:11:31servers. So, they can communicate to
  6197. 4:11:33servers or we can say services here.
  6198. 4:11:36With this, why do we use message queue?
  6199. 4:11:38The reason is it can also be used to
  6200. 4:11:41handle the failed calls. And with this,
  6201. 4:11:43one of the major reason of using message
  6202. 4:11:45queue is handles the load which we are
  6203. 4:11:47giving to the third party or the service
  6204. 4:11:50application here, email service. At this
  6205. 4:11:52point, let's talk about direct
  6206. 4:11:54communication again. If we again send
  6207. 4:11:57the request directly from our e-commerce
  6208. 4:11:59application to email services, that
  6209. 4:12:01means every time a new order is placed,
  6210. 4:12:03this call is going to be there. If our
  6211. 4:12:06email server is down or anything, that
  6212. 4:12:08call is going to handle the failure case
  6213. 4:12:10also. That will be responsibility of our
  6214. 4:12:13e-commerce application. But now, since
  6215. 4:12:15we are using message queue, these both
  6216. 4:12:17queries and these both troubles will be
  6217. 4:12:20handled by our message queue itself. So,
  6218. 4:12:22we don't have to care about the failed
  6219. 4:12:24request also. So now, what happens is
  6220. 4:12:26there are various ways of sending
  6221. 4:12:28request from message queue to the
  6222. 4:12:30server. Normally, our message queues
  6223. 4:12:32follows FIFO. We all know about FIFO.
  6224. 4:12:35FIFO basically means first-in,
  6225. 4:12:37first-out. That means whatever request
  6226. 4:12:40comes first will be delivered first to
  6227. 4:12:43our consumer. And this is how the series
  6228. 4:12:46will follow. So, in this case, the first
  6229. 4:12:49request will be taken care and then
  6230. 4:12:51after that second, after then third, and
  6231. 4:12:54so forth.
  6232. 4:12:55So, this is what the first-in, first-out
  6233. 4:12:56principle is and it is used by our
  6234. 4:12:58message queue in order to process the
  6235. 4:13:00request. So, in FIFO, we have two
  6236. 4:13:03approaches. First is
  6237. 4:13:05strict order approach, which basically
  6238. 4:13:07means, let's say this is our message
  6239. 4:13:08queue again,
  6240. 4:13:10and while processing the orders or
  6241. 4:13:12requests, let's say first process is
  6242. 4:13:14processed by our consumer, done.
  6243. 4:13:17After that, we'll come to the second
  6244. 4:13:19one. So, the second one is also
  6245. 4:13:21processed.
  6246. 4:13:22Then, while processing the third
  6247. 4:13:24request, it breaks down. So, maybe
  6248. 4:13:26something is wrong with the request or
  6249. 4:13:28maybe our consumer is down for some
  6250. 4:13:30time. Because of any reason,
  6251. 4:13:33our request was not processed. So, if we
  6252. 4:13:36are following strict order, even if
  6253. 4:13:39anything fails at this level, our queue
  6254. 4:13:42will not proceed. Our consumer, even if
  6255. 4:13:45it starts working again, can't process
  6256. 4:13:47the fourth request. So, this is not
  6257. 4:13:50recommended at all because of obvious
  6258. 4:13:52reasons, because we want our process to
  6259. 4:13:55keep continue even if one or other
  6260. 4:13:57request fails.
  6261. 4:14:00So, the second part solve this issue,
  6262. 4:14:02which is unordered queue. So, in
  6263. 4:14:04unordered queue, what we'll do, we'll
  6264. 4:14:07again create like this only structure of
  6265. 4:14:10our message queue. So, every time our
  6266. 4:14:12consumer or subscriber
  6267. 4:14:14takes back our request, so one, two, and
  6268. 4:14:18even if there is some issue with the
  6269. 4:14:20third request, our system will jump to
  6270. 4:14:23the other request in order to fulfill
  6271. 4:14:25that. So, our consumer will get the
  6272. 4:14:27request to perform the operations. Our
  6273. 4:14:29system will not break. What will happen
  6274. 4:14:32to the third request? Let's talk about
  6275. 4:14:33that in a bit. So, after unordered
  6276. 4:14:36queue, it is also possible that the
  6277. 4:14:38things which we are getting
  6278. 4:14:40in request in the message queues, maybe
  6279. 4:14:43our first request, second request, third
  6280. 4:14:45request, and fourth request,
  6281. 4:14:48between all of them, our fourth request
  6282. 4:14:50is like very important to us, is of high
  6283. 4:14:54priority, and maybe second request is
  6284. 4:14:56also of high priority, but not first and
  6285. 4:14:59third. So, in this case we can't follow
  6286. 4:15:01FIFO principle, right? So, our consumer
  6287. 4:15:03needs to get the second and the fourth
  6288. 4:15:05request first. This is where we used
  6289. 4:15:08priority queue.
  6290. 4:15:09What we will do, we will attest a
  6291. 4:15:11priority
  6292. 4:15:12like number or some ID to every call.
  6293. 4:15:16And whose priority is higher, those
  6294. 4:15:18calls will be picked first by the
  6295. 4:15:20consumers. So, in this structure if we
  6296. 4:15:22talk about if the priority of every call
  6297. 4:15:26is set, let's say the priority of first
  6298. 4:15:28call is 10, the priority of second call
  6299. 4:15:31is maybe three, the priority of third
  6300. 4:15:33call is eight, and priority of fourth
  6301. 4:15:35call is one. So, that means the first
  6302. 4:15:39call which our C is going to cater is
  6303. 4:15:42fourth one.
  6304. 4:15:43After fourth, it will cater the second
  6305. 4:15:45call. After second, it will cater the
  6306. 4:15:48third call, and after third, it can
  6307. 4:15:50cater the first call. Also, these
  6308. 4:15:53numbers are in ascending order. I have
  6309. 4:15:55created uh my system in that way that
  6310. 4:15:58one will be the most important priority
  6311. 4:16:00request, but you can reverse the order
  6312. 4:16:02as well. So, this is how our priority
  6313. 4:16:05queue is going to take request. After
  6314. 4:16:07understanding priority queue, there are
  6315. 4:16:09two more concepts here.
  6316. 4:16:11So, the one concept is pull request. So,
  6317. 4:16:14in pull request or pull-based request,
  6318. 4:16:16what happens is whenever we have some
  6319. 4:16:19data in our queue, let's again put 1 2 3
  6320. 4:16:23or some random values. Now, these
  6321. 4:16:25requests are in queue, which means that
  6322. 4:16:28the queue message queue will take care
  6323. 4:16:30of them.
  6324. 4:16:31But, the consumer here will decide when
  6325. 4:16:35to pick the request. Maybe our consumer
  6326. 4:16:37is working with some other task, and the
  6327. 4:16:39moment it gets free, it can pull the
  6328. 4:16:42request. So, message queue will not send
  6329. 4:16:45the request to consumers. Our consumer
  6330. 4:16:48will pull the request accordingly.
  6331. 4:16:50Again, the reverse of this is push-based
  6332. 4:16:53request. In push-based request, what
  6333. 4:16:55happens is the opposite of pull-based
  6334. 4:16:58request, which means if we have some
  6335. 4:17:01request in our queue, our message queue
  6336. 4:17:04is going to start the communication with
  6337. 4:17:07consumer and going to push our request
  6338. 4:17:09to the consumers. So, here the
  6339. 4:17:11communication starts from message queue,
  6340. 4:17:14and in the previous case of pull-based,
  6341. 4:17:16it will start with consumer. Also, I can
  6342. 4:17:18change the
  6343. 4:17:20uh direction of our request.
  6344. 4:17:22So, because it is pulling, so request
  6345. 4:17:24will come from here. So, I hope up to
  6346. 4:17:27now the things are clear.
  6347. 4:17:28Now, let's talk about pub/sub models.
  6348. 4:17:31So, in pub/sub kind of architecture,
  6349. 4:17:33what normally happens is we have
  6350. 4:17:35publishers here who are like sending the
  6351. 4:17:38message to our message queue.
  6352. 4:17:40We have message queue in between,
  6353. 4:17:42and here we can have multiple
  6354. 4:17:43subscribers as well. So, what we are
  6355. 4:17:45going to do, these publishers are going
  6356. 4:17:48to send some or the other message to our
  6357. 4:17:50message queue, which will be taken care
  6358. 4:17:52by our message queue by providing them
  6359. 4:17:54to the subscribers. Maybe this is using
  6360. 4:17:56push, and this is using pull. So, this
  6361. 4:17:59is how the system works in pub/sub
  6362. 4:18:01model. Pub/sub is nothing but the
  6363. 4:18:03understanding of our message queues
  6364. 4:18:05only. So, in this model, we include
  6365. 4:18:07subscribers
  6366. 4:18:09and publishers as well. Now, as
  6367. 4:18:11discussed in the previous example, our
  6368. 4:18:13message queue needs to perform some
  6369. 4:18:15operations in pub/sub model as well.
  6370. 4:18:17First thing is it will hold the request.
  6371. 4:18:20It will send these requests one by one
  6372. 4:18:22to the subscribers. And also, in case of
  6373. 4:18:26any failed response, message queue needs
  6374. 4:18:29to take care of that. With this, our
  6375. 4:18:31message queue is also responsible in a
  6376. 4:18:33way to take care of subscribers as well.
  6377. 4:18:36By taking care, I mean our message queue
  6378. 4:18:38needs to check subscriber is working or
  6379. 4:18:41not. If it is not working, our message
  6380. 4:18:43queue will not send the request to that
  6381. 4:18:46particular subscriber, and it will
  6382. 4:18:48redirect the request to maybe some other
  6383. 4:18:50subscriber. This is how the things work.
  6384. 4:18:52So, our message queue basically
  6385. 4:18:54maintains or the whole state of the
  6386. 4:18:57request as well.
  6387. 4:18:58Send the request to subscribers. Take
  6388. 4:19:01care of the failed request by using DLQ.
  6389. 4:19:04We will see that in a minute. And also
  6390. 4:19:07take care of the subscribers health.
  6391. 4:19:09Also keep checking whether the
  6392. 4:19:10subscriber is it still alive to work or
  6393. 4:19:13not. We can also say that it's still
  6394. 4:19:15alive or not. We can also say that our
  6395. 4:19:17message queue takes care of request and
  6396. 4:19:19takes the response from subscribers as
  6397. 4:19:22well. So, that means our message queue
  6398. 4:19:23know which subscriber is working fine or
  6399. 4:19:26fast and which is not working that
  6400. 4:19:28delicately. So, every time we get a
  6401. 4:19:31priority queue or a priority task, it
  6402. 4:19:33will be sent to the subscriber who is
  6403. 4:19:35working efficiently. So, this is what
  6404. 4:19:37Pub/Sub basically is. Let's talk about
  6405. 4:19:39some of the factors which can affect
  6406. 4:19:41Pub/Sub. The first thing which you can
  6407. 4:19:43talk about is message order. Let's talk
  6408. 4:19:46about message order. So, we are saying
  6409. 4:19:48that all the calls which are going to
  6410. 4:19:50message queue are async calls. Then why
  6411. 4:19:53do we need to maintain order? Okay.
  6412. 4:19:55First thing is that even if we want to
  6413. 4:19:57maintain some order, we know that we can
  6414. 4:20:00use priority queue.
  6415. 4:20:01And this is the only way of maintaining
  6416. 4:20:03any kind of order in message queue.
  6417. 4:20:05There is no other way. Maybe it is
  6418. 4:20:07handled by our application or maybe we
  6419. 4:20:09can add some additional information of
  6420. 4:20:11order in the request itself. This is how
  6421. 4:20:14we can handle message orders. Other than
  6422. 4:20:16that, it is for async calls where the
  6423. 4:20:19order doesn't matter. The second thing
  6424. 4:20:21we can talk about is how our messages
  6425. 4:20:24are going to be consumed. So, we know
  6426. 4:20:26that publishers publish the message and
  6427. 4:20:29they will store in message queue and
  6428. 4:20:30consumers are going to consume this. But
  6429. 4:20:32how they are going to consume this? We
  6430. 4:20:34are not maintaining any order. That
  6431. 4:20:36means these consumptions are random.
  6432. 4:20:40Maybe out of 100 request, the consumer
  6433. 4:20:43can pick up the 100 requests first. Even
  6434. 4:20:46if we are using priority queue, we can
  6435. 4:20:48make the priority of 100 requests first.
  6436. 4:20:50So, there is no particular order in
  6437. 4:20:53which the requests are going to be
  6438. 4:20:54fulfilled.
  6439. 4:20:55Consumers can pick any of the request
  6440. 4:20:58anytime. Also, message queue can send
  6441. 4:21:00any of the request anytime based on the
  6442. 4:21:02priority either or it is completely
  6443. 4:21:05random. Let's talk about the third
  6444. 4:21:06factor, which is poison message. So, all
  6445. 4:21:10the poison messages means all the
  6446. 4:21:13messages that subscribers are not able
  6447. 4:21:16to process. Maybe because they are bad
  6448. 4:21:19requests or maybe invalid. It is also
  6449. 4:21:23possible that they are failing like
  6450. 4:21:25after multiple returns as well. Also, if
  6451. 4:21:27we think about it, let's say a request
  6452. 4:21:30failed at the consumer side. And then,
  6453. 4:21:32if we send those requests again and
  6454. 4:21:34again, those are going to use or consume
  6455. 4:21:37the consumers energy and will do
  6456. 4:21:39nothing. Those requests are basically
  6457. 4:21:41our poison request who are taking time
  6458. 4:21:44and resource from our consumer as well
  6459. 4:21:46as message queue but are not able to
  6460. 4:21:47fulfill. Now, at this point, let's talk
  6461. 4:21:50about DLQ. So, this is our message
  6462. 4:21:52queue. This is our publisher or
  6463. 4:21:54producer, we can say. And here, we have
  6464. 4:21:56our consumer or subscriber. Now, let's
  6465. 4:21:59say we have a request which has not been
  6466. 4:22:02processed by our subscriber due to any
  6467. 4:22:04of the reasons. So, in this case, our
  6468. 4:22:07message queue can't stop. It will send
  6469. 4:22:09the next request. But, this first
  6470. 4:22:12request will go to a different queue for
  6471. 4:22:15retry or to maintain that this request
  6472. 4:22:18was failed. This queue basically, which
  6473. 4:22:21is maintained by our message queue only,
  6474. 4:22:23is called DLQ. DLQ stands for dead queue
  6475. 4:22:27or dead letter queue. So, our message
  6476. 4:22:30queue maintains this DLQ in order to
  6477. 4:22:32keep track of the failed request as
  6478. 4:22:34well.
  6479. 4:22:35So, that even if the publisher have put
  6480. 4:22:38the async operations, eventually
  6481. 4:22:41they will have to know that these cases
  6482. 4:22:43are failed. So, we will see the failed
  6483. 4:22:45logs or failed requests through any
  6484. 4:22:47logs, from which we will see, "Okay,
  6485. 4:22:50this request was failed."
  6486. 4:22:51Or other than that, we can also send
  6487. 4:22:53some or the other information to our
  6488. 4:22:55publisher in a async manner or in a
  6489. 4:22:58report kind of manner, where we will
  6490. 4:22:59give the stats of these many requests
  6491. 4:23:01are passed, these many requests are
  6492. 4:23:03failed. And here are the reasons of the
  6493. 4:23:05failed request. So, this DLQ is the
  6494. 4:23:08mechanisms through which our message
  6495. 4:23:10queue will take care of the failed
  6496. 4:23:12requests as well. I hope you get it now.
  6497. 4:23:15Also, poison. Poison means any request
  6498. 4:23:18which can't be processed, which is using
  6499. 4:23:20or consuming the resources, but can't be
  6500. 4:23:22processed.
  6501. 4:23:24So, with this, let's move to our four
  6502. 4:23:26factor. Fourth factor basically is
  6503. 4:23:29duplicacy. So, if, let's say, our
  6504. 4:23:32message queue have processed this
  6505. 4:23:35request when it is already processed by
  6506. 4:23:37subscriber one, then it should not
  6507. 4:23:40travel to subscriber two by any other
  6508. 4:23:43way.
  6509. 4:23:43Because the request is already
  6510. 4:23:45processed, it should not be processed
  6511. 4:23:47again. What if this request contains
  6512. 4:23:50some important information which can't
  6513. 4:23:52be done twice? So, in order to maintain
  6514. 4:23:55duplicacy, our message queue only takes
  6515. 4:23:58care of this thing.
  6516. 4:23:59So, with taking care of each request and
  6517. 4:24:03dead request, it is also duty of message
  6518. 4:24:06queue that whenever a process is done,
  6519. 4:24:09it should be deleted from our message
  6520. 4:24:11queue, so that we don't perform any
  6521. 4:24:13operation on that particular request. It
  6522. 4:24:15should be deleted. And if configured,
  6523. 4:24:18our message queue keeps end track of
  6524. 4:24:20this, that there was some request which
  6525. 4:24:22was obtained from P1 producer
  6526. 4:24:25and was handled by our S1 subscriber.
  6527. 4:24:28So, this is how our message queue will
  6528. 4:24:30take care of the duplicacy of the
  6529. 4:24:32request as well. After discussing all
  6530. 4:24:35these things, let's talk about some of
  6531. 4:24:36the use cases. Like where we can use
  6532. 4:24:39message Q easily and where we should
  6533. 4:24:41avoid using message Q. Let's say if we
  6534. 4:24:44are working on any async kind of
  6535. 4:24:46environment, any async operations, any
  6536. 4:24:49async request, we can easily go for
  6537. 4:24:52message Q. There is no doubt about that.
  6538. 4:24:54Similarly, if we talk about any
  6539. 4:24:56decoupling activity, which can be like
  6540. 4:24:59taking care of user analytics we want to
  6541. 4:25:01see and capture, maybe we want to
  6542. 4:25:02capture where our user lands, if our
  6543. 4:25:05order is failing, why they are failing,
  6544. 4:25:07and all the log systems. We can easily
  6545. 4:25:09do this with our message Q as well.
  6546. 4:25:12There is no doubt about this.
  6547. 4:25:14The third area can be our load balancing
  6548. 4:25:17acts. So, if we think about it, our
  6549. 4:25:19message Q does everything what a load
  6550. 4:25:22balancer does, right? So, our message Qs
  6551. 4:25:25can be used as load balancers as well.
  6552. 4:25:29Where it will take care of all the
  6553. 4:25:31request, it will keep them in a Q, and
  6554. 4:25:33it will provide those requests to
  6555. 4:25:35servers respectively.
  6556. 4:25:37Also, we can use the message Q with this
  6557. 4:25:39with the deferred system. Where we have
  6558. 4:25:42some operations, but we can basically do
  6559. 4:25:44them later, like generating reports and
  6560. 4:25:47everything. After every day, we can
  6561. 4:25:49generate some reports. Or maybe there
  6562. 4:25:51can be any schedule processes for doing
  6563. 4:25:53anything. Let's say we are adding
  6564. 4:25:55products into our catalogs like whole
  6565. 4:25:57day, and in the evening we want them to
  6566. 4:25:59be live. So, we will schedule a process
  6567. 4:26:01in order to work with them. So, the
  6568. 4:26:03message Qs can be used with load
  6569. 4:26:05balancer as well as they can be used by
  6570. 4:26:07our deferred system. Now, message Qs are
  6571. 4:26:10a costly service. So, if we don't have a
  6572. 4:26:13lot of request, we should not use
  6573. 4:26:15message Qs, right? Because it is a
  6574. 4:26:17costly service, and why waste our cost
  6575. 4:26:21when we can use them in a synchronous
  6576. 4:26:22manner? Or maybe if the request count is
  6577. 4:26:25low, we will directly call the
  6578. 4:26:27subscriber from producers. Like for
  6579. 4:26:29emailing, we will directly send the
  6580. 4:26:30request for our email service. With
  6581. 4:26:33this, if we are using any real-time
  6582. 4:26:35application, we can't use message queues
  6583. 4:26:37basically. Why? Because we need the
  6584. 4:26:40response to be in a synchronous manner.
  6585. 4:26:42We need the response to be immediate.
  6586. 4:26:44And if we are using message queue,
  6587. 4:26:46our request can take some time
  6588. 4:26:49to basically respond from any email
  6589. 4:26:51service or any other services which we
  6590. 4:26:53are using. So, in these cases, we should
  6591. 4:26:55avoid using message queues. And also, we
  6592. 4:26:57should avoid using message queues
  6593. 4:26:59whenever we need some acknowledgement.
  6594. 4:27:04Because if we need some acknowledgement,
  6595. 4:27:05that means our server or our system is
  6596. 4:27:09waiting for the some response to get.
  6597. 4:27:11But message queue normally doesn't do
  6598. 4:27:12it. In this example, where we were
  6599. 4:27:14sending some reports, this will be also
  6600. 4:27:17done by some or the other intervention
  6601. 4:27:19of our code only, so that we can see the
  6602. 4:27:21processes. But if we want response from
  6603. 4:27:23every request, it can't be done by
  6604. 4:27:25message queue. So, whenever we need
  6605. 4:27:27acknowledgement, we don't use message
  6606. 4:27:29queues here. And with this, we have
  6607. 4:27:31covered about message queues. Now, let's
  6608. 4:27:33talk about faults. Whenever we think
  6609. 4:27:36about faults or error, we basically,
  6610. 4:27:39while building any system or while
  6611. 4:27:41building any software as well, we have
  6612. 4:27:42to take care of each and every fault and
  6613. 4:27:44error. Now, while studying system
  6614. 4:27:46design, we have encountered very
  6615. 4:27:48components and we know what the
  6616. 4:27:50complexity our software also holds. So,
  6617. 4:27:53let's talk about faults and error. So,
  6618. 4:27:55our faults or errors can be called as in
  6619. 4:27:58any condition, if our system is not
  6620. 4:28:01responding in a way it should respond,
  6621. 4:28:03that means we have some issue or some
  6622. 4:28:06fault or some error with our system. We
  6623. 4:28:09can easily categorize them into three
  6624. 4:28:11categories. The first category is
  6625. 4:28:13hardware. So, hardware faults are
  6626. 4:28:15basically belongs to our server. Maybe
  6627. 4:28:18we are using databases, so DB can also
  6628. 4:28:20crash.
  6629. 4:28:21We are using message queues, we are
  6630. 4:28:23using each and every components, and
  6631. 4:28:25every component can fail our system if
  6632. 4:28:27they stops working. So, this hardware
  6633. 4:28:30issue or hardware fault is our first
  6634. 4:28:32type, and the nature of our hardware
  6635. 4:28:34failures is it can be random, right?
  6636. 4:28:37Like our system can fail entirely
  6637. 4:28:39anytime. There can be various reasons of
  6638. 4:28:42its failure. We can have like a disk is
  6639. 4:28:44full or something out of memory issue,
  6640. 4:28:47or let's say our server is down. We also
  6641. 4:28:49use network cables, so maybe they are
  6642. 4:28:51damaged. Maybe there is an issue with
  6643. 4:28:54the power supply. Maybe one or the other
  6644. 4:28:56node is not configured correctly, or
  6645. 4:28:58maybe it is experiencing some spike in
  6646. 4:29:00request, so it is down because of
  6647. 4:29:02overheating. Also, we are using
  6648. 4:29:04databases, so database can also be
  6649. 4:29:06compromised, right? These all things are
  6650. 4:29:08basically the fault of our hardware. We
  6651. 4:29:11do have some control over it, like we
  6652. 4:29:12can pass some alerts whenever we are
  6653. 4:29:15sensing any risk out of them, but they
  6654. 4:29:18are like inevitable. Whenever a hardware
  6655. 4:29:21fails, it mostly fails abruptly. That's
  6656. 4:29:24why we use multiple systems in databases
  6657. 4:29:27as well, and in terms of servers as
  6658. 4:29:29well, so that if any server crashes or
  6659. 4:29:31any databases goes down,
  6660. 4:29:34we can use our system by using the other
  6661. 4:29:36database or other server, maybe. So,
  6662. 4:29:38this is what the hardware issue is. Now,
  6663. 4:29:42let's talk about the second category of
  6664. 4:29:44our issues, which is software issue.
  6665. 4:29:47Now, our software issue can be caused
  6666. 4:29:48through anything. Maybe the code which
  6667. 4:29:51we are working on is written badly.
  6668. 4:29:53Maybe we have forgot to handle any
  6669. 4:29:56exception or anything. Maybe while
  6670. 4:29:58testing our application, we tested all
  6671. 4:30:00of the cases, but there are some edge
  6672. 4:30:01cases which comes to our picture when
  6673. 4:30:03the user actually uses the application.
  6674. 4:30:06So, there can be some edge cases. Also,
  6675. 4:30:09while coding in microservices or using
  6676. 4:30:11APIs, we also configure them into our
  6677. 4:30:13configurations. Maybe there can be some
  6678. 4:30:16issue with the configuration itself that
  6679. 4:30:18our system stops responding or maybe not
  6680. 4:30:21responding in a way which it should be
  6681. 4:30:23responding. Other than that, we can have
  6682. 4:30:25some deployment issues as well. There
  6683. 4:30:27can be some issues with the development
  6684. 4:30:28environment and the production
  6685. 4:30:30environment mismatch. So, something is
  6686. 4:30:32working on production but not working on
  6687. 4:30:34our development or vice versa. These
  6688. 4:30:36things come on a day-to-day basis in the
  6689. 4:30:38life of software developers. So, don't
  6690. 4:30:40worry about this but we have to take
  6691. 4:30:41care of this and this is our software
  6692. 4:30:44issue or software fault we can say. With
  6693. 4:30:46this, if I have to write any other
  6694. 4:30:48thing, uh one of the major pain points
  6695. 4:30:50can be merge requests. So, let's say two
  6696. 4:30:53developers working on similar feature or
  6697. 4:30:55same file parallelly and when the code
  6698. 4:30:58compiles of both of them, we need to
  6699. 4:31:00thoroughly test both of the
  6700. 4:31:02functionalities so that it doesn't break
  6701. 4:31:04down each other on the production or on
  6702. 4:31:06staging or on any environment. So, this
  6703. 4:31:08is one of the most pain point thing of
  6704. 4:31:11any developer. Other than that, our
  6705. 4:31:12software fault or software issue can be,
  6706. 4:31:15let's say, our requests are taking too
  6707. 4:31:17much time. So, the performance is also
  6708. 4:31:20something which software should take
  6709. 4:31:22care of. But the good and the bad thing
  6710. 4:31:24about software issues is that it is not
  6711. 4:31:27random. There has to be something in the
  6712. 4:31:29configuration or code file which we can
  6713. 4:31:31find out and solve that issue. Also,
  6714. 4:31:34this is not random, this is a good thing
  6715. 4:31:37so that we can find out the issue. But
  6716. 4:31:39with not random, it is also
  6717. 4:31:40deterministic which means by following
  6718. 4:31:43the same steps, we can get that proper
  6719. 4:31:46error again and again. And if that error
  6720. 4:31:49is not fixed sooner, that can basically
  6721. 4:31:51break our system or give a bad
  6722. 4:31:53experience to our users.
  6723. 4:31:55So, we need to fix software issues very
  6724. 4:31:58carefully and ASAP. Now, the third fault
  6725. 4:32:01is not really a fault but it comes into
  6726. 4:32:04the category of fault and is considered
  6727. 4:32:06the most important faults in our system
  6728. 4:32:09which is human fault. We know that every
  6729. 4:32:12human is unpredictable and we don't have
  6730. 4:32:15any control over ourselves. We can do
  6731. 4:32:17basically anything. So, this kind of
  6732. 4:32:19issue or error can cause unpredictably.
  6733. 4:32:23Why? Because humans are considered to be
  6734. 4:32:26most unreliable components of software
  6735. 4:32:28development among all.
  6736. 4:32:30Every component, whenever they are
  6737. 4:32:32failing or not working, give us an alert
  6738. 4:32:35beforehand. But with humans, it is
  6739. 4:32:38sometimes possible, sometimes not
  6740. 4:32:39possible based on the value of that
  6741. 4:32:41particular developer or anyone attached
  6742. 4:32:43to the software development cycle. But
  6743. 4:32:45with saying these all things about
  6744. 4:32:47developers or humans, we also need to
  6745. 4:32:49know that it is one of the most powerful
  6746. 4:32:52component or person in the software
  6747. 4:32:55development life cycle. If human stops
  6748. 4:32:57working, nothing is going to work. Even
  6749. 4:33:00if the code is written by AI agents or
  6750. 4:33:03AI anything, Claude, OpenAI, or any
  6751. 4:33:05other open source, it is still needs to
  6752. 4:33:08be rectified by human only. Because we
  6753. 4:33:11need some accountability over that code
  6754. 4:33:13because that code is going to build a
  6755. 4:33:15product which is going to be used by
  6756. 4:33:17humans only. So, we need human in
  6757. 4:33:20between all these processes. So, this is
  6758. 4:33:22one of the most powerful thing. That
  6759. 4:33:24means that makes us responsible for
  6760. 4:33:27handling the code even more. So, these
  6761. 4:33:29are the three categories in which we
  6762. 4:33:30divide our faults normally into. The
  6763. 4:33:33software issue, the hardware issue, and
  6764. 4:33:36the human fault or human error. Human
  6765. 4:33:39issues can be solved by having good
  6766. 4:33:41behavior or not doing the band-aid fixes
  6767. 4:33:43or quick fixes in the wrong manner, but
  6768. 4:33:46going deep into the system and fixing
  6769. 4:33:48the thing entirely for each and every
  6770. 4:33:50area. Also, by software issues, we
  6771. 4:33:53should not write bad code, handle
  6772. 4:33:55exceptions properly, see the edge cases,
  6773. 4:33:57maybe do testing thoroughly of each and
  6774. 4:33:59every component every time you make a
  6775. 4:34:01build. It can be solved, right? Also,
  6776. 4:34:04deployment issues can be solved if you
  6777. 4:34:05configure each and every environment
  6778. 4:34:07variable carefully. There can be other
  6779. 4:34:09configurations for databases or for
  6780. 4:34:11other APIs which can be configured on
  6781. 4:34:14development environment as well in order
  6782. 4:34:16to replicate that issue and eventually
  6783. 4:34:18solve it. Now, if we talk about
  6784. 4:34:20hardwares, hardwares are something which
  6785. 4:34:21we don't have any control on. So, if any
  6786. 4:34:24hardware is going down, it is mostly
  6787. 4:34:27unpredictable. So, this is also a danger
  6788. 4:34:29point. That's why we have to scale our
  6789. 4:34:31system in order to handle the system
  6790. 4:34:34perfectly or in a better manner. So,
  6791. 4:34:37this is all about faults and errors.
  6792. 4:34:39Now, let's understand about the last
  6793. 4:34:41topic of system design, which is most
  6794. 4:34:44important but many playlists skip this
  6795. 4:34:46one. This is our monitoring and
  6796. 4:34:49observability. We all know that in our
  6797. 4:34:52product life cycle, our product is once
  6798. 4:34:54developed will be deployed to any of
  6799. 4:34:56these servers and then will be
  6800. 4:34:58maintained. Now, creating a product is
  6801. 4:35:01entirely different from maintaining that
  6802. 4:35:04product. Here, we don't have to maybe
  6803. 4:35:06change a lot of code and add lot of
  6804. 4:35:08features, but we have to keep monitoring
  6805. 4:35:11whether our product is working fine or
  6806. 4:35:14not. So, we have to take care of these
  6807. 4:35:16following things. Are there any errors
  6808. 4:35:18in our system or not? Is every component
  6809. 4:35:20basically is working fine or not? How's
  6810. 4:35:22the health of each and every server,
  6811. 4:35:24every component, every database? If
  6812. 4:35:27there are coming edge cases or any
  6813. 4:35:29errors in our system, are we recording
  6814. 4:35:31them correctly? Do we have logs in place
  6815. 4:35:34so that each and every customer request
  6816. 4:35:36can be tracked and can be seen whether
  6817. 4:35:38they are working fine or not. And if
  6818. 4:35:40there is any error, can we like go to
  6819. 4:35:42the RCA of that particular error so that
  6820. 4:35:45our developer can debug and perform the
  6821. 4:35:47required operation. And also, if any
  6822. 4:35:49other component is going down or
  6823. 4:35:51basically down at this particular
  6824. 4:35:53moment, do we have other systems in
  6825. 4:35:55place who will take care of the system?
  6826. 4:35:57And also, do we have proper alert
  6827. 4:35:59systems so that we can rectify or get
  6828. 4:36:01these messages beforehand so that we can
  6829. 4:36:04take care of the situation in a better
  6830. 4:36:05way. So we need to take care of all of
  6831. 4:36:07these things plus more in the
  6832. 4:36:09maintenance phase or while maintaining
  6833. 4:36:11our application. For example, let's talk
  6834. 4:36:13about microservices. So one microservice
  6835. 4:36:17can do two operations, right? Either it
  6836. 4:36:19can handle a request and work on it or
  6837. 4:36:22the other thing it can do is it can even
  6838. 4:36:24redirect the request to any other
  6839. 4:36:26microservices. So in this case, we have
  6840. 4:36:28to monitor two things. The first thing
  6841. 4:36:30is how our APIs are performing and while
  6842. 4:36:34handling any request or whenever a
  6843. 4:36:36microservice or a server is working on
  6844. 4:36:38anything, we need to also check
  6845. 4:36:40how it is affecting our machine. Like
  6846. 4:36:43how much memory it is consuming, what
  6847. 4:36:45are the consequences of this particular
  6848. 4:36:47request if it gets overloaded or
  6849. 4:36:49something. So we have to take care of
  6850. 4:36:51the machine as well as the APIs at the
  6851. 4:36:54same time. Now let's talk about both of
  6852. 4:36:56them one by one. So if we talk about API
  6853. 4:36:59monitoring, the first thing we need to
  6854. 4:37:01check is our throughput. Now what is
  6855. 4:37:03throughput? Throughput basically means
  6856. 4:37:05how many requests can our server handle.
  6857. 4:37:08Let's say our server can handle 10k
  6858. 4:37:11requests per second. So that means if
  6859. 4:37:15our server is already hitting 9k or
  6860. 4:37:18maybe 8k responses, we have to configure
  6861. 4:37:21some alerts in order to know whether we
  6862. 4:37:23are going to hit our throughput or not
  6863. 4:37:25and maybe we'll migrate the data or the
  6864. 4:37:27request to some other servers at the
  6865. 4:37:29particular time because we need this
  6866. 4:37:31server to perform or to be alive. The
  6867. 4:37:34other thing which we can monitor while
  6868. 4:37:35using the APIs can be our error codes.
  6869. 4:37:39Like we all know about 500 issues and
  6870. 4:37:43400 issues and maybe we are using some
  6871. 4:37:45redirects also so 300 issues as well. We
  6872. 4:37:49need to know how many 500s are we
  6873. 4:37:51getting, how many 400 requests are we
  6874. 4:37:53getting, how many 300 requests are we
  6875. 4:37:55getting. So the first thing is when we
  6876. 4:37:57are getting too many error things, we
  6877. 4:37:59need to know the RCA of it. So, that
  6878. 4:38:02means our logging should be in places so
  6879. 4:38:05that our developer can work. One thing
  6880. 4:38:06is that. Also, we need to put some alert
  6881. 4:38:09whenever the count increases after a
  6882. 4:38:11particular point so that we can get to
  6883. 4:38:13know about the 500 issues or 400 issues
  6884. 4:38:16or 300 issues and fix them accordingly.
  6885. 4:38:18Third thing is our health check. So, in
  6886. 4:38:21order to check health of any components,
  6887. 4:38:24we have already seen that we can do it
  6888. 4:38:25by two ways, passive and active. So, we
  6889. 4:38:28have to constantly monitor the 200
  6890. 4:38:31responses as well and see if our
  6891. 4:38:33component is in a working condition or
  6892. 4:38:36not and is giving the positive replies
  6893. 4:38:38or not. The fourth thing is to check our
  6894. 4:38:41latency. Latency is a very good metrics
  6895. 4:38:44in order to check the performance of any
  6896. 4:38:45system. How can we check the latency?
  6897. 4:38:48Suppose we have some 10 requests and
  6898. 4:38:51suppose these values are the response
  6899. 4:38:53time of those APIs. Now, we need to know
  6900. 4:38:57the response time of our particular
  6901. 4:38:59server, of our particular API system.
  6902. 4:39:02So, how do we calculate them? If we use,
  6903. 4:39:05let's say, average, the average gives
  6904. 4:39:07the wrong value because, let's say, our
  6905. 4:39:09average value for this will come to 6.8
  6906. 4:39:12seconds, right? But, if we see most of
  6907. 4:39:15our requests are being handled in a much
  6908. 4:39:18lower rate. So, this average value
  6909. 4:39:20basically gives a wrong impression of
  6910. 4:39:22our services. So, apart from average, we
  6911. 4:39:26check P values. What is P? P basically
  6912. 4:39:29stands for percentile. So, what is a
  6913. 4:39:31percentile? Percentile basically says if
  6914. 4:39:34we are writing P90, we are saying that
  6915. 4:39:37our 90% of the request can be handled in
  6916. 4:39:41a particular seconds or maybe
  6917. 4:39:44milliseconds, whatever the time is. In
  6918. 4:39:46our current example, if we arrange them
  6919. 4:39:48in an ascending order, something like
  6920. 4:39:50this, we can see that we have 10
  6921. 4:39:52requests and P90 will be calculated here
  6922. 4:39:55and all of the requests like 90% of the
  6923. 4:39:58requests will take lesser time than this
  6924. 4:40:00time. So, this is our 12 seconds maybe.
  6925. 4:40:03So, let me also remove this milliseconds
  6926. 4:40:05for a bit. 12 seconds should not be idle
  6927. 4:40:07time for any API response. This is just
  6928. 4:40:09for the example, okay? Similarly, if we
  6929. 4:40:12talk about let's say P50 here, P50 will
  6930. 4:40:15be 4 seconds, right? So, that means 50%
  6931. 4:40:18of our request are currently covered
  6932. 4:40:21under 4 seconds. So, we can see that P50
  6933. 4:40:24is 4 second and somehow P90 is 12
  6934. 4:40:27second, that means something should be
  6935. 4:40:29off between these two. We have to check
  6936. 4:40:31all the APIs which are giving us the
  6937. 4:40:33response of near 12 seconds. And we have
  6938. 4:40:36to basically optimize them and improve
  6939. 4:40:38the performance of our entire system.
  6940. 4:40:40Again, these P90, P50, P70, P maybe
  6941. 4:40:4699.9,
  6942. 4:40:47when we have a large system and a lot of
  6943. 4:40:49calls, so all these matrices and values
  6944. 4:40:52percentiles help us in jot down how our
  6945. 4:40:55system is working in a majority portion.
  6946. 4:40:58And also, we can optimize the
  6947. 4:41:00performance of our system by checking
  6948. 4:41:02the differences of different P values.
  6949. 4:41:04So, it is very useful in case of
  6950. 4:41:06optimizing our code as well and also, we
  6951. 4:41:08need to know how our system is
  6952. 4:41:10eventually working. So, this is what
  6953. 4:41:13percentile is used for and we use it to
  6954. 4:41:16calculate the latency of our project.
  6955. 4:41:18Again, here also, we can provide some
  6956. 4:41:20alert system. Let's say our P90 here is
  6957. 4:41:2312 second and if our responses are
  6958. 4:41:26taking 50 second or something, we should
  6959. 4:41:28get an additional alert because this is
  6960. 4:41:31an unusual behavior for us now. So, this
  6961. 4:41:34is how we take care of latency of our
  6962. 4:41:36system as well. So, after APIs, let's
  6963. 4:41:38talk about machine monitoring. By
  6964. 4:41:40machine, I mean hardware, right? The
  6965. 4:41:42factors of measuring how our hardwares
  6966. 4:41:45are working, the first factor if we talk
  6967. 4:41:47about it can be our CPU usage, like how
  6968. 4:41:50much power is being consumed by that
  6969. 4:41:52particular operation or that particular
  6970. 4:41:55hardware while doing some particular
  6971. 4:41:56operation. And if that usage basically
  6972. 4:41:59exceeds any limit, let's say I'm taking
  6973. 4:42:01example of 75, maybe you can take 70, 90
  6974. 4:42:04accordingly. We should get an alert that
  6975. 4:42:07your CPU usage is already attained a
  6976. 4:42:1075%. So you need to take care of the
  6977. 4:42:12systems. Again, the other thing can be
  6978. 4:42:15memory usage. Let's say already we have
  6979. 4:42:17consumed 90% of the memory.
  6980. 4:42:20So we know now that our database will
  6981. 4:42:23not be able to hold more values. So we
  6982. 4:42:25need to immediately either scale our
  6983. 4:42:27system or we need to see whether we are
  6984. 4:42:30getting some error or not. Similarly, we
  6985. 4:42:32have a lot of input-output operations as
  6986. 4:42:35well. So we have to take care of disk IO
  6987. 4:42:37operations as well as network also. In
  6988. 4:42:40both of these cases, we can set the
  6989. 4:42:42limit and once the limit is exceed, we
  6990. 4:42:45can send alerts accordingly. So this is
  6991. 4:42:47all about monitoring our APIs and
  6992. 4:42:49machine. Now, let me give you a quick
  6993. 4:42:51recap of all the things which we have
  6994. 4:42:53discussed so far. We have discussed
  6995. 4:42:55about system design, what is system
  6996. 4:42:57design, what are the different
  6997. 4:42:59components of system design. Then we
  6998. 4:43:01have discussed about data intensive and
  6999. 4:43:03compute intensive applications, then
  7000. 4:43:05functionality and non-functionality
  7001. 4:43:07requirements, what is DNS, APIs. We have
  7002. 4:43:10also understood REST APIs in details.
  7003. 4:43:13After that we move on to DB, our SQL and
  7004. 4:43:16no SQL DB with detail. Also, cache and
  7005. 4:43:19their strategies of eviction as well as
  7006. 4:43:22how do we store the data in cache. We
  7007. 4:43:24have completed all the cache part very
  7008. 4:43:25extensively so that we don't get any
  7009. 4:43:28issues afterwards.
  7010. 4:43:29We have also covered load balancing and
  7011. 4:43:31load balancer in which we have talked
  7012. 4:43:33about how a load balancer acts as a
  7013. 4:43:35mediator between the coming request and
  7014. 4:43:38the request which goes to server. Also,
  7015. 4:43:41it works as an orchestration between
  7016. 4:43:43different servers and take care of each
  7017. 4:43:45and everyone's health. With that, we
  7018. 4:43:47have covered various forms of
  7019. 4:43:48replication, partitioning. Ultimately,
  7020. 4:43:51one of the most important theorems which
  7021. 4:43:53we're going to cover in each and every
  7022. 4:43:55interview, that is CAP theorem. Whether
  7023. 4:43:58you'll choose availability over
  7024. 4:44:00consistency or vice versa. After CAP
  7025. 4:44:03theorem, we have covered message queues,
  7026. 4:44:05fault tolerance, and then we had a brief
  7027. 4:44:07discussion about monitoring and
  7028. 4:44:09observability also.
  7029. 4:44:11So, with all these topics, now we are
  7030. 4:44:13ready to give any interview or to design
  7031. 4:44:17any system design component which we
  7032. 4:44:19want to. We have a solid foundation now.
  7033. 4:44:22Now, we can design any system. So,
  7034. 4:44:25congratulations, you have unlocked a new
  7035. 4:44:27trick, a new component, a new system in
  7036. 4:44:30your resume, in your abilities. Also, if
  7037. 4:44:33you feel like I have missed any point or
  7038. 4:44:35any topic, please mention that in the
  7039. 4:44:37comment. I may make a video of it in the
  7040. 4:44:40coming days.
  7041. 4:44:41With this, let's move to the last part
  7042. 4:44:43of our video, which is designing a
  7043. 4:44:46system. So, whenever you appear in an
  7044. 4:44:48interview of system design or maybe you
  7045. 4:44:50will design a system by yourself, it is
  7046. 4:44:52very important to think out loud. So,
  7047. 4:44:55you have to take your decisions very
  7048. 4:44:57specifically. No one is looking for the
  7049. 4:44:59perfect solution, but everyone here is
  7050. 4:45:01looking for what are the trade-offs you
  7051. 4:45:03make, what are the components you use.
  7052. 4:45:05Also, when I'm talking about components,
  7053. 4:45:08whenever you are using a component, it
  7054. 4:45:10is very important that you use it for
  7055. 4:45:12the right reasons. Just because you know
  7056. 4:45:15about all the components, it is not
  7057. 4:45:16recommended to add all the components
  7058. 4:45:18into your system. Because remember,
  7059. 4:45:21every component you add will add the
  7060. 4:45:23costing of your system also. So, we have
  7061. 4:45:26to use the component and design the
  7062. 4:45:28system very efficiently so that it can
  7063. 4:45:31be cost-efficient as well as work
  7064. 4:45:33efficiently to solve our purpose. So,
  7065. 4:45:36for now, let's design a video streaming
  7066. 4:45:39application.
  7067. 4:45:40So, see, we already have a lot of
  7068. 4:45:42applications. We have YouTube for
  7069. 4:45:44streaming. We also have, let's say,
  7070. 4:45:46Hotstar, Netflix, Prime. Also, if we
  7071. 4:45:50talk about streaming applications, we
  7072. 4:45:52can also say G Meet is also a streaming
  7073. 4:45:55application because we can do the live
  7074. 4:45:57streaming.
  7075. 4:45:58Uh we can call it a Zoom call or a G
  7076. 4:46:00Meet also. So, Zoom can also come into
  7077. 4:46:03this category where we stream our video
  7078. 4:46:05live to other members. So, a call is
  7079. 4:46:09basically also an example of a streaming
  7080. 4:46:10application. Now, with this, we can also
  7081. 4:46:13use LMS or any other software for
  7082. 4:46:16streaming our data. We can have more
  7083. 4:46:18applications. I'm just jotting this down
  7084. 4:46:20so that you get the idea of the
  7085. 4:46:22applications which use streaming. Now,
  7086. 4:46:24before starting, it is very important to
  7087. 4:46:27jot down the requirement. So, for our
  7088. 4:46:29video, we doesn't care about user login.
  7089. 4:46:32We don't care about their reviews and
  7090. 4:46:35maybe rating also. We don't care about
  7091. 4:46:38any other operation, but we only need to
  7092. 4:46:41know how a video basically process from
  7093. 4:46:44one system to finally our client system.
  7094. 4:46:48This is what we need to know in this
  7095. 4:46:50particular example. Now, whenever you
  7096. 4:46:53appear on an interview also, it is very
  7097. 4:46:55important to jot down the requirement
  7098. 4:46:57because let's say they give you an
  7099. 4:46:59example of designing any software like
  7100. 4:47:01Netflix, you need to know what are the
  7101. 4:47:04features the interviewer is interested
  7102. 4:47:06in. With that, while creating your
  7103. 4:47:09systems also, you need to know
  7104. 4:47:10specifically what you are designing
  7105. 4:47:12because you may be designing an entire
  7106. 4:47:14LMS, but for that moment, you need to
  7107. 4:47:17pick feature by feature and design each
  7108. 4:47:20component and at the end, maybe collect
  7109. 4:47:22all the findings and create a system.
  7110. 4:47:25This is how every system is built. Now,
  7111. 4:47:28here, we only are talking about this
  7112. 4:47:30process
  7113. 4:47:31of getting a video to our client system
  7114. 4:47:34from any server or from any other
  7115. 4:47:36source. Okay, so let's start from very
  7116. 4:47:38basic.
  7117. 4:47:39What is a video? A video is nothing but
  7118. 4:47:42a collection of different images we can
  7119. 4:47:45say and with this we also have some
  7120. 4:47:47audio here.
  7121. 4:47:48So different images are combined with
  7122. 4:47:50audio to create a video, okay? So in
  7123. 4:47:53order to calculate different images we
  7124. 4:47:55can see FPS or frame per second. We can
  7125. 4:47:58have 30 or 60.
  7126. 4:48:00These two things are available. Also, we
  7127. 4:48:02can use 120 or 240 if they're available.
  7128. 4:48:05I'm not that sure about it. But yeah,
  7129. 4:48:07these two frame rates are quite common
  7130. 4:48:10in our day-to-day life.
  7131. 4:48:11So what does it mean to have 30 and 60
  7132. 4:48:14FPS? Basically, in 1 second, if we are
  7133. 4:48:18using 30 FPS, our video will be divided
  7134. 4:48:21into 30 images.
  7135. 4:48:23And if we are using 60, for 1 second, it
  7136. 4:48:26will be divided into 60 images.
  7137. 4:48:28So that means the size of 60 FPS will be
  7138. 4:48:31more than that of a 30 FPS video. But if
  7139. 4:48:35we talk about the video smoothness, also
  7140. 4:48:38the 60 FPS video will be more smoother
  7141. 4:48:41than a 30 FPS video. Now with this,
  7142. 4:48:44let's stick to 30 FPS thing. Let's say a
  7143. 4:48:47size of a normal video, let I'm like I
  7144. 4:48:51am recording a video in 30 FPS right
  7145. 4:48:53now.
  7146. 4:48:54My recording, let's say went for 1 hour
  7147. 4:48:57and for the 1 hour recording video, the
  7148. 4:48:59size of my recording is basically
  7149. 4:49:02around 2GB.
  7150. 4:49:04So if, let's say you are watching this
  7151. 4:49:06video and if you want to download the
  7152. 4:49:09entire video in the start before playing
  7153. 4:49:12and then you play,
  7154. 4:49:14what will happen? It will add latency to
  7155. 4:49:17our system because downloading 2GB will
  7156. 4:49:19take time for sure. Also, you have to
  7157. 4:49:22see buffer and let's say you downloaded
  7158. 4:49:25all the 2GBs of content into your system
  7159. 4:49:27just to see a particular section or
  7160. 4:49:29maybe you see the first 10 seconds of
  7161. 4:49:31the video and didn't like it. So, you
  7162. 4:49:33wasted your internet and storage. So,
  7163. 4:49:35this is not a good option to download
  7164. 4:49:372GB entirely first hand and then see a
  7165. 4:49:40video. It is completely waste of time.
  7166. 4:49:42So, we have to think about something
  7167. 4:49:44else. So, the thing is here we have to
  7168. 4:49:46use some network which is suitable for
  7169. 4:49:48our system. So, in order to process
  7170. 4:49:51information in video or images form, we
  7171. 4:49:53normally go for TCP. Because TCP
  7172. 4:49:56maintains a sequence of things. Why do
  7173. 4:49:59we need sequence? Let's see in a bit.
  7174. 4:50:01So, in TCP we have two protocols. One is
  7175. 4:50:05RTMP which is Real Time Messaging
  7176. 4:50:07Protocol and the other one is RTSP which
  7177. 4:50:09is Real Time Streaming Protocols. Both
  7178. 4:50:12of these protocols basically help us in
  7179. 4:50:14order to stream a particular video to
  7180. 4:50:16our device. Now, what this protocol does
  7181. 4:50:18is let's say we have a complete video
  7182. 4:50:20here. It is going to divide our video
  7183. 4:50:23into different chunks called as
  7184. 4:50:26segments. And our client machine or the
  7185. 4:50:28server from which we are watching is
  7186. 4:50:31going to pull these segments one by one.
  7187. 4:50:34Now, in message queues we have talked
  7188. 4:50:35about pull and push. This time I'm
  7189. 4:50:37saying pull. So, just connect the dots.
  7190. 4:50:40Keep connecting the dots while I'm
  7191. 4:50:41saying things, okay? So, this is going
  7192. 4:50:43to pull our segments one by one based on
  7193. 4:50:46the network. Now, we'll come to this
  7194. 4:50:48part in a bit. So, this will solve our
  7195. 4:50:50issues.
  7196. 4:50:51This will give us a low latency because
  7197. 4:50:54we don't have to download the entire
  7198. 4:50:55video. We can download some segments
  7199. 4:50:57also. With this we'll now be able to
  7200. 4:50:59stream in a much better way and it will
  7201. 4:51:02be more efficient than downloading the
  7202. 4:51:04entire video which was consuming our
  7203. 4:51:06resources and network bandwidth. Now,
  7204. 4:51:08this time we have to only download some
  7205. 4:51:10segments which is going to be more
  7206. 4:51:13efficient because this time we are
  7207. 4:51:15downloading in parts. So, suppose we are
  7208. 4:51:18watching one video, first 10 seconds is
  7209. 4:51:20over and now we don't want to watch the
  7210. 4:51:22entire video. So maybe some or the other
  7211. 4:51:25sections will be pre-downloaded for us
  7212. 4:51:27so that if you go ahead we don't see
  7213. 4:51:30buffer, but we don't have to download
  7214. 4:51:32the entire video which will save us a
  7215. 4:51:34lot of space and lot of network
  7216. 4:51:36bandwidth as well. Now with FPS we also
  7217. 4:51:39have one more factor which changed the
  7218. 4:51:41game entirely which is resolution. With
  7219. 4:51:45videos we have different resolutions,
  7220. 4:51:47right? We can use 4K, we can use HD, we
  7221. 4:51:50can use maybe 720p,
  7222. 4:51:53we can use 480 or 240 or even 144p. Even
  7223. 4:51:5964p was an option. I'm not sure whether
  7224. 4:52:02our video will be able to render in that
  7225. 4:52:04quality or not. I have like way before
  7226. 4:52:06when I was in school, maybe I saw that.
  7227. 4:52:09After that I didn't. So yeah, these are
  7228. 4:52:11all the resolutions on which we can see
  7229. 4:52:14our video. 4K will obviously have better
  7230. 4:52:17quality and 144p
  7231. 4:52:20is going to have bad quality. But why
  7232. 4:52:23are we talking about resolutions is even
  7233. 4:52:25if let's say we are streaming a 4K
  7234. 4:52:28video,
  7235. 4:52:29which I already said for 1 hour video we
  7236. 4:52:32need to store it in 2GB space. Suppose
  7237. 4:52:36we divide this into multiple segments
  7238. 4:52:38for each second, then 1 second will be
  7239. 4:52:410.57
  7240. 4:52:42MB. Now this looks small but is a huge
  7241. 4:52:45size if we talk about network. What if
  7242. 4:52:47we have a poor network? What if we are
  7243. 4:52:50traveling? So our network is keep on
  7244. 4:52:52fluctuating on a different level. Also
  7245. 4:52:54if we sit by working on our office or on
  7246. 4:52:56system, our networks keep on fluctuating
  7247. 4:52:59as well. So that means we are not going
  7248. 4:53:02to have a common bandwidth throughout
  7249. 4:53:04our video viewing experience. So our
  7250. 4:53:06network will keep on increasing and
  7251. 4:53:08decreasing. So we have to take care of
  7252. 4:53:10that also. Let's say I record videos in
  7253. 4:53:13HD only and if I start recording in 4K,
  7254. 4:53:17that means the size of 2GB can become
  7255. 4:53:21big, right? The 2GB can go to 4GB. Maybe
  7256. 4:53:24I use multi-cam setups. At that time,
  7257. 4:53:27the 4GB can also go up to 8GB, maybe.
  7258. 4:53:30Also, in my videos, I'm not doing much
  7259. 4:53:32activity. The frames are pretty still.
  7260. 4:53:35Only my hand movements can be shown once
  7261. 4:53:37in a while. I'm sitting still. I'm just
  7262. 4:53:39facing the camera or the screen. I'm not
  7263. 4:53:41doing much activities. Also, the screen
  7264. 4:53:43you are seeing is black on which I'm
  7265. 4:53:45writing in white. So, there are no
  7266. 4:53:47colors, also. So, this is pretty easy
  7267. 4:53:50video to see. But, if I start recording
  7268. 4:53:53outside or maybe I start recording any
  7269. 4:53:55action sequence, so in that case, the
  7270. 4:53:57size can also get to 16GB for the same
  7271. 4:54:014K video of 1 hour. So, this size can
  7272. 4:54:04also be increased to four times. So,
  7273. 4:54:06that will be around 4MB. So, the issue
  7274. 4:54:09here is for even 1-second video, we are
  7275. 4:54:12using 4MB. And if our network is not
  7276. 4:54:15that good, we are still going to see
  7277. 4:54:17buffer or face loading issues, which is
  7278. 4:54:20going to basically ruin the experience
  7279. 4:54:22of our viewer, and they will leave the
  7280. 4:54:24application. This is not what we want
  7281. 4:54:27entirely, right? So, we have to do
  7282. 4:54:28something about it. Also, if you think
  7283. 4:54:30about it, there are different screens on
  7284. 4:54:32which you work on. We maybe watching
  7285. 4:54:34something on TV. We maybe using our
  7286. 4:54:36laptop screen. Uh also, we can use our
  7287. 4:54:39mobile screen, maybe our watch as well.
  7288. 4:54:42Maybe we are seeing it on iPad. We don't
  7289. 4:54:44want 4K qualities in all of them, right?
  7290. 4:54:47Let's say we are watching on watch, we
  7291. 4:54:49don't want 4K.
  7292. 4:54:51Maybe on watch, 480 works fine. And
  7293. 4:54:54also, on iPad, also, I don't think 4K is
  7294. 4:54:57required. Maybe we can show them in SD,
  7295. 4:54:59and it will be enough. On TV, we
  7296. 4:55:01basically require 4K. Like, we don't
  7297. 4:55:03have to compromise here. But, on laptop,
  7298. 4:55:06also, we can switch between HD and 4K.
  7299. 4:55:09We can choose. We can pick based on the
  7300. 4:55:11requirement or based on the
  7301. 4:55:12configuration, based on the network
  7302. 4:55:14also. On mobile also, we can work pretty
  7303. 4:55:17well with SD. We don't need 4K here as
  7304. 4:55:19well. So, that means not only network,
  7305. 4:55:22but based on our screen size, we can
  7306. 4:55:24change the resolution of our video. So,
  7307. 4:55:26why are we changing?
  7308. 4:55:28Because it is going to reduce the size
  7309. 4:55:30of our segment. And the lesser the size,
  7310. 4:55:32the faster it will be to show. So, in
  7311. 4:55:34order to maintain streaming, we have one
  7312. 4:55:37mechanisms, which is adaptive bitrate
  7313. 4:55:39streaming. What it basically conveys is,
  7314. 4:55:42let's say you start watching a video on
  7315. 4:55:44300 Mbps. And your networks keep on
  7316. 4:55:47fluctuating. Maybe it went down till 10
  7317. 4:55:50Mbps. So, when the network fluctuates,
  7318. 4:55:52we can't download with the same
  7319. 4:55:54consistency. And if we are not able to
  7320. 4:55:56download with the same consistency,
  7321. 4:55:58there's high chance of seeing the buffer
  7322. 4:56:01icon. And we don't want that.
  7323. 4:56:03So, in order to achieve this, what can
  7324. 4:56:05we do? We have a source video
  7325. 4:56:08of 4K, right?
  7326. 4:56:10We have to divide this video into
  7327. 4:56:12different segments, that we know. Also,
  7328. 4:56:15we have to divide those segments into
  7329. 4:56:17different qualities. So, for example,
  7330. 4:56:20this 4K video is divided into 10
  7331. 4:56:22segments of 4K quality. But now, also we
  7332. 4:56:25have to divide these segments into more
  7333. 4:56:27configurations, the lower
  7334. 4:56:29configurations. Now, our basic
  7335. 4:56:31resolutions and segments are ready. Now,
  7336. 4:56:34let's see in real time how our function
  7337. 4:56:37really works. So, let's say here we have
  7338. 4:56:39our client who is watching something on
  7339. 4:56:42our desktop screen. In starting, the
  7340. 4:56:44network connection was 300 Mbps. So, we
  7341. 4:56:48start showing them the 4K video. So, it
  7342. 4:56:51sees segment one, maybe segment two also
  7343. 4:56:53he watches. And then, the network drops
  7344. 4:56:56to 10 Mbps. So, at this point, maybe
  7345. 4:56:59this is already loaded into their system
  7346. 4:57:02in the form of cache. So, they can still
  7347. 4:57:04see this section into 4K. But after
  7348. 4:57:06that, we need to load the data again.
  7349. 4:57:09And the network is now at 10 Mbps. So,
  7350. 4:57:12we can't load the 4K data. What we will
  7351. 4:57:14do, we will load the lower configuration
  7352. 4:57:17data. Maybe according to this we will
  7353. 4:57:19pick 480p.
  7354. 4:57:22So, we will start downloading this data.
  7355. 4:57:24And once the network restores to maybe
  7356. 4:57:26300 or 150 Mbps, we will start switching
  7357. 4:57:30our configurations accordingly. So, we
  7358. 4:57:32can go to 1080p as well. And if it then
  7359. 4:57:37drops to 50 Mbps, we can still give them
  7360. 4:57:40720p. And at the end, if network
  7361. 4:57:43basically restores to 300 Mbps,
  7362. 4:57:47we can show them the last segment in 4K
  7363. 4:57:49again. So, this is how an YouTube or any
  7364. 4:57:52other stream basically works.
  7365. 4:57:55They will keep switching our quality
  7366. 4:57:57based on our network bandwidth because
  7367. 4:57:59we are mostly seeing that entire video
  7368. 4:58:01on a single screen.
  7369. 4:58:03And based on our network, they will keep
  7370. 4:58:05switching the qualities. And we see the
  7371. 4:58:07entire video into one go
  7372. 4:58:10without seeing the much difference there
  7373. 4:58:12because normally the network doesn't
  7374. 4:58:14drop so drastically. We just keep these
  7375. 4:58:16segments in order to show them into
  7376. 4:58:18different devices. If you pay attention
  7377. 4:58:21in most of the streaming applications
  7378. 4:58:23where you are seeing the video, in the
  7379. 4:58:25side corner, you also see a segment
  7380. 4:58:27where you can switch your configurations
  7381. 4:58:29again. You can choose between auto,
  7382. 4:58:33maybe HD, or maybe they will directly
  7383. 4:58:36use the configuration that is 720p or
  7384. 4:58:39480p,
  7385. 4:58:40something like this.
  7386. 4:58:42So, you can choose between them. And if
  7387. 4:58:44you choose auto,
  7388. 4:58:46they are going to perform this
  7389. 4:58:47orchestration or this game while you
  7390. 4:58:49watch the entire video. So, what did we
  7391. 4:58:52see here? If the network throughput,
  7392. 4:58:55that means our network speed, is greater
  7393. 4:58:57than the bitrate of the downloaded
  7394. 4:59:00segment, that means we have downloaded
  7395. 4:59:02in a lower quality and now network is
  7396. 4:59:05greater than that. So we can now switch
  7397. 4:59:09to a better quality. Similarly, if the
  7398. 4:59:12network throughput is less than
  7399. 4:59:15the bitrate of
  7400. 4:59:17downloaded segment, so in this case what
  7401. 4:59:21we will do, we will switch down the
  7402. 4:59:23quality of our video. So after getting
  7403. 4:59:26all this knowledge, after discussing all
  7404. 4:59:28these things with your interviewer or
  7405. 4:59:30with your team,
  7406. 4:59:31then you start deciding what components
  7407. 4:59:33do you use in your system design. Now
  7408. 4:59:36the path is very clear, right? We need
  7409. 4:59:38to store the current data or the actual
  7410. 4:59:41data somewhere. Maybe this is our CDN
  7411. 4:59:44where the actual data or the actual
  7412. 4:59:46video is uploaded and it is uploaded
  7413. 4:59:48from where? From any server. Now from
  7414. 4:59:51here we need to create the segments, so
  7415. 4:59:53that means we need to give this data to
  7416. 4:59:55a service which can basically transform
  7417. 4:59:58our video. So we can name that we can
  7418. 5:00:00say it is a transformation service. Now
  7419. 5:00:03after this we have to send the segments
  7420. 5:00:06to our queue. We can use priority queue
  7421. 5:00:08here, right? So we can create a queue.
  7422. 5:00:11And why we are using priority queue? You
  7423. 5:00:13need to answer yourself. Now
  7424. 5:00:15from this message queue, our system is
  7425. 5:00:18going to use the services of downgrading
  7426. 5:00:21each and every quality. So we use worker
  7427. 5:00:24term here. Worker is basically used in
  7428. 5:00:26order to denote another service. So we
  7429. 5:00:29may be using worker one for downgrading
  7430. 5:00:32it to HD. Also we can use worker two for
  7431. 5:00:35maybe downgrading it to 720p and so
  7432. 5:00:39forth. So after working with these
  7433. 5:00:41resolutions, again we need a message
  7434. 5:00:43queue to store the results into segments
  7435. 5:00:46and again we need a priority queue here.
  7436. 5:00:48Okay, with this we will send this
  7437. 5:00:50request to a distributed CDN kind of
  7438. 5:00:52services from where it can go into
  7439. 5:00:55different part of our servers. So, if
  7440. 5:00:58someone is watching from India, they can
  7441. 5:01:00access the data from this server and
  7442. 5:01:03they can watch it from US server and the
  7443. 5:01:05other servers are for other regions.
  7444. 5:01:07Now, by doing this, we have completed
  7445. 5:01:10the entire system design of our video
  7446. 5:01:12streaming application. And from this
  7447. 5:01:14server,
  7448. 5:01:15eventually, we are going to access it on
  7449. 5:01:17our client machine on which our user is
  7450. 5:01:20going to watch their video. So, now
  7451. 5:01:22after creating this diagram, let's also
  7452. 5:01:24do some maths.
  7453. 5:01:26Let's say our source video is of 50 GB
  7454. 5:01:30and it is of 20 minutes. So, it is a
  7455. 5:01:33high-quality video, maybe 4K, and it is
  7456. 5:01:35having 60 FPS. So, that's why the size
  7457. 5:01:38is too big. So, this is we are taking as
  7458. 5:01:40an average size of the video which we
  7459. 5:01:42are going to stream. Maybe we are
  7460. 5:01:44streaming an entire match. So, this is
  7461. 5:01:46the size which we are going for our
  7462. 5:01:4720-minute video. Now, let's decide how
  7463. 5:01:50many segments can we divide it to. So,
  7464. 5:01:52I'm doing an easy maths. So, 20 minutes
  7465. 5:01:55multiplied by
  7466. 5:01:5760, that will convert it into seconds.
  7467. 5:02:00So, it will be 1 2 0 0 seconds. And
  7468. 5:02:04also, I am making the segments also
  7469. 5:02:06equal, so that means 1 2 0 0
  7470. 5:02:09segments as well. Also, with this, let's
  7471. 5:02:11talk about different resolutions. So,
  7472. 5:02:13let's say 4K is of 50 GB, which will be
  7473. 5:02:17divided by our segment. So, here the
  7474. 5:02:19size of our segment is too large, right?
  7475. 5:02:21Because it is a 4K video. Maybe we can
  7476. 5:02:24divide it into more segments. So, but
  7477. 5:02:27for this example, let's stick to it.
  7478. 5:02:29Let's say our 1080 pixel video,
  7479. 5:02:32let it be for 20 GB and again, we are
  7480. 5:02:35going to divide it by 1200. It is going
  7481. 5:02:38to be again 16.6 MB.
  7482. 5:02:41With this, let me write all the
  7483. 5:02:42resolutions.
  7484. 5:02:43Now, this is what the calculation looks
  7485. 5:02:45like if we divide it for each segment
  7486. 5:02:47for each resolution. For 720, it will be
  7487. 5:02:508.3 MB. For 480, it will be 4.17. And
  7488. 5:02:54for 250, it will be 2.08 MB. That means
  7489. 5:02:57if we combine them all, it is going to
  7490. 5:03:00be 72.65
  7491. 5:03:02MB, approximately. So, that means the
  7492. 5:03:05size of one segment of our video is
  7493. 5:03:08going to be 72 MB, which is too huge, I
  7494. 5:03:11feel. So, maybe we can divide it into
  7495. 5:03:13further segments. But for now, this is
  7496. 5:03:15going to be the size of our one segment.
  7497. 5:03:18And accordingly, we need to load the
  7498. 5:03:20data into our stream.
  7499. 5:03:22Also, while calculating the size, we
  7500. 5:03:24should also take care of our user. That
  7501. 5:03:26means we should know how many users are
  7502. 5:03:29going to see the stream. So, if the user
  7503. 5:03:31count is, let's say, 100, we can use the
  7504. 5:03:34same server or a single server only,
  7505. 5:03:36right? For 100 number. If we count this,
  7506. 5:03:39it will comes down to 87.5 GB.
  7507. 5:03:42So, we can store 87.5
  7508. 5:03:44GB
  7509. 5:03:46into a single server, and from which
  7510. 5:03:48these 100 users can view our streaming
  7511. 5:03:51easily. What if the user changes from
  7512. 5:03:54100 users to 100,000 users? Now, this
  7513. 5:03:58will change our picture. Now, this is
  7514. 5:04:00where we will know that our one server
  7515. 5:04:03is maybe not enough to cater so much
  7516. 5:04:05requests. So, we will divide our servers
  7517. 5:04:08into two, maybe. And based on the number
  7518. 5:04:11of users, we have to keep increasing the
  7519. 5:04:13count of servers. Maybe we'll use
  7520. 5:04:15message queues. Maybe we'll add some
  7521. 5:04:17caching algorithms. And all of this will
  7522. 5:04:19be managed through our diagram. Here, I
  7523. 5:04:22have not used any caching. But we can
  7524. 5:04:25use the caching on CDN level.
  7525. 5:04:28Or we can use the cache on our browser
  7526. 5:04:30level,
  7527. 5:04:31which is going to hold the segments of
  7528. 5:04:33the coming part. So, this is what the
  7529. 5:04:35design look like. This is what the
  7530. 5:04:37calculation is. And this is what is the
  7531. 5:04:39logic behind the system design of my
  7532. 5:04:42system, which will stream our video to a
  7533. 5:04:4610,000 or a 100,000 user. Now, this
  7534. 5:04:49diagram can be different in your case
  7535. 5:04:52also. It is very possible that you will
  7536. 5:04:54design a system better than me, and it
  7537. 5:04:57is also possible that you will see some
  7538. 5:04:59other YouTube video who have designed
  7539. 5:05:01the video better than me or maybe worse
  7540. 5:05:03than me. So, these designs are basically
  7541. 5:05:06your practice.
  7542. 5:05:08The more you practice, the more you see
  7543. 5:05:10other designs, the more you read
  7544. 5:05:12different papers, your designs are going
  7545. 5:05:14to improve. And more your design
  7546. 5:05:16improves, the quality of your system or
  7547. 5:05:18your workflow increases with time. So,
  7548. 5:05:21with this, I end the discussion of
  7549. 5:05:23system design.
  7550. 5:05:24And I really hope you all enjoyed this
  7551. 5:05:26series so far. This was a long video,
  7552. 5:05:29and we covered a lot of topic. I hope
  7553. 5:05:31you all enjoyed it. Now, you can like
  7554. 5:05:34this video or comment the most
  7555. 5:05:35interesting section you found out in
  7556. 5:05:37this video. Also, you can comment out
  7557. 5:05:40any topic which you want to see in
  7558. 5:05:42future on Telescop. With this, my name
  7559. 5:05:45is Akshat Agarwal, signing off. Thank
  7560. 5:05:47you.

About this transcript

This page contains the full transcript of System Design One Shot Full Course by Telusko, generated from the public captions YouTube serves with the video. The transcript has 52,361 words across 7,560 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.