跳到正文
Databricks:Blog·· 56 分鐘前AI 評分60

Databricks 宣佈 Spark Structured Streaming 原生 Lakebase Postgres sink 正式推出

Announcing General Availability of native Lakebase Postgres sink for Spark Structured Streaming

AI 導讀

Databricks 宣佈,Spark Structured Streaming 的原生 Lakebase sink 現已正式開放使用,可將計算結果直接寫入 Lakebase,並內置批次處理、連線重用、重試及 upsert。

正文

Low-latency use cases like fraud detection, live personalization, and real-time feature computation need computed results in a serving-ready OLTP store. Lakebase Postgres is Databricks' fully managed, serverless PostgreSQL database offering, with autoscaling, branching, native Unity Catalog integration, and sub 10ms reads. But writing to it from a streaming pipeline has meant building a custom foreach writer and handling batching, retries, and upserts yourself, which is complex to get right and hard to keep performant under load.

Today, we are announcing the general availability of the native Lakebase sink for Apache Spark™ Structured Streaming, a connector that writes computed results directly to Lakebase and handles all of that for you. The same connector works in both micro-batch and Real-Time Mode. We show it on a feature pipeline that ingests watch and like events from a video streaming platform, computes each user's recent activity, and upserts one feature row per user into Lakebase in hundreds of milliseconds. You get that performance without building or maintaining the connector yourself.

Why the native Lakebase sink matters 

The problem it solves

A foreach writer has to do a lot at once: batch writes for throughput, a bounded queue for backpressure, pool connections and refresh OAuth tokens, retry transient failures with backoff, deduplicate within a batch. It also has to implement upsert semantics correctly, including keeping the greatest timestamp or updating a column only when an incoming event has a newer ordering value.

Even for an experienced developer, writing and tuning all of this is weeks of work, and it does not end there. The connector turns into something you maintain, fixing throughput regressions, credential refresh, and keeping the code current with the latest versions. For a sense of what that looks like in practice, see this reference implementation. 

"We stream real-time data into Lakebase so our risk agent can pull a user's recent activity when it investigates an alert. We initially built that path with a custom ForeachWriter, which meant building and maintaining the write infrastructure ourselves. As part of our development work, we found that the native sink reduces the custom work required for connection pooling, batching, and retries." - Maria Marinova, Sr. Lead Software Engineer, DraftKings

What it handles for you

The native sink takes care of it. You wire up a Lakebase write in minutes with a single writeStream call, and spend no time on performance, tuning, or maintaining the connector. You get: 

  • Ease of use: no custom sink code; batching, connection reuse, and error handling are built in.
  • Upserts: declare an upsertkey and the sink maintains one row per key.
  • Merge strategies for upserts: declarative custom merge logic for how non-key columns update on conflict. See supported merge strategies.
  • Reliable delivery: transient JDBC errors (connection failures, deadlocks, rate limiting) are retried automatically with exponential backoff.
  • A familiar API: the same writeStream and .toTable() used for Unity Catalog tables.
  • Tunable batching: optional caps on batch size and interval when the defaults don't fit your SLA.
  • UC-managed authentication: the connector authenticates to Lakebase automatically as the running user or service principal. 

"Testing real-time data flows into Lakebase is an important part of validating how our risk agent can quickly access the freshest available user data when making decisions. As we evaluated the native sink, we found that its built-in connection pooling, batching, and retries reduce the custom infrastructure we need to build and maintain." - Georgi Stoychev, Sr. Software Automation Engineer, DraftKings

Example: A real-time feature pipeline for recommendation systems

Use case

A video streaming provider wants to recommend videos to its viewers. A recommendation model needs what users watched most recently. So for each user we compute the last 6 videos they watched and the last 3 they liked. We store that as one row per user in Lakebase. At inference time, the recommendation model looks up that row by user_id to rank what to show next.

The pipeline

A feature is an input the model uses to make a prediction. Here, the features are each user's recent activity, computed from a stream of events. Each event has a user_id and a video_id and an event type (WATCH/LIKE). For each user we keep their recent watches and likes as one row, keyed by user_id. Every new event for a user_id updates that row in place.

The pipeline is three stages:

  • Stream: read engagement events from Kafka.
  • Transform: group by user_id and maintain each user's last-6 watched and last-3 liked with transformWithState, emitting one feature row per user.
  • Write: upsert that row into Lakebase with the native sink.

Writing to Lakebase

Writing to Lakebase is a single writeStream call. How you point it at Lakebase depends on whether the target table is registered in Unity Catalog.

