Simple implementation of Redis lock

Business scenario: Prevent users from frequently brushing data in a short period of time under high concurrency

Business difficulties: If you first check whether there is a key and then insert it, under high concurrency, multiple threads may check that the key does not exist before inserting, and thus all acquire the lock.

Solution: Use the user id to generate a unique key, use the setnx command with redis (that is, insert if it does not exist, return 0 if the key exists), and use redis' expire command if successful. If it fails, a lock reentrance can be performed

Disadvantage: Too much reliance on redis, lack of risk prevention measures


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325973222&siteId=291194637