home
diamond Go Premium
Data Engineering Path  ·  PySpark

Scenario: Streaming Kafka Ingestion

Scenario: Real-Time Fraud Detection via Kafka Ingestion

The Challenge

A data engineering team needs to run a Spark Structured Streaming application that ingests continuous payment streams from an Apache Kafka cluster, joins them with static blacklist databases, flags suspicious transactions, and writes the flagged outputs to a downstream real-time analytics dashboard database. The job must run 24/7 without memory leaks, resource starvation, or latency spikes.


1. Optimal Spark-Submit Configuration

spark-submit \
    --master yarn \
    --deploy-mode cluster \
    --name "Realtime-Kafka-Fraud-Detector" \
    --num-executors 6 \
    --executor-cores 3 \
    --executor-memory 8g \
    --driver-memory 4g \
    --packages org.apache.spark:spark-sql-kafka-0-10_2.12:3.3.0 \
    --conf spark.streaming.backpressure.enabled=true \
    --conf spark.streaming.kafka.maxRatePerPartition=5000 \
    --conf spark.sql.streaming.forceDeleteTempCheckpointLocation=false \
    --conf spark.cleaner.referenceTracking.cleanCheckpoints=true \
    hdfs:///scripts/realtime_fraud_detector.py

2. Parameter Explanations & Rationale

  • --packages org.apache.spark:spark-sql-kafka-...: Critical for dependency loading. Instead of manually downloading jar packages, Spark automatically downloads the Spark-Kafka integration connector from Maven Central and distributes it to all worker nodes.
  • Moderate Resources (--num-executors 6 --executor-cores 3 --executor-memory 8g): Streaming applications don't process giant historical batches. Instead, they handle small micro-batches every few seconds. Allocating moderate resources prevents wasting expensive cluster capacity on a job that runs 24/7.
  • spark.streaming.backpressure.enabled=true: Vital for pipeline stability. If a sudden event occurs (e.g., Black Friday traffic) and Kafka receives an enormous spike of messages, backpressure automatically slows down Spark ingestion rates to match Spark's processing capacity, preventing the executors from running out of memory.
  • spark.streaming.kafka.maxRatePerPartition=5000: Sets a safety limit on the maximum number of messages Spark will ingest per Kafka partition per second during startup or recovery, preventing the system from being overwhelmed.
  • spark.cleaner.referenceTracking.cleanCheckpoints=true: Enables automatic metadata and checkpoint garbage collection. This is critical for streaming jobs running for months on end to prevent memory leaks in the Driver JVM.
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.