SQLiteにPostgresのNOTIFY/LISTEN機能を追加するRust拡張「Honker」
Simon Willison Blog は、PostgreSQL の NOTIFY/LISTEN セマンティクスを SQLite に実装し、Rust 拡張と言語バインディングを提供するオープンソースプロジェクト「honker」を紹介している。
キーポイント
SQLite へのメッセージング機能の追加
PostgreSQL の NOTIFY/LISTEN セマンティクスを模倣した機能を SQLite に実装し、Rust で書かれた拡張モジュールとして提供している。
直感的なキューとストリーミング API
Python などの言語バインディングにより、複雑な設定なしでメールキューや Kafka スタイルの永続ストリームを記述できる簡潔なコード例が提示されている。
独自 SQL 関数の拡張
通知機能に関連する 20 以上のカスタム SQL 関数(notify など)を追加し、データベース内で直接メッセージング処理を可能にしている。
トランザショナルアウトボックスパターンの実装
honker はトランザクションのコミット成功時のみアイテムをキューに追加するトランザショナルアウトボックスパターンを実装しており、データ整合性を保証します。
低遅延なリアルタイム通知機能
WAL モードを使用し、フル SQL クエリの実行コストをかけずに .db-wal ファイルを 1ms ごとにポーリングすることで、ほぼリアルタイムのデータ取得を実現しています。
カスタム SQL 関数の追加
20 以上の独自 SQL 関数が追加され、notify や honker_stream_read_since などの関数により、イベント通知やストリーミング読み取りが容易になっています。
重要な引用
"Postgres NOTIFY/LISTEN semantics" for SQLite, implemented as a Rust SQLite extension
The design of this looks very solid.
It also adds 20+ custom SQL functions including these two:
honker implements the transactional outbox pattern, which ensures items are only queued if a transaction successfully commits.
workers can poll the .db-wal file with a stat call every 1ms to get as close to real-time as possible without the expense of running a full SQL query.
影響分析・編集コメントを表示
影響分析
この技術は、小規模から中規模のアプリケーションにおいて、外部のメッセージングインフラを依存関係として排除し、SQLite のみで堅牢な非同期処理やイベント駆動システムを構築することを可能にする。特に、開発環境やエッジデバイス、軽量なマイクロサービス間でのデータ連携において、アーキテクチャの複雑さを劇的に削減する効果がある。
編集コメント
SQLite の機能拡張という観点では非常に魅力的なアプローチだが、大規模システムにおける可用性や分散処理の観点からは、既存の専用ブローカーとの比較検討が必要である。
SQLite向けの「Postgres NOTIFY/LISTEN セマンティクス(semantics)」を、Rust製のSQLite拡張機能および各種言語バインディングとして実装し、その活用を支援するものです。
この設計は非常に堅牢に見えます。キューを扱うPythonコードが以下のように書けます:
import honker
db = honker.open("app.db")
emails = db.queue("emails")
emails.enqueue({"to": "alice@example.com"})
消費(ワーカープロセス内)
async for job in emails.claim("worker-1"):
send(job.payload)
job.ack()
また、Kafkaスタイルの永続ストリーム(durable streams)も以下のように書けます:
stream = db.stream("user-events")
with db.transaction() as tx:
tx.execute("UPDATE users SET name=? WHERE id=?", [name, uid])
stream.publish({"user_id": uid, "change": "name"}, tx=tx)
async for event in stream.subscribe(consumer="dashboard"):
await push_to_browser(event)
さらに、以下の2つを含む20以上のカスタムSQL関数が追加されます:
SELECT notify('orders', '{"id":42}');
SELECT honker_stream_read_since('orders', 0, 1000);
この拡張機能にはWALモード(Write-Ahead Logging mode)が必須であり、ワーカーは完全なSQLクエリを実行するコストをかけずにリアルタイムに可能な限り近づけるため、1msごとにstat呼び出しで.db-walファイルをポーリングできます。
honkerはトランザクショナルアウトボックスパターン(transactional outbox pattern)を実装しており、これによりトランザクションが正常にコミットされた場合のみアイテムがキューに追加されることが保証されます。このパターンに関する私の最も気に入っている解説は、Brandur LeachによるTransactionally Staged Job Drains in Postgresです。SQLite向けにこのパターンの新しい実装が見られるのは素晴らしいことです。
Via Show HN
Tags: databases, postgresql, sqlite, rust
原文を表示
"Postgres NOTIFY/LISTEN semantics" for SQLite, implemented as a Rust SQLite extension and various language bindings to help make use of it.
The design of this looks very solid. It lets you write Python code for queues that looks like this:
import honker
db = honker.open("app.db")
emails = db.queue("emails")
emails.enqueue({"to": "alice@example.com"})
# Consume (in a worker process)
async for job in emails.claim("worker-1"):
send(job.payload)
job.ack()And Kafka-style durable streams like this:
stream = db.stream("user-events")
with db.transaction() as tx:
tx.execute("UPDATE users SET name=? WHERE id=?", [name, uid])
stream.publish({"user_id": uid, "change": "name"}, tx=tx)
async for event in stream.subscribe(consumer="dashboard"):
await push_to_browser(event)It also adds 20+ custom SQL functions including these two:
SELECT notify('orders', '{"id":42}');
SELECT honker_stream_read_since('orders', 0, 1000);The extension requires WAL mode, and workers can poll the .db-wal file with a stat call every 1ms to get as close to real-time as possible without the expense of running a full SQL query.
honker implements the transactional outbox pattern, which ensures items are only queued if a transaction successfully commits. My favorite explanation of that pattern remains Transactionally Staged Job Drains in Postgres by Brandur Leach. It's great to see a new implementation of that pattern for SQLite.
Via Show HN
Tags: databases, postgresql, sqlite, rust
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み