0

I have an app where users can upvote or downvote a blog post. To implement the voting functionality, I created the below model ...

 class Upvote(ndb.Model):
   user_id = ndb.IntegerProperty(required=True)
   blog_id = ndb.IntegerProperty(required=True)

When an user upvotes a post, I save it via:

Upvote(user_id=User.key.id(), blog_id=Blog.key.id()).put()

And to see if an user has already upvoted I query:

Upvote.query(Upvote.user_id=User.key.id(), Upvote.blog_id=Blog.key.id())

Is this the most efficient way to implement such a voting system? I want to make sure as it seems the Upvote model can get very large. Of course I am prematurely optimizing now but in a theoretical situation with millions of users I want the most efficient/cheapest method.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
JK140
  • 775
  • 2
  • 10
  • 19

1 Answers1

1

Well, for one I'd call the model just Vote and I'd add a BooleanProperty called upvote, set to True if it's an upvote and set to False if it's a downvote.

Otherwise you'd have to repeat the story for downvotes and without some extra logic you'd leave room for a user to cast 2 votes, one up and one down, for the same post, which IMHO doesn't make a lot of sense.

Yes, there could be many Vote entities, but they are small and not related to each-other, scaling pretty good.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97