Why are there duplicate keys in level 0 of RocksDB, but not in level 1 and beyond?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 为什么rockdb里的level 0存在重复key,level 1及以后的N就没有

| username: 近墨者zyl

There is duplicate data in the level 0 SST file, I don’t understand why.

| username: Jiawei | Original post link

Secondly, RocksDB is based on the LSM tree engine, and the write process is as shown in the figure above. When writing, data is directly appended to the memtable. Once the memtable is full, it becomes an immutable memtable. During the writing process, operations such as adding 2 to key ‘a’ and deleting 2 may occur, resulting in two separate records being written.

Then, the LSM tree will perform compaction, which is divided into two types:

  • Minor compaction: This converts the immutable memtable in memory into an L0 level sstable. At this point, the sstable content is the same as the immutable memtable in memory.
  • Major compaction: This occurs from L0 to L1 and involves merging the L0 level sstables. The main task here is to merge multiple versions and sort the data.

Therefore, the L0 level may contain duplicates and be unordered, while levels from L1 downward are ordered.

| username: 近墨者zyl | Original post link

The key contains the TSO time, so how can it be duplicated? Adding first and then deleting, the TSO is different, so it’s not a duplication.

| username: 张雨齐0720 | Original post link

TSO does not guarantee non-duplicate data. It is just a sequence identifier for transaction start and commit. It is not a guarantee of non-duplicate data. It is normal for the same key to have different versions in L0.

| username: 裤衩儿飞上天 | Original post link

Is there a TSO in the key? I understand there isn’t.

| username: TiDBer_jYQINSnf | Original post link

Refer to this.

| username: TiDBer_jYQINSnf | Original post link

Here’s the link:

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.