{"id":1943,"date":"2013-10-03T09:00:00","date_gmt":"2013-10-03T16:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/dataplatforminsider\/2013\/10\/03\/concurrency-control-in-the-in-memory-oltp-engine\/"},"modified":"2024-01-22T22:49:09","modified_gmt":"2024-01-23T06:49:09","slug":"concurrency-control-in-the-in-memory-oltp-engine","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/sql-server\/blog\/2013\/10\/03\/concurrency-control-in-the-in-memory-oltp-engine\/","title":{"rendered":"Concurrency Control in the In-Memory OLTP Engine"},"content":{"rendered":"
We believe that the In-Memory OLTP engine advances the industry state of the art with respect to concurrency control. The main reason for this advancement is due to the combination<\/i> of lock free algorithms and the row-versioned architecture of the engine. <\/span><\/p>\n This post examines what we precisely mean when we describe the In-Memory OLTP engine as being \u2018lock free\u2019, both in abstract terms but more importantly in terms of impact on user workloads.<\/span><\/p>\n Let\u2019s start with a brief definition capturing the core attributes implied by the term \u2018lock free\u2019 in In-Memory OLTP: <\/span><\/p>\n \u201cAt steady state<\/i>, transaction carrying threads<\/i> executing in the context of the<\/i> In-Memory OLTP<\/i> engine<\/i> are designed to require no blocking operation.\u201d <\/span><\/p>\n Element by element, this definition implies the following exclusions:<\/span><\/p>\n Once we agree on the definitions above, we note that in the context of any database system, locking behavior always implies two distinct considerations: we use locks for logical data protection (also known as transactional isolation) and for physical data protection. <\/span><\/p>\n One example of logical data protection<\/b> is row payload protection. Row payload protection means that while a user modifies a row another user does not modify the same row. This is what logical (transactional) locks are used for in traditional SQL Server (row locks, page locks, database locks and even app locks can be used for the same purpose). With In-Memory OLTP we rely on row versioning to ensure that row content is never modified by two users at the same time \u2013 in other words we don\u2019t use transactional locks because we never update data in place. If two or more users try to update the same row at the same time, one will succeed while the others will fail due to a write\/write conflict. Note that this can be achieved without any locking by creating a new version for each user and then trying to install each version atomically in the same index location (by using \u2018<\/span>InterlockedCompareExchange<\/span><\/a>\u2019 \u2013 or ICX). Out of the multiple users that try to update the same row at the same time, one will succeed and the others will fail this hardware level ICX operation \u2013 and that translates directly into the behavior reported back by the system.<\/span><\/p>\n Physical data protection<\/b> is a different problem altogether. In traditional database systems (<\/span>SQL Server but also more broadly in the industry<\/span><\/a>) we protect internal data structures via spinlocks and latches. Broadly speaking (and oversimplifying), the difference between spinlocks and latches is that a thread trying to acquire a spinlock will spin when the lock is found to be currently held by a different thread whereas for latches the acquiring thread will yield its CPU time back to the OS if the latch is found to be currently held by a different thread. Given this difference, traditional SQL uses latches for waits that could take a while (getting a page in the buffer pool) while spinlocks are used for short term waits (waiting for a memory only linked list traversal for instance). However, both locks and latches have in common the fact that they are \u2018region locks\u2019. We use the term \u2018region lock\u2019 to describe the mechanism used to protect a region of code or data structures against simultaneous thread access. Region locks implement an \u2018Acquire\/Release\u2019 pattern \u2013 where a lock (either latch or spinlock) is first acquired<\/b>, the protected region executes, and then the lock is released<\/b>. The problem with that approach is that it does not scale. In a system with many cores or very high concurrency the region being protected becomes a bottleneck. For instance, in Windows Server the scheduling quantum is around 180ms, so if a thread that holds a spinlock gets preempted, that spinlock will be held for 180ms regardless of how short the protected region would be otherwise. There are other negative side effects from region locks because they all involve writing to shared cache lines even when the lock is acquired for read access \u2013 which becomes problematic in many-core and NUMA systems or under high concurrency. The lock free engine avoids these issues by implementing all operations in an atomic fashion. In other words, the In-Memory OLTP engine does not define any protected regions in transaction executing paths. The data structures and algorithms are structured such that state transitions are atomic and therefore are not subject to the whims of the scheduling subsystem. In addition, many operations are done without any shared-cache line modifying instructions at all (meaning that the entire operation does not even use ICX but rather touches in write mode only cache lines that are private to the local processor) which improves scalability and concurrency to the limits supported by the hardware. <\/span><\/p>\n The prime example of that is index traversal: the engine walks both hash and range indices without any locks or ICX instructions. In the process the engine detects if the underlying data structure has changed in a manner that could invalidate the current traversal and re-attempts the small portion of the traversal that was invalidated. These re-traversal are extremely rare even at very high concurrency (in the early days of the In-Memory OLTP engine we have measured under 100 retries for million tx \/ sec workload) \u2013 so their measurable performance impact is virtually non-existent. When the engine needs to modify one of these lock free data structures it does so via ICX \u2013 which makes the modification visible atomically.<\/span><\/p>\n A careful observation of current hardware trends presents overwhelming evidence that the number of available cores in any given computing platform is likely to rise with time. In this context, concurrency control that is at its core lean and efficient is absolutely crucial to achieving first-rate performance. With In-Memory OLTP we have taken these insights to heart and built an engine that relies on no locks, waits, latches or other synchronization primitives to ensure consistency of execution. We believe this approach removes locking and latching as a concern for even our most demanding users.<\/span><\/p>\n For more information, download SQL Server CTP1<\/a>and get started today, or see more blogs in the series introduction and index here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" We believe that the In-Memory OLTP engine advances the industry state of the art with respect to concurrency control. The main reason for this advancement is due to the combination of lock free algorithms and the row-versioned architecture of the engine.<\/p>\n","protected":false},"author":1457,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"_classifai_error":"","_classifai_text_to_speech_error":"","footnotes":""},"post_tag":[],"product":[],"content-type":[2445],"topic":[],"coauthors":[2487],"class_list":["post-1943","post","type-post","status-publish","format-standard","hentry","content-type-thought-leadership"],"yoast_head":"\n\n
\n
\n
\n
\n
\n