Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kafkaを利用したアプリケーションでのオフセットの制御について/Kafka Offsets
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Tomoyoshi Ogura
May 15, 2017
Programming
1.3k
1
Share
Kafkaを利用したアプリケーションでのオフセットの制御について/Kafka Offsets
2017-05-14に行われた「Scala将軍達の後の祭り2017」で発表した時の資料です。
Tomoyoshi Ogura
May 15, 2017
More Decks by Tomoyoshi Ogura
See All by Tomoyoshi Ogura
Apache Kafkaとストリーム処理/Reactive Streams
tarugo07
4
3.4k
DDDで利用するアーキテクチャと プレゼンテーション層について/DDD Architecture
tarugo07
0
4k
Other Decks in Programming
See All in Programming
AIエージェントで業務改善してみた
taku271
0
540
Claude CodeでETLジョブ実行テストを自動化してみた
yoshikikasama
0
840
Claude Code × Gemini × Ebitengine ゲーム制作素人WebエンジニアがGoでゲームを作った話
webzawa
0
160
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
610
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
280
AWS re:Invent 2025の少し振り返り + DevOps AgentとBacklogを連携させてみた
satoshi256kbyte
3
170
Surviving Black Friday: 329 billion requests with Falcon!
ioquatix
0
830
実践CRDT
tamadeveloper
0
590
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
280
JOAI2026 1st solution - heron0519 -
heron0519
0
150
AIベース静的検査器の偽陽性率を抑える工夫3選
orgachem
PRO
4
360
Making the RBS Parser Faster
soutaro
0
520
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
200
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
110
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
99
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Optimising Largest Contentful Paint
csswizardry
37
3.7k
So, you think you're a good person
axbom
PRO
2
2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
160
The Curse of the Amulet
leimatthew05
1
12k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
It's Worth the Effort
3n
188
29k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Technical Leadership for Architectural Decision Making
baasie
3
340
Transcript
Kafkaを利用したアプリケーションで のオフセットの制御について Tomoyoshi Ogura 2017/05/14 Scala将軍達の後の祭り2017
自己紹介 Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • 小椋友芳
• ChatWork株式会社コアテクノロジー開発室所属 • Scala歴4年 • twitter: @tomoyoshi_ogura • github: tarugo07
アジェンダ Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • Kafkaのオフセットの重要性
• オフセットとコミットの基本 • アプリの処理に合わせたオフセットの制御方法
オフセットとアプリケーション Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • オフセットはアプリケーションに大きな影響を与える
◦ 同じメッセージを重複して処理 ◦ メッセージをロストする危険性 ◦ パフォーマンスの劣化
オフセットとコミット Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • オフセットとはパーティション内でメッセージを一意に識別する番号
• コンシューマはパーティションのメッセージをどこまで読み取ったかオフセット で追跡する • オフセットは特別な__consumer_offsetsトピックで管理される • コミットはパーティションの現在のオフセットを更新すること
Consumerの重要なプロパティ Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • auto.offset.reset
◦ オフセットがない場合の動作 ◦ デフォルトはlatest • enable.auto.commit ◦ コンシューマが自動的にオフセットをコミット • max.poll.records ◦ 一回のpoll()で取得するメッセージ件数
コミットの方法 Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • オフセットのコミット方法は複数ある
◦ 自動コミット ◦ commitSync() ◦ commitAsync()
自動コミット Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • コンシューマが自動でオフセットをコミットする
• enable.auto.commit = true • コミット間隔のデフォルトは5秒 ◦ auto.commit.interval.msで制御 • poll()で取得したメッセージの最大のオフセットをコミット
commitSync() Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • オフセットのコミットを明示的に制御したい場合に使用
• auto.commit.offset = false • メッセージの欠落を排除しリバランスでの重複メッセージの件数を減らす • commitSync()はpoll()で取得した最新のオフセットをコミット ◦ 明示的にオフセット値を指定することも可能 • コミットに失敗すると例外
commitAsync() Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • commitSync()の非同期版
• auto.commit.offset = false • poll()で取得した最新のオフセットをコミット • ブローカーのレスポンスを待たずにコミットのリクエストを投げて終了 • コミットのリトライをしない
アプリでAt Most Onceの処理をしたい Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved.
• At Most Once ◦ 最高1回処理するが保証がない • 自動コミットを利用する ◦ enable.auto.commit = true ◦ auto.commit.interval.msは短く設定 • consumer.commitSync()を実行しない • poll()でオフセットがコミットされる
アプリでAt Least Onceの処理をしたい Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved.
• At Least Once ◦ 最低一回処理するが重複する可能性もある • commitSync()をアプリの処理が終わった後に実行 ◦ auto.commit.offset = false
アプリでExactly Onceの処理をしたい Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. •
Exactly Once ◦ 正確に一回処理する • オフセットの管理とアプリケーションの処理を一つのアトミックなトランザク ションで処理する ◦ オフセットをRDBなどの外部ストレージに保存すると簡単 • ConsumerRebalanceListerとseek()を利用 ◦ auto.commit.offset = false
Exactly Onceの実装イメージ Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved.
Exactly Onceの実装イメージ Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved.
まとめ Scala将軍達の後の祭り2017 2017/05/14 © ChatWork All rights reserved. • Kakfaを使う場合はオフセットの管理に気をつけよう
• アプリケーションの性質に合わせて正しいオフセットの制御を ◦ At Most Once ◦ At Least Once ◦ Exactly Once