Community研究&データ分析github.com

Kafka コンシューマ/プロデューサ構築

信頼性の高いApache Kafkaコンシューマとプロデューサを構築するためのスキルです。コンシューマグループ、パーティション割り当て、オフセットコミット戦略、at-least-once / exactly-once、冪等/トランザクショナルプロデューサ、リバランス、デッドレター処理をカバーします。Kafkaコンシューマ/プロデューサの作成、オフセットコミットやコンシューマグループの設定、スループットのチューニング、リバランスやポイズンメッセージの処理を行う際に使用します。

プロデューサ構築 とは?

このスキルは、Apache Kafka を使用した信頼性の高いメッセージングシステムを構築するためのベストプラクティスを提供します。特に、コンシューマグループのパラレル処理、オフセットコミット戦略(手動コミット vs 自動コミット)、exactly-once セマンティクスの実現方法、およびリバランス時の安全なハンドリングに焦点を当てています。ワークフローは、パーティション数に基づく並列性の設計、処理完了後のオフセットコミット、冪等なシンクの実装、リバランス処理、ポイズンメッセージのデッドレターキューへのルーティングといったステップを含みます。Python のコード例を用いて手動コミットパターンや冪等プロデューサの設定を示し、スループットチューニングやよくある落とし穴(自動コミットによるメッセージ喪失、過剰なコンシューマ数、長時間処理による再バランス誘発など)についても解説しています。このスキルは、Claude Code、Cursor、Codex などの AI エージェントと組み合わせて使用することを想定しています。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Unknown-333/awesome-data-engineering-skills/tree/main/skills/building-kafka-consumers

Installed? Explore more 研究&データ分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Kafka コンシューマの構築

使用するタイミング

  • Kafka コンシューマ/プロデューサの作成またはデバッグ時。
  • オフセットコミット戦略と配信保証の選択時。
  • コンシューマグループの並列性、リバランス、またはデッドレター処理のチューニング時。
  • ストリーム処理/ウィンドウ処理には使用しない(processing-streaming-data を使用すること)。

ワークフロー

- [ ] ターゲットの並列性に合わせてパーティションをサイジングする (コンシューマ数 <= パーティション数)
- [ ] 処理が成功した後にオフセットをコミットする
- [ ] シンクを冪等にする (イベントキーによる upsert)
- [ ] リバランスを処理する (リボーク時にコミット、長時間のポーリングギャップを避ける)
- [ ] ポイズンメッセージをデッドレターキューにルーティングする
  1. パーティションが並列性を制限する。 コンシューマグループのスケールアウトはパーティション数が上限であり、余分なコンシューマはアイドル状態になります。ピークスループットに合わせてパーティションを選択してください。
  2. 処理後にコミットする。 処理が確実に完了した後にオフセットをコミットします。前にコミットすると、クラッシュ時にメッセージが失われます。
  3. 冪等なシンク。 At-least-once ではリトライ時に重複が発生します。安定したイベントキーで upsert することで、再処理が無害になります。
  4. リバランスは発生する。 パーティションリボーク時にコミットし、poll() の間隔を max.poll.interval.ms 未満に保つことで、ブローカーがコンシューマを追い出さないようにします。
  5. ポイズンメッセージ はエラー情報とともにデッドレターキューに送信し、1つの不良レコードがパーティション全体をブロックしないようにします。

パターン

処理後の手動コミット:

consumer = KafkaConsumer("orders", group_id="etl",
                         enable_auto_commit=False,
                         max_poll_records=500)
for msg in consumer:
    try:
        upsert(process(msg))          # キーで冪等
        consumer.commit()             # 成功後にのみコミット
    except PoisonError:
        send_to_dlq(msg)
        consumer.commit()             # 不良レコードをスキップ

冪等 / トランザクショナルプロデューサ — enable.idempotence=true を設定してリトライの重複を排除し、トランザクションを使用してトピック間で読み取り-処理-書き込みの exactly-once を実現します。

スループットチューニング — max.poll.records、fetch.min.bytes を増やし、バッチで処理します。処理を高速に保ち、リバランスによる排除を回避します。

