---
id: notification-delivery
name: Notification Delivery
version: 1.0.0
summary: >-
  Finance emits order-notification commands over Kafka; Notification API
  applies Redis idempotency, renders MJML email templates, and submits MIME
  messages over SMTP with bounded retries and dead-lettering.
owners:
  - platform-engineering-team
badges:
  - content: Platform Capability
    backgroundColor: blue
    textColor: blue
steps:
  - id: finance
    title: Finance emits order notification command
    summary: >-
      Publishes placement, completion, or cancellation notification commands
      over Kafka without coupling order status to email delivery.
    service:
      id: order-orchestration
      version: 1.0.0
    next_steps:
      - id: place-order
        label: New order placed
      - id: complete-order
        label: Order completed
      - id: cancel-order
        label: Order cancelled or orchestration timed out
  - id: place-order
    title: PlaceOrderCommand
    message:
      id: PlaceOrderCommand
      version: 1.0.0
    next_step:
      id: placement-channel
      label: Publish to finance.place-order
  - id: complete-order
    title: CompleteOrderCommand
    message:
      id: CompleteOrderCommand
      version: 1.0.0
    next_step:
      id: notification-api
      label: Deliver via finance.complete-order
  - id: cancel-order
    title: CancelOrderCommand
    message:
      id: CancelOrderCommand
      version: 1.0.0
    next_step:
      id: notification-api
      label: Deliver via finance.cancel-order
  - id: placement-channel
    title: Finance placement channel
    channel:
      id: bookworm.{env}.finance.place-order
      version: 1.0.0
    next_step:
      id: notification-api
      label: Consume placement command
  - id: notification-api
    title: Notification API deduplicates and renders email
    summary: >-
      Checks a 24-hour Redis idempotency guard keyed by message ID, skips blank
      recipients, and renders the order email template into a MIME message.
    service:
      id: notification-api
      version: 1.0.0
    next_steps:
      - id: smtp-provider
        label: Valid recipient and template rendered
      - id: notification-skipped
        label: Recipient email absent
  - id: notification-skipped
    title: Email delivery skipped
    summary: >-
      Returns cleanly without calling SMTP when no recipient address is present.
    custom:
      title: Email skipped
      type: Outcome
      icon: CheckCircleIcon
      color: gray
  - id: smtp-provider
    title: SendGrid - SMTP submission
    summary: >-
      Submits the rendered MIME email over SMTP with MailKit resilience (Mailpit
      locally, SendGrid in Azure).
    service:
      id: sendgrid-api
      version: 1.0.0
    next_steps:
      - id: notification-sent
        label: SMTP provider accepted message
      - id: notification-failed
        label: Submission failed after retries
  - id: notification-sent
    title: Email submitted to SMTP provider
    summary: >-
      Provider accepted the message for delivery. Notification emits no
      delivery-result callback.
    custom:
      title: Email submitted
      type: Outcome
      icon: CheckCircleIcon
      color: green
  - id: notification-failed
    title: Notification dead-letter queue
    summary: >-
      Exhausted transient failures route the command to the dead-letter queue
      for operator inspection.
    channel:
      id: bookworm.{env}.notification.dead-letter-queue
      version: 1.0.0
---

## Trigger & Business Outcome

This flow is triggered when [[service|order-orchestration]] publishes [[command|PlaceOrderCommand]], [[command|CompleteOrderCommand]], or [[command|CancelOrderCommand]] over Kafka. Success is confirmed when [[service|notification-api]] passes its Redis idempotency check, renders the MJML order template, and hands the MIME message off to [[service|sendgrid-api]] over SMTP (or Mailpit in local development) without maintaining any relational email outbox.

## Interactive Flow Visualization

<NodeGraph />

## Step-by-Step Walkthrough

<Steps title="Stateless Email Rendering and Resilient SMTP Handoff">
	<Step title="Consume order notification command">
		[[service|notification-api]] consumes [[command|PlaceOrderCommand]],
		[[command|CompleteOrderCommand]], or [[command|CancelOrderCommand]] from Kafka partitioned by
		Order ID.
	</Step>
	<Step title="Apply Redis idempotency and validate recipient">
		A Redis-backed idempotency guard (24-hour TTL keyed by message ID) prevents duplicate sends on
		Kafka redeliveries or worker retries. If the recipient email address is blank, processing ends
		cleanly without calling SMTP.
	</Step>
	<Step title="Render MJML template and submit over SMTP">
		[[service|notification-api]] renders the order email template into a MIME message and submits it
		to [[service|sendgrid-api]] (or Mailpit locally) through a resilient SMTP pipeline. No
		delivery-result callback is published back to [[domain|ordering]].
	</Step>
</Steps>

## Failure, Retry & Compensation Behavior

| Failure Point                            | System Behavior                                                                                                                                                                                                 | Compensation / Recovery                                                                                                                                                                  |
| :--------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Blank recipient email**                | Handler returns immediately without calling SMTP                                                                                                                                                                | No error or retry; order state is unaffected                                                                                                                                             |
| **Transient SMTP throttling or timeout** | MailKit resilience and worker cooldown retries execute                                                                                                                                                          | If retries exhaust, the command routes to <ResourceLink id="bookworm.{env}.notification.dead-letter-queue" type="channel">`bookworm.{env}.notification.dead-letter-queue`</ResourceLink> |
| **Finance timeout reissue**              | <ResourceLink id="order-orchestration" type="service">order-orchestration</ResourceLink> emits a new <ResourceLink id="PlaceOrderCommand" type="command">PlaceOrderCommand</ResourceLink> with a new message ID | Bypasses per-message-ID deduplication and may send a duplicate placement email                                                                                                           |

:::note
**Stateless worker boundary:** [[service|notification-api]] owns no PostgreSQL database and no email outbox. SMTP acceptance confirms handoff to the mail provider, not final inbox delivery.
:::

## Participating Resources

- **Domains & Systems:** [[domain|platform]], [[domain|ordering]], [[system|notification-system]], [[system|sendgrid]]
- **Services:** [[service|order-orchestration]], [[service|notification-api]], [[service|sendgrid-api]]
- **Messages:** [[command|PlaceOrderCommand]], [[command|CompleteOrderCommand]], [[command|CancelOrderCommand]]
- **Channels:** <ResourceLink id="bookworm.{env}.finance.place-order" type="channel">`bookworm.{env}.finance.place-order`</ResourceLink>, <ResourceLink id="bookworm.{env}.finance.complete-order" type="channel">`bookworm.{env}.finance.complete-order`</ResourceLink>, <ResourceLink id="bookworm.{env}.finance.cancel-order" type="channel">`bookworm.{env}.finance.cancel-order`</ResourceLink>, <ResourceLink id="bookworm.{env}.notification.dead-letter-queue" type="channel">`bookworm.{env}.notification.dead-letter-queue`</ResourceLink>
- **Related Views:** [[flow|user-checkout]], [[flow|order-lifecycle]]