If the Lakebase table is registered in Unity Catalog, write with .toTable() using the fully qualified catalog.schema.table. The connector manages credentials as the running user or service principal, and creates the table if it doesn't exist:

If the Lakebase table isn't registered in Unity Catalog, write to the Lakebase endpoint directly with format("postgresql") (see Lakebase tables not registered with Unity Catalog). The same format("postgresql") path also supports writing to external PostgreSQL databases (e.g. Amazon RDS or self-hosted) via a Unity Catalog connection (DBR 19+, Public Preview).

Serving the features

The sink writes the features into Lakebase within 100-200 milliseconds of being produced, with no offline-to-online sync in between. Applications and models can query them in single-digit milliseconds. This supports two patterns: 

  • An application fetching a user's features by user_id, a low-latency single-row lookup.
  • A model reads those same features for real-time inference, so its predictions reflect what the users just did.

For a deeper look at the implementation, see the companion repo, a self-serve example you can run end-to-end.

How the sink performs at scale

We ran the pipeline at a sustained input rate of ~20K events/sec against 5 million active users, each accumulating watch and like history. The cluster used was a 40-core classic compute (DBR 18 LTS) writing to an 8-CU Lakebase project with 5-minute RTM checkpoint intervals.

Every event is an upsert that overwrites that user's existing feature row in Lakebase. As state builds up, the sink is continuously upserting against a table with millions of existing rows, finding and replacing the row for each user_id on every event. This is the workload pattern that breaks custom foreach writers at scale.

End-to-end latency is measured from the Kafka input timestamp to the row landing in Lakebase.

Metric

Value

Input throughput

20k Events per sec (1 hour sustained)

Active user (state cardinality) 

5,000,000

P50 (end-to-end latency)

183 ms

P99 (end-to-end latency)

354 ms

Note: This illustrative example uses synthetic data replayed into Kafka. The test ran for 60 minutes, achieving ~20K EPS and 63.5 million upserts.

These latencies are specific to this setup. Your numbers will differ with input rate, the amount of state you keep, RTM cluster size, and Lakebase size. Size both the cluster and Lakebase to sustain the throughput you need. The sink also lets you tune how it batches writes using batchinterval and batchsize. See performance tuning.

Lakebase server-side throughput

At this throughput on an 8-CU project, pg_stat_statements shows 63.5 million upserts averaging ~0.015 milliseconds per statement. Lakebase had headroom to spare. For a deeper look at how Lakebase achieves this, see the blog, How Lakebase Architecture Delivers 5x Faster Postgres Writes.

Conclusion

With Real-Time Mode computing features as events arrive and the native Lakebase sink enabling writes directly to a serving-ready OLTP database, together they unblock real-time feature computation and ingestion in one streaming pipeline.

The native sink gives you a first-class experience when writing to Lakebase, using the same writeStream API you already know. We showed it on a recommendation feature pipeline, but the same pattern works wherever a streaming pipeline needs to write computed results to a database for immediate serving.

The native Lakebase sink for Spark Structured Streaming is now Generally Available.

Try it yourself

FAQ

What is the native Lakebase sink for Spark Structured Streaming?

It’s a Structured Streaming connector that writes computed results directly to Lakebase. It handles batching, retries, connection pooling, deduplication, upserts, and workspace-managed authentication, so you don’t build a custom foreach writer. The same connector works in both micro-batch and Real-Time Mode.

Is the native Lakebase sink generally available?

Yes. The native Lakebase sink for Spark Structured Streaming is now generally available on Databricks Runtime 18 LTS and above. The RealTime and ProcessingTime triggers run on classic compute; on serverless, use Trigger.AvailableNow. It isn’t supported in Lakeflow Declarative Pipelines.

How do I write to Lakebase with the sink?

For a Lakebase table registered in Unity Catalog, use .toTable() with the fully qualified catalog.schema.table; the connector manages credentials and creates the table if it doesn’t exist. For an unregistered table, use format("postgresql") with the Lakebase endpoint and dbtable.

Does the sink support upserts and custom merge logic?

Yes. Declare an upsertkey and the sink maintains one row per key. Merge strategies (DBR 19+) control how non-key columns update on conflict, such as keeping the latest non-null value or the value from the newest event, with no hand-written ON CONFLICT SQL.

What use cases is the native Lakebase sink built for?

The native Lakebase sink is built for operational dashboards, app databases, and low-latency feature stores.. Examples include real-time feature computation for recommendations and personalization, and fraud detection.

來源:Databricks:Blog · databricks.com