Heuristic:4paradigm OpenMLDB Partition Balancing Strategy
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Distributed_Systems |
| Last Updated | 2026-10-12 16:00 GMT |
Overview
Choose partition_num and replica_num based on cluster size, throughput needs, and fault tolerance requirements to balance data distribution and query parallelism.
Description
OpenMLDB tables are partitioned for parallelism and replicated for fault tolerance. The default configuration is partition_num=8 and replica_num=3 (1 leader + 2 followers). The system_table_replica_num defaults to 1 for internal tables. The openmldb_ops.py tool provides scaleout and scalein commands that rebalance partitions across tablet servers using a greedy algorithm: it finds the tablet with the most partitions and migrates to the tablet with the fewest, repeating until balanced (difference <= 1).
Key considerations:
- More partitions = more parallelism but more overhead (each partition is a separate RocksDB instance).
- More replicas = better fault tolerance but more storage and write amplification.
- ScaleIn requires
replica_num <= remaining_tablets; otherwise the operation fails. - Pre-upgrade with single-replica tables (
replica_num=1) requires adding a temporary replica first (unless--allow_single_replicais set).
Usage
Use this heuristic when creating tables or scaling an OpenMLDB cluster. The partition and replica numbers should be chosen based on the number of tablet servers, expected throughput, and fault tolerance requirements.
The Insight (Rule of Thumb)
- Action: Set
partition_numandreplica_numat table creation, or usescaleout/scaleinfor rebalancing. - Value:
partition_num: Default 8. Set to ~2x the number of tablet servers for even distribution. More partitions = more parallel query slots.replica_num: Default 3 (1 leader + 2 followers). Use 1 for dev/testing, 3 for production. Must be <= number of tablet servers.system_table_replica_num: Default 1 (internal tables only).
- Trade-off:
- More partitions: Higher throughput and parallelism, but more memory overhead and metadata.
- More replicas: Higher availability and read throughput, but more storage and slower writes.
- Scaling constraint:
replica_nummust be <=len(alive_endpoints)after scale-in, or the operation will fail.
Reasoning
Partition count determines the maximum parallelism for a single table — each partition can serve queries independently. With 8 default partitions and 3 tablets, each tablet handles ~3 partitions, providing good parallelism. When scaling out to more tablets, the BalanceInDatabase function migrates partitions from the most-loaded to least-loaded tablet until the difference is at most 1. When scaling in, the tool checks that replica_num <= remaining_endpoints for every partition key — if any table has more replicas than remaining tablets, the scale-in is rejected. For rolling upgrades, single-replica tables need a temporary replica added so the leader can be migrated before the tablet is taken offline.
Code Evidence
Default partition and replica flags from `src/flags.cc:64-66`:
DEFINE_uint32(partition_num, 8, "config the default partition_num");
DEFINE_uint32(replica_num, 3, "config the default replica_num. if set 3, there is one leader and two followers");
DEFINE_uint32(system_table_replica_num, 1, "config the default replica_num of system table.");
Balance algorithm from `tools/openmldb_ops.py:289-300`:
for endpoint in endpoints:
if len(all_dict[endpoint]) > len(all_dict[migrate_out_endpoint]):
migrate_out_endpoint = endpoint
if len(all_dict[endpoint]) < len(all_dict[migrate_in_endpoint]):
migrate_in_endpoint = endpoint
log.info("max partition endpoint: {migrate_out_endpoint} num: {num}, ".format(
migrate_out_endpoint=migrate_out_endpoint, num=len(all_dict[migrate_out_endpoint])) +
"min partition endpoint: {migrate_in_endpoint} num: {num}".format(
migrate_in_endpoint=migrate_in_endpoint, num=len(all_dict[migrate_in_endpoint])))
if not len(all_dict[migrate_out_endpoint]) > len(all_dict[migrate_in_endpoint]) + 1:
break
ScaleIn replica constraint from `tools/openmldb_ops.py:374-379`:
for key, value in replica_map.items():
if value > len(desc_endpoints):
db, name = db_map[key]
log.error("replica num of table {name} in {db} is {value}, left endpoints num is {len}, cannot execute scale-in".format(
name=name, db=db, value=value, len=len(desc_endpoints)))
return Status(-1, "cannot execute scale-in")
Pre-upgrade single-replica handling from `tools/openmldb_ops.py:501-519`:
one_replica = replica_map[partition.GetKey()] == 1
if one_replica:
if allow_single_replica:
continue
# select the tablet with min_partition_num to add replica to
min_partition_num = sys.maxsize
for cur_endpoint in all_dict:
if cur_endpoint == endpoint: continue
if len(all_dict[cur_endpoint]) < min_partition_num:
min_partition_num = len(all_dict[cur_endpoint])
desc_endpoint = cur_endpoint