Sachin, sorry Sachin, I do not think I have access to the attendance, but anyhow let me just check and also for the PPTs, sorry guys I did not get time last week, so anyhow after the class today after my dinner I will definitely upload. I am just sharing my screen, can one of you confirm that you are able to see my screen. Okay, cool, thank you very much. Hey, cool, still I see 155, let us give one more minute. I think 155 is fine, we can get started. Okay, let us get started. So, the whole course we were trying to understand how the hardware works, how the hardware interacts with the software, how our application performs, what are the basis for our application performance. We did understand the breakup of the architecture, how the instructions have been sent to the processor, how processor processes them, the role of other components such as RAM storage, the network devices. And we have also seen in detail how system processes are getting created, how we are able to identify the processes, and how can we identify which processes consuming more resources. So, all these things we have understood. So, it is time for us to understand the memory. So, today we are going to look beneath the software layer. We will see that processor's performance depends not only on the instructions it executes, but also on how quickly it can obtain the data data those instructions need. Now, have you ever seen two implementations that produce the same result, but run at noticeably different speeds? It could be related to database, it could be related to databases, analytics, image processing, or application development. So, basically, this is a performance oriented architecture session, not simply list of memory definitions. It is definitely not that. Okay, cool. Now, we will move in four steps. First, why computers need a hierarchy. Second, how cache and locality work. Third, how cache decides where data goes and what gets replaced. Finally, we will connect these ideas to the way a matrix is reversed into the memory. So, basically, today we will see, we will start with the conceptual session. We will continue with that tomorrow and then we will also get into the practical session demonstrations. So, basically, the CPU memory performance gap. That is one motivation for us to understand better. Then, how cache hits misses or cache lines and locality. The third is basically the mechanisms, mapping replacement and write policies. So, one question that we need to ask ourselves throughout. What data will the processor need next? And where is that data likely to be? If we start linking the processors to the data requirement, then definitely we will be able to start understanding the importance of the memory. Now, we should be able to explain the performance difference after this session. Not just memorize that one loop is faster and another loop is slower. Not like that. Cool. Now, by the end of this session, between today and tomorrow, we should be able to explain the hierarchy. Describe what a cache hit and miss mean. Distinguish temporal from spatial locality. And the reason about how a program's access interacts with caching. That is our objective between today and tomorrow. So, now, if we expect this, then which outcome is least familiar for you? Is it locality? Or is it cache mapping? Or write policies? Or row major layout? Or row major layout? Obviously, All of you should have one or two, at least one or two least familiar concepts, which we will get a better understanding by end of the session. So, now the question comes, is a fast CPU enough? The assumption that processor speed alone determines the program speed. Is that good enough? Imagine a processor capable of executing billions of operations per second. Does that guarantee? Does that guarantee that every application will be fast? We have got one of the best processor. But is it going to guarantee that every application that we are going to run will be running fast? First, what if the processor needs data that has not arrived yet? Now, I'll just give some 10-20 seconds. Just think about it. Especially, what if the processor needs data that has not arrived yet? Instructions we know. It will go to the processor. But how about data? Just 10 seconds, please think about it. Okay. So, any response? Anything? Please give me in the chat. I see no from Gyan and Ateesh. Not sure what is that for. Okay. Understood. No means, not only the processor is enough. But what else it requires? Okay. Where will the data come from? Okay. I do see two responses. One says memory, another says hard disk. Okay. CPU waste time for data is a wastage leads to slow processing. I understand. Processor has to wait for the data. Without data, it will not be able to run. I understand. Most used data kept in cache to speed up. Okay. Perfect. Do that. What do you mean by other factors also matter? What other factors? A production server runs several processes. One process is eligible to execute but is waiting for CPU time. Another process is waiting for data to arrive from a disk operation. Oh, great. Now, what is the difference between RAM and main memory? So, hardware, software optimization. That is interesting. Okay. See, basically, we need to think, how is it going to pair? What can make the CPU wait? The CPU data will make it wait? RAM is a temporary. RAM is main and RAM is temporary memory. Both are correct. Perfect. Cool. For example, RAM is memory. It is temporary, but the disk is persistence. If processor is starved for data, then it enters still state. Still state means it doesn't know what to do next and it hangs. That hanging of processing is definitely not a desired state. Oops. Satya. I feel data can come from any source. Then that will be moved to the flow. In the previous classes like IO buses, it will go through the CPU and processing happens. Thank you very much for linking. Okay. Thank you very much for linking. This is one of the responses that I was expecting and thank you very much for that response Satya. Great. Now, here the key thing is the processor can be extremely fast, but it cannot operate on data that has not arrived. That is the key. Having just a fast CPU is not enough unless you supply the data that is required. For example, if you want to determine if the value is against a certain comparator, then that comparator needs to come from the memory. If it is not in the memory, it has to be read from the hard disk. When you say hard disk, it is the storage. The storage can be local. The storage can be remote. If it is remote, it can be within the network. It could be outside the network. If it is outside the network, it could be reachable over virtual private network or it could be reachable over public internet. If it is reachable over public internet, wherever it is available should be reachable. So, so many things, so many things comes into the play. So, this could lead to a CPU memory performance gap. The processor execution capability and memory access latency have not improved at the same pace. One is processor, second is main memory. Let us speak about DRAM or SDRAM. So, a CPU can have many execution resources, but those resources need instructions, not only instructions, but also data. If an instruction depends on a value that is still being fetched from the memory, the processor may have to wait. The bottleneck is then data moment, not arithmetic. The calculation still can be performed at a faster pace because we do have CPU available for the application or the calculation, but the data moment is a bottleneck. Now, when you say memory, memory, we always call it as memory wall. What is a memory wall? The performance gap between processor capability and memory access. That is the memory wall. So, two different resources. One is memory. Second is processor. And in case if memory is deteriorating the processing capability of the CPU, then we call it as memory wall. If it is constrained by the time needed to move data, it could be nanoseconds. It could be nanoseconds. It could be microseconds. It could be milliseconds as well. So, this is one reason. This is one reason that we try to keep the data in a network that is accessibly fast enough. So that it could be a database. It could be a flat file. It could be an image. It could be a video. If it is an image processing software, then what is going to be retrieved is an image. It could be from an S3 bucket. So, the application where we are running could be Azure environment. The S3 bucket, basically the storage we are using from AWS. And it is pretty much possible that we wanted to use the database from the GCP. Three different environments, multi-cloud. Now, unless we ensure that the database access and the image access from the S3 bucket is fast enough, then the virtual machines that even have fastest CPU and also more number of CPU cores is not going to help. So, if you remember in our last session, for the same program, we saw two different performance, for the same program. At the point of time, I said like the number of CPUs being same, why was the performance was slower? We did observe that. Same program run in two different instances, has two different performance measures. So, that was within the CPU. Number of cores, type of CPU, if that itself, there is a performance gap, then in addition to memory, the new resource, will we be able to experience it as an end user? May or may not be. There is no straight answer to it. As long as it is in nanoseconds and milliseconds, as an end user, I will not be able to experience it. But I will assume that it takes a couple of seconds. A couple of seconds is too much of a time for me to wait as an end user. So, I will definitely observe the difference. So, can a program be limited by data moment rather than arithmetic? We have the answer now. The answer is memory wall. It is pretty much possible. Now, if we are going to observe this and if we understand this, how are we going to handle it? Now, if a program performs very little arithmetic, let us assume our application is performing a very little arithmetic, but repeatedly fetches data from far away memory locations. What might limit its speed? One definitely could be the latency, the memory latency. Second, it could be the network bandwidth also. It is not only about the memory. Now, we have gotten another entity. See, the memory is accessible faster. But the bandwidth is slow. What can we do? So, do we increase the bandwidth and then check if memory access is faster? And then finally observe, is the processor processing as expected? And what is the difference between or what is the distinction between latency and bandwidth? Latency is measured in time and bandwidth is measured by the amount of data transferred per unit time. It could be like 2mbps, that means 2mbps, or it could be 2gbps, or it could be 2pbps. That is how the bandwidth is measured. How the latency is measured? Latency is measured against time. How much time you took to access? So, now the bandwidth could be the reason that you are taking more time to access the memory from the external system. You might have good bandwidth, but still your memory accessibility is slower. You are able to observe it. How? Because basically I was going through the logs. It says that it is taking to access it by 2ms and 3ms, which is not acceptable. If there are like 15 processors to be processed by the CPU, and all the 15 processors related to the same application, and if the application is waiting for a response from the completion of these 15 processors, then the user is waiting. So, you say like 2ms per process, then for 15 processors, it is 30ms. 30ms. User will be able to experience it. 30ms, 1 second. That is experienceable. Notice the expectation from the end user is, even if you see in most of the portals, for example, to obtain the OTP. You will be sent OTP, both to your mobile and email as well. But still, as a user, if you are going to experience, as a user, if you are going to experience like 3 to 5 seconds of delay, then are you going to wait? Though the portal is going to tell, this OTP is valid for 10 minutes. That means even if it arrives at the 9th minute, it is still valid, you can supply it, and it can be authenticated. That is pretty much possible. But as an end user, are we simply going to wait for 9 minutes? Do we have so much of patience? No, definitely not. Just give me a minute, please. Hi, guys. Just can one of you confirm. Am I audible? Am I clear? I am able to see the activity in my mic. I just wanted a confirmation from your end as well. Okay, great. Thank you so much. Now, if a program performs very little arithmetic, but repeatedly fetches data, OTP is a great example. The program just generates probably four digits, six digits, and gives to the API that is going to send it to your email and SMS. And after that, immediately it will display the next page for you to supply the OTP for authentication. All these things will happen within not even one second. Now, immediately the screen that comes up asking for the OTP, what you are going to do is immediately you are going to check your mobile or email. Why I have not received yet? So, if you look at these two processes, initiating the OTP is just a small logic which it will initiate and give it to the API. But the other end, the screen comes up immediately for accepting the OTP. But this OTP has to travel through network. It has to travel outside of the network where the application is running. It has to go to the gateway where the interface is accepting the OTP to be transmitted to two different networks. One is your mobile network. Second is your computer network. And from there, it has to find out which is your mail server. Put the mail into that mail server and the mail server needs to accept the mail. Put it in your inbox. The moment it arrives into the server inbox, it has to come into your Outlook in case if you are using Outlook. Or you need to refresh the Gmail page so that you will get it. On the other hand, the message also needs to go to the mobile network and the mobile network has to send it to your number. Your number should receive it and then it will display. Your number should receive it. There are two processors that are running in parallel. But processor is displaying the pitch immediately. So, now you should be in a position to understand why it is taking like two seconds at least for us to receive the OTP. And why we don't have patience. As an end user, we will not have. XYZ bank is sending the OTPs so fast compared to ABC bank, we will start doing that comparison immediately. And today having multiple bank accounts with multiple banks is a normal thing. So, the user experience is going to be compared immediately. Some of the bank has pressure. Sometimes the bank has a provider. It has a provider. So, now X, Y, Z bank might be having the mobile network gateway connected to Airtel. Whichever connection you have in your mobile, they might have an API gateway with Airtel. Another bank might have the gateway with Jio. and you may have probably why so now it has to again go through two different networks all this process needs to happen so now at one end of the process CPU has completed its job now the data is supposed to be supplied so now what will happen to the application application will move from the CPU that particular set of instructions is completed so it will the next set of instructions it will wait for and all the instructions will be in the queue same application some other user might be looking at the bank balances some might be transacting money some would be like changing the password some might be looking at the statements past three months statement somebody wants to download so so many processes that comes at the same time but every process has a different requirement for the data now if you are downloading the statement for the last three months basically the application needs to get your authentication go back select the data from the database for three months import it create it as a PDF and then give it to your interface it just when you are seeing that please click here to download your PDF the bank's website that is just an instruction set processed by the CPU to display the information on the screen that's all moment you click it is again going to go back to the storage retrieve the PDF and then it will start downloading to your laptop this has its own cycle of data requirement now changing the password it actually does not require any data to be travelled and depends on the logic in which the application has been built for the password change generally one cycle of authentication is what this is one additional cycle of authentication required for the password change so that will happen very fast that is the reason in most of the banks website today if you observe if you are trying to download your statement for the last three months or six months some of the banks allow you to download the statement for five years and seven years that has been given a lowest rarity because it has to coordinate with the storage get the data create the PDF store the PDF and display the link from where the PDF can be downloaded so that's the reason some of the banks have started displaying once it is ready we will inform you then you can come back and download earlier basically if you are the user and if you are accessing the PDF file to be downloaded for last three months the processor will change and it will the arrow mark will change in your laptop and it will be rounding roaming clock whatever it is so we used to wait until all these processes are completed and the link coming up on the web page but nowadays they are very clear it may take like three seconds it may take even 30 seconds it may take even one hour they simply say that once it is ready we will inform you and this statement will be available for you for the next 24 hours or 48 hours to download so they have also changed the reason there is a mismatch between the CPU performance and the memory performance and the data availability for the application for that particular feature that is not the case of OTP authentication that is not the case to look for a transaction because it is getting displayed on the screen only but for download they have changed their logic so can a program be limited by data moment the answer is it can be limited by data moment okay especially in case of fetching the data for three months last three months maximum what it will it will ask for from date and to date if it is arbitrary dates that we wanted to supply or last three months one option may be there if you select that also a little data a little instruction to be processed now we have seen like how the data movement is limiting the program performance because of two different resources one is processor and main memory now let us look at the hierarchy of the memory we have understood in detail the processor now let us look at the hierarchy of the memory if you remember we have seen this diagram once before when we got introduced to the memory hierarchy initially in the introduction session see at the top what we are seeing is the registers though it is within the CPU it is also a storage it is also a memory which hold values used directly by instructions whatever comes out of the instructions is stored in the registers now below them are L1 L2 and L3 cache this also we saw in detail this is also part of the CPU how it access how fast it is L1 cache is very fast because it is easily accessible by the CPU and it is next to the CPU now the fast actually the speed actually reduces as we move downwards in this hierarchy now we have main memory it is a dynamic read random access memory it is a read only memory now this DRAM is the first layer of memory outside of the CPU even this memory also is going to reside in the same motherboard CPU also is in the same motherboard within CPU we have registers L1 L2 and L3 next to the CPU we will have the RAM residing in the same motherboard now beyond DRAM beyond it could be SD RAM also that is synchronized dynamically random access memory nowadays SD RAM is what being used in the systems that is faster than DRAM is faster than RAM so in today's world we started using SD RAM but it is volatile means you cannot depend on the data availability that is stored in the SD RAM so beyond RAM is the persistency that comes in registers L1 L2 L3 and local main memory all of them do not have the quality persistence the persistence comes only with the local hard disk now RAM also it has its own slot within the motherboard similarly hard disk also is connected to the motherboard so until here it is all within the motherboard these four is within the CPU registers L1 L2 L3 cache registers these up to local hard disk everything is in the same motherboard so obviously the access speed will be comparatively more in case if you are accessing it from the S3 bucket obviously is a remote storage because it is in the AWS network assuming that you are running an application in Azure and your storage bucket is in AWS then for the application the S3 is remote yeah this is where exactly the bandwidth ability also comes into the picture the moment your storage is remote now capacity tends to increase downward what I mean is that how much is the capacity of registers how much data they will be able to store in L1 to L3 cache how much data that can be stored in SDRAM and how much data it can be stored in hard disk it is basically the capacity is increasing but latency also increases parallel both of them is in the increasing trend from top to bottom definitely if it is a remote storage in remote storage there is no limitation for S3 what it only thing is your cost is going to increase but your latency is also going to increase and capacity also increases so everything increases from top to bottom ability to save the amount of data to save how fast you can access the data and the persistence so let me just hold on here let me ask you a query suppose if you are going to store 25,000 images that is the rate in which you are receiving you receive 25,000 images in an hour from different sources from your customers network from your end customers upload sources can be different but you are receiving 25,000 images in an hour and these images are to be processed and it will be processed by an application written by us only and it will meet the expectations of the customer along with image upload or along with image transfer what is expected for the image transition is also going to be available so that means data itself the expectation that is the instruction set the size all the three matters now the application needs to restore or retrieve the data from the remote storage as revoked let us say that one image on an average it takes one millisecond to process will you be able to process all the incoming 25,000 messages in one hour itself so all you have to do is how many milliseconds per hour and 25,000 milliseconds if it is more than one hour or less than one hour that's all you need to just take like 10 20 seconds question is very simple you are receiving 25,000 images in an hour on an average and on an average every image requires one millisecond to process even if your image takes yeah that's sure Amit even if your image takes two milliseconds to process still you will be able to complete the entire processing of the 25,000 images because it should not take more than a minute but I'm telling like one hour so that means suddenly you observe that your system has gone slow this is what is the performance and it used to perform whatever you receive in one hour can be performed in 30 seconds that's the capability you have but suddenly you see that the image processing time has increased to 30 seconds per image if all the images comes together also we will not be bad because it is you still have one hour satya 25 thousand images are coming in the same second but still you have one hour to deliver SLA so yeah if you are calculating from the performance perspective definitely it is bad and nobody would have sized the system to receive together even together also they would have said probably 250 images they would have so that's where exactly this 30 seconds is coming now every image is taking 30 seconds to process and it is a streaming it is coming from different sources not coming together but you are receiving images and overall around 25,000 images you have received in the last one hour if it takes per image 30 seconds will you be able to complete all the images within that hour itself yes or no earlier it was so what will be your concern if you are not able to process then what will be your concern your concern would be like why is it taking 30 seconds now what are all the reasons possibly it could be what do you think what will you look at first missing image and taking a long time in processing no we won't miss images the application is written in such a manner that we don't lose images that is stored in the S3 getting from S3 to in memory takes some time that is one possibility I agree Satendra what other possibilities storage block processing block code taking long time transfer of data okay storage block if you are using S3 the probability is very less but still it is possible probability is very very less storage block because S3 is unlimited and they do have their own way of handling their storage if storage is the blocking then definitely we can go back to AWS second is processing block what could block the process you know processing is going to happen with CPU and the instruction set is going to come from application and the data is going to come from S3 at least I believe we are on the same page until here code not being parallelized to account for set of images let us assume that sequentially only we are processing still the 30 seconds is too much for us to handle 25,000 images let us assume we are not doing parallel we are doing sequential image by image only if it performs at the normal performance it should happen in 3 milliseconds now it takes 30 seconds so one one is definitely processing block code taking a long time is somehow related to processing only because the instruction is going to come from application and generally we will have the application running in the same environment though we may have data coming from the remote we will not have CPU on a different virtual machine and application running in a different virtual machine generally that's not the best practice so the instruction set should come fast so obviously the third possibility is data now where is the role of RAM because the data has to be loaded to the RAM and it has to be retrieved from the RAM it's not going to be read directly from the application is not going to read it directly and use it directly from the storage the intermediate step is memory so now there are two possibilities one is memory and it is running high server overload and throttling Neeraj I believe is related to utilization now what is utilizing if RAM is high utilized then what is it utilizing then we need to go back to the process loop which process is consuming more memory then that would be related to image processing is it related to image processing or is it related to data recognition so step by step we will go so now there are three entities or three resources that are involved one is your processor second is your memory third is your storage that is perfect statement third is storage but now is network playing the role of choke up that may not be the case because we are assuming memory utilization is high so it could be the instruction set could be we might have released the application yesterday new application or the upgraded application maybe application is not behaving as expected so now four things that stands in front of us and we need to rule out one by one we can't rule out more than one at a time that is the best practice so rule out the fastest that is basically we will start it from CPU then we will go to memory and anyhow we see memory is utilizing high then we will see which process that process is related to which module of the application so basically it could be loading the image from remote possible so if it is loading the image from it's taking for example 20 seconds then still 21st second it should have completed why it has taken another 10 more seconds so this is how we will start looking at the performance and in my experience most of the time almost like 70% of the time it is the instruction set that leads to the memory utilization if the instruction set is the problem then CPU also will be in the threshold it will lead to memory threshold because CPU will not consume the data from the memory and it will start piling it up now the processor is going to continue all the process which is getting instruction set means whatever the application is going to tell what to do to the CPU so it gets broken down we saw that in detail it gets broken down creates system calls and then given as a ISA to the CPU and then based on the ISA it will move it will move up the ladder it will wait for the processor to be available and then in the sequence it will happen processor is so fast it will not keep much instructions in the queue unless the instruction itself has some issue maybe we are referring to the wrong volume let us assume that we are referring to the wrong volume then your number of images will get piled up because images will not get processed so your storage will have stress memory also will have stress CPU also will have stress if it is bandwidth related then storage will not have stress but RAM and memory and processor will have the stress if code is the issue still memory also will receive the stress not the network and storage and then we will go ahead and look at the code you modify upload probably it is a small configuration wherein we are allocating less RAM for the application we were supposed to allocate 100 MP RAM but we are doing only 1 MP RAM because 0s were missing type of error in the configuration file so once you update then so now still why is it showing CPU stressing the core in which the application is getting processed is stressed not the overall CPU but the overall memory will get stressed but the next thing is the persistence suppose if you restart the server suddenly somebody restarted the server it is by mistake not intentional you restart the server then obviously whatever was there in the RAM that is your memory main memory cache unregisters everything is getting fresh it will get refreshed but whatever is available in your storage S3 bucket or whatever is available in your local hard disk is continued to be available that is the difference between this storage and all of them are storages only this is within the CPU this is within the motherboard and this is remote so everything increases the capacity increases the latency increases turn to retrieve increases and also cost also increases the cost is only the utilization of registers and cache by the CPU but memory not much of incremental cost but storage each S3 bucket for example will increase the cost suppose if 25,000 files are coming every hour and you are able to 3 every time then the cost for the S3 is not going to increase it will be like within whatever is expected but if it is piling up then that's not going to be the case so the memory hierarchy not only increases or decreases the speed but also increases the cost but we can't do because it's only transactional cost not capital investment but the transaction cost is also going to increase substantially if you are going to add more CPUs then that is going to add the operational cost permanently though it is an operational cost every month you have to pay for the same CPUs that is running there so we need to be aware of capacity latency and cost and we need to balance it okay perfectly fine now before going ahead and understand we need to be in a position to identify where an application currently executing local variables may reside local variables if you have written it in the application is expected to reside in the memory not beyond memory the compiler and the processor decisions determine whether it is going to be residing in the registers L1 L2 L3 cache or memory most of the time it will be there is no simple one-to-one mapping from source variable to a fixed level we can't say okay the local variable global variable obviously it will be in the RAM because the moment it goes into the registry upon completing that ISA that memory is also lost the variable is also lost local variable can reside anywhere between register to SDRAM but the global variables will generally reside in the SDRAM not even in L3 cache because the overall ISA when it is completed this gets refreshed but your SDRAM will get refreshed only if the system is rebooted so one way of looking at it is how many global variables you have how many local variables I am just giving you one example of application behavior now the most precision is the local hard disk and the network storage extend the broader storage hierarchy these two extends the broader storage hierarchy they are not additional CPU cache levels it has nothing to do with CPUs though this is connected to the local motherboard it is outside of the CPU though it is definitely outside of the network itself so it is outside of the CPU so before we get to understand what next how to read the hierarchy let us quickly take a 5 minute break and then let us come back but before going to the break any queries any question either you raise your hand or you can put it across in the chat any queries any questions I do not see anybody raising the hand and okay so I have written a program wherein I am using variables to obtain the values so now within the variable there are two things local variable means within the function global variable means within the module or within the program within the class now you might have multiple functions within a single program so the moment the function is completed the local variable no I just stopped sharing the screen video so the moment function is completed the local variable will be wiped out because the local variables generally stored between the register and the cache if it is a global variable generally it is go and resides in the memory so that the program from function to another function it can refer the current value of the variable now how the compiler and the processor determine whether it should go like L2 L3 and so on your code they are volatile krishna they are volatile memory register L1 L2 L3 is volatile even RAM is also volatile but the impact is only if you are going to restart until then it is just so now when your instruction from the application is saying that let us take it as a python you are defining it as a global variable the instruction set will go to the processor ISA will say we have seen like how it uses the registers how it goes to the PUs and how it processes the instruction set part of it this data also will go you might have set it to value 0 or if it is a string you might have set a default value along with that value it will be sent so it is noted that it is a local variable if you don't define also it is a local variable that's what the instruction set will go so it will store it in register if it is immediately needed otherwise L2 or L3 L2 and L3 only if it is within the function but no I'm not sharing I stopped sharing the screen so if it is within the function and within the program but defined out of the function and out of the module then it is a global variable so the instruction set will go okay this is the variable this is the value but it is global the global indicates the processor to store that variable and the value into the RAM not in cache or not in the register but it will store it in the register while calculating and then it will get refreshed once that particular function is completed even though it is a global variable it will get erased from the register but it will be available in the memory so for the next function if the same variable is being used for comparison then it is not available in the register now it will go back to the memory and check if it is having the value for that variable if total program compilation is computation and instruction is completed then it will wipe out the global variable also from the memories and that instruction we normally give we will release local variable anyhow gets released from the function completion global variable as a best practice if you are a developer you know that we will release all the variables before we close the program the moment the instruction will go as part of the ISA release the memory for this variable and the value so it will get raised from the memory this is how the decision is made a decision is basically depending on the instruction that we are giving to the processor hope you are on the same page any other queries questions are we good reaction that would help I'll be able to see how many reactions would have come okay great okay so 2842 we'll have 7 minute break I'm sharing just for our understanding because later we should not have difficulty in the recording to at least you know like how much time is going to be spared the 7 minutes break I'll be back thank you guys as women can see as there you can go yeah I can Thank you. Hi, everyone. Thank you. So, Thank you. So, Thank you. So, Thank you. I'm, Thank you. So, Thank you. Because, So, Thank you. So, And, Okay, And, So, And, And our focus is going to be mainly on CPU cache and DRAM. We will not be getting into the deeper of the storage because the storage hierarchy has its own way of working. It's totally different from the memory hierarchy. So our focus is on the memory hierarchy and not the storage hierarchy. So now is an SSD cache? Hard disk also has a cache. Is SSD cache the same thing as the L1 cache? No. The CPU cache is a hardware transfer unit within the CPU cache hierarchy. Now that we know why the hierarchy exists, let us look at the mechanism that keeps frequently needed data. Very close to cache. Okay. Now, the locality comes into the picture. The idea is to retain copies of the data that has been used recently and to fetch the data in blocks. So nearby values are available too. Those fast storage structures are cache. How can a CPU avoid fetching every requested value from memory? What kind of data does an application tend to reuse? This is something that we need to understand. What kind of data does an application tend to reuse? What do you think? A couple of examples, please. Let me look at the chat. Yeah, Satendra, RAM is the connecting between the storage and the memory hierarchy. The question here is, I do not see any response at this point of time. So let me, what kind of data does an application tend to reuse? We easily say that whatever is being reused will be kept either in cache or memory because it is nearby. So it is fast and it is small. But what type of data is being used by applications in general? Example, chat history in LLM applications. Loop counter. Perfect. Structure. That is also a good example. Websites. What will get stored in the cache in case if you are browsing a website? I understand. I understand you are referring to the information that is getting stored in your local cache. Is it CPU cache or ADIS cache? That is an application cache. The application will be looking for some storage. Global variables. Perfect. Authentication. Authentication. Authentication. Authentication once done, why is it required to reuse? Because once it is done, then for the entire session you are going to use the same link. The connectivity. Cookies. Where does cookies get stored? Is it getting stored in memory? Or is it getting stored in the hard disk? Okay. Fair enough. RBAC. Rules for APIs. In our cached in sessions are ready. Reduce is memory database. Yes, that is one good example. User-centric data like balances in any banking application. Again, if you want to clearly understand, the only question that you need to ask yourself is, is that data stored in memory cache, CPU cache, or storage cache? API keys. API keys. Yes, that is one good example. Frequently accessed to resources, calculations, programs. Calculations and programs are going to be part of instruction set. Why it has to be stored? Okay. The data store in the browse cache. Browsing cache is stored in memory or hard disk. Global variables. Yes, those are used frequently in CPU cache. Tokens. Yes, possible. Because it could be one great example is personally identifiable information. PII. So somebody from security background will be able to map it to PII immediately. PII. So for example, I have a company and I'm doing a business on my own. I started it new in the month of May. PII. So the question comes to me is, do I need to register for GST? Then I go and see. I've been told that unless your business is 20 lakhs per annum, you don't require a GST and you don't need to charge GST. So you don't have to go for the GST registration now. But when you are approaching 18 lakhs or 19 lakhs and you're going to be sure that you're going to touch 20 lakhs in that particular year, you need to go for the GST registration. This is the advice given. And in the month of November, I say I'm touching 19 lakhs. So immediately they say, oh yeah, we need to register. Going forward, once your GST registration is successful, you should start charging GST for your invoices. Now I go for GST registration. As part of the GST registration, there are two things that is personally identifiable information. One is that my PAN card number, permanent account number. And the second one is the other. These two are very much necessary. And apart from that, as part of the process, I also need to authenticate myself with OTP in relation to the other. So now there are three P personally identifiable information. One is PAN, second is AADAR, and the third one is the OTP. So where do you think that this information will be stored by the program when I'm going to apply for GST? I have provided my PAN number. I have provided my AADAR number. Since I have provided my AADAR number, the OTP came to my mobile. I am giving the OTP. So three things, PAN, AADAR, and OTP. Where do you think that these three values will be stored? Will it be stored in registers, cache, memory, or a disk? Yes, I say cache. Pretty much it will be stored in the cache while processing a disk. Why it will be stored in a disk? Okay. One thing is the PII's are not supposed to be stored in any of the storage devices. So with or without concern, the best practice is not to store them. In our application, we removed AADAR or PII data from cache as per UIID. Yeah, that's right. Satana is giving the full process. But one thing is any PII, we cannot store it in the storage with or without concern. That is the best practice. So now where will it get stored? When the application is generating the OTP, let us take the instance of OTP. Now the application is generating the OTP. And this OTP is getting transferred via the API to my mobile and email. I am supposed to do both the OTP. I am supposed to do both the OTPs, email and mobile OTP until I authenticate. Where will this OTP be stored in the system? Is it in register? Is it in cache? Or is it in memory? Until my input. When it is generated, it will be stored in the register perfectly fine. And then it will be moved to the cache only because the instruction set is completed. Now the module is completed. Where will it go from the cache? It will go to the memory. Now we are not supposed to move the OTP from memory to hard disk. So that means we need to hold it in the memory. So that when I am entering the OTP into the website. Again, the authentication program is going to take my value, input value. And it is supposed to store it against, sorry, it is supposed to compare against what it generated. Now what it generated is in the memory. Now what I am typing is getting into the register. So it will refer the memory because the instruction set is going to ask it to generate the OTP to be compared. So it will refer to the memory, get it, compare it. And if it is successful, it is going to allow you to submit your application. This is how it handles. But it will never go to database. OTPs will never go to database. And that is the best practice. Even the service provider, for example, if I have a RTL account, RTL mobile, even the RTL is not going to store that OTP. Once it has sent the SMS that will be erased from its system. How it will get erased? The program itself will erase it from the memory once that is completed. Submission is completed. So only I will hold the OTP in my message, which I can give. Once it reached my mobile device, except for the memory where the application is running, there also only the generated OTP will be saved. There won't be any reference to what has been sent to me. So it's two different entities. Now, even in my mobile, it is stored in the storage. Even in the server also, it is stored in the storage, but not in hard disk or remote, but in the memory. It won't go beyond memory in the hierarchy. Okay. So once I authenticated, it will be erased from the memory also. We need to ensure once the authentication is successful, we need to ensure that the application is erasing it from the memory. And I am also supposed to remove the OTP SMS from my mobile. I'm also supposed to delete the OTP email from my mail client. So shared responsibility model. I have to delete my instance and application is supposed to erase it from the memory. In message, we see OTP is valid for 10 minutes. Even then, it will be kept in the memory for 10 minutes. That's all. So after that, you enter the OTP after 10 minutes, you will get a message that it has expired and you need to generate OTP again. So yes, it will be saved in the memory till 10 minutes in there. So now, Sundar, you ask, what will happen if that server got rebooted? It will go off from the memory, the generated OTP, the generated OTP which has been saved for comparison will be erased when the system comes up. That's where exactly the high availability disaster recovery comes into the picture. So the application will take care of it. That is up to the application architecture and the infrastructure architect to decide how are they going to. So if we did not enter, it will be erased from the memory. We have to regenerate. That is how it works. So at our end, it is in the permanent memory of the phone and the email. But at the server end, it will be in the main memory. We will lose it if the system restarts. But if you restart your mobile or if you restart your desktop or the laptop, whichever device you have received your email, that's not going to wipe out the OTP from your records. You will still have that SMS you can retry and then enter the OTP again. But in case if the server reboots, you have to regenerate even if it is within five minutes. So that is how the hierarchy, the cache memory and the locality. So locality, it is basically the local storage. So how can CPU avoid waiting for RAM for every data across? Basically to keep copies of recently or nearby used data in small fast memories called cache. It can save in the cache. But if there is a risk of application erasing from the cache, it will move to memory and then remove it from the cache. That is how it works. Okay, fine. Now, we have seen what kind of data does the application reuse. Now CPU requests a value. Now the CPU requests some value. The processor checks the relevant cache level. If it is there, we have a hit. That means we were able to retrieve the value from the cache. It could be L1, it could be L2. If it is not a hit in L1, then it will check L2. And if it is not a hit in L2, it will look at L3. If L3 may contain it, it is going to retrieve and use it. Only if it is absent from the relevant caches, the request needs to be served from the memory. So that means the saving of the information also is in the same order. It will move from the register to L1, from L1 to L2, from L2 to L3. And if it has to be removed from the L3, then only it will move the data. So if it is not able to fetch from memory, for example, the OTP has expired 10 minutes over. Now I am entering the OTP because I received it. I have it in my mobile. Now it says that your OTP expired. How? How does it know? Basically, it is not able to retrieve it from the memory. That's it. As a developer, we are not going to look at, okay, what is the time generation of the OTP, the time at which the OTP is generated, whether it is expired? No. Instead, our logic would be, the moment a code is generated and 10 minutes is over, we will be removing it from the memory. That is the best practice and optimal utilization of the memory hierarchy. Once it is not able to retrieve or fetch it from the RAM or the memory, obviously, it is going to say you are, even if it is 9 minutes, 36 seconds, sometimes you might have experienced that. They won't say your OTP expired or something like that. Application is just going to say you need to regenerate OTP and provide us a name. That's all they are going to say. But internally, the moment they are not able to fetch it from the memory, they are going to say regenerate. That's it. And for the application logic perspective, if it is not there in the memory, it is expired. But we will not display the message, your code has expired or something like that. We will just give two options, cancel, regenerate. Normally, users will regenerate and give the code immediately. That's how it works. So now, this is a simplified cache lookup. And all these are local, all, until memory, it is a local hierarchy. So if L1 misses, but L2 hits, did we need to fetch the data from RAM 2? No. If you are able to hit at L2, definitely the data is not going to be there in L3 or RAM. It will not be. The moment you hit, the process stops. So the key message is missing at this level, rather than simply saying cache paste. What does it mean? When we are trying to write our application logs, suppose if we are able, if we are not able to fetch and we are missing the OTP, we are supposed to write this log, application log, so that tomorrow if somebody wants to look at like why there are so many OTP authentication misses that are happening. Maybe that is one of the monitoring counter. Suppose if you are doing like 100k OTP authentication in a month, how many hit at first and how many miss at first? Maybe if you are seeing that 10% of the OTP authentication is being done, not being done in the first instance itself. Maybe 10% is an acceptable level. But what if it is 25%, then definitely it is not an acceptable level, because the user experience is going to get impacted. There will be complaint from the customers that OTP authentication is we have to do double, triple. But if it is only 10%, if it is only 3%, but we need to know that. To know that we have to write at what level we are able to hit the, even if it is going to hit L3 to receive, or fetch the OTP or memory, we know now that it is going to take longer to fetch from the memory compared to L1 or L2. L3 is going to be far more time consuming. And fetching from the memory is a little bit more than the L3. So again, the user experience may come, oh, the OTP authentication is slow. It is successful. So we are at 2%. 98% of the OTPs are getting authenticated in the first attempt. But it takes one and a half second. It takes two and a half seconds. The average we are expecting is 30 milliseconds. Why is that delay? Is it the application? Is it CPU? Is it memory? Or is it an external API? Because you have a dependency with the ARCLE network or J network or Y network, whatever it is, telephone network. If it is related to telephone network, then you need to fix that by speaking to the API vendor. So for that we need to precisely write where we are getting delayed. Maybe we are able to retrieve it from L3 or memory very fast, no issues. But delivery of OTP is taking that much time. So the authentication also is happening that much. If that is the case, then the cache hit or the cache miss. Whether you are able to get the value from the cache or whether you are able to get the value from memory or you are not able to get the value at all, makes a lot of difference to identify where the problem is. So to do that, we will log into the application log and write very clearly from where we are getting this value for the authentication. Is it L1, L2, L3 or memory? And if it is a miss means definitely it is not available in the memory. If it is L2 means L1 is a miss. If you are able to fetch it from L3, that means L1 and L2 is a miss. Because the CPU is going to look at register first, if not L1, if not L2, if not L3, if not memory. If not, application closes. It reprocesses. So because of this, the instruction set is also getting repeated and the CPU is utilizing. If you are having 25% of the OTP authentication is being done in the second attempt, that means 25%, 25,000 requests was additionally processed by your processor, by your system. And there is a cost involved because your application is sending an OTP. You may be paying the vendor for OTP transfer. So the 25,000, if it is 2%, probably 500. That is okay. Because we cannot avoid. If we can, then we will bring it to 1%. That is optimization. But 25% is huge and we need to fix it immediately. Because processing also is high processing cost and also transmission cost. Both are getting and storage cost. In case if you are occupying the memory, double the time. Then the storage cost also, though memory is the costliest storage unit of the storage hierarchy and register is the costliest unit in the memory hierarchy. So we should try to avoid using the costliest unit all the time. That is the reason we store our data in the storage in a volume and then retrieve it, though it is a little slow. Only for processing, we will use memory. Memory comes in both hierarchy and that is the connecting unit between the memory hierarchy and the storage hierarchy. So cache hit versus cache mesh. This is where we are going to pass today. Sorry for taking extra five minutes. I just wanted to complete until cache hit and cache miss. Any queries, any questions before we conclude our session today? Why is memory hierarchy designed around the principle of locality of reference? What happens if an application does not exhibit? Okay. That is application to be fixed. The impact is basically your local is being utilized higher and then cost is going to be according to that. Why should I keep code for 10 minutes always? No. 10 minutes validity is desired by the application. There are, suppose if you are going to the ATM and if you are going to, today you have multiple ways of withdrawing money and you have another process to withdraw your money is using the UPI QR code. Now I am using that UPI option. I have entered my amount. From that time, from that time, if you look at the ATM, any bank, there will be a clock which will be running on top right of the screen. It will start with some 75 seconds. So that means you are supposed to receive the QR code. You are supposed to enter the pin for confirmation of the withdrawal within 75 seconds. Once it is done, then it will say now this process is going to withdraw money for you from the machine and that process will have only 60 seconds to complete. In case if it is not able to withdraw the money within 60 seconds, then it is going to cancel the transaction. I am not sure if any of you have experienced it. In case if you are, if you have not, you can just look at the ATM. So time is not the criteria. It is what we have set the criteria is the time. Okay. Is RAM can be used as a cache for persistent storage? We can use. Technically it is possible again, but as a best practice it is not. Because if you start using for persistent storage cache the memory, then whatever the program requires the memory, it will not be available for you. So that's the reason even the browsing cache is stored in the hard disk, not in the memory in your laptop. If you are clearing up your browsing history, then you can see hard disk will blink. So it is clearing it off from the hard disk. So whenever you are trying to access your browsing history, it is going to load from your local hard disk. So it uses the hard disk as a storage and not memory as a storage. It is not a best practice. Can I? Yes, you can, but it is a costly affair. Any other queries? Questions from anyone? Okay, guys. Thank you very much. We will catch up at two o'clock tomorrow. We will continue with this and we will see some demonstrations of how to observe it. Take care. Bye-bye. Bye-bye.