I would like to keep some application configuration entries as entities in the datastore. Now, when I update these entries from the Datastore Viewer (development environment) or Google Cloud Platform Datastore user interface (production environment), the new values aren't seen by the application. This is because ndb caches the entities.
My (quite open) question is: what is your technique for keeping configuration entries in the datastore, updating them from Google's user interface, and also providing fresh values to the application?
I have thought of the following:
Set a reasonable
_memcache_timeout
on thendb.Model
subclass which defines the configuration entries - but memcache usage is suboptimal in this case (unnecessary datastore reads are performed).Keep cache settings at maximum, but implement a
flush
operation in the admin area of the application to flush entities individually. This is tricky because you can't be sure of the actual cache key of the entity. But normally it should be_memcache_prefix + key.urlsafe()
, where_memcache_prefix
is defined in ndb.context module.Update all configuration entries from the admin area of the application, and don't use Google's datastore user interfaces - this requires extra effort.
Option 1 - Most work
Implement your own admin module in App Engine using the NDB client libraries. If you make changes to the entity via your own app then it would naturally write the correct memcache key for you.
Option 2 - Trading cost for update delays
As you already suggested, reduce maximum cache age for the model. For example, in most cases allow a 1 hour period before changes are live is not an unreasonable.
Option 3 - Least work, but a bad idea at scale
Flush the entire cache via the Cloud Console for memcache.
Warning: If you run at significant scale this is a bad idea, as it could cause a huge spike in requests. For example, if you are running 1 million reads/second to datastore after a 90% cache hit rate, you could spike to 10 million reads/second immediately upon flush.