How to access the google app engine ndb datastore from a python2.7 microservice in the same app project

Chez

I have been searching away at this but cannot seem to find a simple example of how to access the ndb datastore from a python microservice which lives in the same application as other non service modules.

What I want to do is access the actual Model classes of the datastore, i.e. Users...and then query on that class from the microservice.

I know you cannot use the google cloud datastore api in app engine standard, but there must be another way, surely?

The same applies to the shared memcache, if I make an API call to an endpoint in a module (no service) from the microservice and set something in memcache at the endpoint, I cannot see that in the microservice. So when Google talk about shared datastore and memcache across everything in the same application including microservices, how do they propose you access it?

I'm sure I'm missing something, just can't find it.

Dan Cornilescu

To clarify: the microservice term doesn't mean anything particularly special in GAE with respect to Datastore interaction (or otherwise) - all GAE services/modules are equals from this perspective. What makes a GAE service/module a microservice is simply the functionality it performs, not its implementation or how it uses the infrastructure, see Microservices Architecture on Google App Engine.

All services of the same GAE project/application that access the datastore using the ndb library can do that by default, without any restriction or additional service configurations.

The only trick is that all services referencing a particular entity type must have a consistent view of that entity's model definition. This is a requirement coming from the particular implementation of the ndb client library, not from the datastore itself.

The simplest way to obtain such consistent view is IMHO by sharing the same ndb model definition source file(s), which can be achieved by symlinking the same actual source file(s) (or the directories holding them) across the multiple service/module directories, as described in Sharing entities between App Engine modules.

In other words all your services/modules needing to query/access/reference Users entities one way or the other would actually have the same Users model definition available to do so.

Care must be exercised when deploying changes to the model definitions (be it in different services or even between different versions of the same service) either by:

  • ensuring backward-compatibility with migration strategies
  • proper deployment orchestration - i.e. ensuring incompatible versions/services never run simultaneously

The same technique can be used for memcache, in a similar manner: a shared source code file would export the definitions of memcache keys for the memcached values that need to be shared across services. Or, better yet, provide actual read/write functions for the corresponding data, to ensure not only that the data is stored under the right keys, but it also has the matching format.

With such shared code in place the memcached data representing certain pieces of information becomes shared across the same app's services/modules (again, regardless of them being microservices or not). Almost as good as the info shared via the datastore, except without transaction support to guarantee consistency.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make query of AND & OR operation from list of elements we we to search in ndb datastore using python in Google App Engine

upgrading from python2.7 to python3.7 in google app engine and ndb datastore

How to access one Cloud Datastore(Google App Engine) from Android app and django-website?

How do I update a query from Google App Engine NDB?

Google App Engine modules no datastore access

Access Google Cloud Datastore from another app/project

How to delete all entities for NDB Model in Google App Engine for python?

Google App Engine: Counting entries from the datastore

Inconsistent Fetch From Google App Engine Datastore

Transferring data from product datastore to local development environment datastore in Google App Engine (Python)

Switching from db.ReferenceProperty to ndb with Google App Engine?

Memory leak in Google App Engine / Datastore / Flask / Python app

google app engine - ndb query to only get a few columns in python

Google App Engine NDB Queries and exceeding memory

Google App Engine NDB Query on Many Locations

Filtering by BooleanProperty using NDB in Google App Engine

Google App Engine - NDB - INDEX Quesions

Google App Engine NDB Retrieving Float Property

Google App Engine about key in ndb

How to access Google cloud library in python3 standard app engine project?

how can we run google app engine with python3 with ndb on local

Making a Google App Engine datastore key from a string

AttributeError when listing all entities from Google App Engine datastore

Google app engine: creating datastore entity from the post data

Import Error when trying to migrate Google App Engine Python 2 app using NDB to the Cloud NDB Python 3 library

How to store user-specific data from Android in Google App Engine Datastore. Ancestor or not?

How can I reduce Google App Engine datastore latency?

How to filter by date/time in Google App Engine Datastore using GoLang

How to refresh configuration entries kept in Google App Engine Datastore?