利用redis + lua解决抢红包高并发的问题

抢红包的需求分析

抢红包的场景有点像秒杀,但是要比秒杀简单点。
因为秒杀通常要和库存相关。而抢红包则可以允许有些红包没有被抢到,因为发红包的人不会有损失,没抢完的钱再退回给发红包的人即可。
另外像小米这样的抢购也要比淘宝的要简单,也是因为像小米这样是一个公司的,如果有少量没有抢到,则下次再抢,人工修复下数据是很简单的事。而像淘宝这么多商品,要是每一个都存在着修复数据的风险,那如果出故障了则很麻烦。

淘宝的专家丁奇有个文章有写到淘宝是如何应对秒杀的:《秒杀场景下MySQL的低效–原因和改进》

http://blog.NoSQLfan.com/html/4209.html

基于redis的抢红包方案

下面介绍一种基于redis的抢红包方案。

把原始的红包称为大红包,拆分后的红包称为小红包。

1.小红包预先生成,插到数据库里,红包对应的用户ID是null。生成算法见另一篇blog:http://blog.csdn.net/hengyunabc/article/details/19177877

2.每个大红包对应两个redis队列,一个是未消费红包队列,另一个是已消费红包队列。开始时,把未抢的小红包全放到未消费红包队列里。

未消费红包队列里是json字符串,如{userId:’789′, money:’300′}。

3.在redis中用一个map来过滤已抢到红包的用户。

4.抢红包时,先判断用户是否抢过红包,如果没有,则从未消费红包队列中取出一个小红包,再push到另一个已消费队列中,最后把用户ID放入去重的map中。

5.用一个单线程批量把已消费队列里的红包取出来,再批量update红包的用户ID到数据库里。

上面的流程是很清楚的,但是在第4步时,如果是用户快速点了两次,或者开了两个浏览器来抢红包,会不会有可能用户抢到了两个红包?

为了解决这个问题,采用了lua脚本方式,让第4步整个过程是原子性地执行。

下面是在redis上执行的Lua脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- 函数:尝试获得红包,如果成功,则返回json字符串,如果不成功,则返回空
-- 参数:红包队列名, 已消费的队列名,去重的Map名,用户ID
-- 返回值:nil 或者 json字符串,包含用户ID:userId,红包ID:id,红包金额:money
 
-- 如果用户已抢过红包,则返回nil
if redis.call( 'hexists' , KEYS[ 3 ], KEYS[ 4 ]) ~= 0 then
   return nil
else
   -- 先取出一个小红包
   local hongBao = redis.call( 'rpop' , KEYS[ 1 ]);
   if hongBao then
     local x = cjson.decode(hongBao);
     -- 加入用户ID信息
     x[ 'userId' ] = KEYS[ 4 ];
     local re = cjson.encode(x);
     -- 把用户ID放到去重的set里
     redis.call( 'hset' , KEYS[ 3 ], KEYS[ 4 ], KEYS[ 4 ]);
     -- 把红包放到已消费队列里
     redis.call( 'lpush' , KEYS[ 2 ], re);
     return re;
   end
end
return nil

下面是测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class TestEval {
     static String host = "localhost" ;
     static int honBaoCount = 1_0_0000;
 
     static int threadCount = 20 ;
 
     static String hongBaoList = "hongBaoList" ;
     static String hongBaoConsumedList = "hongBaoConsumedList" ;
     static String hongBaoConsumedMap = "hongBaoConsumedMap" ;
 
     static Random random = new Random();
 
//  -- 函数:尝试获得红包,如果成功,则返回json字符串,如果不成功,则返回空
//  -- 参数:红包队列名, 已消费的队列名,去重的Map名,用户ID
//  -- 返回值:nil 或者 json字符串,包含用户ID:userId,红包ID:id,红包金额:money
     static String tryGetHongBaoScript =
//          "local bConsumed = redis.call('hexists', KEYS[3], KEYS[4]);\n"
//          + "print('bConsumed:' ,bConsumed);\n"
             "if redis.call('hexists', KEYS[3], KEYS[4]) ~= 0 then\n"
             + "return nil\n"
             + "else\n"
             + "local hongBao = redis.call('rpop', KEYS[1]);\n"
//          + "print('hongBao:', hongBao);\n"
             + "if hongBao then\n"
             + "local x = cjson.decode(hongBao);\n"
             + "x['userId'] = KEYS[4];\n"
             + "local re = cjson.encode(x);\n"
             + "redis.call('hset', KEYS[3], KEYS[4], KEYS[4]);\n"
             + "redis.call('lpush', KEYS[2], re);\n"
             + "return re;\n"
             + "end\n"
             + "end\n"
             + "return nil" ;
     static StopWatch watch = new StopWatch();
 
     public static void main(String[] args) throws InterruptedException {
//      testEval();
         generateTestData();
         testTryGetHongBao();
     }
 
     static public void generateTestData() throws InterruptedException {
         Jedis jedis = new Jedis(host);
         jedis.flushAll();
         final CountDownLatch latch = new CountDownLatch(threadCount);
         for ( int i = 0 ; i < threadCount; ++i) {
             final int temp = i;
             Thread thread = new Thread() {
                 public void run() {
                     Jedis jedis = new Jedis(host);
                     int per = honBaoCount/threadCount;
                     JSONObject object = new JSONObject();
                     for ( int j = temp * per; j < (temp+ 1 ) * per; j++) {
                         object.put( "id" , j);
                         object.put( "money" , j);
                         jedis.lpush(hongBaoList, object.toJSONString());
                     }
                     latch.countDown();
                 }
             };
             thread.start();
         }
         latch.await();
     }
 
     static public void testTryGetHongBao() throws InterruptedException {
         final CountDownLatch latch = new CountDownLatch(threadCount);
         System.err.println( "start:" + System.currentTimeMillis()/ 1000 );
         watch.start();
         for ( int i = 0 ; i < threadCount; ++i) {
             final int temp = i;
             Thread thread = new Thread() {
                 public void run() {
                     Jedis jedis = new Jedis(host);
                     String sha = jedis.scriptLoad(tryGetHongBaoScript);
                     int j = honBaoCount/threadCount * temp;
                     while ( true ) {
                         Object object = jedis.eval(tryGetHongBaoScript, 4 , hongBaoList, hongBaoConsumedList, hongBaoConsumedMap, "" + j);
                         j++;
                         if (object != null ) {
//                          System.out.println("get hongBao:" + object);
                         } else {
                             //已经取完了
                             if (jedis.llen(hongBaoList) == 0 )
                                 break ;
                         }
                     }
                     latch.countDown();
                 }
             };
             thread.start();
         }
 
         latch.await();
         watch.stop();
 
         System.err.println( "time:" + watch.getTotalTimeSeconds());
         System.err.println( "speed:" + honBaoCount/watch.getTotalTimeSeconds());
         System.err.println( "end:" + System.currentTimeMillis()/ 1000 );
     }
}

测试结果20个线程,每秒可以抢2.5万个,足以应付绝大部分的抢红包场景。

如果是真的应付不了,拆分到几个redis集群里,或者改为批量抢红包,也足够应付。

总结:

redis的抢红包方案,虽然在极端情况下(即redis挂掉)会丢失一秒的数据,但是却是一个扩展性很强,足以应付高并发的抢红包方案。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值