-->

[Kafka] Kafka 구성요소

Distributed Partitioned Immutable Log = Kafka

 

Topic

Partition으로 구성된 일련의 로그 파일 

  • RDBMS의 Partitioned Table과 유사한 기능
  • Topic은 Key-Value 형식의 메세지 구조이며, Value로 어떤 타입의 메세지도 가능(문자열, 숫자값, 객체, Json, Avro, Protobuf 등)
  • 로그 파일과 같이 연속적으로 추가, 발생하는 데이터를 저장하는 구조
  • 시간의 흐름에 따라 메시지가 순차적으로 물리적인 파일에 write 됨 (RDB처럼 Update 같은 개념이 없이 전부 Append한다. TSDB처럼)
  • 하나의 Topic은 1개 이상의 Partition을 가질 수 있다.

 

Partition

Topic의 partition은 kafka의 병렬 성능과 가용성 기능의 핵심 요소

메시지는 병렬 성능과 가용성을 고려한 topic 내의 개별 partition에 분산 저장됨.

또한 topic의 partition 들은 단일 kafka broker 뿐만 아니라 여러 개의 kafka broker 들에 분산 저장 됨.

두번째 Broker가 죽으면 첫번째 Broker의 Partition #1이 Follower에서 Leader가 되어 정합성 보장.

Offsets

개별 파티션은 정렬되고, 변경 할 수 없는(immutable) 일련의 레코드로 구성된 로그 메시지

  • 개별 레코드는 offset으로 불리는 일련 번호를 할당 받음
  • 개별 Partition은 다른 파티션과 완전히 독립적임
  • 개별 Partition 내에서 정렬되고 offset이 할당됨

 

Producer

Producers are clients that write events to Kafka. The producer specifies the topics they will write to and the producer controls how events are assigned to partitions within a topic. This can be done in a round-robin fashion for load balancing or it can be done according to some semantic partition function such as by the event key.

  • Producer는 Topic에 메세지를 보냄(메세지 write)
  • Producer는 성능 / 로드밸런싱 / 가용성 / 업무 정합성 등을 고려하여 어떤 브로커의 파티션으로 메세지를 보내야 할지 전략적으로 결정됨

Topic과 Value는 필수값으로 Broker에 value를 전달하면 Partition에 나누어 저장됨.

Consumer

Consumers are clients that read events from Kafka.

The only metadata retained on a per-consumer basis is the offset or position of that consumer in a topic. This offset is controlled by the consumer. Normally a consumer will advance its offset linearly as it reads records, however, because the position is controlled by the consumer it can consume records in any order. For example, a consumer can reset to an older offset to reprocess data from the past or skip ahead to the most recent record and start consuming from “now”.

This combination of features means that Kafka consumers can come and go without much impact on the cluster or on other consumers.

  • Consumer는 Topic에서 메세지를 읽어 들임.
  • 여러 개의 Consumer들로 구성 될 경우 어떤 브로커의 파티션에서 메세지를 읽어들일지 전략적으로 결정함

 

auto.offset.reset

Consumer가 Topic에 처음 접속하여 Message를 가져올 때 가장 오래된 처음 (Broker의 message) offset 부터(earliest) 가져올 것인지, 가장 최근인 마지막 offset 부터 가져올 것 인지를 설정하는 파라미터

  • auto.offset.reset = ealiest :  처음 offset 부터 읽음
  • auto.offset.reset = latest : 마지막 offset부터 읽음 (default)
  • kafka-console-consumer 명령어를 사용 할 때 --from-beginning을 사용해야만 auto.offset.reset이 earlist로 지정됨

 

Serializer/Deserializer

Producer → Broker로 Key,Value가 전송될 때 Serialize 되어 ByteArray로 전달 된다. 그리고 Broker → Consumer는 ByteArray를 Deserialize하여 key, value를 획득한다.

ByteArray를 사용하여 전송하면 네트워크 대역폭도 잘 사용할 수 있고 압축도 된다. 자바 코드에서는 아래처럼 적용한다.

Kafka에서 기본적으로 제공하는 Serializer는 StringSerializer, ShortSerializer, IntegerSerializer, LongSerializer, DoubleSerializer, BytesSerializer가 있다. (그러나 업무에선 Custom 만들어야 할 듯)

 

Partitioner

메세지는 Producer를 통해 전송시 Partitioner를 통해 토픽의 어떤 파티션으로 전송되어야 할 지 미리 결정됨.

메세지 Key는 업무 로직이나 메세지 Produce/Consume시 분산 성능 영향을 고려하여 생성

  • Key 값을 가지지 않는 경우,
    라운드 로빈, 스티키 파티션 등의 전략이 선택되어 파티션 별로 메세지가 전송 될 수 있음.
    → Topic이 여러 개의 파티션을 가질 때 메세지의 전송 순서가 보장되지 않은 채로 Consumer에서 읽혀질 수 있음.
    (전송 순서를 보장하려면 Partition을 하나로 가져야 하는데 그럼 분산시스템의 이점이 없어짐)
  • Key 값을 가지는 경우,
    특정 key값을 가지는 메세지는 특정 파티션으로 고정되어 전송됨.
    → 단일 파티션 내에서 전송 순서가 보장되어 Consumer에서 읽혀짐.

+ Recent posts