Skip to main content

Kafka schema registry

· 6 min read
Haril Song
Owner, Software Engineer at 42dot

Problems

  • 메세지 스펙이 변경될 경우, 의존하고 있는 모듈 or Git Repository 마다 DTO 업데이트가 필요하다.
    • 하위호환성이나 상위호환성이 자주 깨진다.
    • DTO 관리의 복잡도가 선형적으로 증가한다.
    • Java 는 특히 Json 기반의 메세지를 다루기에 불편한 점이 많다.
  • 카프카는 ByteArray 형태로 메세지를 전송하나, 애플리케이션 레벨에서는 이를 역직렬화하여 관리하는 것이 권장된다.
    • payload 에 데이터를 담을 때마다 ByteArray 로 직렬화하는 과정, 그리고 이 반대 과정이 매번 이루어진다.
    • 코드 복잡도 상승
    • ByteArray - JSON - Object

ByteArray + DTO + ObjectMapper 방식

Kafka 메시지가 JSON 형식이라고 가정:

data class User(
val id: String,
val name: String,
val email: String?,
val age: Int?,
val createdAt: Long
)

val rawBytes: ByteArray = record.value()
val user = objectMapper.readValue(rawBytes, User::class.java)
  • 스키마 정보를 코드에서 직접 관리 (e.g. DTO 클래스)
  • Kafka 메시지의 구조가 JSON 포맷으로 되어 있어야 가능
  • Schema Registry 불필요
  • 메시지 구조가 바뀌면 DTO도 바뀌어야 하고, 호환성 검사 수동

GenericRecord (Avro + Schema Registry)

val record = consumerRecord.value() as GenericRecord
val name = record.get("name").toString()
  • DTO 없이도 동작 가능 (GenericRecord), 또는 generated class 사용 가능
  • 메시지 구조 변경 시 Registry의 호환성 정책으로 안전하게 진화 가능

SpecificRecord (Avro + Schema Registry)

// user.avsc
{
"type": "record",
"name": "User",
"fields": [...]
}
// 자동 생성
public class User extends SpecificRecordBase implements SpecificRecord {
private String id;
private String name;
...
}
@KafkaListener(topics = ["\${kafka.topic.user}"], groupId = "\${spring.kafka.consumer.group-id}")
fun consume(user: User) {
val userId = user.getId()
logger.info("Received user with id: {}, name: {}", userId, user.getName())

users[userId] = user
}

코드가 생성되어있기 때문에 직접 참조 가능

  • 정적 타입 지원
    • 직렬화/역직렬화 시 안정성 보장
    • IDE 지원 우수
  • Kafka Schema Registry와 완전 호환
  • 성능 우수
    • GenericRecord 는 리플렉션을 활용하여 비교적 느림

Schema 정의 및 사용

  • IntelliJ Junie 를 사용해서 샘플 작성
plugins {
id("com.github.davidmc24.gradle.plugin.avro") version "1.9.1"
}

repositories {
mavenCentral()
maven {
url = uri("https://packages.confluent.io/maven/")
}
}

dependencies {
// Avro and Schema Registry
implementation("org.apache.avro:avro:1.11.3")
implementation("io.confluent:kafka-avro-serializer:7.5.1")
implementation("io.confluent:kafka-schema-registry-client:7.5.1")
}

avro {
isCreateSetters.set(true)
isCreateOptionalGetters.set(false)
isGettersReturnOptional.set(false)
fieldVisibility.set("PRIVATE")
outputCharacterEncoding.set("UTF-8")
stringType.set("String")
templateDirectory.set(null as String?)
isEnableDecimalLogicalType.set(true)
}

User schema 정의

{
"namespace": "com.haril.kafkaschemaregistrydemo.schema",
"type": "record",
"name": "User",
"fields": [
{
"name": "id",
"type": "string"
},
{
"name": "name",
"type": "string"
},
{
"name": "email",
"type": ["null", "string"],
"default": null
},
{
"name": "age",
"type": ["null", "int"],
"default": null
},
{
"name": "createdAt",
"type": {
"type": "long",
"logicalType": "timestamp-millis"
}
}
]
}

자동으로 User 클래스가 생성된 것을 확인할 수 있고,

다른 모듈에서 참조하여 사용할 수 있다.

Schema 의 업데이트

  • 레지스트리에 스키마 정보가 없을 경우, kafka 는 메세지가 발행될 때 연결된 schema registry 에 스키마를 업로드한다.
  • Web UI 를 사용하여 업데이트할수도 있다.

