English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# Implementing Multitenancy Using Namespaces

The Namespaces API allows you to easily enable multitenancy in your application, simply by selecting a namespace string for each tenant in `appengine_config.py` using the `namespace_manager` package.

- Setting the Current Namespace
- Avoiding Data Leaks
- Deploying Namespaces
  - On a per-User Basis
  - In the Datastore
  - In the Memcache
  - In the Task Queue
  - In the Blobstore
  - In the Datastore Viewer
  - In the Bulkloader

## Setting the Current Namespace

You can get, set, and validate namespaces using `namespace_manager`. The namespace manager allows you to set a current namespace for namespace-enabled APIs (currently the datastore, memcache, and task queue). You set a current namespace up-front using `appengine_config.py`, and the datastore and memcache automatically use that namesapce.

Most App Engine developers will use their Google Apps domain as the current namespace. Google Apps lets you deploy your app to any domain that you own, so you can easily use this mechanism to configure different namespaces for different domains. Then, you can use those separate namespaces to segregate data across the domains. For more information about setting multiple domains in the Google Apps dashboard, see Deploying Your Application on Your Google Apps URL.

The following code sample shows you how to set the current namespace to the Google Apps domain that was used to map the URL. Notably, this string will be the same for all URLs mapped via the same Google Apps domain.

To set a namespace in Python, use the App Engine configuration system `appengine_config.py` in your application's root directory. The following simple example demonstrates how to use your Google Apps domain as the current namespace:

    from google.appengine.api import namespace_manager

    # Called only if the current namespace is not set.
    def namespace_manager_default_namespace_for_request():
        # The returned string will be used as the Google Apps domain.
        return namespace_manager.google_apps_namespace()

If you do not specify a value for `namespace`, the namespace is set to an empty string. The `namespace` string is arbitrary, but also limited to a maximum of 100 alphanumeric characters, periods, underscores, and hyphens. More explicitly, namespace strings must match the regular expression `[0‑9A‑Za‑z._‑]{0,100}`.

By convention, all namespaces starting with "`_`" (underscore) are reserved for system use. This system namespace rule is not enforced, but you could easily encounter undefined negative consequences if you do not follow it.

## Avoiding Data Leaks

One of the risks commonly associated with multitenant apps is the danger that data will leak across namespaces. Unintended data leaks can arise from many sources, including:

- Using namespaces with App Engine APIs that do not yet support namespaces. For example, Blobstore does not support namespaces. If you use Namespaces with Blobstore, you need to avoid using Blobstore queries for end user requests, or Blobstore keys from untrusted sources.
- Using an external storage medium (instead of memcache and datastore), via `UrlFetch` or some other mechanism, without providing a compartmentalization scheme for namespaces.
- Setting a namespace based on a user's email domain. In most cases, you don't want all email addresses of a domain to access a namespace. Using the email domain also prevents your application from using a namespace until the user is logged in.

## Deploying Namespaces

The following sections describe how to deploy namespaces with other App Engine tools and APIs.

- On a per-User Basis
- In the Datastore
- In the Memcache
- In the Task Queue
- In the Blobstore
- In the Datastore Viewer
- In the Bulkloader

### On a per-User Basis

Some applications need to create namespaces on a per-user basis. If you want to compartmentalize data at the user level for logged-in users, consider using User.user_id() value, which returns a unique, permanent ID for the user. The following code sample demonstrates how to use the Users API for this purpose:

    from google.appengine.api import users

    def namespace_manager_default_namespace_for_request():
        # assumes the user is logged in.
        return users.get_current_user().user_id()

Typically, Apps that create namespaces on a per-user basis also provide specific landing pages to different users. In these cases, the application needs to provide a URL scheme dictating which landing page to display to a user.

### In the Datastore

By default, the datastore uses the current namespace setting in the namespace manager for datastore requests. The API applies this current namespace to Key or Query objects when they are created. Therefore, you need to be careful if an application stores Key or Query objects in serialized forms, since the namespace is preserved in those serializations.

If you are using deserialized Key and Query objects, make sure that they behave as intended. Most simple applications that use datastore (`put`/`query`/`get`) without using other storage mechanisms will work as expected by setting the current namespace before calling any datastore API.

Note: An application that reads Keys, or other namespace-aware objects, from untrusted sources (like the web browser client) introduces security vulnerabilities. Applications that rely on keys from untrusted sources must incorporate a security layer verifying that the current user is authorized to access the requested namespace.

Query and Key objects demonstrate the following, unique behaviors with regard to namespaces:

- Query and Key objects inherit the current namespace when constructed, unless you set an explicit namespace.
- When an application creates a new Key from an ancestor, the new key inherits the namespace of the ancestor.

The following code example shows the `SomeRequest` request handler for incrementing the count for the current namespace and the arbitrarily named `-global-` namespace in a `Counter` datastore entity.

    from google.appengine.api import namespace_manager
    from google.appengine.ext import db
    from google.appengine.ext import webapp

    class Counter(db.Model):
       """Model for containing a count."""
       count = db.IntegerProperty()

    def update_counter(name):
       """Increment the named counter by 1."""
       def _update_counter(name):
           counter = Counter.get_by_key_name(name)
           if counter is None:
               counter = Counter(key_name=name);
               counter.count = 1
           else:
               counter.count = counter.count + 1
           counter.put()
       # Update counter in a transaction.
       db.run_in_transaction(_update_counter, name)

    class SomeRequest(webapp.RequestHandler):
       """Perform synchronous requests to update counter."""
       def get(self):
           update_counter('SomeRequest')
           # try/finally pattern to temporarily set the namespace.
           # Save the current namespace.
           namespace = namespace_manager.get_namespace()
           try:
               namespace_manager.set_namespace('-global-')
               update_counter('SomeRequest')
           finally:
               # Restore the saved namespace.
               namespace_manager.set_namespace(namespace)
           self.response.out.write('<html><body><p>Updated counters')
           self.response.out.write('</p></body></html>')

