# Merge

+ Two regions are merged into one Region
+ Why merge
  + Data can be deleted
  + Too much regions implies cost (e.g. `heartbeat`, `Region cache`, memory)
  + Transaction in the same `Region` can be optimized by `1PC`
+ When to merge
  + If the size of `Region` is too small
  + The key count of `Region` is not many
+ Two phase merge
  + **`Region 2`** merges into **`Region 1`** 
  + Move replicas of both regions to the same nodes
  + **`Region 2`** starts merging, stops read/write operations
  + **`Region 1`** deletes metadata of **`Region 2`**, and extends its key range
  + **`Version = max(Region 1, Region 2) + 1`** 

![`smart`](RaftRegionMerge_aa_001_20221023.png)

------------------------------------------------------------

