Course contentsShow
← Data Engineering
Lesson 815 of 82512. End-to-End Platform IntegrationFree lesson

Synchronizing State Between Streaming and Batch Stores

Maintain data consistency when using lambda or kappa architectures by reconciling streaming state with historical batch data snapshots.

In this lesson, you will learn how to maintain a "single version of truth" when your system writes state to both high-velocity streaming stores and high-latency batch data lakes.

The Challenge of Split-Brain State

In advanced architectures, we often use a Lambda or Kappa approach where real-time services (like Flink or Spark Streaming) maintain low-latency state, while batch processes (like Spark SQL on S3) maintain historical snapshots. The danger is "state divergence": where the streaming store reflects an event, but the batch store lags behind, or worse, holds conflicting data due to out-of-order processing.

Reconciliation via Versioned Snapshots

To synchronize these layers, we don’t try to force instantaneous consistency. Instead, we implement event-time-based checkpointing. Every record in your stream must carry a source_timestamp and a unique event_id.

When the batch process runs its daily or hourly snapshot, it does not simply overwrite the partition. Instead, it performs a merge or upsert based on the event version. If the batch store uses a format like Delta Lake or Apache Iceberg, we can use an upsert pattern to ensure that the batch store only adopts changes that are "settled" (e.g., older than the streaming watermark).

-- Conceptual merge to synchronize batch state with streaming inputs
MERGE INTO historical_orders AS target
USING streaming_delta_buffer AS source
ON target.order_id = source.order_id
WHEN MATCHED AND source.event_timestamp > target.last_updated_at THEN
  UPDATE SET *
WHEN NOT MATCHED THEN
  INSERT *;

Why this matters

By treating your batch layer as a refined, long-term version of your stream, you ensure that downstream analytical queries are consistent regardless of which engine they query. The streaming store handles the "now," and the batch store provides the "provenance." If your pipeline failures require a root cause analysis, having these layers synchronized ensures you aren't debugging inconsistent data states when looking at historical logs.

Key Takeaway: Synchronize state by using deterministic, versioned merges that rely on event-time watermarks to reconcile streaming updates against batch historical snapshots.