Using Google Cloud Datastore on GCE

Cloud Datastore is a kind of Key-Value store provided as a full-managed service on Google Cloud.

It has been well known as a backend of GAE but it is also a good choice when we build apps on GCE.


Check official document for more details.


What is Cloud Datastore?

Cloud Datastore consists from these ideas.
  • Kind — A name of group of Entities
  • Key — A key to identify Entity. It can form Path with parents.
  • Entity — An object having Key and Properties


Understanding the idea of Key will make most difference for beginners.
So, let’s see it’s details.


Key in Cloud Datastore

There are 3 kinds of key.
Kind of key
What is it?
Basic key
A key having id and associated with project
Key with a parent
A key which has a parent key.
It is uniquely identified by <parent id, id>
Partial key
A key not having id/name field (yet).
It is used when you add an entity with auto generated id.
Key has these fields.
yes (no for partial key)
int for id, string for name.
(note: 1234 and ‘1234’ are differ!)


Reuse of auto generated id

Because auto generated ids are frequently reused by Datastore, a newly generated id may has the same id of recently deleted entry.
Let’s consider what happens on a service having lots of users with associated private entities.


As a bad example,
If you assign an auto generated id to new user and delete the user’s entity when they unregister, a new user may have same id with old user.
It may cause unintentional privacy leak.


For Good practice,
  • Assign unique id or name from other identification (like Email, Facebook, Firebase, etc)
  • never delete user’s entity.
  • Use allocateIds method.


Remember, the uniqueness of Key is assured by its *path*.
Auto generated id may be duplicated if they has different Kind or Parent but it’s OK.


For more detail,

Appendix. Difference between GAE

You can see the difference of library from its import examples.


For GCE,
# Imports the Google Cloud client library
from import datastore


Yes, it’s not from google.appengine.*
As documented below, the use of is encouraged for apps those are hosted on GCE.


So, what’s the difference between them?
Actually, library doesn’t have NDB APIs. People who started learning Datastore recently should not be confused with old documents!


Here’s latest Python document:


Have fun!