### In the Memcache

By default, memcache uses the current namespace from the namespace manager for memcache requests. In most cases, you do not need to explicitly set a namespace in the memcache, and doing so could introduce unexpected bugs.

However, there are some unique instances where it is appropriate to explicitly set a namespace in the memcache. For example, your application might have common data shared across all namespaces (such as a table containing country codes).

Warning! If you explicitly set a namespace in the memcache, it will ignore the current settings from the namespace manager.

The following code snippet demonstrates how to explicitly set the namespace in the memcache:

Using the Python API for memcache, you can get the current namespace from the namespace manager or set it explicitly when you create the memcache service. The following code sample demonstrates how to get the current namespace from the namespace manager:

    from google.appengine.api import memcache
    from google.appengine.api import namespace_manager

    // Set memcache using the current namespace from the namespace manager.
    namespace = namespace_manager.get_namespace();
    namespace_manager.set("abc");
    try:
        memcache.add("key", value)
    finally:
        namespace_manager.set(namespace)

The example below sets the namespace explicitly when you store a value in memcache:

    // Store an entry to the memcache explicitly
    memcache.add("key", data, namespace='abc')

### In the Task Queue

By default, push queues use the current namespace as set in the namespace manager at the time the task was created. In most cases, you do not need to explicitly set a namespace in the task queue, and doing so could introduce unexpected bugs.

Warning! Tasks in pull queues do not provide any namespace functionality. If you use namespaces with pull queues, you need to ensure that namespaces are saved in the payload and restored as needed by the application.

Task names are shared across all namespaces. You cannot create two tasks of the same name, even if they use different namespaces. If you wish to use the same task name for many namespaces, you can simply append each namespace to the task name.

When a new task calls the task queue add() method, the task queue copies the current namespace and (if applicable) the Google Apps domain from the namespace manager. When the task is executed, the current namespace and Google Apps namespace are restored.

If the current namespace is not set in the originating request (in other words, if `get_namespace()` returns `''`), you can use `set_namespace()` to set the current namespace for the task.

There are some unique instances where it is appropriate to explicitly set a namespace for a task that works across all namespaces. For example, you might create a task that aggregates usage statistics across all namespaces. You could then explicitly set the namespace of the task. The following code sample demonstrates how to explicitly set namespaces with the task queue.

    from google.appengine.api import namespace_manager
    from google.appengine.api import taskqueue
    from google.appengine.ext import db
    from google.appengine.ext import webapp

    class Counter(db.Model):
        """Model for containing a count."""
        count = db.IntegerProperty()

    def update_counter(name):
        """Increment the named counter by 1."""
        def _update_counter(name):
            counter = Counter.get_by_key_name(name)
            if counter is None:
                counter = Counter(key_name=name);
                counter.count = 1
            else:
                counter.count = counter.count + 1
            counter.put()
        # Update counter in a transaction.
        db.run_in_transaction(_update_counter, name)

    class TaskQueueHandler(webapp.RequestHandler):
        # TaskQueueHandler is mapped to '/_ah/counter'
        """Task queue handler for update_counter."""
        def post(self):
            name = self.request.get('counter_name')
            update_counter(name)

    class SomeRequestViaTaskQueue(webapp.RequestHandler):
        """Perform asynchronous requests to update counter."""
        def get(self):
            # Asynchronously updates counter.
            taskqueue.add(url='/_ah/counter',
                          params={'counter_name':
                                  'SomeRequestViaTaskQueue'})
            # try/finally pattern to temporarily set the namespace.
            # Save the current namespace.
            namespace = namespace_manager.get_namespace()
            try:
                # Asynchronously updates counter.
                namespace_manager.set_namespace('-global-')
                taskqueue.add(url='/_ah/counter',
                              params={'counter_name':
                                      'SomeRequestViaTaskQueue'})
            finally:
                # Restore the saved namespace.
                namespace_manager.set_namespace(namespace)
            self.response.out.write('<html><body><p>')
            self.response.out.write('Counters will be updated')
            self.response.out.write('</p></body></html>')

### In the Blobstore

The Blobstore is not segmented by namespace. To preserve a namespace in Blobstore, you need to access Blobstore via a storage medium that is aware of the namespace (currently only memcache, datastore, and task queue). For example, if a blob's key is stored in a datastore entity, you can access it with a datastore key or query that is aware of the namespace.

If the application is accessing Blobstore via keys stored in namespace-aware storage, the Blobstore itself does not need to be segmented by namespace. Applications must avoid blob leaks between namespaces by:

- Not using `BlobInfo.gql()` for end-user requests. You can use BlobInfo queries for administrative requests (such as generating reports about all the applications blobs), but using it for end-user requests may result in data leaks because all BlobInfo records are not compartmentalized by namespace.
- Not using Blobstore keys from untrusted sources.

### In the Datastore Viewer

The Datastore Viewer in the Admin Console allows you to set the namespace for queries by requesting extra options. To use it:

1. Go to the Datastore Viewer tab in your application's Admin console.
2. In the Show entities of kind dropdown, select the kind you wish to view. The Datastore Viewer displays the entities for that kind in a results table.
3. Expand the Options panel. From there, you can specify the desired namespace and associated queries.

### In the Bulk Loader

The bulk loader supports a `--namespace=NAMESPACE` flag that allows you to specify the namespace to use. Each namespace is handled separately and, if you want to access all namespaces, you will need to iterate through them.