I have 4 EC2 servers running a Django 1.3.2 application with uWSGI. They all share a MySQL server on Amazon RDS. I'm experiencing some behavior where if a new objects is created through the admin and I try:
get_object_or_404(Class, pk=new_object.pk)
sometimes it will find an object, but other times it will return a 404.
What could be going on?
This is how I'm using get_object_or_404:
# Module level variables
if settings.DEBUG:
articles = News.objects.filter(status='live')
else:
articles = News.objects.all()
dev view(request, slug):
article = get_object_or_404(articles, slug=slug)
....
If I restart all my uWSGI processes, the problem goes away.
Questions and things I've tried:
Everything works as expected on a single server, but as soon have multiple servers, I start to experience the behavior.
Module level variables are definitely a problem - querysets are "lazy" only as long as they are not used (iteration, evalution, whatever), as soon as you start retrieving records it does populate a cache (hint: Django is open-source, you can just ReadTheCode(tm) when the doc is not enough).
You might also have troubles with transaction isolation level if you use InnoDB tables (cf Django's doc on this last point).