How to make like feature / increment values without transactions?

What I want to do

I want to have a like functionality like facebook youtube instagram have

What I did

It works with transactions. But if 50 users like something at the same time, 49 transactions will fail. it becomes clogged

Has someone an idea to solve that easily? My approach would be to have sharded counters, every user has his own counter, and a scheduled cloud function adds up everything together. but i dont like that approach, for obvious reasons

// i read in old discussions things about upserts, but I really don’t understand how an upsert shall solve that https://dgraph.io/docs/howto/upserts/

Try this

So, instead of a number, you should use Edges.

e.g: { User A [Predicate: like] } ->{ node Type: Like [Predicate: like] → } → { User B }
So the like will be an intermediate list of nodes. Or you can just use simple edges.
e.g: { User A [Predicate: like] } -> { User B }

I’d prefer the intermediate nodes cuz I can add extra information to the action. Making the like action richer of information.

Also, Edges are always unique. You can’t like twice with that approach.

BTW, you can’t do anything without transactions.

Cheers.

2 Likes

Also check out these learning guides

https://dgraph.io/learn/courses/datamodel/sql-to-dgraph/develop/compare-schema/

https://dgraph.io/learn/courses/datamodel/evolution/develop/graphs-data-modeling/graph-data-model/

They both cover how to handle likes in a social media kind of way.

1 Like