12-04 更新 实时
首页/ 技术栈/ 正文

Lakebase Postgres 原生 Spark 接收器正式发布

技术栈 来源:Databricks Blog 2026-11-24 00:32 2 阅读 机器翻译
Lakebase Postgres 原生 Spark 接收器正式发布

了解 Real-Time Mode 和原生 Lakebase sink 如何共同在单个流式管道中实现实时特征计算,并将结果摄入 Lakebase。

在实时特征管道上观看它的实际运行:为 500 万用户向 Lakebase upsert 约 20K EPS,端到端 p99 低于 500 ms。

欺诈检测、实时个性化以及实时特征计算等低延迟用例,需要将计算结果存放在可直接用于服务的 OLTP 存储中。Lakebase Postgres 是 Databricks 提供的全托管、无服务器 PostgreSQL 数据库产品,具备自动扩缩容、分支、原生 Unity Catalog 集成以及低于 10ms 的读取能力。但从流式管道向其写入,意味着要自行构建自定义 foreach writer,并自行处理批处理、重试和 upsert,这既难以正确实现,也难以在高负载下保持性能。

今天,我们宣布用于 Apache Spark™ Structured Streaming 的原生 Lakebase sink 正式可用,这是一个连接器,可将计算结果直接写入 Lakebase,并为你处理上述所有工作。同一个连接器在微批和 Real-Time Mode 下均可工作。我们在一个特征管道上演示了它:该管道从视频流媒体平台摄入观看和点赞事件,计算每个用户最近的活动,并在数百毫秒内为每个用户向 Lakebase upsert 一行特征。无需自行构建或维护该连接器,你就能获得这样的性能。

原生 Lakebase sink 为何重要

一个 foreach writer 必须同时完成很多工作:为提升吞吐而批量写入、用有界队列实现背压、池化连接并刷新 OAuth Token、对瞬时故障进行带退避的重试、在批次内去重。它还必须正确实现 upsert 语义,包括保留最大的时间戳,或仅在传入事件具有更新的排序值时更新某一列。

即便对经验丰富的开发者来说,编写并调优这一切也要花上数周时间,而且事情并不会到此结束。这个连接器会变成你需要持续维护的东西:修复吞吐回退、刷新凭证,还要让代码跟上最新版本。想了解这在实践中是什么样子,可以看看这份参考实现。

“我们将实时数据流式写入 Lakebase,这样我们的风险智能体在调查告警时就能调取用户近期的活动记录。我们最初是用自定义的 ForeachWriter 来构建这条通路的,这意味着写入基础设施得由我们自己搭建和维护。在开发过程中我们发现,原生 sink 减少了连接池化、批处理和重试所需的定制工作。”—— Maria Marinova,DraftKings 资深首席软件工程师

原生 sink 会处理这一切。只需一次 writeStream 调用,你就能在几分钟内接好 Lakebase 写入,无需在性能、调优或维护连接器上花费时间。你可以获得:

易用性:无需自定义 sink 代码;批处理、连接复用和错误处理均为内置。

Upsert:声明一个 upsertkey,sink 就会为每个键维护一行。

Upsert 的合并策略:以声明式方式自定义合并逻辑,决定发生冲突时非键列如何更新。参见支持的合并策略。

可靠投递:瞬时的 JDBC 错误(连接失败、死锁、限流)会通过指数退避自动重试。

熟悉的 API:与用于 Unity Catalog 表的同一套 writeStream 和 .toTable()。

可调的批处理:当默认值不符合你的 SLA 时,可选择限制批大小和间隔。

UC 管理的身份验证:连接器会以当前运行用户或服务主体的身份自动向 Lakebase 完成身份验证。

“测试实时数据流入 Lakebase,是验证我们的风险智能体在做出决策时能否快速访问最新可用用户数据的重要一环。在评估原生 sink 时,我们发现它内置的连接池、批处理和重试机制减少了我们需要自行构建和维护的定制基础设施。”——Georgi Stoychev,DraftKings 高级软件自动化工程师

示例:面向推荐系统的实时特征管道

某视频流媒体服务商希望向观众推荐视频。推荐模型需要知道用户最近观看了什么。因此,我们为每个用户计算其最近观看的 6 个视频和最近点赞的 3 个视频,并以每个用户一行的形式存入 Lakebase。在推理时,推荐模型通过 user_id 查找该行,从而对接下来要展示的内容进行排序。

特征是模型用于做出预测的输入。在这里,特征是每个用户近期的活动,由事件流计算得出。每个事件包含 user_id、video_id 以及事件类型(WATCH/LIKE)。对于每个用户,我们将其最近的观看和点赞记录作为一行保存,并以 user_id 为键。每当某个 user_id 出现新事件,就原地更新该行。

该流水线分为三个阶段:

Stream:从 Kafka 读取互动事件。

Transform:按 user_id 分组,并使用 transformWithState 维护每个用户最近 6 次观看和最近 3 次点赞,为每个用户输出一行特征。

