[Spring Boot] Distributed Server Concurrent Control
Spring BootWhen requests are made from multiple servers at once for shared resources, the synchronization speed cannot keep up with the processing speed, breaking data accuracy and causing data concurrency problems.
Lock
One of the mutual exclusion techniques for simultaneity control
Lock based on database server configuration
Distributed Server + Single Database
- Optimistic Lockdown
- Pessimistic Lockdown
- Distributed Lock
Distributed Servers + Distributed Databases
- Distributed Lock
Optimistic Lock
A technique of locking a transaction as it is unlikely to occur.
JPA locks with its own version management function (version).
- Implicit locks are automatically applied if @Version or @OptimisticLocking annotation is set in Entity.
- When a deletion query occurs, a row exclusive lock is implicitly provided for the corresponding row.
Check the version at the start and end of the transaction.
- If the versions are different at the time of termination, the rollback is performed, but it consumes a lot of resources because it needs to be rolled back as much as it has been processed.
Method 1. Using Annotation @OptimisticLocking
kotlin
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@Entity
class Account()
Method 2. Using Annotation @Version
kotlin
@Entity
class Account(
@Version
val version: Int
)
@Transactional
@Lock(value = LockModeType.OPTIMISTIC)
fun creatAccount()
- NONE: If there is Version in the Entity field, apply an optimistic lock.
- OPTIMISTIC_FORCE_INCREMENT: Version information increases with optimistic lockdown.
- READ: JPA 1 Compatibility Application
- OPTIMISTIC: Locks optimism
- WRITE: JPA 1 Compatibility Application
- OptimisticLockException occurs when the version information changes and the transaction crashes.
Preemption Lock (Pessimistic Lock)
Pre-locking techniques under the assumption that a transaction conflict must occur
Use the DBMS Lock function.
A deadlock may occur, so a maximum time setting is required.
kotlin
@Transactional
@Lock(value = LockModeType.PESSIMISTIC_WRITE)
fun creatAccount()
- PESIMISTIC_READ: Shared lock. Data can be read from other transactions, but does not allow you to change or delete.
- PESSIMISTIC_WRITE: Exclusive lock. Preventing other transactions from reading, changing, or deleting data.
- PESIMISTIC_FORCE_INCREMENT: Same as exclusive lock. If @Version is specified in Entity, it increases version information (version)
Distributed Lock
It uses a common repository such as a database to check if resources are being used.
@Transactive does not work simultaneously with distributed locks because it is a Spring AOP method.
- COMMIT must be performed before unlocking the lock so that the concurrency problem does not occur.
Redis (Remote Dictionary Server)
An in-memory-based, non-relational database management system that stores and manages unstructured data in memory with a dictionary (key-value) structure.
It is also classified as NoSQL.
It supports redundancy in an asynchronous replication method.
It is used as a database, cache, message broker, and streaming engine.
- Lettuce
- Redis client built on an asynchronous event-based Java network framework (Netty)
- The user directly uses the Redis command setnx to implement it in the form of a spin lock.
- It continuously attempts to acquire a lock until it acquires a lock.
- If the lock is not released normally, it can fall into an infinite loop.(Dead Lock Occurred)
- Each time you attempt to acquire a lock, you send a request to Redis to see if a lock exists, which can delay the response time by putting a load on Redis.
- The maximum number of attempts or expiration time should be implemented to avoid falling into an infinite loop.
- Redission
- Redis client built on an asynchronous event-based Java network framework (Netty)
- Use the pub/sub function.
- Lock expiration time is implemented so that it does not fall into an infinite loop.
- It is applying its own TTL using Lua Script.
- Jedis
- Java client for Redis designed for performance and ease of use
- It was used as the Redis primary client in Spring Boot, but the default client was changed to Letuce from Spring Boot 2.0 because it was unsafe for threads and had little feedback.
Apache Zookeeper
Coordination Service System
Provides distributed processing tools to securely handle partial failures
Manage data in a hierarchical tree in znode
Used in many open sources, including Hbase, Kafka, Hadoop, Kubernetes, etc.
Reference
Managing concurrency across multiple servers with distributed locks using MySQL
Concurrent Control with Redisson Distributed Lock
Installation of redis and implementation of distributed locks using redisson
[Joo Keeper, Zookeeper] Introduction and architecture of Apache Zookeeper
[Apache Zookeeper] The basic characteristics of the main keeper.
Troubleshooting concurrency V3 - Leveraging Distributed Lock in a Distributed DB Environment