Question

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:

  1. Are module level variables the problem? I've tried to debug this but since querysets are lazy it doesn't seem like a problem.
  2. Is it a cache issue? I've restarted memcache and cleared Nginx's cache but the issue remains.
  3. Is it an RDS issue? I haven't found the answer to this.
  4. Is it a queryset cache issue? I haven't found the answer to this, and don't know how.

Everything works as expected on a single server, but as soon have multiple servers, I start to experience the behavior.

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top