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.
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
How agents, apps and people reach compute: a client session per identity.
Execution
Spark's execution modes, plus engines that read the same tables.
Data foundation
Open tables in a governed catalog, on storage you own.
Serving
Teams
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
Produce with timestamps
A generator writes keyed events to Kafka with the send time embedded.
- 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
Keep state
Stateful steps such as deduplication, windows or a custom transformWithState processor keep per-key state in RocksDB.
- 4
Emit
Results go to an output Kafka topic or a key-value store.
- 5
Measure
A consumer subtracts the embedded send time from arrival time and reports p50, p95 and p99.
Bill of materials
Components and versions
| Capability | Component | Role in this architecture | Version | License |
|---|---|---|---|---|
| SourcesSource systems | Payments, clicks, game events | Producers with event timestamps | ||
| IngestionEvent streaming | Apache Kafka | Input topic, 16 partitions in the reference runs | Confluent 7.6.0 image | Apache-2.0 |
| ExecutionReal-Time Mode | Spark Real-Time Mode | Continuous processing trigger | Spark 4.1 to 4.3 | Apache-2.0 |
| ExecutionReal-Time Mode | Apache Spark | transformWithState for keyed state, on RocksDB | 4.3.0 | Apache-2.0 |
| ExecutionReal-Time Mode | Apache Flink | The engine being compared or migrated from | Apache-2.0 | |
| Data foundationOpen table format | Apache Icebergor Delta Lake | Optional history table written by a separate query | Apache-2.0 | |
| ServingApps and APIs | Kafka sink topic | Output and latency topics | ||
| ServingApps and APIs | Redisor Valkey | Latest state for applications | ||
| ServingApps and APIs | Fraud engine, app UI, live board | Read 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.pyDemo 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.
Related architectures
All architecturesPlatform
The agentic lakehouse
Omnigent and MLflow over sandboxed Spark Connect sessions, on Iceberg and Unity Catalog
Streaming
Streaming medallion
Kafka, Spark Structured Streaming and Apache Iceberg in Unity Catalog
Platform
The open lakehouse on a laptop
Spark 4.3, Apache Iceberg, Unity Catalog, Kafka, Airflow, MLflow and an agent, in Docker
