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
Tomoyoshi Ogura
May 15, 2017
Programming
1
1.3k
Kafkaを利用したアプリケーションでのオフセットの制御について/Kafka Offsets
2017-05-14に行われた「Scala将軍達の後の祭り2017」で発表した時の資料です。
Tomoyoshi Ogura
May 15, 2017
Tweet
Share
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基盤のご紹介
orgachem
PRO
6
2.2k
技術検証結果の整理と解析をAIに任せよう!
keisukeikeda
0
130
OTP を自動で入力する裏技
megabitsenmzq
0
120
Agentic AI: Evolution oder Revolution
mobilelarson
PRO
0
190
Kubernetesでセルフホストが簡単なNewSQLを求めて / Seeking a NewSQL Database That's Simple to Self-Host on Kubernetes
nnaka2992
0
160
AHC061解説
shun_pi
0
400
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
750
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
610
メタプログラミングで実現する「コードを仕様にする」仕組み/nikkei-tech-talk43
nikkei_engineer_recruiting
0
200
Swift ConcurrencyでよりSwiftyに
yuukiw00w
0
270
Claude Codeセッション現状確認 2026福岡 / fukuoka-aicoding-00-beacon
monochromegane
4
440
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
130
Featured
See All Featured
The SEO Collaboration Effect
kristinabergwall1
0
400
A designer walks into a library…
pauljervisheath
210
24k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
220
Exploring anti-patterns in Rails
aemeredith
2
290
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
86
Making Projects Easy
brettharned
120
6.6k
Side Projects
sachag
455
43k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
830
Building Adaptive Systems
keathley
44
3k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
110
Site-Speed That Sticks
csswizardry
13
1.1k
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