---
title: Use real-time mode in Lakeflow pipelines
description: Configure real-time mode in Lakeflow pipelines to achieve sub-second latency for operational streaming workloads.
canonical_url: https://docs.databricks.com/gcp/en/ldp/real-time
---

# Use real-time mode in Lakeflow pipelines

Real-time mode enables ultra-low-latency data processing and targets sub-second end-to-end latency, as low as five milliseconds. Use real-time mode for operational workloads that require immediate response to streaming data, such as fraud detection and real-time personalization.

Real-time mode is also available directly in Structured Streaming outside of pipelines. See [Real-time mode in Structured Streaming](https://docs.databricks.com/gcp/en/structured-streaming/real-time/index.md).

## How real-time mode achieves low latency

Real-time mode differs from standard continuous processing in three key ways:

* **Long-running batches**: The system processes data as it becomes available in the source within long-running batches (default is five minutes).
* **Simultaneous stage scheduling**: All query stages are scheduled at the same time. The compute resource must have enough available task slots to cover all stages concurrently. See [Compute sizing](https://docs.databricks.com/gcp/en/ldp/real-time.md#compute-sizing).
* **Streaming shuffle**: Data is passed between stages as soon as it is produced, rather than waiting for an upstream stage to complete before starting the downstream stage.

The checkpoint interval (configured via `pipelines.trigger.interval`) controls how frequently state and source offsets are persisted to durable storage. Longer intervals reduce checkpointing overhead but increase recovery time after a failure and delay metrics reporting. Shorter intervals improve durability but add overhead.

## Real-time mode and continuous pipelines

Real-time mode is a specialized type of continuous trigger. Continuous mode is still required; real-time mode adds flow-level latency optimizations on top. To use real-time mode, the pipeline must first run in continuous mode. Real-time mode then applies additional optimizations at the flow level to achieve sub-second latency beyond what standard continuous processing provides.

Enabling real-time mode requires three configuration steps:

1. Set the pipeline to continuous mode.
2. Enable real-time mode at the pipeline level.
3. Define a real-time update flow.

## Requirements

| Requirement   | Value                         |
| ------------- | ----------------------------- |
| Compute type  | Classic compute or serverless |
| Pipeline mode | Continuous                    |

## Configure real-time mode

### Step 1: Set the pipeline to continuous mode

In your pipeline settings, set **Pipeline mode** to **Continuous**, or set it in the pipeline JSON:

```json
{
  "continuous": true
}
```

### Step 2: Enable real-time mode at the pipeline level

In your pipeline settings, add the following key to the Spark configuration under **Advanced > Spark config**:

```ini
spark.databricks.streaming.realTimeMode.enabled = true
```

You can also set this in the pipeline JSON:

```json
{
  "continuous": true,
  "spark_conf": {
    "spark.databricks.streaming.realTimeMode.enabled": "true"
  }
}
```

### Step 3: Define a real-time update flow

Real-time mode requires an update flow. Use `dp.create_sink()` or `@dp.foreach_sink` to define the output target, then use the [`@dp.update_flow`](https://docs.databricks.com/gcp/en/ldp/developer/ldp-python-ref-update-flow.md) decorator with `pipelines.trigger` set to `"RealTime"` and `target` pointing to the sink.

```python
from pyspark import pipelines as dp

# Define the output sink
dp.create_sink(
    "my_kafka_sink",
    "kafka",
    {
        "kafka.bootstrap.servers": "<bootstrap-servers>",
        "topic": "<output-topic>",
    }
)

# Define the real-time update flow targeting the sink
@dp.update_flow(
    name="my_rtm_flow",
    target="my_kafka_sink",
    spark_conf={
        "pipelines.trigger": "RealTime",
        "pipelines.trigger.interval": "5 minutes",  # optional; defaults to 5 minutes
    }
)
def my_real_time_flow():
    return (
        spark.readStream
            .format("kafka")
            .option("kafka.bootstrap.servers", "<bootstrap-servers>")
            .option("subscribe", "<input-topic>")
            .load()
    )
```

Flow-level configuration parameters:

| Parameter                    | Required | Default       | Description                                                                                                                                    |
| ---------------------------- | -------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `pipelines.trigger`          | Yes      | —             | Set to `"RealTime"` to enable real-time mode for this flow.                                                                                    |
| `pipelines.trigger.interval` | No       | `"5 minutes"` | Checkpoint interval. Controls how often state and offsets are committed. Shorter values improve recoverability; longer values reduce overhead. |

## Code examples

### Kafka to Kafka

Read from a Kafka topic and write to a Kafka output target:

```python
from pyspark import pipelines as dp

dp.create_sink("kafka_output_sink", "kafka", {
    "kafka.bootstrap.servers": broker_address,
    "topic": output_topic,
})

@dp.update_flow(
    name="kafka_rtm_flow",
    target="kafka_output_sink",
    spark_conf={
        "pipelines.trigger": "RealTime",
        "pipelines.trigger.interval": "5 minutes",
    }
)
def kafka_rtm_flow():
    return (
        spark.readStream
            .format("kafka")
            .option("kafka.bootstrap.servers", broker_address)
            .option("subscribe", input_topic)
            .option("startingOffsets", "latest")
            .load()
            .selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)", "timestamp")
    )
```

### Kafka to a `ForEach` sink

Read from a Kafka topic and handle each row with a Python function in a [`ForEach` sink](https://docs.databricks.com/gcp/en/ldp/for-each-sink.md):

```python
from pyspark import pipelines as dp

@dp.foreach_sink(name="events_foreach_sink")
def handle_event(row):
    # Custom per-row logic here, such as a call to an external API.
    ...

@dp.update_flow(
    name="foreach_rtm_flow",
    target="events_foreach_sink",
    spark_conf={
        "pipelines.trigger": "RealTime",
        "pipelines.trigger.interval": "5 minutes",
    }
)
def foreach_rtm_flow():
    return (
        spark.readStream
            .format("kafka")
            .option("kafka.bootstrap.servers", broker_address)
            .option("subscribe", input_topic)
            .option("startingOffsets", "latest")
            .load()
            .selectExpr("CAST(key AS STRING) AS key", "CAST(value AS STRING) AS value")
    )
```

### Enrich with a broadcast join

Join a Kafka stream against a static lookup table. Real-time mode supports broadcast (stream-to-static) joins. It also supports stream-to-stream inner joins in Databricks Runtime 18 LTS and above with additional Spark configuration, but not stream-to-stream outer joins. See [Stream to stream joins](https://docs.databricks.com/gcp/en/structured-streaming/real-time/setup.md#stream-stream-join).

```python
from pyspark import pipelines as dp
from pyspark.sql.functions import broadcast, expr

dp.create_sink("enriched_output_sink", "kafka", {
    "kafka.bootstrap.servers": broker_address,
    "topic": enriched_output_topic,
})

@dp.update_flow(
    name="enriched_events_flow",
    target="enriched_output_sink",
    spark_conf={
        "pipelines.trigger": "RealTime",
        "pipelines.trigger.interval": "5 minutes",
    }
)
def enriched_events():
    lookup = spark.read.table("catalog.schema.lookup_table")
    return (
        spark.readStream
            .format("kafka")
            .option("kafka.bootstrap.servers", broker_address)
            .option("subscribe", input_topic)
            .load()
            .withColumn("event_key", expr("CAST(value AS STRING)"))
            .join(broadcast(lookup), expr("event_key = lookup_key"))
            .select("event_key", "lookup_value", "timestamp")
    )
```

### <a id="aggregation" />Aggregation

Count events by key using a stateful `groupBy`. Set `spark.sql.shuffle.partitions` to match the input partition count for stateful operations:

```python
from pyspark import pipelines as dp
from pyspark.sql.functions import col

dp.create_sink("event_counts_sink", "kafka", {
    "kafka.bootstrap.servers": broker_address,
    "topic": output_topic,
})

@dp.update_flow(
    name="event_counts_flow",
    target="event_counts_sink",
    spark_conf={
        "pipelines.trigger": "RealTime",
        "pipelines.trigger.interval": "5 minutes",
        "spark.sql.shuffle.partitions": "8",
    }
)
def event_counts():
    return (
        spark.readStream
            .format("kafka")
            .option("kafka.bootstrap.servers", broker_address)
            .option("subscribe", input_topic)
            .load()
            .selectExpr("CAST(key AS STRING) AS event_type", "timestamp")
            .groupBy(col("event_type"))
            .count()
    )
```

## Supported sources and sinks

| Connector                          | As source      | As sink       | Notes                                                                                                                                                                        |
| ---------------------------------- | -------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Apache Kafka                       | ✓              | ✓             | —                                                                                                                                                                            |
| Azure Event Hubs (Kafka connector) | ✓              | ✓             | Uses the Kafka-compatible interface.                                                                                                                                         |
| Delta                              | Not supported  | Not supported | —                                                                                                                                                                            |
| `ForEach` sink                     | Not applicable | ✓             | Runs your Python function or writer object for each row. See [Use a `ForEach` sink to write each row in pipelines](https://docs.databricks.com/gcp/en/ldp/for-each-sink.md). |

## <a id="compute-sizing" />Compute sizing

### <a id="serverless-pipelines-on-the-preview-channel" />Serverless pipelines on the preview channel

> [!NOTE]
> **Beta**
>
> Running multiple real-time flows per pipeline without task slot contention, and source stage adaptive repartitioning, are in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md). They are available only on serverless pipelines that use the [preview channel](https://docs.databricks.com/gcp/en/release-notes/dlt/index.md#runtime-channels).

Databricks reserves task slots for each real-time flow, so you can run multiple real-time flows in one pipeline without the flows competing for task slots. You don't need to size compute.

Source stage adaptive repartitioning automatically adjusts the number of tasks that read from each Kafka or Kinesis source to match its traffic. To use it, leave `maxPartitions` unset on Kafka sources, and leave `maxPartitions` and `shardsPerTask` unset on Kinesis sources. If you set these options, the source uses the fixed number of tasks you configured instead. Shuffle partition counts are not adjusted, so set `spark.sql.shuffle.partitions` for stateful flows as shown in [Aggregation](https://docs.databricks.com/gcp/en/ldp/real-time.md#aggregation).

### Serverless pipelines on the current channel

Databricks sizes compute for you. The number of tasks that read from each source is fixed, so set `maxPartitions` on Kafka sources, or `maxPartitions` or `shardsPerTask` on Kinesis sources to handle your peak traffic.

### Classic compute

The compute must have enough task slots to run all tasks across all query stages at the same time. The following table shows the required task slots for common pipeline types:

| Pipeline type                                | Configuration                                      | Required task slots |
| -------------------------------------------- | -------------------------------------------------- | ------------------- |
| Single-stage stateless (Kafka source + sink) | `maxPartitions` = 8                                | 8                   |
| Two-stage stateful (Kafka source + shuffle)  | `maxPartitions` = 8, shuffle partitions = 20       | 28 (8 + 20)         |
| Three-stage (Kafka source + two shuffles)    | `maxPartitions` = 8, two shuffle stages of 20 each | 48 (8 + 20 + 20)    |

If you don't set `maxPartitions`, use the number of partitions in the Kafka topic.

## Operator support

| Category      | Operator                        | Supported                                                                    |
| ------------- | ------------------------------- | ---------------------------------------------------------------------------- |
| Stateless     | Selection, Projection           | ✓                                                                            |
| UDFs          | Scala UDF                       | ✓ (with limitations)                                                         |
| UDFs          | Python UDF                      | ✓ (with limitations)                                                         |
| Aggregation   | sum, count, max, min, avg       | ✓                                                                            |
| Windowing     | Tumbling, Sliding               | ✓                                                                            |
| Windowing     | Session                         | Not supported                                                                |
| Deduplication | `dropDuplicates`                | ✓ (unbounded state)                                                          |
| Deduplication | `dropDuplicatesWithinWatermark` | ✓                                                                            |
| Joins         | Broadcast table join            | ✓                                                                            |
| Joins         | Stream-to-stream inner join     | ✓ (Databricks Runtime 18 LTS and above, with additional Spark configuration) |
| Joins         | Stream-to-stream outer join     | Not supported                                                                |
| Custom        | `transformWithState`            | ✓ (with behavioral differences)                                              |
| Custom        | `union`                         | ✓ (with limitations)                                                         |
| Custom        | `forEach`                       | ✓                                                                            |
| Custom        | `flatMapGroupsWithState`        | Not supported                                                                |
| Custom        | `mapPartitions`                 | Not supported                                                                |
| Custom        | `forEachBatch`                  | Not supported                                                                |

### `transformWithState` in real-time mode

`transformWithState` is supported in real-time mode with the following differences from micro-batch processing:

* `handleInputRows` is invoked once per row rather than once per key per batch. The `inputRows` iterator yields a single value per invocation.
* Event-time timers are not supported. Processing-time timers fire when a long-running batch terminates if no data has arrived.
* `transformWithStateInPandas` is not supported.

### Pandas UDFs in real-time mode

To minimize latency with pandas UDFs, set `spark.sql.execution.arrow.maxRecordsPerBatch` to `1`. This optimizes for latency at the expense of throughput. If throughput is also important, set this value to `100` or higher.

## Monitor real-time mode performance

Real-time mode exposes latency metrics in `StreamingQueryProgress` under the `latencies` field. Access these metrics via a `StreamingQueryListener` or by inspecting the `lastProgress` property on the streaming query.

| Metric                   | Description                                                                                                                                         |
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `processingLatencyMs`    | Time between when a record is read by the flow and when it is fully processed by the flow                                                           |
| `sourceQueuingLatencyMs` | Time between when a record is successfully written to the message bus (for example, log append time in Kafka) and when it is first read by the flow |
| `e2eLatencyMs`           | Total end-to-end latency from when the record is produced at the source to when it is fully processed by the flow                                   |

Each metric is reported as p50, p90, p95, and p99 percentiles.

## Limitations

On classic compute and on serverless pipelines that use the current channel, one real-time flow per pipeline is recommended. Multiple flows are allowed, but they compete for task slots, which increases latency. To run multiple real-time flows in one pipeline without this latency increase, see [Serverless pipelines on the preview channel](https://docs.databricks.com/gcp/en/ldp/real-time.md#serverless-pipelines-on-the-preview-channel).

For a complete list of operator and source limitations, see [Real-time mode limitations](https://docs.databricks.com/gcp/en/structured-streaming/real-time/limitations.md).

## Additional resources

* [Real-time mode concepts](https://docs.databricks.com/gcp/en/structured-streaming/real-time/concepts.md)
* [Real-time mode reference](https://docs.databricks.com/gcp/en/structured-streaming/real-time/reference.md)
* [Triggered vs. continuous pipeline mode](https://docs.databricks.com/gcp/en/ldp/concepts/pipeline-mode.md)
* [Configure classic compute for pipelines](https://docs.databricks.com/gcp/en/ldp/configure-compute.md)
