惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Announcements
Recent Announcements
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
I
Intezer
T
Tenable Blog
C
Check Point Blog
N
News and Events Feed by Topic
Project Zero
Project Zero
The Register - Security
The Register - Security
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
The Cloudflare Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
N
Netflix TechBlog - Medium
博客园 - 三生石上(FineUI控件)
腾讯CDC
T
Tailwind CSS Blog
SecWiki News
SecWiki News
S
Secure Thoughts
C
Cisco Blogs
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
小众软件
小众软件
S
Security @ Cisco Blogs
罗磊的独立博客
F
Full Disclosure
aimingoo的专栏
aimingoo的专栏
Google Online Security Blog
Google Online Security Blog
T
Threat Research - Cisco Blogs
博客园 - 聂微东
P
Privacy International News Feed
V
Visual Studio Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Aggregate Boundaries in Go: One Rule That Beats 90% of DDD Books
Gabriel Anha · 2026-04-29 · via DEV Community

You've seen this Slack thread. A team is six months into a "DDD rewrite," there's a 400-page book on someone's laptop, and the open question is still: should Inventory be inside the Order aggregate, or outside? Senior engineers argue both sides. The PR has 47 comments. Nothing ships.

Here is the rule that resolves 90% of those threads:

One aggregate change per database transaction. Everything else is eventually consistent.

That's the rule. Vaughn Vernon defended it across a three-part paper called Effective Aggregate Design (Part 1, Part 2, Part 3). Most DDD books bury it under 200 pages of context maps and ubiquitous language. In practice, it is the only aggregate-design rule worth memorising before you write any Go.

Why This One Rule Carries So Much Weight

The aggregate boundary is the consistency boundary. That phrase sounds abstract until you read it as a transaction guarantee:

  • Inside the boundary: invariants are kept transactionally. Order.Confirm() either commits with all line items, the total, and the new status — or it commits none of them.
  • Outside the boundary: invariants are eventually consistent. The warehouse system finds out that an order was placed after the order commits, via a domain event.

If two aggregates need to change in lockstep, you have two bad options. One is wrapping them in one database transaction across two aggregate roots. That forces a single database, blocks under contention, and locks you out of any future split. The other is distributed transactions, a coordination cost most teams cannot afford and most ORMs cannot model honestly.

The rule cuts the question in half before you write code. Can these two things tolerate a few milliseconds of lag between them? If yes, they belong in separate aggregates and you talk to them via events. If no, they belong inside the same aggregate and you hold the line on transactional integrity.

The Test: "An Order Is Its Lines"

When a domain expert says "an order is its lines," they are telling you the consistency boundary. An order with no items is nonsense. An order whose total disagrees with the sum of its line items is a bug, not a state. Those are invariants — they must always hold.

So Order and LineItem go in the same aggregate. The aggregate root is Order. Outside callers never touch a LineItem directly. They go through Order.AddItem(...), which keeps the total in sync.

package order

import (
    "errors"
    "time"
)

type Status string

const (
    StatusDraft     Status = "draft"
    StatusConfirmed Status = "confirmed"
    StatusCancelled Status = "cancelled"
)

type LineItem struct {
    sku      string
    quantity int
    price    Money
}

func NewLineItem(
    sku string, qty int, price Money,
) (LineItem, error) {
    if sku == "" {
        return LineItem{}, errors.New("sku required")
    }
    if qty <= 0 {
        return LineItem{}, errors.New("qty must be > 0")
    }
    return LineItem{
        sku: sku, quantity: qty, price: price,
    }, nil
}

func (li LineItem) Subtotal() Money {
    return Money{
        amountCents: li.price.amountCents *
            int64(li.quantity),
        currency: li.price.currency,
    }
}

Enter fullscreen mode Exit fullscreen mode

LineItem is a value object inside the aggregate. The aggregate root owns the slice and is the only thing that mutates it.

type Order struct {
    id        string
    status    Status
    items     []LineItem
    total     Money
    createdAt time.Time
}

var (
    ErrEmptyOrder       = errors.New("order has no items")
    ErrAlreadyConfirmed = errors.New("order already confirmed")
    ErrNotDraft         = errors.New("only draft orders accept items")
)

func (o *Order) AddItem(item LineItem) error {
    if o.status != StatusDraft {
        return ErrNotDraft
    }
    newTotal, err := o.total.Add(item.Subtotal())
    if err != nil {
        return err
    }
    o.items = append(o.items, item)
    o.total = newTotal
    return nil
}

func (o *Order) Confirm() (Event, error) {
    if o.status == StatusConfirmed {
        return nil, ErrAlreadyConfirmed
    }
    if len(o.items) == 0 || o.total.IsZero() {
        return nil, ErrEmptyOrder
    }
    o.status = StatusConfirmed
    return OrderConfirmed{
        OrderID:     o.id,
        Total:       o.total,
        ConfirmedAt: time.Now(),
    }, nil
}

Enter fullscreen mode Exit fullscreen mode

Three things matter here:

  1. The line items live inside the order. There is no LineItemRepository. The repository works at the aggregate level — OrderRepository.Save(*Order) writes the order row and all its line item rows in one transaction.
  2. Confirm() returns an event. The event records what happened so other aggregates can react to it later, outside this transaction.
  3. The total invariant (total equals sum of subtotals) is enforced by the only path that mutates items. There is no setter that can break it.

The persistence layer follows the same shape. One transaction, one aggregate.

package postgres

import (
    "context"
    "database/sql"

    "myapp/order"
)

type OrderRepo struct {
    db *sql.DB
}