Schema 호환성 정책

대표적으로는 아래와 같다.

모드설명예시
BACKWARD이전 버전의 Consumer는 새 메시지를 이해 가능필드 추가 가능, 제거는 불가
FORWARD새 버전의 Consumer는 이전 메시지를 이해 가능필드 제거 가능, 추가는 불가
FULL양방향 모두 호환제한적 변경만 허용
NONE어떤 변경도 호환성 보장 안 함변경 시 consumer crash 위험 ↑

BACKWARD 정책을 사용할 때는 스키마에 필드를 추가할 때 default 값을 지정해줘야 예전 버전의 스키마를 사용하는 Consumer 도 안전하게 스키마를 역직렬화할 수 있다. null 을 default 로 지정하는 것도 가능하며 이 경우 optional 필드임을 의미하게 된다.

Kafka Streams 는 BACKWARD 만 지원한다.

FULL 정책은 양방향 호환성이 유지되므로 편리하지만 Kafka Streams 는 BACKWARD 정책만 지원하므로 선택지가 제한된다.

만약 GenericRecord 방식으로 사용할 경우, 스키마를 동적으로 로드한다. 이 경우 스키마가 변경되더라도 서비스의 재배포가 필요없다.

ConsumerRecord<String, GenericRecord> record = ...
GenericRecord value = record.value();

Integer age = (Integer) value.get("age");
String name = value.get("name").toString();

props.put("specific.avro.reader", false) 설정으로 활성화할 수 있으며, Map 으로 사용하는 것과 비슷한 경험을 제공할 수도 있다.

항목SpecificRecordGenericRecord
사용 방식Avro 스키마로 Java/Kotlin 클래스 미리 생성런타임에 스키마 파싱 후 동적으로 사용
성능빠르고 타입 안전약간 느리고 타입 안정성 떨어짐
유연성스키마 변경 시 코드 재생성 필요스키마 변경에도 유연하게 대응 가능
권장 상황스키마가 고정된 서비스스키마가 자주 바뀌거나 다양할 때

다음과 같은 사용이라면 GenericRecord 사용을 고려할 수 있다.

  • 다양한 스키마를 처리해야 하는 Kafka consumer 플랫폼
  • 스키마 registry 기반 멀티팀 환경 (스키마 버전이 자주 바뀌는 경우)
  • Avro 스키마가 외부에 의해 관리되고 있어 내부에서 클래스를 만들기 곤란할 때

Producer 에서는 명확한 데이터 스키마가 있어야하므로 .avsc 파일을 통해 객체를 생성하고, Consumer 쪽에서는 GenericRecord 를 사용하여 동적으로 대응하는 방법도 유용하다.

Schema management and Monitoring

Landoop UI

스키마 변경 내역을 계속 기록한다.

Kafka UI 에서는 value 가 schema registry 로 변경된 것을 확인할 수 있다.

Conclusion

Pros

  • 여러개의 중첩된 DTO 를 다루는 대신, 하나의 avsc 파일만 관리하면 되서 비교적 관리 부담이 줄어든다.
  • 모든 서비스는 Schema Registry에서 실시간으로 스키마 조회
    • 메시지에 스키마 정보가 포함되지 않으므로, 네트워크 대역폭을 효율적으로 사용하게 된다
  • Kafka 메시지는 스키마 ID (magic byte + schema ID) 를 포함하므로, 컨슈머는 로컬에 .avsc가 없어도 자동 역직렬화 가능
  • 여러 팀에서 하나의 스트림 파이프라인 or 토픽에 메세지를 발행하는 경우 특히 유용
    • 파이프라인에 이상한 데이터가 들어오지 않게 된다

Cons

  • 별도 API 서버를 통해 배포해야 한다.
  • 인프라 팀과 협업이 필요하다.
  • Schema registry 가 다운될 경우 파이프라인이 멈출 수 있기 때문에, 관리 포인트가 오히려 증가할 수 도 있다.

그래서 언제 쓰면 좋을까?

  • 프로젝트 초기여서 설정부터 하는 경우
  • 하나의 파이프라인을 여러 팀이 공유하여 사용하는 경우
  • 회사에 카프카를 전문적으로 다루는 별도의 팀이 있을 경우
  • protobuf 에 익숙한 경우

Reference

Daily note 작성법

· 3 min read
Haril Song
Owner, Software Engineer at 42dot

Overview

