🐝
Mess around software system design
  • README
  • ArchitectureTradeOffAnalysis
    • Estimation
    • Middleware
    • Network
    • Server
    • Storage
  • Conversion cheat sheet
  • Scenarios
    • TinyURL
      • Estimation
      • Flowchart
      • Shortening mechanisms
      • Rest API
      • Performance
      • Storage
      • Follow-up
    • TaskScheduler
      • JDK delay queue
      • Timer based
      • RabbitMQ based
      • Kafka-based fixed delay time
      • Redis-based customized delay time
      • MySQL-based customized delay time
      • Timer TimingWheel
      • Industrial Scheduler
      • Workflow Engine
      • Airflow Arch
    • GoogleDrive
      • Estimation
      • Flowchart
      • Storage
      • Follow-up
    • Youtube
      • Estimation
      • Flowchart
      • Performance
      • Storage
      • Follow-up
      • Netflix
    • Uber
      • Estimation
      • Rest api
      • Flowchart
      • KNN algorithms
      • Geohash-based KNN mechanism
      • Redis implementation
      • Storage
    • Twitter
      • Estimation
      • Flowchart
      • Storage
      • Scalability
      • Follow-up
    • Instant messenger
      • Architecture overview
      • Presence
      • Unread count
      • Notifications
      • Read receipt
      • Large group chat
      • Storage-Offline 1:1 Chat
      • Storage-Offline group chat
      • Storage-Message roaming
      • NonFunc-Realtime
      • NonFunc-Reliability
      • NonFunc-Ordering
      • NonFunc-Security
      • Livecast-LinkedIn
    • Distributed Lock
      • Single machine
      • AP model based
      • CP model based
      • Chubby-TODO
    • Payment system
      • Resilience
      • Consistency
      • Flash sale
    • Key value store
      • Master-slave KV
      • Peer-to-peer KV
      • Distributed cache
  • Time series scenarios
    • Observability
      • TimeSeries data
      • Distributed traces
      • Logs
      • Metrics
      • NonFunc requirments
  • Search engine
    • Typeahead
    • Search engine
    • Distributed crawler
      • Estimation
      • Flowchart
      • Efficiency
      • Robustness
      • Performance
      • Storage
      • Standalone implementation
      • Python Scrapy framework
    • Stream search
  • Big data
    • GFS/HDFS
      • Data flow
      • High availability
      • Consistency
    • Map reduce
    • Big table/Hbase
    • Haystack
    • TopK
    • Stateful stream
    • Lambda architecture
    • storm架构
    • Beam架构
    • Comparing stream frameworks
    • Instagram-[TODO]
  • MicroSvcs
    • Service Registry
      • Flowchart
      • Data model
      • High availability
      • Comparison
      • Implementation
    • Service governance
      • Load balancing
      • Circuit breaker
      • Bulkhead
      • Downgrade
      • Timeout
      • API gateway
      • RateLimiter
        • Config
        • Algorithm comparison
        • Sliding window
        • Industrial impl
    • MicroSvcs_ConfigCenter-[TODO]
    • MicroSvcs_Security
      • Authentication
      • Authorization
      • Privacy
  • Cache
    • Typical topics
      • Expiration algorithm
      • Access patterns
      • Cache penetration
      • Big key
      • Hot key
      • Distributed lock
      • Data consistency
      • High availability
    • Cache_Redis
      • Data structure
      • ACID
      • Performance
      • Availability
      • Cluster
      • Applications
    • Cache_Memcached
  • Message queue
    • Overview
    • Kafka
      • Ordering
      • At least once
      • Message backlog
      • Consumer idempotency
      • High performance
      • Internal leader election
    • MySQL-based msg queue
    • Other msg queues
      • ActiveMQ-TODO
      • RabbitMQ-TODO
      • RocketMQ-TODO
      • Comparison between MQ
  • Traditional DB
    • Index data structure
    • Index categories
    • Lock
    • MVCC
    • Redo & Undo logs
    • Binlog
    • Schema design
    • DB optimization
    • Distributed transactions
    • High availability
    • Scalability
    • DB migration
    • Partition
    • Sharding
      • Sharding strategies
      • Sharding ID generator overview
        • Auto-increment key
        • UUID
        • Snowflake
        • Implement example
      • Cross-shard pagination queries
      • Non-shard key queries
      • Capacity planning
  • Non-Traditional DB
    • NoSQL overview
    • Rum guess
    • Data structure
    • MySQL based key value
    • KeyValueStore
    • ObjectStore
    • ElasticSearch
    • TableStore-[TODO]
    • Time series DB
    • DistributedAcidDatabase-[TODO]
  • Java basics
    • IO
    • Exception handling
  • Java concurrency
    • Overview
      • Synchronized
      • Reentrant lock
      • Concurrent collections
      • CAS
      • Others
    • Codes
      • ThreadLocal
      • ThreadPool
      • ThreadLifeCycle
      • SingletonPattern
      • Future
      • BlockingQueue
      • Counter
      • ConcurrentHashmap
      • DelayedQueue
  • Java JVM
    • Overview
    • Dynamic proxy
    • Class loading
    • Garbage collection
    • Visibility
  • Server
    • Nginx-[TODO]
  • Distributed system theories
    • Elementary school with CAP
    • Consistency
      • Eventual with Gossip
      • Strong with Raft
      • Tunable with Quorum
      • Fault tolerant with BFT-TODO
      • AutoMerge with CRDT
    • Time in distributed system
      • Logical time
      • Physical time
    • DDIA_Studying-[TODO]
  • Protocols
    • ApiDesign
      • REST
      • RPC
    • Websockets
    • Serialization
      • Thrift
      • Avro
    • HTTP
    • HTTPS
    • Netty-TODO
  • Statistical data structure
    • BloomFilter
    • HyperLoglog
    • CountMinSketch
  • DevOps
    • Container_Docker
    • Container_Kubernetes-[TODO]
  • Network components
    • CDN
    • DNS
    • Load balancer
    • Reverse proxy
    • 云中网络-TODO
  • Templates
    • interviewRecord
  • TODO
    • RecommendationSystem-[TODO]
    • SessionServer-[TODO]
    • Disk
    • Unix philosophy and Kafka
    • Bitcoin
    • Design pattern
      • StateMachine
      • Factory
    • Akka
    • GoogleDoc
      • CRDT