よくある落とし穴

  • 自動コミット + 低速処理 — 処理が完了する前にオフセットが進み、クラッシュ時にメッセージが失われます。処理後の手動コミットを推奨します。
  • パーティション数より多いコンシューマ — 余分なコンシューマはアイドル状態になります。スケールするには再パーティショニングしてください。
  • ポーリング間の長時間処理 — max.poll.interval.ms を超え、無限リバランスを引き起こします。制限付きバッチで処理するか、バックグラウンドワーカーを使用してください。
  • デッドレターのパスがない — 1つのポイズンメッセージがパーティション全体をブロックします。
  • 冪等なシンクなしで exactly-once に依存する — どの at-least-once のホップでも重複が再導入されます。

Individual skills in this repo

This repo contains 9 individual skills — each has its own dedicated page.

Unknown-333/authoring-airflow-dags

Write production-grade Apache Airflow DAGs using the TaskFlow API — idempotent tasks, correct scheduling and catchup, retries/SLAs, connections/variables, and avoiding top-level code. Use when creating or reviewing Airflow DAGs, scheduling pipelines, wiring task dependencies, configuring retries/backfills, or fixing non-idempotent tasks.

Unknown-333/building-dagster-assets

Build Dagster pipelines using software-defined assets — asset dependencies, partitions, resources and IO managers, asset checks, and schedules/sensors. Use when creating Dagster assets or jobs, modeling data as assets, adding partitions or backfills, wiring resources/IO managers, or migrating from task-based orchestration to assets.

Unknown-333/building-dbt-models

Build well-structured dbt models — staging/intermediate/marts layers, ref() and source(), materializations, and incremental models with the right strategy. Use when creating or refactoring dbt models, choosing table vs view vs incremental, structuring a dbt project, or writing incremental logic.

Unknown-333/building-feature-pipelines

Build ML feature pipelines and feature stores — point-in-time-correct joins to avoid label leakage, offline/online parity, feature freshness and backfills, and materialization with tools like Feast. Use when engineering features for ML, preventing train/serve skew or data leakage, building a feature store, or backfilling historical features for training.

Unknown-333/building-iceberg-tables

Design and operate Apache Iceberg tables — partitioning and hidden partitioning, partition/schema evolution, snapshots and time travel, compaction and small-file cleanup, and MERGE/upsert for lakehouse tables on Spark, Flink, Trino, or Snowflake. Use when creating or maintaining Iceberg tables, choosing partitioning, evolving schema/partitions, or fixing small-file and metadata bloat.

Unknown-333/building-ingestion-pipelines

Build batch and incremental data ingestion (extract-load) pipelines — full vs incremental extraction, change data capture (CDC), watermarks and high-water marks, API pagination and rate limits, and choosing managed EL tools (Fivetran, Airbyte) vs custom code. Use when ingesting data from databases, APIs, files, or SaaS into a warehouse/lake, or designing incremental extraction and CDC.

Unknown-333/debugging-data-pipelines

Systematically root-cause data pipeline failures and data incidents — job errors, wrong or missing data, duplicates, and freshness misses — by tracing lineage upstream, isolating the failing stage, reconciling against source, and planning a safe fix and backfill. Use when a pipeline fails, numbers look wrong, data is missing or duplicated, a dashboard is stale, or a stakeholder reports a data discrepancy.

Unknown-333/designing-backfills-and-replays

Plan and run safe data backfills and replays — idempotent reprocessing of historical windows, partition-by-partition execution, isolating backfill compute from production, verifying results, and avoiding double-counting or changed history. Use when backfilling a new or fixed model, reprocessing after a bug, replaying events, or loading history for a new pipeline without corrupting existing data.

Unknown-333/designing-data-contracts

Define and enforce data contracts between producers and consumers — explicit schema, semantics, ownership, SLAs, and versioning — to prevent silent upstream changes from breaking downstream pipelines. Use when a producer schema change could break consumers, defining an interface between teams/services and the warehouse, or adding schema enforcement at ingestion.

関連スキル