Prompt
I'm building transactional notifications for account and order events
Latest observation
A solid way to build transactional notifications for account and order events is to treat them as a small event-driven messaging system, not just “send an email when X happens.”
1) Define the event catalog
Start by listing the exact events you want to notify on.
Account events
- Account created
- Email verified
- Password changed
- Password reset requested / completed
- Two-factor authentication enabled/disabled
- Login from new device
- Account suspended / reactivated
Order events
- Order placed
- Payment authorized
- Payment failed
- Order packed
- Shipped
- Delivered
- Partially refunded / fully refunded
- Cancelled
- Returned
For each event, define:
- Trigger source: what system action emits it
- Recipient: customer, admin, or both
- Channels: email, SMS, push, in-app
- Priority: high/medium/low
- Template variables: order number, masked email, delivery ETA, etc.
2) Use a canonical event schema
Use one normalized payload structure for all notifications.
Example:
{
"event_type": "order.shipped",
"event_id": "evt_123",
"occurred_at": "2026-10-13T10:20:30Z",
"user_id": "usr_456",
"order_id": "ord_789",
"channel_preferences": ["email", "push"],
"data": {
"order_number": "A10023",
"tracking_number": "1Z999...",
"carrier": "UPS",
"estimated_delivery": "2026-10-16"
}
}
Include:
event_idfor idempotencyoccurred_at- entity IDs
datafor template fields- optional
localeandtimezone
3) Separate event capture from delivery
Don’t send notifications directly from business logic if you can avoid it.
Recommended flow:
- App performs action
- Domain event emitted
- Event stored or queued
- Notification service consumes event
- Service decides:
- whether notification is required
- which channels to use
- which template to render
- whether to suppress duplicates
- Delivery provider sends message
This makes retries, auditing, and scaling much easier.
4) Build template and content management
Use templates per:
- event type
- channel
- locale
Example:
order.shipped.email.enorder.shipped.sms.enaccount.reset_password.email.es
Template variables should be explicit and validated to avoid broken messages.
5) Add user preferences and compliance
Respect:
- communication preferences
- locale/language
- quiet hours for non-critical channels
- legal requirements
For transactional notifications:
- Some can be exempt from marketing consent, but still should follow privacy rules.
- Critical security messages may need to bypass some preference settings, depending on policy and law.
6) Handle retries and idempotency
Transactional notifications must avoid duplicates.
Best practices:
- Deduplicate by
event_id - Store delivery attempts
- Retry transient failures with backoff
- Mark permanently failed messages
- Keep an audit log
7) Decide which events are immediate vs batched
Immediate:
- password reset
- payment failed
- order shipped
- login alert
Can be batched or delayed:
- order status updates if frequent
- low-priority account reminders
8) Consider a state machine for orders
Orders often have transitions. Only notify on valid transitions.
Example:
paid -> packed -> shipped -> delivered
This avoids sending “shipped” twice if the system reprocesses the same update.
9) Observability and support
Track:
- event received
- template rendered
- provider accepted
- provider delivered / bounced / failed
- latency by event type
- failure rate by provider/channel
This is essential for support teams and debugging.
10) Suggested architecture
A simple practical setup:
- Application services emit domain events
- Message broker/queue: Kafka, SNS/SQS, RabbitMQ, Pub/Sub
- Notification service:
- routing
- templates
- preference checks
- delivery orchestration
- Delivery providers:
- Email: SES, SendGrid, Mailgun
- SMS: Twilio
- Push: FCM/APNs
- In-app: database/websocket
11) Example notification rules
account.password_reset_requested- email only
- immediate
- no opt-out
order.shipped- email + push
- immediate
- include tracking link
order.payment_failed- email immediately
- SMS if high-value order or repeated failure
12) Recommended minimum data model
notification_eventsnotification_templatesnotification_deliveriesuser_notification_preferencesnotification_logs
If you want, I can help you with any of these next:
- a notification event schema
- a database schema
- a microservice architecture
- sample templates for account/order notifications
- API design for creating and sending notifications