Powered by GitBook
On this page
  • Lock
  • Lock lifetime
  • Optimistic vs pessimisstic lock
  • Optimistic lock
  • Pessimisstic lock
  • Shared vs exclusive locks
  • Shared lock
  • Exclusive lock
  • Intentional shared/exclusive lock
  • Row vs table locks
  • Table locks
  • Add/Release Table lock
  • AUTO_INC lock
  • Row vs gap vs next key locks
  • Record lock
  • Gap lock
  • Next-key lock

Was this helpful?

  1. Traditional DB

Lock

PreviousIndex categoriesNextMVCC

Last updated 1 year ago

Was this helpful?

Lock

  • Within InnoDB, all locks are put on index.

Lock lifetime

  • Only when rollback or commit happens, the lock will be released.

Optimistic vs pessimisstic lock

Optimistic lock

SELECT * FROM your_tab WHERE id = 1; -- Get a = 1
-- operations
UPDATE your_tab SET a = 3, b = 4 WHERE id = 1 AND a =1

Pessimisstic lock

SELECT * FROM your_tab WHERE id = 1 FOR UPDATE; -- put lock on record 1 
-- operations
UPDATE your_tab SET a = 3, b = 4 WHERE id = 1;

Deadlock from pessimistic lock

  • SELECT ... FOR UPDATE will easily cause deadlocks.

Deadlock pattern

  • Use optimistic lock to replace pessimistic pattern.

-- Original pessimstic pattern
Begin()  -- Begin transaction
data := SelectForUpdate(id)  -- Find existing data and compute SELECT * FROM xxx WHERE id = 1 FOR UPDATE
newData := calculate(data)   -- computation

Update(id, newData) -- Write computation result back to DB. UPDATE xxx SET data = newData WHERE id =1
Commit()