회사에서 시켜서, 혹은 개인의 니즈로 인해 daily 라고 부르는 일일 노트를 적곤 한다.

필자 또한 daily 를 적는데, 처음에는 회사의 요구로 인해 적기 시작했지만 적는 방식에 대해 많은 시행착오를 거쳐서 현재는 개인적인 용도로도 많이 적게 되었다.

이런 daily 는 어떻게 작성해야 편하게 적으면서도 활용도를 최대한 높일 수 있는지 간단하게 적어보려고 한다.

Useful Developer Tools - CLI Edition

· 6 min read
Haril Song
Owner, Software Engineer at 42dot

Overview

Following up on the previous post about the best apps, let's look at recommended Command Line Interface (CLI) tools. All the tools introduced here have been used by me for at least 6 months and are essential installations when setting up new equipment.

zoxide

GitHub - ajeetdsouza/zoxide: A smarter cd command. Supports all major shells.

A tool that eliminates the need to remember paths for directories you've visited before. For example:

cd ~/.config/somewhere/longlong/path

# Next time, you can visit directly with this command
z path

This is incredibly convenient as you no longer need to remember where specific configuration files are located. Using zi displays a priority list, allowing for fuzzy searching.

Once you try it, you'll never want to go back to the time before using this tool.

mise

GitHub - jdx/mise: dev tools, env vars, task runner

A version manager for various environment variables, languages, and packages. It's highly stable due to its architecture and fast thanks to its Rust implementation. Its intuitive commands significantly reduce the learning curve.

I've already introduced it in a blog post, showing how much I love this tool. While there are several similar tools that can serve mise's role, I personally think it's the most recommendable among them. For example, direnv can be completely replaced by mise, and nix is overly complex with reduced versatility.

If you're a programming multilingual dealing with various languages, you should definitely try this.

chezmoi

chezmoi

If you use multiple devices, synchronizing development environments can be quite troublesome, especially for developers who frequently use CLI.

If you're not planning to give up on synchronization entirely and are wondering how to synchronize, try chezmoi. Even if you purchase new equipment, you won't need to spend time on initial setup.

Like mise, there's a post about how to use it on this blog for reference.

fzf

GitHub - junegunn/fzf: :cherry_blossom: A command-line fuzzy finder

With about 70k GitHub Stars, does it need more explanation? (For reference, spring-framework has 57k).

This open-source fuzzy finder, maintained by Korean developer junegunn, boasts incredible versatility through standard input/output piping.

If you need search functionality, regardless of the type, just use fzf. It's so widely used by various packages that you might have been using it indirectly without knowing of its existence.

fd

GitHub - sharkdp/fd: A simple, fast and user-friendly alternative to 'find'

Replaces the find command.

Written in Rust and up to 50% faster than find. It has clean highlighting and much more intuitive command options compared to find.

ripgrep

GitHub - BurntSushi/ripgrep: ripgrep recursively searches directories for a regex pattern while respecting your gitignore

Replaces the grep command. Although named ripgrep, it uses rg as the command.

Similar to fd, it's written in Rust. Compared to grep, you can get much more diverse information from the output. The commands are intuitive and easy to use, and with its speed, there's no reason not to use it.

Truly 'RIP, grep'.

lsd

GitHub - lsd-rs/lsd: The next gen ls command

Replaces the ls command.

The ls command is incredibly frequently used. While the command itself is old, it doesn't provide diverse information in its output. Using lsd can completely replace the existing ls.

bat

GitHub - sharkdp/bat: A cat(1) clone with wings.

Replaces the cat command.

While the cat command is simple output, using bat provides code highlighting. Sharp developers might worry about line output interfering with shell piping, but it doesn't interfere at all. Don't worry and enjoy this modern tool.

I use bat with an alias set to cat.

HTTPie

HTTPie – API testing client that flows with you

Replaces curl.

Although there's also an APP version, making me wonder which article to include it in, I personally only use the CLI version, so I included it here.

The reason I prefer HTTPie over curl is that it's very, very intuitive. Simple GET requests can be sent like this:

https httpie.io/hello

The response comes formatted like this:

Think about curl responses. Developers like pretty things too.

Orbstack

OrbStack · Fast, light, simple Docker & Linux

Replaces Docker Desktop.

When using Docker containers, it becomes a bit faster and eliminates some bugs. But its true value shows when using VMs - it can run VMs very lightly on Macs, where traditionally VMs were difficult to use. If you need to test on OSes like Ubuntu or Kali Linux, try using orbstack for very fast and convenient management. Personally, it was a very interesting experience.

