Güvenilir Olay Yayımlama için Outbox Deseni
İşlemsel outbox deseninin veritabanı yazmalarının ve Kafka olaylarının tutarlı kalmasını nasıl garanti ettiğini ve çift yazma nedeniyle veri kaybını nasıl önlediğini öğrenin.
Güvenilir Olay Yayımlama için Outbox Deseni, CoddyKit'te ücretsiz bir Apache Kafka & Stream Processing Fundamentals dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Apache Kafka & Stream Processing Fundamentals öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Apache Kafka & Stream Processing Fundamentals kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
The Dual-Write Problem
A service often needs to update its database and publish a Kafka event. Doing both as separate steps risks one succeeding and the other failing.
This dual-write problem leaves the database and Kafka inconsistent.
Why You Can't Just Try-Catch
If you commit the DB then publish, a crash in between loses the event.
If you publish then commit, a failed commit emits a phantom event. There is no atomic transaction spanning a relational DB and Kafka by default.
The Outbox Idea
The outbox pattern records the event in the same database transaction as the business change, in a dedicated outbox table.
One atomic commit now contains both the state change and the intent to publish.
The Outbox Table
A typical outbox schema captures everything needed to build the Kafka record.
CREATE TABLE outbox (
id UUID PRIMARY KEY,
aggregate_type VARCHAR,
aggregate_id VARCHAR,
event_type VARCHAR,
payload JSONB,
created_at TIMESTAMP DEFAULT now()
);Writing Atomically
Within one transaction, write the business row and the outbox row together.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 'a1';
INSERT INTO outbox (id, aggregate_type, aggregate_id, event_type, payload)
VALUES (gen_random_uuid(), 'account', 'a1', 'DebitMade',
'{"amount":100}');
COMMIT;Relaying to Kafka
A separate relay reads new outbox rows and publishes them to Kafka.
Two common approaches: poll the table, or use Change Data Capture to stream the inserts.
CDC + Debezium
Debezium has a dedicated Outbox Event Router SMT. It tails the DB transaction log, picks up outbox inserts, and routes each to the right Kafka topic — no polling, low latency.
At-Least-Once Delivery
The relay guarantees at-least-once delivery: an event is never lost, but it may be published more than once after a crash and retry.
Therefore consumers must be idempotent.
Idempotent Consumers
Use the event id (or aggregate id + version) to deduplicate downstream.
// Pseudocode
if (alreadyProcessed(event.id)) {
return; // skip duplicate
}
apply(event);
markProcessed(event.id);Ordering & Partitioning
To preserve per-aggregate order, use the aggregate_id as the Kafka message key.
All events for one account then land on the same partition and are consumed in order.
Pros & Cons
Pros: no dual-write loss, works with any DB, decouples publishing.
Cons: extra table and relay, at-least-once duplicates, slight latency. Still the standard solution for reliable event publishing.
Quick Check
Test your understanding of the outbox pattern.
Recap
You learned the outbox pattern.
- Solves the dual-write problem by committing state + event atomically.
- A relay (polling or CDC/Debezium) publishes outbox rows to Kafka.
- Delivery is at-least-once, so consumers must be idempotent.
- Key by aggregate id to preserve ordering.
Sıkça Sorulan Sorular
“Güvenilir Olay Yayımlama için Outbox Deseni” dersi ücretsiz mi?
Evet — “Güvenilir Olay Yayımlama için Outbox Deseni” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Apache Kafka & Stream Processing Fundamentals kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Apache Kafka & Stream Processing Fundamentals kursu toplamda 4 dersten oluşur.
“Güvenilir Olay Yayımlama için Outbox Deseni” dersinde ne öğreneceğim?
İşlemsel outbox deseninin veritabanı yazmalarının ve Kafka olaylarının tutarlı kalmasını nasıl garanti ettiğini ve çift yazma nedeniyle veri kaybını nasıl önlediğini öğrenin. Apache Kafka & Stream Processing Fundamentals ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Apache Kafka & Stream Processing Fundamentals öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Apache Kafka & Stream Processing Fundamentals, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Güvenilir Olay Yayımlama için Outbox Deseni” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Apache Kafka & Stream Processing Fundamentals dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Apache Kafka & Stream Processing Fundamentals dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Kafka ile Olay Kaynaklama
- Değişiklik Verisi Yakalama (CDC)
- Mikro Hizmetler Arası İletişim Kalıpları
- Güvenilir Olay Yayımlama için Outbox Deseni