Wrong data storage in GAE NDB

38 views
Skip to first unread message

Ankur Jatt

unread,
Jul 9, 2015, 7:33:42 AM7/9/15
to google-a...@googlegroups.com
Here is the situation
I have a model like

class Content(ndb.Model):
    likeCount=ndb.IntegerProperty(default=0)
    likeUser  = ndb.KeyProperty(kind=User, repeated=True)

When a new content generate than a new "Content" object is generated like

content_obj = Content(parent=objContentData.key, #Where ContentData is another ndb.Model subclass
                                 likeUser=[],
                                 likeCount=0
                               )

And when any user like the same content than below function gets called

def modify_like(contentData_key, user_key):
    like_obj = Content.query(parent=contetData_key).get()
    if like_obj:
        like_obj.likeUser.append(user_key)
        like_obj.likeCount += 1
        like_obj.put()

###################Problem#############
Now the problem is that when at the same time more than 4 user like the same content than this object write wrong data.
So how can I solve this problem

Ryan (Cloud Platform Support)

unread,
Jul 9, 2015, 9:29:19 AM7/9/15
to google-a...@googlegroups.com, ankurand...@gmail.com
Salutations Ankur,

You should look into sharded counters. When you have too many updates to the same entity you can run into situations where you have multiple updates happening at once, which leads to to incorrect results.

Christian F. Howes

unread,
Jul 10, 2015, 2:45:29 AM7/10/15
to google-a...@googlegroups.com
depending on how many writes you expect per second, transactions might be sufficient to solve the problem.  they are write rate limited, so you may still need sharded counters or some other way to batch writes as suggested by Ryan.
Reply all
Reply to author
Forward
0 new messages