`kafka`

Allow configuration`partition.assignment.strategy`

To change the partition strategy of consumer groups.`kafka`

The following partition policies are provided

`RangeAssignor`

`RoundRobinAssignor`

`StickyAssignor`

The default is`RangeAssignor`

At the same time,`kafka`

It also allows us to customize the partition policy by simply inheriting`AbstractPartitionAssignor`

Abstract class is enough.

#### 1、RangeAssignor

There is a consumption section on leave`c1, c2`

, all subscription`t0, t1`

, each`topic`

There are two zones under it`t0p0, t0p1, t1p0, t1p1`

The partition is as follows:

```
c1: t0p0, t1p0
c2: t0p1, t1p1
```

If each`topic`

If there are three partitions, the distribution will be uneven

```
c1: t0p0, t0p1, t1p0, t1p1
c2: t0p2, t1p2
```

The algorithm follows each`topic`

Under the number of partitions, the average score.

#### 2、RoundRobinAssignor

The consumption groups are sorted by dictionary and then allocated by polling.

There is a consumption section on leave`c1, c2`

。 All subscription`t0, t1`

。 each`topic`

There are three zones below.

The division is as follows

```
c1: t0p0, t0p2, t1p1
c2: t0p1, t1p0, t1p2
```

There is a consumption section on leave`c1, c2, c3`

。 among`c1`

subscribe`t0`

, `c2`

subscribe`t1`

, `c3`

subscribe`t0, t1, t2`

, `t0`

There is 1 partition`t0p0`

, `t1`

There are 2 partitions`t1p0`

, `t1p1`

, `t2`

There are three partitions`t2p0, t2p1, t2p3`

The division is as follows

```
c1: t0
c2: t1p0
c3: t1p1, t2p0, t2p1, t2p3
```

#### 3、StickyAssignor

The allocation strategy follows the following two principles

- The partition should be distributed as evenly as possible
- The partition should be assigned as much as possible to the last one

The first condition has a higher priority than the second

There is a consumption section on leave`c1, c2, c3`

。 All subscription`t0, t1`

Each partition has 3 partitions

The division is as follows

```
c1: t0p0, t1p0
c2: t0p1, t1p1
c3: t0p2, t1p2
```

You can see that`RoundRobinAssignor`

The algorithm is similar.

Let’s look at what happens when the partitions of different consumer groups are inconsistent

There is a consumption section on leave`c1, c2, c3`

。 among`c1`

subscribe`t0`

, `c2`

subscribe`t1`

, `c3`

subscribe`t0, t1, t2`

, `t0`

There is 1 partition`t0p0`

, `t1`

There are 2 partitions`t1p0`

, `t1p1`

, `t2`

There are three partitions`t2p0, t2p1, t2p3`

The division is as follows

```
c1: t0p0
c2: t1p0, t1p1
c3: t2p0, t2p1, t2p3
```

Stick it here`RoundRobinAssignor`

The partition of the algorithm is compared

```
c1: t0p0
c2: t1p0
c3: t1p1, t2p0, t2p1, t2p3
```

You can see`StickyAssignor`

The allocation ratio of the algorithm`RoundRobinAssignor`

Better. namely`The partition should be distributed as evenly as possible`

Suppose C1 quits the subscription. What happens to partition allocation at this time?

```
c2: t0p0, t1p0, t1p1
c3: t2p0, t2p1, t2p3
```

#### 4. Summary

`RocketMQ`

proposal**The consumer group subscribes to only one topic**In my actual development process, it is basically the same. If you subscribe to more than one`topic`

, the consumer group will not work properly. about`kafka`

For example, a consumer group can subscribe to more than one`topic`

It’s really flexible.