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

前端 未结 8 1642
别跟我提以往
别跟我提以往 2021-01-30 06:46

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

相关标签:
8条回答
  • 2021-01-30 07:15

    Locks can be added on database entities, e.g. tuples, transactions.

    Latches can be added on underlined data representation, e.g. page table in memory which maps page identifier to a specific frame.

    0 讨论(0)
  • 2021-01-30 07:20

    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.

    0 讨论(0)
  • 2021-01-30 07:23

    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
    0 讨论(0)
  • 2021-01-30 07:23

    Quoting from OLTP Through the Looking Glass, and What We Found There by Stonebraker et al.

    Locking. Traditional two-phase locking poses a sizeable overhead since all accesses to database structures are governed by a separate entity, the Lock Manager.

    Latching. In a multi-threaded database, many data structures have to be latched before they can be accessed. Removing this feature and going to a single-threaded approach has a noticeable performance impact.

    This interpretation then associates locking with database level objects e.g. rows, whereas latches operate at the lower level of data structures.

    0 讨论(0)
  • 2021-01-30 07:30

    The different between Locks and Latches:

    Reference taken from this blog.

    Locks ensure that same record cannot be modified by two different connections and Latches ensure that record resides in a proper data page for further reading and writing operation.

    Locks provide a consistency of logical transaction and Latches provide a consistency of the memory area.

    The DBA can control and manage database locks by applying different Isolation Levels and for Latches, DBA doesn’t have any control because it’s managed by the SQL Server.

    0 讨论(0)
  • 2021-01-30 07:31

    Another name for a latch is 'spin lock'. It's a simple 'while loop' until bit will be zero (depending on implementation). The execution thread is never asleep while the latch is not available. No any queue. A spin lock is useful for short-time memory object locking, but wasteful if held for a longer duration. See the "Spinlock" article on Wikipedia

    Locks are usually supported by the system and in case that they are taken, your thread will be put to sleep so it won't consume any processor resources. Each lock keeps an internal queue of all suspended threads.

    The lock manager is the subsystem that can provide you as spin locks as heavyweight locks for concurrency support.

    See also the article by Tom Kyte about latches and locks.

    0 讨论(0)
提交回复
热议问题