Streaming · Open reference architecture

Real-time streaming with Spark Real-Time Mode

Kafka in, Kafka out, milliseconds in between

Continuous processing for fraud, recommendations and live boards, measured end to end.

Laptop (Docker)Updated 2026-11-21

In one paragraph

Real-time streaming is the architecture for decisions that must happen while an event is still in flight: score a payment, update a recommendation, move a leaderboard. In open source Apache Spark, Real-Time Mode is a Structured Streaming trigger that processes records continuously instead of in micro-batches. The reference pipelines read from Kafka, transform or keep state with transformWithState, and write back to Kafka or a key-value store, with latency measured from producer timestamp to consumer.

Streaming

Kafka in, Kafka out, milliseconds in between

Sources

Ingestion

Open lakehouse platform

Engine access

Execution

Data foundation

Serving

Teams

Lit boxes are part of this architecture, with the versions its reference repo pins. Switch to Full platform to see every capability. Select any box or team for details. marks a swappable choice.

When to use it

  • Your SLA is in tens or hundreds of milliseconds, not seconds.
  • You are porting Apache Flink jobs and want to know which ones move to Spark.
  • You already run Spark for batch and want one engine for both.

When to pick something else

  • Writing large analytical tables: use the streaming medallion pattern and keep this path thin.
  • Python UDFs in the hot path: Real-Time Mode does not support them.

Walkthrough

How data moves through it

  1. 1

    Produce with timestamps

    A generator writes keyed events to Kafka with the send time embedded.

  2. 2

    Process continuously

    A Structured Streaming query with the real-time trigger processes each record as it arrives instead of waiting for a micro-batch.

  3. 3

    Keep state

    Stateful steps such as deduplication, windows or a custom transformWithState processor keep per-key state in RocksDB.

  4. 4

    Emit

    Results go to an output Kafka topic or a key-value store.

  5. 5

    Measure

    A consumer subtracts the embedded send time from arrival time and reports p50, p95 and p99.

Bill of materials

Components and versions

Versions are the ones pinned in the reference repositories below.
CapabilityComponentRole in this architectureVersionLicense
SourcesSource systemsPayments, clicks, game eventsProducers with event timestamps
IngestionEvent streamingApache KafkaInput topic, 16 partitions in the reference runsConfluent 7.6.0 imageApache-2.0
ExecutionReal-Time ModeSpark Real-Time ModeContinuous processing triggerSpark 4.1 to 4.3Apache-2.0
ExecutionReal-Time ModeApache SparktransformWithState for keyed state, on RocksDB4.3.0Apache-2.0
ExecutionReal-Time ModeApache FlinkThe engine being compared or migrated fromApache-2.0
Data foundationOpen table formatApache Icebergor Delta LakeOptional history table written by a separate queryApache-2.0
ServingApps and APIsKafka sink topicOutput and latency topics
ServingApps and APIsRedisor ValkeyLatest state for applications
ServingApps and APIsFraud engine, app UI, live boardRead results within milliseconds

People and agents

Who works with it

Data engineers

Pipelines that are correct, idempotent and cheap to rerun.

Works with: Event streaming, Real-Time Mode, Open table format

Run it

Reference code

Five Real-Time Mode pipelines on Spark 4.1 with Kafka and a latency consumer, compared against micro-batch.

# Start Kafka + Spark
docker compose up -d

# Create Kafka topics
docker exec kafka kafka-topics --bootstrap-server localhost:9092 \
    --create --topic rtm-input --partitions 16 --if-not-exists
docker exec kafka kafka-topics --bootstrap-server localhost:9092 \
    --create --topic rtm-output --partitions 16 --if-not-exists
docker exec kafka kafka-topics --bootstrap-server localhost:9092 \
    --create --topic rtm-latency --partitions 16 --if-not-exists

# Start the data generator (in a separate terminal)
python data_generator/producer.py --rate 1000 --keys 100

# Run a pipeline
docker exec spark spark-submit /opt/spark-data/pipelines/01_stateless_passthrough.py
lisancao/spark-demosLaptop (Docker)

Demo 14 (bookshop recommender, Spark 4.3.0) and demo 10 (chained transformWithState lobby board) compare micro-batch with Real-Time Mode and commit the numbers in NOTES.md.

Four streaming patterns (stateless, tumbling window, sliding window, keyed state) written for both Flink 1.20 and Spark 4.1 Real-Time Mode, with migration notes.

Caveats from the reference repos

  • Open source Spark 4.1 Real-Time Mode is stateless only; stateful queries need a later release (the stateful demos run on Spark 4.3).
  • Python UDFs are not supported in Real-Time Mode.
  • PySpark 4.1 has no trigger(realTime=...) argument; the quickstart uses a Py4J helper.
  • transformWithState requires the RocksDB state store provider.
  • Size task slots to source partitions plus shuffles times shuffle partitions, or the query stalls.

Industry lenses

Where this architecture shows up

Each card opens the board with that industry's sources. Full industry pages: Banking, Payments and fintech, Healthcare providers, Manufacturing, Energy and utilities, Retail and e-commerce, Telecommunications, Cybersecurity.

Answers

Frequently asked questions

How fast is Spark Real-Time Mode?
In the spark-rtm-quickstart run committed in results/latency-results.md (stateless, 500 events per second, 16 cores), end-to-end p50 was 17 ms and p99 was 21 to 26 ms across six runs. In spark-demos demo 14 on Spark 4.3.0, a stateless enrichment step measured p50 0 ms and p99 1 ms in real-time mode against 115 ms and 204 ms in micro-batch.
Can Spark replace Flink for low-latency streaming?
For many stateless and windowed jobs, yes; flink-to-spark-rtm writes the same four patterns for both engines. Keyed state with timers is where migrations need the most care, since Spark expresses it through transformWithState.
Does Real-Time Mode support stateful queries?
Not in open source Spark 4.1. The stateful reference demos (transformWithState, dedup and windows) run on Spark 4.3.