[並行控制] Concurrency control 指南

Intro

Concurrency control – 並行控制


Database – 資料庫管理系統

悲觀鎖 – PCC

悲觀並行控制 – Pessimistic Concurrency Control,縮寫「PCC」

  • 「先取鎖再訪問」
  • 效率不佳,且有增加產生死鎖(deadlock)機會

MySQL 實現案例 (InnoDB Transaction)

# Row-level Lock for share
SELECT * FROM parent WHERE NAME = 'Jones' FOR SHARE;

# Row-level Lock for update
SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;

MySQL – Locking Reads
Postgresql Explicit Locking – Row-Level Locks

樂觀鎖 – OCC

樂觀並行控制 – Optimistic Concurrency Control,縮寫「OCC」

  • 透過讀取資料時分派一個版本(時間戳),通過校驗後寫入否則中斷(實作上可利用寫入連帶驗證版本達成)
  • 適合交易之間資料競爭(data race)概率較小的應用情境

MySQL 實現範例

update `DB`.`table` set quantity = quantity -1 , version = version + 1 where id = 1 and version = 0;

OS – 作業系統

IPC (Inter-Process Communication) – 行程間通訊

(TBC)

Leave a Reply

Your email address will not be published. Required fields are marked *