Opened 5 years ago

Closed 14 months ago

Last modified 14 months ago

#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 Simon Charette, 5 years ago

Component: Database layer (models, ORM)Documentation
Needs documentation: set
Summary: ATOMIC_REQUESTS=True and caching might be dangerousExtend documentation about transaction rollbacks to mention global state mutations such as caching
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

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 on transaction.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 using transaction.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.

comment:2 by Angelica Ferlin, 20 months ago

Owner: changed from nobody to Angelica Ferlin
Status: newassigned

comment:3 by Angelica Ferlin, 20 months ago

Has patch: set

comment:4 by Angelica Ferlin, 19 months ago

Owner: Angelica Ferlin removed
Status: assignednew

comment:5 by Natalia Bidart, 19 months ago

Has patch: unset
Needs documentation: unset
Version: 2.2dev

comment:6 by Lufafa Joshua, 15 months ago

Owner: set to Lufafa Joshua
Status: newassigned

comment:8 by Natalia Bidart, 14 months ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:9 by Natalia <124304+nessita@…>, 14 months ago

Resolution: fixed
Status: assignedclosed

In aa80b357:

Fixed #30601 -- Doc'd the need to manually revert all app state on transaction rollbacks.

comment:10 by Natalia <124304+nessita@…>, 14 months ago

In c8ac50c:

[5.0.x] Fixed #30601 -- Doc'd the need to manually revert all app state on transaction rollbacks.

Backport of aa80b357fbef46e5b6faa08d63bcfd4fe21f3776 from main

comment:11 by Natalia <124304+nessita@…>, 14 months ago

In 696fbc3:

[4.2.x] Fixed #30601 -- Doc'd the need to manually revert all app state on transaction rollbacks.

Backport of aa80b357fbef46e5b6faa08d63bcfd4fe21f3776 from main

comment:12 by Natalia <124304+nessita@…>, 14 months ago

In 9b18af4f:

Refs #30601 -- Fixed typos in docs/topics/db/transactions.txt.

comment:13 by Natalia <124304+nessita@…>, 14 months ago

In acd4595:

[5.0.x] Refs #30601 -- Fixed typos in docs/topics/db/transactions.txt.

Backport of 9b18af4f6f12b9d25157e0b5afc3dca198f6dd06 from main

comment:14 by Natalia <124304+nessita@…>, 14 months ago

In 3fae5d9:

[4.2.x] Refs #30601 -- Fixed typos in docs/topics/db/transactions.txt.

Backport of 9b18af4f6f12b9d25157e0b5afc3dca198f6dd06 from main

Note: See TracTickets for help on using tickets.
Back to Top