#30601: ATOMIC_REQUESTS=True and caching might be dangerous
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  sebastian-philipp                  |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30601>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/060.78b79a7675f0154a74bb3ac8d7f5922c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to