比如現(xiàn)在想實(shí)現(xiàn)類似一個(gè)秒殺的功能,10個(gè)用戶搶2個(gè)商品,redis或者memcache或者其它的nosql產(chǎn)品,有沒有類似的功能,就是我有個(gè)計(jì)數(shù)器(庫(kù)存,比如等于2)然后,開始搶商品的時(shí)候,前2個(gè)用戶,已經(jīng)將庫(kù)存減到0了,當(dāng)?shù)?個(gè)人來?yè)尩臅r(shí)候,計(jì)數(shù)器不會(huì)變?yōu)?1,而是還是為0,并且這個(gè)操作會(huì)返回一個(gè)操作失敗,或者類似于mysql的返回影響行數(shù)為0的這種反饋信息,讓我知道的?
ringa_lee
reids has DECR (atomic decrement), and memcache also has the decrement function. The difference between them is that when redis’s atomic decrement reaches 0, it will become a negative number, while memcache’s will always be 0
No. It will become -1, I'm afraid you have to make your own judgment.
Optimistic lock solution
Do not check the lock, pass it directly, but check the version number after passing it, and do not operate it correctly, otherwise roll back (requires transaction support)
One problem is that maybe users in orders 2 and 3 didn’t get it, but users in order 4 and 5 got it (in this case, you’ll know after you write a demo and test it)