Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a lock and a latch in the context of concurrent access to a database?

I am trying to understand a paper on concurrent B-tree, in which the author mentioned latch vs lock, and how latches do not need a "Lock Manager". I have been trying to figure out what are differences between those two for two days.

Google resulting in:

"locks assure logical consistency of data. They are implemented via a lock table, held for a long time (e.g. 2PL), and part of the deadlock detection mechanism.

latches are like semaphores. They assure physical consistency of data and resources, which are not visible at the transactional level"

However, I am still pretty confused. Can some one elaborate on this? and what exactly does a lock manager do?

Thanks in advance.

like image 941
Viele Avatar asked Jun 24 '10 15:06

Viele


People also ask

What is the difference between lock and latch in Oracle?

Simplistically: a lock is something you queue up to get and simply wait until it is available; a latch is something for which you periodically check and grab if it happes to be available. By the way, there are other serialization techniques that are available. Oracle internally uses them as well.

What is the difference between latch and lock in SQL Server?

To allow for maximum concurrency and provide maximum performance, latches are held only for the duration of the physical operation on the in-memory structure, unlike locks, which are held for the duration of the logical transaction.

What is latch concurrency?

Latches are simple, low-level system lock (serialization mechanisms) that coordinate multi-user access (concurrency) to shared data structures, objects, and files. Latches protect shared memory resources from corruption when accessed by multiple processes.

What is the difference between lock and deadlock?

Deadlocks are a related behavior, but they're more complicated. A deadlock happens when multiple lock waits happen in such a manner that none of the users can do any further work. For example, the first user and second user both lock some data. Then each of them tries to access each other's locked data.


3 Answers

From CMU 15-721 (Spring 2016), lecture 6 presentation, slides 25 and 26, which cites A Survey of B-Tree Locking Techniques by Goetz Graefe:

Locks
→ Protects the index’s logical contents from other txns.
→ Held for txn duration.
→ Need to be able to rollback changes.

Latches
→ Protects the critical sections of the index’s internal data structure from other threads.
→ Held for operation duration.
→ Do not need to be able to rollback changes.

Locks and latches

like image 118
user454322 Avatar answered Oct 18 '22 23:10

user454322


It really depends on your DBMS, but here's a good explanation for Oracle.

http://www.dba-oracle.com/t_lru_latches.htm

Latches are like locks for RAM memory structures to prevent concurrent access and ensure serial execution of kernel code. The LRU (least recently used) latches are used when seeking, adding, or removing a buffer from the buffer cache, an action that can only be done by one process at a time.

like image 14
Matt Rogish Avatar answered Oct 19 '22 00:10

Matt Rogish


Following is from SQL Server stand point.

Latches are short-term light weight synchronization objects. Unlike locks, latches do not hold till the entire logical transaction. They hold only on the operation on the page.

Latches are used by the engine for synchronization of multiple threads (for example trying to insert on a table). Latches are not for developer or application - it is for the engine to do it's task. Latches are internal control mechanism. Whereas locks are for the developer and application to control. Latches are for internal memory consistency. Locks are for logical transactional consistency.

Waits caused by latches are very important for diagnosing performance issues. Take a look at Diagnosing and Resolving Latch Contention on SQL Server - Whitepaper. The PAGEIOLATCH_EX is an important wait type.

References

  1. SQL Server Latches and their indication of performance issues
  2. Knee-Jerk Wait Statistics : PAGELATCH
  3. Inside SQL Server: Indexing and Locking
like image 7
LCJ Avatar answered Oct 19 '22 00:10

LCJ