atuin

GitHub - atuinsh/atuin: ✨ Magical shell history

While chezmoi could synchronize tool settings, atuin can synchronize command history used at work. No more struggling to remember what commands you used at work.

warning

One downside is that if you use Warp as your terminal, it's difficult to fully utilize atuin. Warp provides its own history feature that interferes with atuin. As a workaround, you can use the following command to search command history:

atuin history list | fzf

trash-cli

Implements "trash bin" functionality in the terminal. Therefore, you no longer need to fear rm -rf /. Because you can restore anything.

Do you need more reason than being free from rm -rf /, the developer's greatest enemy?

Conclusion

So far, I've introduced several tools that I personally really like.

In fact, there are many more tools worth recommending, but whatever anyone recommends, what works best for you is what you're comfortable with. Find and try others that suit your environment.

info

You can see all the tools I use here.

Useful Developer Tools - App Edition

· 7 min read
Haril Song
Owner, Software Engineer at 42dot

Overview

It's been 13 years since I started using only MacOS.

Even before starting my career as a developer, I had a hobby of exploring various tools to use Mac more conveniently. It was fun just trying out new tools.

Many tools have come and gone through that journey.

This time, I want to introduce the tools that have survived and continue to help me overcome various challenges. I'll be introducing them in two parts - Apps and CLI, and this article will cover the App edition.

Tools for Network Monitoring

· 5 min read
Haril Song
Owner, Software Engineer at 42dot

banner

Overview

This is a brief session prepared for those who don't know where to start with their presentations. We'll introduce various tools that can be used for network monitoring and experimentation, along with explanations of how to use them.

Environment

What kind of environment should we prepare to study networking? While it depends on the topic you want to study, let's look at some commonly used methods.

Docker Network Types

· 7 min read
Haril Song
Owner, Software Engineer at 42dot

Overview

Docker has six network types in total:

  • Bridge
  • Host
  • IPvlan
  • MACvlan
  • Overlay
  • None

I think many backend developers either don't know much about network types or only use bridge networking even if they do. I was also curious about this topic and conducted a study. This article is an excerpt from the content I presented during that study.

We'll conduct practical exercises using Orbstack to run VMs.

Goodbye 2024, Hello 2025

· 11 min read
Haril Song
Owner, Software Engineer at 42dot

reminiscence

Overview

warning

As this is based on my personal diary, some parts might feel a bit cringeworthy 😂

So many things happened in 2024.

I'm starting this retrospective with such a cliché sentence because I can't think of anything better. Not everyone can start their writing with a sentence like "Mother died today." Or can they? I'm not sure.

[Book Review] Code Writing Guide

· 4 min read
Haril Song
Owner, Software Engineer at 42dot

Cover of Munetoshi Ishikawa's Code Writing Guide

info

This review was written with a copy of the book provided by the publisher, and this did not influence the content or evaluation of the review.

Overview

What makes code easy to review?

The Guide to Writing Readable and Review-Friendly Code is a book written by a current LINE developer based on their experiences. It thoroughly covers various methods and principles to enhance code readability.

Personally, I think it's an excellent introductory book on code conventions. Let me explain why.

Naver DAN 24 Review

· 10 min read
Haril Song
Owner, Software Engineer at 42dot

overview

Overview

  • 참가 일시: Nov 11, 2024
  • 장소: 코엑스 그랜드블룸
  • 관련 링크: DAN 24

운좋게도 네이버에서 주관한 DAN24 에 다녀올 수 있었습니다. 결론부터 말씀드리면, 24년에 참여한 컨퍼런스 중 가장 수준이 높았다고 할 수 있을 것 같아요. 아래는 대략적인 내용을 적어둔 것이며, 자세한 내용은 DAN24 공식페이지를 참고해주세요.

KafkaKRU Meetup Review

· 2 min read
Haril Song
Owner, Software Engineer at 42dot

KafkaKRU 밋업 리뷰: Event Sourcing부터 리더 파티션 밸런싱까지

2024년 11월 21일, 서울 중구 삼화타워에서 열린 KafkaKRU 밋업에 참석했습니다. 사실 대기자 명단에 있었어서 참석이 어려운 상태였던 것 같지만, 열정으로 봐주셔서 다행히 쫓겨나지는 않았습니다. 결과적으로는 예상을 훨씬 뛰어넘는 값진 시간이었어요.