NServiceBus持久化解析:需持久化的数据是什么?
Great question—this is a super common point of confusion when switching from web app persistence (where user sessions are front-and-center) to distributed messaging with NServiceBus. Let’s break down exactly what NServiceBus needs to persist, and why those pieces are critical to keeping your distributed workflows reliable.
Core Data NServiceBus Persists
Here’s the key stuff that gets stored in your chosen persistence provider (like SQL Server, RavenDB, etc.):
Saga State
Sagas are NServiceBus’s tool for managing long-running business processes—think order fulfillment, customer onboarding workflows that span hours or days. Unlike stateless web requests, these workflows need to remember their progress between incoming messages. For example, an order saga might track whether payment was received, inventory was reserved, or shipping was initiated. This state gets persisted so if the service restarts mid-workflow, it can pick up right where it left off instead of starting over from scratch.Outbox Records
The Outbox pattern ensures atomicity between local database changes and message sends. When you process a message, update your database, and send a follow-up message, the Outbox first records the outgoing message in persistent storage. Only after your local database transaction commits does NServiceBus actually send the message. If the service crashes before sending, the Outbox will retry sending the message once it restarts—no lost messages, no duplicate database updates. These Outbox records stick around until they’re confirmed delivered.Subscription Metadata
In publish-subscribe scenarios, publishers need to know which subscribers care about which events. This subscription information is persisted so if the publisher restarts, it doesn’t lose track of who to send events to. For example, if a shipping service subscribes to "OrderPaid" events, that subscription record is stored so the order service keeps sending those events even after a reboot.Timeout Tasks
NServiceBus’s Timeout Manager handles delayed actions—like "send a payment reminder 24 hours after order creation" or "retry a failed inventory check in 10 minutes". These timeout details (message content, delay duration, target endpoint) are persisted so the service can still trigger them even if it restarts before the delay elapses.Recoverability Context
When messages fail to process (due to errors), NServiceBus uses recoverability policies to retry them or move them to error queues. For some retry strategies (like delayed retries), the system needs to track how many times a message has been retried, when the next retry should happen, and any error context. This data is persisted to avoid infinite retries or losing context about why a message failed.
Why This Matters (Instead of User Sessions)
NServiceBus is built for distributed systems where services are decoupled and stateless in terms of user interactions—but they do need to track the state of business processes and messaging operations. Persistence here is all about reliability and continuity: ensuring that your workflows don’t break when services restart, messages get lost, or errors occur.
内容的提问来源于stack exchange,提问作者Slava