Write:使用原生 sink 将该行 upsert 到 Lakebase。

写入 Lakebase 只需一次 writeStream 调用。如何将其指向 Lakebase 取决于目标表是否在 Unity Catalog 中注册。

如果 Lakebase 表已在 Unity Catalog 中注册,则使用完全限定的 catalog.schema.table 通过 .toTable() 写入。连接器会以运行用户或服务主体的身份管理凭证,并在表不存在时创建表:

如果 Lakebase 表未在 Unity Catalog 中注册,则使用 format("postgresql") 直接写入 Lakebase 端点(参见未在 Unity Catalog 中注册的 Lakebase 表)。相同的 format("postgresql") 路径还支持通过 Unity Catalog 连接写入外部 PostgreSQL 数据库(例如 Amazon RDS 或自托管)(DBR 19+,公共预览版)。

sink 会在特征生成后的 100-200 毫秒内将其写入 Lakebase,中间没有离线到在线的同步。应用和模型可以在个位数毫秒内查询它们。这支持两种模式:

一个应用通过 user_id 获取用户特征,这是一次低延迟的单行查询。

模型读取这些相同的特征进行实时推理,因此其预测能反映用户刚刚的行为。

如需深入了解实现细节,请查看配套仓库,这是一个可自助运行、端到端跑通的示例。

sink 在大规模场景下的表现如何

我们以约 20K 事件/秒的持续输入速率运行该管道,面向 500 万活跃用户,每个用户都在不断累积观看和点赞历史。所使用的集群是一个 40 核经典计算(DBR 18 LTS),写入一个 8-CU 的 Lakebase 项目,RTM 检查点间隔为 5 分钟。

每个事件都是一次 upsert,会覆盖 Lakebase 中该用户现有的特征行。随着状态不断累积,sink 会持续对一个包含数百万现有行的表执行 upsert,在每次事件中查找并替换每个 user_id 对应的行。正是这种负载模式会让自定义 foreach 写入器在大规模下崩溃。

端到端延迟从 Kafka 输入时间戳开始计量,直到行写入 Lakebase 为止。

20k 事件/秒(持续 1 小时)

活跃用户(状态基数)

P50(端到端延迟)

P99(端到端延迟)

注意:此示意性示例使用合成数据回放到 Kafka。测试运行了 60 分钟,达到约 20K EPS 和 6350 万次 upsert。

这些延迟是针对此配置的。你的数值会因输入速率、保留的状态量、RTM 集群规模和 Lakebase 规模而不同。要为集群和 Lakebase 进行容量规划,以维持所需的吞吐量。该 sink 还允许你使用 batchinterval 和 batchsize 调整其批量写入方式。参见性能调优。

Lakebase 服务端吞吐

在 8-CU 项目上以此吞吐量运行时,pg_stat_statements 显示 6350 万次 upsert 平均每条语句约 0.015 毫秒。Lakebase 仍有充足余量。要深入了解 Lakebase 如何实现这一点,请参阅博客 How Lakebase Architecture Delivers 5x Faster Postgres Writes。

借助 Real-Time Mode 在事件到达时计算特征,以及原生 Lakebase sink 支持直接写入可用于服务的 OLTP 数据库,两者共同在单一流式管道中打通了实时特征计算与摄取。

原生 sink 让你在写入 Lakebase 时获得一流的体验,使用的正是你熟悉的 writeStream API。我们在一个推荐特征管道上做了演示,但凡是流式管道需要把计算结果写入数据库以供即时服务,同样的模式都适用。

用于 Spark Structured Streaming 的原生 Lakebase sink 现已正式可用(Generally Available)。

配套仓库:在你的工作区中端到端运行完整的特征管道。

从 Structured Streaming 写入 Lakebase:sink 配置、支持的触发器、输出模式与调优选项。

Lakebase Postgres:实例设置、UC 集成与服务。

什么是用于 Spark Structured Streaming 的原生 Lakebase sink?

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(Announcing General Availability of native Lakebase Postgres sink for Spark Structured Streaming)

Learn how Real-Time Mode and the native Lakebase sink together enable real-time feature computation and ingestion into Lakebase in one streaming pipeline.

See it in action on a real-time feature pipeline, upserting ~20K EPS for 5 million users into Lakebase at p99 under 500 ms, end-to-end.

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

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

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

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.

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 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).

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.

20k Events per sec (1 hour sustained)

Active user (state cardinality) 

P50 (end-to-end latency)

P99 (end-to-end latency)

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.

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.

Companion repo: run the full feature pipeline end-to-end in your workspace.

Write to Lakebase from Structured Streaming: sink configuration, supported triggers, output modes, and tuning options.

Lakebase Postgres: instance setup, UC integration, and serving.

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 Apache Spark™ 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.

Get the latest posts in your inbox

原文为英文,正文由自动抓取并保留原文。

本文由采集器自动同步

原文发布于 Databricks Blog(Announcing General Availability of native Lakebase Postgres sink for Spark Structured Streaming),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