본문 바로가기

미들웨어/Kafka

Kafka partition에 message저장 방식

반응형

이번 포스팅에서는 message의 병렬처리를 위해 다수의 partition을 생성할 때 partitioner에 따른 동작 방식을 비교해 본다.


1. sticky partitioner(key값이 존재할 때)

  • 3개의 partitions을 갖는 topic을 생성 후 message를 전송한다.
[webwas@ip-172-31-39-9 config]$ kafka-topics.sh --create \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --partitions 3 \
> --replication-factor 1 \
> --topic sticky-test
Created topic sticky-test.
  • partition을 3개 갖는 partition-test라는 topic을 생성한다.
[webwas@ip-172-31-39-9 ~]$ kafka-topics.sh --describe \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --topic sticky-test
Topic: sticky-test      TopicId: Zg-iHSQtTVeXr-X2FEV4dw PartitionCount: 3       ReplicationFactor: 1    Configs: 
        Topic: sticky-test      Partition: 0    Leader: 1       Replicas: 1     Isr: 1
        Topic: sticky-test      Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: sticky-test      Partition: 2    Leader: 2       Replicas: 2     Isr: 2
  • partition이 3개 생성된 것을 확인할 수 있다.

 

  • partition-test topic에 1~9까지의 수를 전송한다.
[webwas@ip-172-31-39-9 config]$ kafka-console-producer.sh \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --topic sticky-test
>1
>2
>3
>4
>5
>6
>7
>8
>9

 

  • consumer를 통해 저장된 데이터를 가져온다.
[webwas@ip-172-31-39-9 config]$ kafka-console-consumer.sh \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --topic sticky-test \
> --from-beginning 
1
2
3
4
5
6
7
8
9
  • 메시지가 전송된 순서대로 가져와진다.

 

  • leader인 partition1에 저장된 message만 가져와 본다.
[webwas@ip-172-31-39-9 config]$ kafka-console-consumer.sh \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --topic sticky-test \
> --from-beginning \
> --partition 1
1
2
3
4
5
6
7
8
9
  • 실제로 partition 1번에만 메시지가 전송되었다.
  • sticky partitioner를 사용할 경우 message를 담을 수 있는 하나의 공간(record batch)이 다 찰 때 까지 하나의 partition만 사용하며 record batch의 size는 batch-size 옵션으로 설정 가능하다.(default : 16384)
  • kafka2.4버전 이후부터 sticky partitioner가 default가 되었다.

 

2. RoundRobin partitioner(key값이 존재하지 않을 때)

  • 동일하게 3개의 partitions을 갖는 topic을 생성 후 message를 전송한다.
[webwas@ip-172-31-39-9 config]$ kafka-topics.sh --create \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --partitions 3 \
> --replication-factor 1 \
> --topic roundrobin-test
Created topic roundrobin-test.

 

  • partitoner는 producer API를 사용하는 시점에 결정된다. 즉 producer를 통해 message를 전송할 때 결정된다.
  • RoundRobin partitioner를 사용해 message를 전송하겠다.
[webwas@ip-172-31-39-9 config]$ kafka-console-producer.sh \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --topic roundrobin-test \
> --producer-property partitioner.class=org.apache.kafka.clients.producer.RoundRobinPartitioner
>1
>2
>3
>4
>5
>6
>7
>8
>9
  • kafka-console-producer.sh 실행시 옵션값으로 partitioner를 RoundRobinPartitioner로 변경한다.

 

  • roundrobin-test topic의 message를 조회한다.
[webwas@ip-172-31-39-9 config]$ kafka-console-consumer.sh \
> --bootstrap-server localhost:9092,localhost:9093,localhost:9094 \
> --topic roundrobin-test \
> --from-beginning
2
4
9
1
3
6
8
5
7
  • 순서가 보장되지 않는다.
  • 각 partition별로 저장된 messsage를 확인해 보면 아래와 같다.
partition 0 1 2 3
partition 0 2 4 9  
partition 1 5 7    
partition 2 1 3 6 8
  • kafka2.4 이전 버전에서는 default partitioner로 roundrobin partitioner를 사용했다.

 

반응형

key값의 존재여부, message순서 보장의 필요성 등의 상황에 따라 partitioner를 선택해 사용하면 될 것 같다.

producer는 partition에 message를 전송하기 전 Accumulator에 message를 버퍼로 쌓아놓고 전송한다.

sticky partitioner를 사용할 경우 Accumulator의 버퍼를 채워서 보내는 이점이 있어 default값으로 변경되었다.

반응형

'미들웨어 > Kafka' 카테고리의 다른 글

Kafka cluster 구성 및 failover 테스트  (0) 2023.03.17
Kafka 설치 및 간단 사용법  (0) 2023.03.16