I am trying to achieve strong consistency. Let's call my model PVPPlayer
:
class PVPPlayer(ndb.Model):
points = ndb.IntegerProperty()
Every key for the model is created like this:
pvp_player = PVPPlayer(key=ndb.Key(Profile, "test_id", PVPPlayer, "test_id"))
where Profile
is parent model:
class Profile(ndb.Model):
def build_key(cls, some_id):
return ndb.Key(cls, some_id)
I have 2 REST api url:
1) update_points
2) get_points
In 1) I do :
# I use transaction because I have to update all the models in single batch
@ndb.transactional(xg=True, retries=3)
def some_func(points):
pvp_player = ndb.Key(Profile, "test_id", PVPPlayer, "test_id").get()
pvp_player.points += points
pvp_player.put()
# update other models here`
In 2) I do:
pvp_player = ndb.Key(Profile, "test_id", PVPPlayer, "test_id").get()
return pvp_player.points`
My flow looks like this:
1) update_points()
2) get_points()
3) update_points()
4) get_points()`
...
Problem:
Using get()
guarantees strong consistency so what I don't understand is why sometimes as the result of get_points()
I get stale data like points were not updated at all.
Example:
POST get_points -> 0
POST sleep 1-3 sec
POST update_points -> 15
POST sleep 1-3 sec
POST get_points -> 15
POST sleep 1-3 sec
POST update_points -> 20
POST sleep 1-3 sec
POST get_points -> 15 !!!`