Prompt
How do I trigger push notifications from database events?
Latest observation
To trigger push notifications from database events, you usually need three pieces:
- A way to detect the DB change
- A backend service to turn that change into a notification
- A push provider to deliver it to devices/web clients
Common approaches
1) Database triggers + backend listener
- Create a trigger in your database on
INSERT,UPDATE, orDELETE. - Have the trigger write an event into an outbox table or publish to a queue.
- A backend worker reads the event and sends the push notification.
Why this is preferred:
It avoids sending notifications directly inside the DB trigger, which is fragile and can slow down transactions.
2) Change Data Capture (CDC)
Use a CDC tool to stream database changes to your app:
- PostgreSQL: logical replication / Debezium
- MySQL: binlog / Debezium
- MongoDB: change streams
Your worker consumes the change stream and sends notifications.
Good for: high-throughput systems and reliable event pipelines.
3) Polling
A scheduled job checks the database for new rows or status changes and sends notifications.
Simplest to build, but least efficient because it adds delay and repeated queries.
Recommended architecture
DB event → outbox table / CDC → background worker → push service
Example flow:
- User order status changes in DB
- Trigger inserts a record into
notification_events - Worker picks it up
- Worker sends push via:
- Firebase Cloud Messaging (FCM) for Android/web
- Apple Push Notification service (APNs) for iOS
- Or a unified provider like OneSignal, Pusher, Expo, etc.
Example: outbox pattern
Database trigger idea
Instead of sending push directly, insert an event:
INSERT INTO notification_outbox (event_type, payload, created_at)
VALUES ('order_shipped', '{"user_id": 123, "order_id": 456}', NOW());
Worker logic
- Query unsent rows from
notification_outbox - Send push
- Mark row as processed
This is reliable because the notification event is stored transactionally with the DB change.
Important best practices
- Don’t send push directly from a DB trigger
- Use an outbox table or message queue
- Make the worker idempotent so duplicate events don’t send duplicate notifications
- Store user device tokens
- Handle retries and dead-lettering
- Respect user notification preferences
If you tell me your stack…
I can give you a concrete implementation for:
- PostgreSQL + Node.js
- MySQL + Python
- Firebase/FCM
- Supabase
- MongoDB change streams
- AWS-based architecture