Earlier I wrote about SQL SERVER – SQLOS Scheduler and the Process States, lots of people liked the blog post and asked me another question – What is Latch?
You can find lots of information about the latch online. Even I have previously written about this subject. Today, I am going to do my best to explain in very simple words and with the related to my earlier youtube video Sleeping vs Suspended Process – SQL in Sixty Seconds #122.
Latches are internal SQL Server mechanisms and they are limited to the SQLOS (SQL Server Operating System). They servers to protect shared memory resources inside the SQL Server Memory Buffer Pool. They are used to provide memory consistency, whereas locks are used by SQL Server to provide logical transactional consistency.
In other words – When SQL Server reads the memory, it will put the latch on the internal memory structure so it can’t be modified by other threads. This way when you have multiple threads are running, there is no corruption of the memory.
Memory Buffer Pool
Let us understand the role of the buffer pool with regards to SQLOS. The buffer pool is actually a physical memory (RAM) where data is read from the disk is stored. This way when the same data is once again required by the end-user query, SQL Server does not have to go back to the disk to read the data and the same data can be served to the query.
It is quite possible that the query is not SELECT but also INSERT, UPDATE or DELETE, in that case, the changes are done in the buffer pool and they are eventually moved to the disk. All of these processes happen very fast and their duration is very small. All the pages which are changed in the buffer pool are called dirty pages and they have to be flushed (or moved) to the disk. All the pages in the buffer which are still not modified are called clean pages.
The latches are an internal mechanism that keeps the entire operation of data moving from disk to memory and memory to disk corruption-free.
Now there are many different kinds of latches, which are not related to the buffer as well. However, the goal of this blog post to explain in the most possible simple words what are latches.
Two Important Videos for Latch
Here are two important videos I recommend for you to understand what is Latch.
If you have any questions, please drop a comment.
Reference: Pinal Dave (https://blog.sqlauthority.com)