Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Multitenancy work in App Engine with Objectify?

How does multi-tenancy with namespaces in app engine works? My application has multiple users and each users are sort of like tenants in multi-tenancy. Their URL starts with domain/customer/companyToken#pageName?param1&param2. So from the Google docs if I want to apply a multi-tenancy with namespace with each customers you need to assign a unique ids for NamespaceManager to each customers So something like below:

NamespaceManager.set(request.getServerName());

Now I have a few questions.

  1. How does the multi-tenancy with Namespace for the App Engine really work?

  2. How does it change the way we access data in general?

  3. How does it change the way we access data using Objectify?

  4. First my understanding of applying above to the application is that when retrieving the data all the data related to above customers(tenants) are gathered together in the same namespace, so how does the way we access data change using the Objectify? Currently Company obj serves as parent of all the obj related to customers. (So in case of my application?)

Thank you so much in advance.

like image 997
user_1357 Avatar asked Feb 25 '23 07:02

user_1357


2 Answers

  1. Google AppEngine is not open-source so only Google really know how this works internally. But there is some publicly-available data about how datastore is built internally: http://www.youtube.com/watch?v=tx5gdoNpcZM. Basically all AppEngine data is in one table (yes one table for ALL AppEngine applications) distributed across multiple computers. Every entity has Key by which it is uniquely identified (id, parent, which you can see in your app), but there is also a data that tells to which app an Entity belongs. This data is AppEngine internal and user does not see it. My guess is that this part is also extended to include namespace data.

  2. It doesn't. Datastore API is namespace-aware so all you code stays the same. It internally knows to which namespace an Entity belongs.

  3. Objectify is built on top of low-level Datastore API, so answer is the same as 2.

  4. Namespaces compartmentalizes data: once you set a namespace, Datastore API will only see Entities that were added under this namespace.

like image 159
Peter Knego Avatar answered Feb 26 '23 19:02

Peter Knego


  1. At the storage levels of datastore, namespace is just like an app-id. Each namespace essentially looks to the datastore as another view into the app's data. This is why operations like query cannot span namespaces (at least for now). Even id ranges are different per namespace.

  2. You need to be aware of which namespace you're really using. e.g. Once an entity is created, it's namespace does not change, so doing a NamespaceManager.set(...) will have no effect on its key. Similarly with a Query object. Once a query is created, its namespace is set. Same with MemcacheService. Hence it's important to know which objects have which namespaces if you're in the habbit of calling NamespaceManager.set more than from the beginning of the request.

  3. It's hard to know how Obectify uses the datastore API, so changing the current namespace multiple times in one request needs to be done with the knowledge of how Objectify uses the datastore API to ensure you don't inadvertently create entities in unintended namespaces.

  4. In general, you meed to know when Objectify creates the low level Key, Entity and Query objects and make sure that the current namespace is set to the namespace you intend. It's easy if there is ever only one namespace per request.

like image 44
Gianni Mariani Avatar answered Feb 26 '23 19:02

Gianni Mariani