multithreading - How to avoid ordering issues for concurrent requests from the same user? -
multithreading - How to avoid ordering issues for concurrent requests from the same user? -
suppose have scheme processes requests concurrently having end result storing field in database. suppose next scenario appears, id
request id, user
user made call, val
value beingness stored in database , t
time:
client server database | id=1, user=x, val=100, t=1 | | |----------------------------> | | | id=2, user=x, val=50, t=2 | | |----------------------------> | id=2, user=x, val=50, t=3 | | |---------------------------->| | | id=1, user=x, val=100, t=4 | | |---------------------------->|
the problem same user makes 2 requests @ same time, due out-of-order execution of tasks in server, lastly request processed first , value inserted in database, while first request comes , overrides info leaving database in inconsistent state.
i've thought of 2 solutions:
add creation_time
field in database , update if objecttobeinserted.creationtimestamp > objectalreadyindb.creationtimestamp
. has limited usage however; suppose instead of database there scheme whom should create request , cannot query database.
user map
of userid
associated semaphore
. when request arrives check if semaphore corresponding user taken; if taken wait otherwise proceed. after inserting in db, release semaphore. has limitation of not beingness able process requests concurrently same user , might have memory issues if there many users in happy case faster (only 1 db call).
are these solution plenty or there improve typical solution problem?
the typical solution add together sequence number. or utilize mq software guarantees ordered processing (these utilize sequence numbers internally guarantee ordering). using mq thought it's easy stall processing should lose message (if lose msg #2, #3, #4 won't processed etc.), , mqs come mechanisms prevent message loss.
edit: there give-and-take in article explains different designs. question, 3rd endpoint must provide transactions, long doesn't have db.
multithreading concurrency race-condition
Comments
Post a Comment