-- Transformed optimistic pattern
for {
-- Look for existing data SELECT * FROM xxx WHERE id = 1
  data := Select(id) 
  newData := calculate(data) -- computation

  -- optimistic lock: write data back to DB 
  UPDATE xxx SET data = newData WHERE id =1 AND data=oldData
  success := CAS(id, newData, data) 
  -- if successfully updated, no one modifies the data
  -- Suitable for read-intensive scenarios
  if success {
    break;
  }
}

Deadlock example

BEGIN;
SELECT * FROM biz WHERE id = ? FOR UPDATE
-- business operations
INSERT INTO biz(id, data) VALUE(?, ?);
COMMIT;

Shared vs exclusive locks

Shared lock

  • Def: If transaction T1 holds a shared (S) lock on row r, then requests from some distinct transaction T2 for a lock on row r are handled as follows:

    • A request by T2 for an S lock can be granted immediately. As a result, both T1 and T2 hold an S lock on r.

    • A request by T2 for an X lock cannot be granted immediately.

  • Add lock:

    1. select ...from XXX where YYY lock in share mode

    2. insert ... into select ...

  • Release lock: commit / rollback

Exclusive lock

  • Def: If a transaction T1 holds an exclusive (X) lock on row r, a request from some distinct transaction T2 for a lock of either type on r cannot be granted immediately. Instead, transaction T2 has to wait for transaction T1 to release its lock on row r.

  • Add lock: Automatically by default

    1. update

    2. delete

    3. insert

    4. select ... from XXX where YYY from update

      • If there is no index on YYY, then it will lock the entire table.

  • Release lock: commit / rollback

Intentional shared/exclusive lock

  • Goal: Divide the operation for adding lock into multiple phases. This is especially useful in cases of table locks.

  • Operation: Automatically added by database. If a shared lock needs to be acquired, then an intentional shared lock needs to be acquired first; If an exclusive lock needs to be acquired, then an intentional exclusive lock needs to be acquired first.

Row vs table locks

  • Row lock implementation is based on index

Table locks

  • If the query does not hit any index, then only table lock could be used.

Add/Release Table lock

  • Add:

    1. Lock Table tableName READ

    2. Lock Table tableName WRITE

    3. discard table

    4. import

  • Release:

    • Commit / Rollback

AUTO_INC lock

  • Be triggered automatically when insert ... into Table xxx happens

Row vs gap vs next key locks

  • By default use next key locks except the following two cases: *

Record lock

Prerequistes

  • Both needs to be met:

    • "Where condition" uses exact match (==) and the record exists.

    • "Where condition" uses primary key or unique index.

Exmaple

-- record lock: locked leaf node 31
select * from table where id = 31 for update

Gap lock

  • Gap lock only exists in repeatable read isolation level.

  • Typically gap lock is open on both the "OPEN" and "CLOSE" part.

Prerequistes

  • One of the following:

    • Where condition uses exact match (==) on a unique index and the record does not exist.

    • Where condition uses range match (>, <,>) on a unique index.

    • Where condition has index but is not unique index.

Example

-- Gap key lock (12, 17)
SELECT * FROM your_tab WHERE id = 15 FOR UPDATE
-- Gap key lock (33, MAX_NUM)
SELECT * FROM your_tab WHERE id > 33 FOR UPDATE

Next-key lock

Prerequistes

  • If it is not covered by gap lock and record lock.

Example

  • Relationship with other locks:

  • Next key = record lock + gap lock + record on the right border

Lock
Lock lifetime
Optimistic vs pessimisstic lock
Optimistic lock
Pessimisstic lock
Deadlock from pessimistic lock
Deadlock pattern
Deadlock example
Shared vs exclusive locks
Shared lock
Exclusive lock
Intentional shared/exclusive lock
Row vs table locks
Table locks
Add/Release Table lock
AUTO_INC lock
Row vs gap vs next key locks
Record lock
Prerequistes
Exmaple
Gap lock
Prerequistes
Example
Next-key lock
Prerequistes
Example
Record lock example 1
Record lock example 2
Gap lock example 1
Gap lock example 2
Interval keys
Next-key lock