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
Oxlintはいいぞ
yug1224
5
950
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
1.2k
Vibe Coding - AI 驅動的軟體開發
mickyp100
0
160
CSC307 Lecture 03
javiergs
PRO
1
480
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
340
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
150
Vibe codingでおすすめの言語と開発手法
uyuki234
0
190
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
5.7k
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
660
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
0
940
ゆくKotlin くるRust
exoego
1
210
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
120
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.8k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
400
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
61
49k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
66
36k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
83
Game over? The fight for quality and originality in the time of robots
wayneb77
1
84
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
800
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
120
Are puppies a ranking factor?
jonoalderson
1
2.6k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
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