Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine HRD query without ancestor

I have a GAE project written in Java and I have some thoughts about the HRD and a problem that I'm not sure how to solve.

Basically I have users in my system. A user consists of a userid, a username, an email and a password. Each time I create a new user, I want to check that there isn't already a user with the same userid (should never happen), username or email.

The userid is the key, so I think that doing a get with this will be consistent. However, when I do a query (and use a filter) to find possible users with the same username or email, I can't be sure that the results are consistent. So if someone has created a user with the same username or email a couple of seconds ago, I might not find it with my query. I understand that ancestors are used to work around this problem, but what if I don't have an ancestor to use for the query? The user does not have a parent.

I'd be happy to hear your thoughts on this, and what is considered to be best practice in situations like these. I'm using Objectify for GAE if that changes anything.

like image 745
Joel Avatar asked Apr 05 '12 13:04

Joel


2 Answers

I wouldn't recommend using email or any other natural key for your User entity. Users change their email addresses and you don't want to end up rewriting all the foreign key references in your database whenever someone changes their email.

Here's a short blurb on how I solve this issue:

https://groups.google.com/d/msg/google-appengine/NdUAY0crVjg/3fJX3Gn3cOYJ

Create a separate EmailLookup entity whose @Id is the normalized form of an email address (I just lowercase everything - technically incorrect but saves a lot of pain when users accidentally capitalize [email protected]). My EmailLookup looks like this:

@Entity(name="Email")
public class EmailLookup {
    /** Use this method to normalize email addresses for lookup */
    public static String normalize(String email) {
        return email.toLowerCase();
    }

    @Id String email;
    @Index long personId;

    public EmailLookup(String email, long personId) {
        this.email = normalize(email);
        this.personId = personId;
    }
}

There is also a (not-normalized) email field in my User entity, which I use when sending outbound emails (preserve case just in case it matters for someone). When someone creates an account with a particular email, I load/create the EmailLookup and the User entities by key in a XG transaction. This guarantees that any individual email address will be unique.

The same strategy applies for any other kind of unique value; facebook id, username, etc.

like image 196
stickfigure Avatar answered Nov 03 '22 07:11

stickfigure


A way around the HRD's eventual consistency, is to use get instead of query. To be able to do this is you need to generate natural IDs, e.g. generate IDs that consists of data you receive in request: email and username.

Since get in HRD has strong consistency, you will be able to reliably check if user already exists.

For example a readable natural ID would be:

String naturalUserId = userEmail + "-" + userName;

Note: in practice emails are unique. So this is a good natural ID on it's own. No need to add a made-up username to it.

like image 23
Peter Knego Avatar answered Nov 03 '22 07:11

Peter Knego