#30601 closed Cleanup/optimization (fixed)
Extend documentation about transaction rollbacks to mention global state mutations such as caching
Reported by: | Sebastian Wagner | Owned by: | Lufafa Joshua |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
ATOMIC_REQUESTS=True essentially creates nested transactions. And nested transactions might not play well with custom caching.
A small example of how to use nested transactions and caching to get inconsistent data:
my_mem_cache_a = ... my_mem_cache_b = ... def set_a(x): with transaction.atomic(): a = ... a.x = x a.save() my_mem_cache_a = x def set_a_b(x, y): with transaction.atomic(): set_a(x) b = ... b.y = y raise Exception() # at this point, my_mem_cache_a is out of sync with the database. b.save() my_mem_cache_b = x set_a_b(...)
(this example also works without transaction.atomic() )
Would be nice to add a warning to
https://docs.djangoproject.com/en/2.2/topics/db/transactions/#tying-transactions-to-http-requests
that ATOMIC_REQUESTS=True might be harmful, if code
- uses caching
- proactively updates data in the cache
- makes the assumption that values are updated in the database after a transaction is finished.
Change History (14)
comment:1 by , 5 years ago
Component: | Database layer (models, ORM) → Documentation |
---|---|
Needs documentation: | set |
Summary: | ATOMIC_REQUESTS=True and caching might be dangerous → Extend documentation about transaction rollbacks to mention global state mutations such as caching |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 19 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 19 months ago
Has patch: | set |
---|
Link to PR: https://github.com/django/django/pull/16848
comment:4 by , 18 months ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:5 by , 18 months ago
Has patch: | unset |
---|---|
Needs documentation: | unset |
Version: | 2.2 → dev |
comment:6 by , 14 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:8 by , 13 months ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
I'm not sure how related to
ATOMIC_REQUEST
this issue actually is, it seems like a common pitfall of mixing database transactions and caching and a good reason to rely ontransaction.on_commit
to defer cache alterations instead.The
atomic
documentation already mentions that model state should be reverted (#28479) so maybe we could adjust this block to mention that all form of state, including global one such as caching, should be reverted and that usingtransaction.on_commit
can be used to deal with global state alterations.Tentatively accepting based on the premise you might be interested in submitting a patch yourself.