{"id":14,"date":"2026-07-06T20:34:56","date_gmt":"2026-07-06T20:34:56","guid":{"rendered":"https:\/\/kindsonthegenius.com\/microservices\/outbox-pattern-reliable-event-publishing\/"},"modified":"2026-07-06T20:36:18","modified_gmt":"2026-07-06T20:36:18","slug":"outbox-pattern-reliable-event-publishing","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/microservices\/outbox-pattern-reliable-event-publishing\/","title":{"rendered":"Outbox Pattern: Reliable Event Publishing in Microservices | Microservices Tutorials"},"content":{"rendered":"\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"Article\",\n  \"headline\": \"Outbox Pattern: Reliable Event Publishing in Microservices | Microservices Tutorials\",\n  \"url\": \"https:\/\/kindsonthegenius.com\/microservices\/outbox-pattern-reliable-event-publishing\/\",\n  \"description\": \"The Outbox pattern ensures atomic database writes and event publishing in microservices. Learn implementation with Kafka and polling relay workers.\",\n  \"datePublished\": \"2026-07-06\",\n  \"dateModified\": \"2026-07-06\",\n  \"author\": {\n    \"@type\": \"Person\",\n    \"name\": \"Kindson Munonye\",\n    \"url\": \"https:\/\/www.kindsonthegenius.com\"\n  },\n  \"publisher\": {\n    \"@type\": \"Organization\",\n    \"name\": \"Kindson The Genius\",\n    \"url\": \"https:\/\/www.kindsonthegenius.com\"\n  },\n  \"mainEntityOfPage\": \"https:\/\/kindsonthegenius.com\/microservices\/outbox-pattern-reliable-event-publishing\/\"\n}\n<\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Updated July 2026.<\/strong> Refreshed for current microservices best practices.<\/p>\n\n\n<h2 class=\"wp-block-heading\">The Dual-Write Problem<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When a service saves to its database AND publishes to Kafka, either operation can fail independently. You might persist an order but never publish the event \u2014 or publish without saving. The Outbox pattern solves this with a single atomic transaction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Outbox Pattern Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Begin database transaction.<\/li>\n<li>Insert\/update business data (e.g., orders table).<\/li>\n<li>Insert event row into <code>outbox<\/code> table in the same transaction.<\/li>\n<li>Commit transaction \u2014 both writes succeed or both roll back.<\/li>\n<li>Separate relay process polls outbox, publishes to Kafka, marks rows as sent.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Outbox Table Schema<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE outbox (\n  id UUID PRIMARY KEY,\n  aggregate_type VARCHAR(100),\n  aggregate_id VARCHAR(100),\n  event_type VARCHAR(100),\n  payload JSONB,\n  created_at TIMESTAMPTZ DEFAULT NOW(),\n  published_at TIMESTAMPTZ\n);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Relay Worker Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Poll in batches with <code>FOR UPDATE SKIP LOCKED<\/code> for concurrency.<\/li>\n<li>Make Kafka publishes idempotent (use event ID as key).<\/li>\n<li>Retry with exponential backoff on broker failures.<\/li>\n<li>Monitor outbox lag (unpublished row count).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatives: Debezium CDC reads database WAL and streams changes to Kafka without polling. Related: <a href=\"\/microservices\/saga-pattern-distributed-transactions\/\">Saga Pattern<\/a> \u00b7 <a href=\"\/microservices\/apache-kafka-microservices-topics-partitions\/\">Kafka Topics Guide<\/a><\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Want live microservices classes?<\/strong> <a href=\"https:\/\/www.alkademy.com\/courses\/microservices-architecture-intermediate-design-communication-resilience\">Join Alkademy<\/a> for instructor-led microservices architecture courses with hands-on projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Go deeper with the book:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/product\/practical-microservices-design-using-cqrs-and-event-sourcing\/\">Practical Microservices Design Using CQRS and Event Sourcing<\/a> by Kindson Munonye.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Kindson Munonye is a software engineer and technical author specializing in microservices, CQRS, event sourcing, and distributed systems. He publishes free step-by-step tutorials and live classes on <a href=\"https:\/\/www.alkademy.com\">Alkademy<\/a>.<\/em><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>The Outbox pattern ensures atomic database writes and event publishing in microservices. Learn implementation with Kafka and polling relay workers.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-messaging-kafka"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/14\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}