Optimization method and system for RocksDB memory management
By hierarchically dividing and dynamically allocating RocksDB memory, combined with the cyclic refresh strategy, the inefficiency and write amplification problems caused by static memory allocation are solved, and more efficient memory utilization and system performance are achieved.
Patent Information
- Application Number
- CN202410877054.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-02
- Publication Date
- 2024-10-22
AI Technical Summary
The existing RocksDB memory management uses a static allocation scheme, which results in low memory utilization and cannot effectively adapt to different workloads, resulting in low system efficiency. There are also optimization difficulties in the allocation between disk buffers and memory components, especially when workloads fluctuate. It is difficult to react correctly.
By dividing the memory space hierarchically according to the data structure of LSM Tree, using dynamic allocation and cyclic refresh mechanism, following the strategy of LSM Tree when merging data, ensuring the logical and physical sequence consistency of the data, and selecting the smallest data when refreshing the disk. The SSTable of log sequence numbers adopts a cyclic refresh strategy to improve memory utilization.
It improves memory usage efficiency, reduces write memory fragmentation, improves losses caused by write amplification issues, and ensures efficient operation of the system under different workloads and continuous utilization of memory.
Smart Images

Figure CN118819839A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of database memory allocation optimization, and in particular to a method and system for optimizing RocksDB memory management. Background Art
[0002] Log-structured merge trees, or LSMTrees, are now widely used in modern NoSQL systems. Unlike traditional local update structures, LSMTrees first buffer all write operations in memory when storing data, and then flush them to disk when the memory is full. Therefore, the components on disk are usually organized into exponentially growing levels, where smaller levels are merged into adjacent larger levels. In order to bind the temporary disk space occupied by merges, modern LSMTree implementations usually divide the partitions of disk components into many fixed-size storage spaces.
[0003] Efficient memory management is critical for the index structure to achieve high performance. Compared with the update replacement system where all pages are managed in the buffer pool, LSMTree generates an additional memory wall. Memory components are allocated a fixed size, which results in the active data set only being allocated a fixed portion of the limited write memory. However, the optimal memory allocation depends largely on the workload, so memory management must also adapt to the workload to maximize system efficiency. Therefore, the possible negative impact on the system caused by fixed or evenly allocated memory is reduced by means of an adaptive memory management structure.
[0004] LSMTree is used to dynamically plan and allocate memory to achieve the effect of adaptive memory management. RocksDB optimizes the fixed memory partitioning to a tuner that dynamically adjusts write memory and buffer cache memory allocation according to workload based on the storage memory management structure of LSMTree.
[0005] However, existing LSMtree implementations, such as RocksDB, use a static memory allocation scheme. Although this allocation method is relatively simple, it results in a waste of efficiency. This situation will cause the following problems:
[0006] 1. Memory allocation between multiple LSM trees. LSM-based storage systems usually contain multiple LSM trees to represent different data sets and secondary indexes. Each LSM tree can have different write characteristics, so it is very important to correctly allocate and release their memory components to maximize system efficiency.
[0007] 2. Manage large memory components. Most existing LSM Tree implementations use monolithic B+ trees or skip lists to manage their memory components, so when the memory is full, they are all refreshed. For larger memories, memory components need to be refreshed continuously, minimizing blocking as much as possible to ensure maximum memory utilization. In addition, since write amplification is determined by the number of disk levels, it may be necessary to adjust the disk structure to reduce write amplification.
[0008] 3. Memory allocation between disk buffer cache and memory components. When RocksDB is used, memory needs to be taken into account both the disk buffer memory and the memory components in it. The system must decide how to allocate between the two. This can be expressed as an optimization problem. However, the challenge lies in accurately modeling workload characteristics, especially when the workload is tilted, and ensuring that the system responds correctly when the workload fluctuates. Summary of the invention
[0009] The purpose of the present invention is to provide a method and system for optimizing RocksDB memory management to solve the problems raised in the above background technology.
[0010] To achieve the above object, the present invention provides the following technical solution: a method for optimizing RocksDB memory management, the method comprising the following steps:
[0011] Step 1: Divide the memory space into layers according to the data structure of LSM Tree;
[0012] Step 2: dynamically allocate storage space in memory in a hierarchical manner to adapt to the memory requirements of different scenarios;
[0013] Step 3: When the storage space of the previous layer of LSM Tree meets the preset merging conditions, the data of this layer is merged through SSTable and transferred to the next layer;
[0014] Step 4: In memory, merge the data according to the structure of LSM Tree;
[0015] Step 5: Refresh the bottom-level data of the LSM Tree to disk for persistent storage;
[0016] Step 6: cyclically execute steps 3 to 5 to implement cyclic refreshing from memory to disk.
[0017] Preferably, the hierarchical division of the LSM Tree dynamically utilizes the memory space, avoids memory fragmentation caused by memory slicing, and improves memory usage efficiency.
[0018] Preferably, during the data writing process, the data is first initially merged in the memory, and the merging strategy of LSMTree is followed during the merging to ensure the consistency of the logical order of the data and the physical storage order.
[0019] Preferably, when a layer of data in the LSM Tree meets the refresh condition, the SSTable with the smallest log sequence number in the layer is selected as the refresh object, and its data is refreshed to the disk to ensure the timing and integrity of the data.
[0020] Preferably, in the process of refreshing data to disk, a cyclic refresh mechanism is adopted to refresh the bottom-level data each time, and reincorporate the refreshed space into the memory management cycle of the LSM Tree to achieve continuous write operations and efficient use of memory space.
[0021] A system for optimizing RocksDB memory management, the system comprising a data stratification module, a storage allocation module, a data merging module and a data storage module;
[0022] The data stratification module divides the memory space into layers according to the data structure of the LSM Tree;
[0023] The storage allocation module dynamically allocates storage space in memory in a hierarchical manner to adapt to the memory requirements of different scenarios;
[0024] The data merging module merges the data in the previous layer of the LSM Tree through SSTable and transfers it to the next layer when the storage space in the previous layer of the LSM Tree meets the preset merging conditions. In the memory, the data is merged according to the structure of the LSM Tree.
[0025] The data storage module refreshes the bottom-level data of the LSM Tree to the disk for persistent storage.
[0026] Preferably, the data stratification module, the stratified division of the LSM Tree, dynamically utilizes the memory space, avoids memory fragmentation caused by memory slicing, and improves the efficiency of memory use.
[0027] Preferably, the data merging module, during the data writing process, first performs a preliminary merge of the data in the memory, and follows the merge strategy of the LSM Tree during the merge to ensure the consistency of the logical order of the data and the physical storage order.
[0028] Preferably, when a layer of data in the LSM Tree meets the refresh condition, the data storage module selects the SSTable with the smallest log sequence number in the layer as the refresh object, and refreshes its data to the disk to ensure the timing and integrity of the data.
[0029] Preferably, the data storage module adopts a cyclic refresh mechanism during the process of refreshing data to the disk, refreshing the bottom-level data each time and reincorporating the refreshed space into the memory management cycle of the LSM Tree to achieve continuous write operations and efficient use of memory space.
[0030] Compared with the prior art, the present invention has the following beneficial effects:
[0031] The present invention proposes a method and system for optimizing RocksDB memory management, which reduces the fragmentation of write memory and better calls the memory computing power through a partitioned memory component structure and a new disk flushing strategy, so as to better utilize the write memory and improve the loss caused by the write amplification problem. The memory is dynamically allocated and managed in the form of LSMTree to avoid the negative impact of performance waste or low utilization caused by the inability of the average memory allocation to adapt to various read-write application scenarios, thereby improving the efficiency of memory use. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 The figure is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0033] In order to make the purpose and technical solution of the present invention clearly and completely described, and the advantages more clearly understood, the embodiments of the present invention are further described in detail with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are part of the embodiments of the present invention, rather than all of the embodiments, and are only used to explain the embodiments of the present invention, and are not used to limit the embodiments of the present invention. All other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0034] Embodiment 1
[0035] See also Figure 1 The present invention provides a technical solution: a method for optimizing RocksDB memory management, the method comprising the following steps:
[0036] Step 1: Divide the memory space into layers according to the data structure of the LSM Tree. The layered division of the LSM Tree dynamically utilizes the memory space, avoids memory fragmentation caused by memory slicing, and improves memory usage efficiency.
[0037] Step 2: Dynamically allocate storage space in memory in a hierarchical mode to adapt to the memory requirements of different scenarios. During the data writing process, the data is first initially merged in memory, following the LSM Tree merge strategy to ensure the consistency of the logical order of the data and the physical storage order.
[0038] Step 3: When the storage space of the previous layer of LSM Tree meets the preset merging conditions, the data of this layer is merged through SSTable and transferred to the next layer;
[0039] Step 4: In memory, merge the data according to the structure of LSM Tree;
[0040] Step 5, refresh the bottom-level data of the LSM Tree to the disk for persistent storage; when a layer of data in the LSM Tree meets the refresh conditions, select the SSTable with the smallest log sequence number in the layer as the refresh object, and refresh its data to the disk to ensure the timing and integrity of the data; in the process of refreshing data to the disk, a cyclic refresh mechanism is adopted to refresh the bottom-level data each time, and the refreshed space is re-incorporated into the memory management cycle of the LSM Tree to achieve continuous write operations and efficient use of memory space.
[0041] Step 6: cyclically execute steps 3 to 5 to implement cyclic refreshing from memory to disk.
[0042] Embodiment 2
[0043] Based on the first embodiment, an optimization solution is proposed to break the memory wall in the LSM-based storage system and dynamically allocate the memory management allocation of the RocksDB database.
[0044] Write memory management:
[0045] The solution optimizes the management of write memory in two ways: (1) instead of setting a size limit for each memory component, all memory components are managed through a shared memory pool; (2) dynamically flushing the components of write memory (i.e., SSTable files) to disk based on different workloads. When an LSMTree does not have enough memory to store incoming write operations, it will request more pages from the pool, and when the system's overall write memory usage is too high, an LSMTree will be selected to flush its memory components to disk.
[0046] Partitioned memory components:
[0047] With the new memory management structure, the memory component can become very large because its size is not bounded. The existing LSMTree implementation uses a skip list or B+ tree to manage the memory component and always flushes the memory component completely to disk. This design reduces memory usage for two reasons:
[0048] First, because about 2 / 3 of the B+ tree pages are full, there is internal fragmentation in the updateable B+ tree when it is in use; second, consider that after a large memtable object is flushed to disk, the memory is released, and the new memtable has low memory utilization in the previous period of time, which can reduce the average memory utilization over time.
[0049] In order to maximize the use of memory, the paper proposes to use a memory-partitioned LSM-tree to manage write memory for the memory component of a single LSM-tree, that is, write memory is also layered. It can be divided into three layers: M0, M1, and M2. There is an active SSTable at M0 to store incoming write operations. When M1 is full, one of its SSTables will be merged to the next layer M1 using memory. When selecting the SST to merge, a greedy selection strategy is used to select SSTables for merging by minimizing the ratio between the size of the overlapping SSTables at the M1 layer and the size of the selected SSTable at M0.
[0050] The memory SSTables of the M2 layer are eventually flushed to disk. For refreshes triggered by the full memory component, the SSTables of M2 will be flushed to disk in a round-robin manner. For refreshes triggered by the length of the transaction log being too long, the SSTable with the minimum log sequence number (LSN) will be flushed to disk.
[0051] Such a structure improves memory utilization and reduces write amplification. First, LSMTree can achieve higher space utilization than B+ tree; second, since the proposed structure is range partitioned, it can use partial refresh, refreshing one memory SSTable at a time, so that the write memory can always be kept full.
[0052] Multi-LSMTree memory management:
[0053] When managing multiple LSMTrees, the fundamental problem that needs to be faced is how to allocate write memory to these LSMTrees. Since write memory is allocated on demand, the problem becomes how to select the LSMTree to be flushed to disk. For log-triggered refreshes, the LSMTree with the smallest LSN (Log Sequence Number) should be refreshed to perform log truncation; for memory-triggered refreshes, existing LSMTree implementations (such as RocksDB and HBase) choose to refresh the LSMTree with the largest memory component. The intuitive feeling is that refreshing this LSMTree can reclaim the most write memory, which can be used for subsequent write operations. However, this strategy may not be suitable for partitioned memory components, because refreshing any LSMTree will reclaim the same amount of write memory due to partial SSTable flushes.
[0054] In this case, an optional method is the minimum log sequence number strategy, that is, each time the oldest LSN is selected for disk flushing. In this way, the refresh rate of LSMTree will be roughly proportional to its write rate, and hotter LSMTree data structures will be refreshed more frequently than colder LSMTree data structures. In addition, when the refresh is dominated by log-triggered refresh, such a strategy also helps log truncation.
[0055] At this time, this optimization scheme proposes an optimal disk flushing strategy: when K LSMTrees are given, the allocation of write memory can be defined as an optimization problem of minimizing write cost, that is, by minimizing the ratio of the write rate of the i-th LSMTree to the size of each entry, multiplied by the write cost of each entry, such a formula can achieve the optimal write memory allocation, as shown in the following formula ①.
[0056]
[0057] where a i is the memory allocation ratio of the i-th LSM-tree, r i is the write rate of the i-th LSM-tree, Ci is the write I / O cost of each entry. i It can be calculated by the sum of the cost of writing the log for the first time and the cost of each merge, as shown in the following formula ②:
[0058]
[0059] where e i is the entry size, P is the disk page size, T is the size ratio of the merge strategy, |L i | is the size of the i-th layer, M Wis the total write memory size. Substitute equation ② into equation ①, and then use the Lagrange multiplier method to get the following equation ③:
[0060]
[0061] That is, the optimal memory allocation ratio of the i-th LSMTree is equal to the ratio of its write rate to the write rate of all LSMTrees. In other words, the write memory allocated to the LSMTree should be proportional to its write rate, and the hotter LSMTree will get more write memory than the colder LSM-tree.
[0062] Embodiment 3
[0063] Based on the second embodiment, a system for optimizing RocksDB memory management is proposed, the system is composed of a data stratification module, a storage allocation module, a data merging module and a data storage module;
[0064] The data stratification module divides the memory space into layers according to the data structure of the LSM Tree. The layered division of the LSM Tree dynamically utilizes the memory space, avoids memory fragmentation caused by memory slicing, and improves memory usage efficiency.
[0065] The storage allocation module dynamically allocates storage space in memory in a hierarchical manner to adapt to the memory requirements of different scenarios;
[0066] Data merging module: when the storage space of the previous layer of LSM Tree meets the preset merging conditions, the data of this layer will be merged through SSTable and transferred to the next layer; in memory, the data will be merged according to the structure of LSM Tree; in the process of writing data, the data will be initially merged in memory, and the merging strategy of LSM Tree will be followed to ensure the consistency of the logical order of data and the physical storage order.
[0067] The data storage module refreshes the bottom-level data of the LSM Tree to the disk for persistent storage. When a layer of data in the LSMTree meets the refresh conditions, the SSTable with the smallest log sequence number in the layer is selected as the refresh object, and its data is refreshed to the disk to ensure the timing and integrity of the data. In the process of refreshing data to the disk, a cyclic refresh mechanism is adopted to refresh the bottom-level data each time, and the refreshed space is re-incorporated into the memory management cycle of the LSM Tree to achieve continuous write operations and efficient use of memory space.
[0068] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for optimizing RocksDB memory management, characterized by: The method comprises the following steps: Step 1: Divide the memory space into layers according to the data structure of LSM Tree; Step 2: dynamically allocate storage space in memory in a hierarchical manner to adapt to the memory requirements of different scenarios; Step 3: When the storage space of the previous layer of LSM Tree meets the preset merging conditions, the data of this layer is merged through SSTable and transferred to the next layer; Step 4: In memory, merge the data according to the structure of LSM Tree; Step 5: Refresh the bottom-level data of the LSM Tree to disk for persistent storage; Step 6: cyclically execute steps 3 to 5 to implement cyclic refreshing from memory to disk.
2. The method for optimizing RocksDB memory management according to claim 1, characterized in that: The hierarchical division of LSM Tree dynamically utilizes memory space, avoids memory fragmentation caused by memory slicing, and improves memory usage efficiency.
3. The method for optimizing RocksDB memory management according to claim 1, characterized in that: During the data writing process, the data is first initially merged in memory, following the LSM Tree merge strategy to ensure the consistency of the logical order of the data and the physical storage order.
4. The method for optimizing RocksDB memory management according to claim 1, characterized in that: When the data in a certain layer of LSMTree meets the refresh conditions, the SSTable with the smallest log sequence number in this layer is selected as the refresh object, and its data is refreshed to the disk to ensure the timeliness and integrity of the data.
5. The method for optimizing RocksDB memory management according to claim 1, characterized in that: In the process of refreshing data to disk, a cyclic refresh mechanism is adopted to refresh the bottom-level data each time and reincorporate the refreshed space into the memory management cycle of the LSM Tree to achieve continuous write operations and efficient use of memory space.
6. A system for optimizing RocksDB memory management according to the method for optimizing RocksDB memory management according to any one of claims 1 to 5, characterized in that: The system is composed of a data stratification module, a storage allocation module, a data merging module and a data storage module; The data stratification module divides the memory space into layers according to the data structure of the LSM Tree; The storage allocation module dynamically allocates storage space in memory in a hierarchical manner to adapt to the memory requirements of different scenarios; Data merging module: when the storage space of the previous layer of LSM Tree meets the preset merging conditions, the data of this layer is merged through SSTable and transferred to the next layer; In memory, data is merged according to the LSM Tree structure; The data storage module refreshes the bottom-level data of the LSM Tree to the disk for persistent storage.
7. The system for optimizing RocksDB memory management according to claim 6, characterized in that: The data stratification module, the stratified division of the LSM Tree, dynamically utilizes the memory space, avoids memory fragmentation caused by memory slicing, and improves the memory usage efficiency.
8. The system for optimizing RocksDB memory management according to claim 6, characterized in that: The data merging module, during the data writing process, first performs a preliminary merging of data in the memory, and follows the merging strategy of the LSM Tree during the merging to ensure the consistency of the logical order of the data and the physical storage order.
9. The system for optimizing RocksDB memory management according to claim 6, characterized in that: The data storage module, when a layer of data in the LSM Tree meets the refresh condition, selects the SSTable with the smallest log sequence number in the layer as the refresh object, and refreshes its data to the disk to ensure the timeliness and integrity of the data.
10. The system for optimizing RocksDB memory management according to claim 6, characterized in that: The data storage module adopts a cyclic refresh mechanism during the process of refreshing data to the disk, refreshing the bottom-level data each time, and reincorporating the refreshed space into the memory management cycle of the LSM Tree to achieve continuous write operations and efficient use of memory space.
Citation Information
Cited By
LSM-Tree structure storage writing performance optimization method and system
CN119556868A
Enterprise database data processing method and system
CN119690964A
Data optimization storage method and equipment based on storage medium merging
CN121455425A
