Cache Cow Has a Bad First Day

Looks like my first production run with memcached wasn't as successful as I thought it would be. At least one case remains that makes SQLAlchemy complain about unbound model objects and, for some reason, the whole thing feels a lot slower before caching kicks in.

I'll give it another go tomorrow (which means today -- maybe removing the time from the date string wasn't without downsides). Sorry for the inconvenience, i.e. about four error-prone hours and 500 hail.

Programming with caching in mind makes me look so different at stuff.

Update: The issue should be fixed by now. As I already knew, the errors were caused by only having some SQLAlchemy declarative model objects cached, but not their (by default) lazy-loaded foreign attributes. So when the cache was hit later, the objects itself were useful for their actual purpose only in a limited scope, as accessing attributes failed due to the session being not longer bound to the original object.

The error message looks something like this:

Traceback (most recent call last):
  File "models.py", line 123, in <module>
UnboundExecutionError: Parent instance <class 'homework.models.Newspost'> is not bound to a Session, and no contextual session is established; lazy load operation of attribute 'tags' cannot proceed

Being a little bit naïve, I set explicit eager loading (lazy=False) for the affected model objects. Unfortunately, that resulted in massive amounts of additional query load which was the reason for everything reacting so much slower.

My current solution is to use eager loading only in the query that will be cached:

from sqlalchemy.orm import eagerload

newsposts = Newspost.query.options(eagerload('tags')).all()