0

I'm working on an app where users can un-bookmark a post they've bookmarked before. But I realized that if multiple requests is sent by a particular user to un-bookmark a post they've bookmarked before, node properties get set multiple times. For example, if user 1 bookmarked post 1, their noOfBookmarks (both user and post) will increase by 1 and when they un-bookmark, their noOfBookmarks will decrease by 1. But sometimes during concurrent requests, I get incorrect or negative noOfBookmarks depending on the number of requests. I'm using MATCH which will return 0 rows when the pattern can't be found.

I think the problem is because of the isolation level neo4j is using. During concurrent requests, the changes made by the first query to run will not be visible to other transactions until the first transaction is committed. So the MATCH is still returning rows, that's why I'm getting invalid properties. I think what I need is for transactions to be executed sequentially or get an exclusive read lock.

I've tried setting a property on the user and post node (before MATCHing the bookmark relationship) which will make the first transaction get a write lock on those nodes. I thought other transactions will wait at this point for the write lock to be released before continuing but it didn't work.

How do I ensure the first transaction during concurrent requests modify the graph and other transactions stop at that MATCH (which is the behaviour during sequential requests)?

This is my cypher query:

MATCH (user:User { id: $userId })
MATCH (post:Post { id: $postId })
WITH user, post
MATCH (user)-[bookmarkRel:BOOKMARKED_POST]->(post)
WITH bookmarkRel, user, post
DELETE bookmarkRel
WITH post, user
SET post.noOfBookmarks = post.noOfBookmarks - 1,
    user.noOfBookmarks = user.noOfBookmarks - 1
RETURN post { .* }

Thank you

Emmanuel
  • 47
  • 2
  • 6
  • No phun intended, but you have to use bookmarks: https://neo4j.com/docs/python-manual/current/bookmarks/#_bookmarks_within_a_single_session – Håkan Löfqvist Dec 21 '22 at 13:57
  • @Hakan Lofqvist thanks for your help. I've been looking into this bookmark concept since yesterday, but the issue I'm having now is there will be no bookmarks until a query is run in a session (calling `session.lastBookmarks` give empty array). So, when multiple requests are sent, there will be no bookmarks yet (unless the requests can wait for each other) as all queries will be executed immediately. This led to the problem of invalid data again. What to do? I'm using NodeJS – Emmanuel Dec 22 '22 at 15:55

0 Answers0