Answer the question
In order to leave comments, you need to log in
How to synchronize counter between MySQL and Redis?
In general, there is a counter for the number of comments on the article, in order to speed up the work, it was decided to cache part of the data in Redis. The application itself uses socket.io. And it will work on multiple servers.
A hashmap was chosen to store the comment count in Redis. Each time a comment is added / removed in a transaction, an action (INSERT, DELETE) and UPDATE is performed on the counter field, from which the current value of the counter is immediately obtained and put into the radish via HSET. It was done this way, and not through increment / decrement, since the node may fall and the value will not be updated. And so it doesn't matter, not this request, so the next one to correct the quantity. But, here the main problem arose, since the nodes can be on different servers, which creates a delay, there may be an attempt to overwrite the new value with the "old" one.
That is:
Server 1:
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question