NDB allows an App Engine app to connect to Cloud Datastore. It helpfully provides 2 types of caching enabled by default: memcache and in-context cache (also referred to as in-memory cache).
Timestamped
App Engine Python SDK 1.9.57
Anything wrong with it?
Yes and no. See this:
When fetching a fresh object even within the same thread, one would expect to get the value from the database and not the one from an in-memory cache. This is considered working as intended.
A happy clueless developer could write this:
LegoBox.count_pieces
has a bug in it where it forgets to call put
(bugs happen y’know).
Unless one is diligent to always disable the in-context cache for the fetches,
this test isn’t good enough,
because it doesn’t expose the bug.
YOLO!
Sadly this behaviour is not mentioned in the official documentation, so there’s a high chance one will discover this “desired” effect in a bitter situation while debugging for hours.
How to disable in-context cache
First strategy is to pass use_cache=False
when making queries.
Second option is to disable the cache per model. In theory this will slow down your app. In practice, I don’t have benchmarks, so I don’t know. Moreover it always depends on what you are doing.
Third stategy is to set a global policy to turn off automatic caching for all NDB models and be done with it.
I was a happy-go-lucky developer who wrote lots of passing tests that interact with NDB models, only later to discover that the code is failing in production. Luckily people smarter than me pointed out what might be the cause. This is a cautionary story to help one avoid such issues.