---
title: "Discussion on \"关于redis分布式锁入门\" | Hashnode"
description: "Discussion on \"关于redis分布式锁入门\". 原理\n单一实例下：使用setnx命令(在key不存在时,创建并设置value 返回1,key存在时,会返回0)来获取锁。\n应用\n以扣库存为例，使用锁能够有效保证不会被超卖；\n\n基础版方案：数据库锁\nSELECT stock from goods where ID =1 for update;\nupdate  goods set stock=stock-1 where id=1 and stock=10\n// 正常情况下看你业务需求，如果能够在落库前确认库存的话，上面这句话后面的stock=10能..."
url: "https://hashnode.com/posts/redis-1/658e75470d01b67cc5169117"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [关于redis分布式锁入门](https://blog.robinjiang.com/redis-1?utm_source=hashnode&utm_medium=feed)

原理 单一实例下：使用setnx命令(在key不存在时,创建并设置value 返回1,key存在时,会返回0)来获取锁。 应用 以扣库存为例，使用锁能够有效保证不会被超卖； 基础版方案：数据库锁 SELECT stock from goods where ID =1 for update; update goods set stock=stock-1 where id=1 and stock=10 // 正常情况下看你业务需求，如果能够在落库前确认库存的话，上面这句话后面的stock=10能...

Aug 6, 2016 · 1 min read · blog.robinjiang.com  
Read the article

## Written by

[robin Jiang](https://hashnode.com/@robinj)

@robinj · SHENZHEN,CN

  
[View profile](https://hashnode.com/@robinj)

Geek,Agile

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
