With the development of online transactions and the e-commerce industry, the high-concurrency pressure on hotspot data in business systems has become a challenge. Frequent balance updates on hot accounts within a short period, or flash sales of hot products during marketing campaigns, are direct manifestations of this scenario. The essence of hotspot updates is the highly concurrent modification of certain field values (such as balance or inventory) in the same row of a database within a brief timeframe. The bottleneck lies in the fact that, to maintain transaction consistency, a relational database must follow a process of "lock acquisition -> update -> log writing and commit -> lock release" for each row update, which is essentially serial. Therefore, the key to improving hot row update capability is to minimize the lock hold time as much as possible.
Although the "Early Lock Release" (ELR) technique was proposed in academia long ago, its complex exception-handling scenarios have resulted in few mature industrial implementations. OceanBase Database has addressed this issue through continuous exploration, proposing a distributed architecture-based implementation to enhance concurrent single-row update capability in similar business scenarios. This ELR feature stands as one of the key capabilities of OceanBase Database's "Scalable OLTP."
In this article, we will demonstrate how to use the ELR feature of OceanBase Database and compare its performance by constructing a scenario of highly concurrent single-row updates. Since verification occurs under high-concurrency stress, we recommend using node specifications at least equivalent to those in this example for a better experience and results. The design and implementation principles of OceanBase Database's ELR are relatively complex and will not be discussed in detail here.
For this example, we use a node with a 16C-128GB configuration. Let's proceed step by step to experience the ELR feature of OceanBase Database.
Step 1: Create a test table and insert test data
First, create a table in the test database and insert test data.
CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
INSERT INTO sbtest1 VALUES(1,0,'aa','aa');
In this example, we use a statement like UPDATE sbtest1 SET k=k+1 WHERE id=1 to perform concurrent updates on the k column via primary key query. You can insert more data for testing, but since this is a stress test targeting a single row, it has little impact on the overall result.
Step 2: Construct a concurrent update scenario
In this example, we use Python multithreading to simulate concurrent updates. We start 50 threads simultaneously, each concurrently incrementing the value of the k field by 1 for the row with id=1. You can directly use the following script ob_elr.py for testing in your own environment by modifying the database connection information within the script.
#!/usr/bin/env python3
from concurrent.futures import ThreadPoolExecutor
import pymysql
import time
import threading
# database connection info
config = {
'user': 'root@test',
'password': '****',
'host': 'xxx.xxx.xxx.xxx',
'port': 2881,
'database': 'test'
}
# parallel thread and updates in each thread
parallel = 50
batch_num = 2000
# update query
def update_elr():
update_hot_row = ("update sbtest1 set k=k+1 where id=1")
cnx = pymysql.connect(**config)
cursor = cnx.cursor()
for i in range(0,batch_num):
cursor.execute(update_hot_row)
cursor.close()
cnx.close()
start=time.time()
with ThreadPoolExecutor(max_workers=parallel) as pool:
for i in range(parallel):
pool.submit(update_elr)
end = time.time()
elapse_time = round((end-start),2)
print('Parallel Degree:',parallel)
print('Total Updates:',parallel*batch_num)
print('Elapse Time:',elapse_time,'s')
print('TPS on Hot Row:' ,round(parallel*batch_num/elapse_time,2),'/s')
Step 3: Execute the test with default configurations
As a baseline, we first test with ELR disabled by default. Execute the ob_elr.py script directly on the test machine.
In this example, we use 50 concurrent threads to perform a total of 100,000 updates.
./ob_elr.py
After execution, the test script outputs the execution time and TPS:
[root@obce00 ~]# ./ob_elr.py
Parallel Degree: 50
Total Updates: 100000
Elapse Time: 54.5 s
TPS on Hot Row: 1834.86 /s
The test results are as follows:
With ELR disabled under the default configuration, the TPS for concurrent single-row updates in this test environment is 1834.86/s.
Step 4: Enable the ELR configuration in OceanBase Database
Next, we enable the hot row feature in OceanBase Database. First, log in to the sys tenant of the cluster as the root user.
[root@obce00 ~]# obclient -h127.0.0.1 -P2881 -uroot@sys -Doceanbase -A -p -c
Then set the following two parameters. The effective scope of the enable_early_lock_release parameter can be specified for a specific tenant or all tenants, i.e., tenant=all.
ALTER SYSTEM SET _max_elr_dependent_trx_count = 1000;
ALTER SYSTEM SET enable_early_lock_release=true tenant= test;
Step 5: Enable ELR in OceanBase Database for testing
After enabling the hot row feature, we run the test again. Before execution, check the record with id=1 in table sbtest1. The value of the k field is 100,000 because 100,000 updates were just performed under the default configuration.
SELECT * FROM sbtest1 WHERE id=1;
+----+--------+----+-----+
| id | k | c | pad |
+----+--------+----+-----+
| 1 | 100000 | aa | aa |
+----+--------+----+-----+
1 row in set
Next, execute ob_elr.py again on the test machine. Still use 50 concurrent threads and update a total of 100,000 times.
./ob_elr.py
After execution, the test script outputs the execution time and TPS:
[root@obce00 ~]# ./ob_elr.py
Parallel Degree: 50
Total Updates: 100000
Elapse Time: 12.16 s
TPS on Hot Row: 8223.68 /s
The test results are as follows:
After enabling ELR in OceanBase Database, the TPS for single-row updates reaches 8223.68/s, which is approximately 3.5 times higher than under the default configuration.
We can also see that the k value in table sbtest1 is 200,000, indicating that 100,000 updates were performed this time as well.
SELECT * FROM sbtest1 WHERE id=1;
+----+--------+----+-----+
| id | k | c | pad |
+----+--------+----+-----+
| 1 | 200000 | aa | aa |
+----+--------+----+-----+
1 row in set
This example only covers the scenario of concurrent single-row updates. OceanBase Database's ELR capability also supports concurrent updates for multi-statement transactions. Significant performance improvements can be achieved depending on the number of statements and the scenario.
Furthermore, OceanBase Database's ELR can be applied in multi-region deployment scenarios with high network latency. For example, if a single transaction takes 30 ms under normal conditions, enabling ELR in a concurrent scenario can result in nearly a hundredfold increase in TPS throughput.
Since OceanBase Database's log protocol is built on Multi-Paxos and optimizes the 2PC commit process, transaction consistency is still guaranteed after enabling ELR, even if a node fails and restarts or a leader switchover occurs. You can try constructing these experiments to experience it firsthand.