Outside Transactions¶
Warning¶
Using the outbox outside a transaction defeats its purpose:
# BAD: Not in a transaction
order = Order.objects.create(...)
send_email.delay(order.id) # Written to outbox, but not atomic with Order
The relay logs a warning when this happens:
When It's Acceptable¶
Outside transactions may be acceptable for:
- Background jobs that don't need atomicity
- Tasks triggered by management commands
- Testing and development
Recommendations¶
- Use transactions: Wrap related operations in
transaction.atomic() - Enable warnings: Check logs for
celery_outbox_not_in_transaction - Consider excluded tasks: If a task doesn't need transactional guarantees, add it to
CELERY_OUTBOX_EXCLUDE_TASKS