Control Concurrent Updates
Use write versions and Sequence columns to order Unique Key loads. Coordinate overlapping UPDATE jobs separately to avoid lost updates. Choose a mechanism based on the source of concurrent writes:
| User scenario | Pain point | VeloDB Cloud solution |
|---|---|---|
| Multiple imports of the same primary key | Determining which record finally takes effect under concurrent imports | MVCC (Multi-Version Concurrency Control), where the version number determines the overwrite order |
| Out-of-order arrival from multi-threaded synchronization | Older data arrives later and incorrectly overwrites newer data | Sequence column: the user specifies the replacement order, and the row with the larger sequence value wins |
Concurrent UPDATE statements on the same keys | Read-modify-write operations can overwrite another writer's changes | Serialize overlapping jobs or use load-based updates with explicit ordering |
Prepare the environment
Use the warehouse SQL Editor or a connected SQL client. Select a database and use a SQL user with the required privileges. For Stream Load, replace <endpoint> with the warehouse endpoint, including the protocol and port, and <sql-user> with your SQL user. Use the endpoint and TLS options provided for your warehouse connection. curl -u prompts for the password. See Connection Methods.
The load-method snippets use separate example schemas and sources. Replace table names, S3 placeholders, and Kafka connection settings before running them. The displayed results are illustrative.
Order writes with MVCC
VeloDB Cloud uses Multi-Version Concurrency Control (MVCC) to manage concurrent updates:
- Each data write operation is assigned a write transaction, which guarantees the atomicity of the write (either fully succeeds or fully fails).
- When the write transaction commits, the system assigns it a version number.
- When you import data multiple times into a Unique Key table, if duplicate primary keys exist, VeloDB Cloud determines the overwrite order by the version number: data with a higher version number overwrites data with a lower version number.
Control concurrent UPDATE statements
Coordinate overlapping updates
An UPDATE statement reads matching rows and writes modified values. Concurrent statements that read the same row can calculate changes from the same earlier state. Serialize jobs that can update the same keys, especially expressions such as SET counter = counter + 1. Transaction atomicity does not by itself prevent lost updates between separate read-modify-write operations.
Request help with managed configuration
FE configuration is managed by VeloDB Cloud. Do not edit fe.conf or assume that enable_concurrent_update provides serializable isolation for your warehouse. Contact VeloDB Cloud support when you need guidance on concurrent update behavior.
Order updates with a Sequence column
Prevent out-of-order overwrites
Unique Key tables enforce primary key uniqueness. However, relying on the import version number alone is not enough in the following situations:
- When you synchronize data into VeloDB Cloud with multiple concurrent threads, data from different threads may arrive out of order.
- Older data may arrive later and incorrectly overwrite newer data.
- When data with the same primary key is imported in the same batch or across different batches, the replacement order is not guaranteed, so the final result is non-deterministic.
To solve this, VeloDB Cloud supports specifying a Sequence column at import time. For rows with the same Key columns, replacement is performed based on the Sequence column value: a larger value replaces a smaller value, and the reverse does not trigger replacement. This puts the decision about ordering in your hands.
In implementation, VeloDB Cloud adds a hidden column __DORIS_SEQUENCE_COL__. The column type is specified by the user when creating the table, and the value is determined at import time. VeloDB Cloud then uses this value to decide which row wins among rows that share the same Key columns.
Warning:
The Sequence column is currently only supported by the Unique model.
Enable the Sequence column
You can enable it in the following ways:
| Scenario | Operation |
|---|---|
| Enable on a new table | Set function_column.sequence_col or function_column.sequence_type in the PROPERTIES of the CREATE TABLE statement |
| Enable on an existing table | Run ALTER TABLE example_db.my_table ENABLE FEATURE "SEQUENCE_LOAD" WITH PROPERTIES ("function_column.sequence_type" = "Date") |
| Check whether it is enabled | Run SET show_hidden_columns=true, then run DESC tablename. If the output contains __DORIS_SEQUENCE_COL__, it is enabled |
The two properties differ as follows:
| Property | Meaning | Whether the schema must contain a corresponding column |
|---|---|---|
function_column.sequence_col | Maps the Sequence column to an existing column in the table | Yes |
function_column.sequence_type | Specifies only the type of the Sequence column, stored in a hidden column | No |
Supported column types: integer types, DATE, and DATETIME. The column type cannot be changed once the table is created.
Verify Sequence column ordering
The following example uses Stream Load to demonstrate the full workflow.
1. Create a table that supports the Sequence column
Create and select the test database, then create the Unique Key table test_table and map the Sequence column to modify_date:
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE test.test_table
(
user_id bigint,
`date` DATE,
group_id bigint,
modify_date date,
keyword VARCHAR(128)
)
UNIQUE KEY(user_id, `date`, group_id)
DISTRIBUTED BY HASH (user_id) BUCKETS 32
PROPERTIES(
"function_column.sequence_col" = 'modify_date',
"enable_unique_key_merge_on_write" = "true"
);
The following output illustrates the column roles. With the nullable column definitions above, Null is Yes:
MySQL> desc test_table;
+-------------+--------------+------+-------+---------+---------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-------+---------+---------+
| user_id | BIGINT | Yes | true | NULL | |
| date | DATE | Yes | true | NULL | |
| group_id | BIGINT | Yes | true | NULL | |
| modify_date | DATE | Yes | false | NULL | REPLACE |
| keyword | VARCHAR(128) | Yes | false | NULL | REPLACE |
+-------------+--------------+------+-------+---------+---------+
If you do not want this column to appear in the table schema, use function_column.sequence_type to specify the type instead:
PROPERTIES (
"function_column.sequence_type" = 'Date'
);
2. Load data and verify the Sequence behavior
When using the column-mapping approach (function_column.sequence_col), the import command does not need any extra Sequence parameters. Save the following tab-separated data in a file named testData, then use Stream Load to import it:
1 2020-02-22 1 2020-02-21 a
1 2020-02-22 1 2020-02-22 b
1 2020-02-22 1 2020-03-05 c
1 2020-02-22 1 2020-02-26 d
1 2020-02-22 1 2020-02-23 e
1 2020-02-22 1 2020-02-24 b
Stream Load command:
curl --location-trusted -u '<sql-user>' -T testData '<endpoint>/api/test/test_table/_stream_load'
Query result:
MySQL> select * from test_table;
+---------+------------+----------+-------------+---------+
| user_id | date | group_id | modify_date | keyword |
+---------+------------+----------+-------------+---------+
| 1 | 2020-02-22 | 1 | 2020-03-05 | c |
+---------+------------+----------+-------------+---------+
Because 2020-03-05 is the largest value in the Sequence column (modify_date), the keyword column finally keeps c.
3. Specify the Sequence column in different load methods
When the table uses function_column.sequence_type, the Sequence column is hidden and must be mapped to an input field during loading. The following examples use a separate table, not the test_table whose Sequence column maps to modify_date above.
CREATE DATABASE IF NOT EXISTS test;
CREATE TABLE test.test_sequence_hidden
(
k1 BIGINT NOT NULL,
k2 DATE NOT NULL,
v1 BIGINT,
v2 VARCHAR(128)
)
UNIQUE KEY(k1, k2)
DISTRIBUTED BY HASH(k1) BUCKETS 1
PROPERTIES (
"function_column.sequence_type" = "DATE"
);
Prepare sequence_data.tsv with the following tab-separated rows. The fourth input field, source_sequence, supplies the hidden Sequence column. It is not a visible table column.
1 2020-02-22 1 2020-02-22 a
1 2020-02-22 1 2020-03-23 w
Map a Sequence column in Stream Load
Map the input fields in file order and name the Sequence field in the request header:
curl --location-trusted -u '<sql-user>' \
-H "columns: k1,k2,v1,source_sequence,v2" \
-H "function_column.sequence_col: source_sequence" \
-T sequence_data.tsv '<endpoint>/api/test/test_sequence_hidden/_stream_load'
Map a Sequence column in Broker Load
Upload the same file to S3 and replace the source-object and credential placeholders below. Use a tab separator and the same field order, then select the fourth input field with ORDER BY source_sequence:
LOAD LABEL test.sequence_hidden_broker
(
DATA INFILE("s3://<bucket>/<prefix>/sequence_data.tsv")
INTO TABLE `test_sequence_hidden`
COLUMNS TERMINATED BY "\t"
(k1,k2,v1,source_sequence,v2)
ORDER BY source_sequence
)
WITH S3
(
"AWS_ENDPOINT" = "https://s3.<region>.amazonaws.com",
"AWS_REGION" = "<region>",
"AWS_ACCESS_KEY" = "<access-key>",
"AWS_SECRET_KEY" = "<secret-key>"
)
PROPERTIES ("timeout" = "3600");
Map a Sequence column in Routine Load
Create the job with your Kafka broker and topic, then send each row above as a separate tab-separated Kafka message:
CREATE ROUTINE LOAD test.sequence_hidden_routine ON test_sequence_hidden
COLUMNS TERMINATED BY "\t",
COLUMNS(k1,k2,v1,source_sequence,v2),
ORDER BY source_sequence
PROPERTIES
(
"desired_concurrent_number" = "1",
"format" = "csv"
)
FROM KAFKA
(
"kafka_broker_list" = "<kafka-broker>:9092",
"kafka_topic" = "my_topic"
);
After a load succeeds and its data becomes visible, the larger Sequence value (2020-03-23) keeps the row whose v2 is w. The modify_date examples below continue to use the original test_table and its column mapping.
4. Verify the replacement order
Return to test.test_table from step 2. Replace the contents of testData with the following rows and rerun the Stream Load command from that step. All Sequence values are smaller than the existing maximum 2020-03-05:
1 2020-02-22 1 2020-02-22 a
1 2020-02-22 1 2020-02-23 b
Query result:
MySQL [test]> select * from test_table;
+---------+------------+----------+-------------+---------+
| user_id | date | group_id | modify_date | keyword |
+---------+------------+----------+-------------+---------+
| 1 | 2020-02-22 | 1 | 2020-03-05 | c |
+---------+------------+----------+-------------+---------+
Because all Sequence values in this import are smaller than the existing maximum 2020-03-05, the result remains unchanged.
Replace the contents of testData with the following rows and rerun the same Stream Load command to import a larger Sequence value:
1 2020-02-22 1 2020-02-22 a
1 2020-02-22 1 2020-03-23 w
Query result:
MySQL [test]> select * from test_table;
+---------+------------+----------+-------------+---------+
| user_id | date | group_id | modify_date | keyword |
+---------+------------+----------+-------------+---------+
| 1 | 2020-02-22 | 1 | 2020-03-23 | w |
+---------+------------+----------+-------------+---------+
Because the Sequence value 2020-03-23 of the newly imported data is greater than the existing maximum 2020-03-05, the original data is replaced.
Retain the largest Sequence value: During imports, VeloDB Cloud compares the Sequence column values across all batches and writes the record with the largest value into the table.
Check Sequence column constraints
-
Specify the required Sequence input: with
function_column.sequence_col, load the mapped ordinary column using the normal column rules. No extra load-time Sequence mapping parameter is needed. Withfunction_column.sequence_type, configure the source-column mapping for Stream Load or Broker Load as shown above. For a full-rowINSERTwith an explicit target-column list, include the mapped ordinary column (unless its default isCURRENT_TIMESTAMP), or the hidden__DORIS_SEQUENCE_COL__when there is no mapped column. AnINSERTwithout a target-column list already includes the mapped ordinary column. For partial column updates, see item 3 below. Missing a required Sequence input causes the following error:Table test_tbl has sequence column, need to specify the sequence column -
Disable the Sequence column constraint check: in scenarios such as table replication or internal data migration, you can disable the mandatory Sequence column check on
INSERTstatements through a session variable:set require_sequence_in_insert = false; -
Combine Sequence columns with partial updates: Merge-on-Write Unique Key tables support partial column updates. In a partial-column-update import, only some columns can be updated each time, so the Sequence column is not required:
- If the import job includes the Sequence column, the behavior is unchanged.
- If the import job does not include the Sequence column, VeloDB Cloud uses the Sequence column value from the matched historical data as the Sequence column value of the updated row.
- If no record with the same Key exists in the historical data, VeloDB Cloud automatically fills in
nullor the default value.
-
Coordinate concurrent partial updates: when concurrent imports occur, VeloDB Cloud uses MVCC to guarantee data correctness. If two batches of imports both update different columns of the same Key, after the import job with the lower version number succeeds, the import job with the higher version number uses the rows written by the lower-version job for the same Key to fill in the missing values.
For equal Sequence values, do not rely on a deterministic arrival order. Choose a source ordering value that distinguishes successive changes.