transaction.atomic good practices

When using transaction.atomic() we should remember that it can commit or rollback just the db related calls inside the atomic() block. It’s not able to rewind a HTTP call, a call to redis or putting a payload on any queueing system. This means in case of a rollback we’d emit an event or call an external API to inform it about an object which effectively was not saved to the database.

In the project I’m working on now a real life scenario would e.g. end up sending an e-mail or an SMS to a person when their order was not created and customer support would need to deal with this.

A good practice is tho have the atomic() block the minimal set of db calls which have to be run together. Everything else should ideally be moved out.

πŸ“– Celery and transaction.atomic Stack Overflow thread

πŸ“– Django docs on transaction.atomic

πŸ“– Database transactions β€” How do they work?

Ania Warzecha's Picture

About Ania Warzecha

Software architect. I get overly excited about things. Oversharing is my superpower βœ¨πŸš€