func (r *OrderRepo) Save(
    ctx context.Context, o *order.Order,
) error {
    tx, err := r.db.BeginTx(ctx, nil)
    if err != nil {
        return err
    }
    defer tx.Rollback()

    if err := upsertOrderRow(ctx, tx, o); err != nil {
        return err
    }
    if err := replaceLineItems(ctx, tx, o); err != nil {
        return err
    }
    return tx.Commit()
}

Enter fullscreen mode Exit fullscreen mode

The transaction touches one aggregate's tables. That's the rule, made concrete.

Inventory Is a Different Aggregate, So It Is Eventually Consistent

When an order is confirmed, the warehouse needs to decrement stock. The naive instinct is to do it in the same transaction: confirm the order and decrement inventory, atomically. That feels safer. It is not.

Confirming an order and decrementing inventory are different consistency boundaries:

  • An order's invariants (status transitions, total integrity, line-item rules) belong to the order's lifecycle.
  • Inventory's invariants (stock counts never go negative, reservations expire, a SKU's available quantity is total minus reserved minus committed) belong to the warehouse's lifecycle.

Forcing them into one transaction means every order confirmation locks rows in two domains. Under load, you get contention, deadlocks, and the slow death of a service that used to be fast. Worse: the day someone wants to shard inventory by warehouse or move it to a separate service, the transactional coupling has to be ripped out everywhere.

Two aggregates. One emits an event. The other reacts.

package inventory

import (
    "context"
    "errors"
)

type SKU string

var ErrInsufficientStock = errors.New("insufficient stock")

type Stock struct {
    sku       SKU
    available int
    reserved  int
}

func (s *Stock) Reserve(qty int) error {
    if qty > s.available-s.reserved {
        return ErrInsufficientStock
    }
    s.reserved += qty
    return nil
}

Enter fullscreen mode Exit fullscreen mode

The application service for inventory listens for OrderConfirmed events and reserves stock. Each reservation is its own transaction, on its own aggregate.

package app

import (
    "context"
    "log/slog"
)

type InventoryHandler struct {
    stocks StockRepository
    log    *slog.Logger
}

func (h *InventoryHandler) On(
    ctx context.Context, evt OrderConfirmed,
) error {
    for _, line := range evt.Lines {
        s, err := h.stocks.Get(ctx, line.SKU)
        if err != nil {
            return err
        }
        if err := s.Reserve(line.Quantity); err != nil {
            h.log.Warn(
                "reserve failed",
                "sku", line.SKU,
                "err", err,
            )
            return err
        }
        if err := h.stocks.Save(ctx, s); err != nil {
            return err
        }
    }
    return nil
}

Enter fullscreen mode Exit fullscreen mode

If reservation fails, the order is already confirmed. The system has to compensate by marking the order as awaiting-restock and notifying the customer. That sounds scary, but the alternative is worse. Distributed transactions across Order and Inventory would have failed at the database driver, with half-applied state and a stack trace nobody wants to debug at 2am.

The publish path is usually a transactional outbox. When Order.Confirm() returns the event, the application service writes the event to an outbox table inside the same transaction that saves the order. A separate process drains the outbox to Kafka, NATS, or whatever bus the system uses. Two transactions, atomic where it counts.

The Counter-Example: The Cart That Knows Too Much

Here is the failure mode the rule is designed to prevent. A Cart aggregate that holds live Product references.

type Cart struct {
    id    string
    items []*Product
}

type Product struct {
    sku   string
    price Money
    stock int
}

Enter fullscreen mode Exit fullscreen mode

This looks innocent. It is not. Product is its own aggregate — it has its own lifecycle (price changes, stock movements, catalog updates). Holding a pointer to it inside Cart means:

  • Adding a Product to a Cart doesn't capture the price at the time of adding. The cart's total recalculates if a price changes.
  • Decrementing Product.stock from a checkout requires a transaction that spans Cart and Product. Two aggregates, one transaction. The rule is violated.
  • Two carts that hold the same *Product pointer can race each other through the Product's stock counter. Goodbye to in-process invariants.

The fix is small and important: a cart references the product by ID and by snapshotted value, not by pointer.

type CartItem struct {
    sku       string
    name      string
    unitPrice Money
    quantity  int
}

type Cart struct {
    id    string
    items []CartItem
}

func (c *Cart) Add(item CartItem) error {
    // ... invariant checks
    c.items = append(c.items, item)
    return nil
}

Enter fullscreen mode Exit fullscreen mode

The cart now owns its own state. Adding a cart item snapshots the price the customer saw. Catalog updates do not silently change the cart's total. If you ever need fresh pricing, you do it explicitly. The application service fetches the current Product, builds a new CartItem, and the cart records the new value.

This is what the rule protects you from: aggregates that pretend the boundary doesn't exist.

The Three Questions That Place Any Boundary

Before you create a new aggregate (or argue about an existing one), answer these:

  1. What invariant must hold transactionally? Write it as a sentence. "An order's total equals the sum of its line items." If the answer is "none," it is probably not an aggregate — it might just be a read model.
  2. Can the world tolerate a small lag here? If it can, draw the line. The two sides become separate aggregates and they communicate through events.
  3. Whose lifecycle is this? State that changes together belongs together. State that changes on different schedules, driven by different actors and different commands, belongs in different aggregates.

Three questions. Most of the "should X be inside Y?" debates collapse once those questions are answered honestly.

You can argue about ubiquitous language and bounded contexts forever and still ship a broken system. Get the consistency boundary right and the next argument is usually about something that actually matters.


If this was useful

The longer version of this argument — outbox patterns, repository design at aggregate granularity, where the application service belongs, and what to do when an aggregate genuinely needs to grow — is two chapters of Hexagonal Architecture in Go. Pairs with The Complete Guide to Go Programming if the language fundamentals are still settling in.

Thinking in Go — the 2-book series on Go programming and hexagonal architecture