In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Redis distributed cache how to achieve Wechat to grab red packets", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Redis distributed cache how to achieve Wechat to grab red packets" bar!
I. scene analysis
Wechat grabbing red envelopes is already a common scene in our lives, especially during the annual meeting of the company at the end of the year and the Spring Festival, when the elder leaders handed out red envelopes and got cramps in their hands, but they didn't get much.
During this period of time, for a single red packet in a single group, qps is also thousands, for the entire WeChat red packet system, the peak concurrency is hundreds of millions.
Gao Feng's red packet grabbing has three main characteristics:
There are many people who pack red packets: that is, there are many tasks to create red packets, that is, the red packet system is distinguished by a single red packet task, which is characterized by more red packet tasks during peak periods.
There are more people grabbing red packets: when you send out red envelopes, dozens or even hundreds of people come to grab your red packets, that is, there are a large number of requests for a single red packet.
Low latency: when you find a red packet, the faster you get it, the happier you are, so you are required to grab the red packet quickly, usually in 1 second.
II. Technical scheme
1. Red packet
First disassemble the amount into a small amount of red packets, such as a total amount of 1000 yuan, send 10, when users click to save, automatically disassemble into 10 random small red packets.
The storage here is a difficult problem, how to store multiple amounts (such as 10 small red packets)?
two。 Grab the red packet
The key technology of grabbing red packets with high concurrency is to control the atomicity of the operation of each small red packet.
For example, 10 red packets are robbed in a group of 100 people, and when one of the 10 red packets is snatched, the stock of red packets is reduced by 1, that is, 19 are left. In the whole process, snatching one and red packet inventory minus one is an atomic operation.
List's pop operation automatically removes an element from the queue when it pops up, which is an atomic operation.
III. Case study
Red packet
/ * API for red packets * / @ GetMapping (value = "/ set") public long setRedpacket (int total, int count) {/ / disassemble red packets Integer [] packet= this.splitRedPacket (total,count); / / generate globally unique id long n=this.incrementId () for red packets; / / use list to store red packets String key=RED_PACKET_KEY+n This.redisTemplate.opsForList () .leftPushAll (key,packet); / / set 3-day expired this.redisTemplate.expire (key,3, TimeUnit.DAYS); log.info ("dismantle red packets {} = {}", key,packet); return n;}
Disassemble the red packet
/ * disassemble the red packet * 1. The amount of the red packet will be disassembled * 2. The amount of red packets should not be too different * total red packets amount * count red packets amount * / public Integer [] splitRedPacket (int total, int count) {int use = 0; Integer [] array = new Integer [count]; Random random = new Random (); for (int I = 0; I < count; ipackets +) {if (I = = count-1) array [I] = total-use Else {/ / Red packet random amount floating coefficient int avg = (total-use) * 2 / (count-I); array [I] = 1 + random.nextInt (avg-1);} use = use + array [I];} return array;}
Grab the red packet
/ * * grab red packet interface * / @ GetMapping (value = "/ rob") public int rob (long redid,long userid) {/ / step 1: verify whether the user has robbed Object packet=this.redisTemplate.opsForHash () .get (RED_PACKET_CONSUME_KEY+redid,String.valueOf (userid)) If (packet==null) {/ / second step: from the list queue, pop up a red packet Object obj=this.redisTemplate.opsForList () .leftPop (RED_PACKET_KEY+redid); if (objuring null) {/ / step 3: grab the red packet and store it this.redisTemplate.opsForHash () .put (RED_PACKET_CONSUME_KEY+redid,String.valueOf (userid), obj) Log.info ("user = {} get {}", userid,obj); / / TODO asynchronously lands data on the database return (Integer) obj;} / /-1 means to grab return-1;} / /-2 means that return-2 has been robbed } at this point, I believe you have a deeper understanding of "Redis distributed cache how to achieve Wechat to grab red packets". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.