Record a redis concurrent processing problem

Time:2019-8-11

Record a redis concurrent processing problem

Scene Analysis

The company is an Internet of Things company, which involves sending instructions to devices. Now the problem is that there is a bug on a product line that connects with SkyCat voice.

Enable combination command mode, such as a scenario mode, 1, turn on bedroom lights, 2 corridor lights, 3 living room lights.

  • The three lights above are just one for our products.SwitchboardOn the top of the panel is aThree-way switchThat is to say, each lamp corresponds to a switch, as follows:

The cat elf sent a control command that day. I was sending the command to the device. The flow chart is as follows.

II. Process analysis

The whole process can be seen above, including the process of triggering the application cloud to issue control instructions from Skycat voice.
That can almost lock the focus on the next parameter.

1. Send down binary instructions

  • Now there are three pavement slabs corresponding to the1 1 1Binary bits, the company is up to eight switches
  • As shown in the above process, when controlling the living room, send parameters0 0 0 0 0 1 0 0

2. Caching mechanism

As you can see above, the state of other switch bits will also be brought when the control command is issued.

  • So when it’s actually under control,Will check the status of other switch bits
  • Then the cache state is updated by the device.

Looking at it here, there will hardly be any problems, and then we will face the real problems.

III. Analysis of Concurrent Problems

The above examples are all single requests, because different switch bits of the same panel depend on the device to update the cache, but the single control time is not a problem, that is, when the user triggers the ceiling cat control lamp, it is a control.

But now we’ve introduced a scenario, where the Skycat Elves have scenario modes, such as setting up scenarios.I'm homeThen all the lights on the three-way switch should turn on all the lights. And the Skycat Wizard is concurrent, that is, it sends three requests to the application cloud at the same time, and then the application cloud sends control instructions.

problem analysis

So the problem is that all three requests depend on each other.

  • If you turn on the living room lamp, you will query the status of the corridor lamp and the bedroom lamp, you will query the cache, and the three control instructions all arrive at the same time, so the device has not updated the cache.
  • For example, the bedroom lamp (000000100) corridor lamp (000000101) has already been opened, but the living room lamp finally comes in and finds that the buffer of other lights is turned off. Then it will bring 0 down ((00000010)) and cause the first two lights to turn on and off.

IV. OPTIMIZATION OF PROCESSING SCHEME

1. Using temporary cache instead of relying on the device to update the cache

That is to say, instead of querying the device’s cache, temporary cache is used directly for 1-2 seconds. When three concurrent requests arrive at the same time, a temporary value is stored directly, telling other requests that other switches are in control for the panel to be controlled.

Sowole process model adopted by server
// Using hash structure, each case corresponds to a key value.
// Update hashkey for 1 s when each key request arrives
$redis->hset("concurrent_control_hash_off".$devSn,$keynum,1);
$redis->expire("concurrent_control_hash_off".$devSn,1);
// Get directly from the temporary cache, if not from the device cache
$res = $redis->hGetAll("concurrent_control_hash_off".$devSn);
  • The method of appeal is still problematic in high concurrency.

2. Guarantee the atomicity of redis operation for a single request

That is, when concurrent requests are made, each redis instruction has no order

$redis->multi();
$redis->hset("concurrent_control_hash_on".$devSn,$keynum,1);
$redis->expire("concurrent_control_hash_on".$devSn,1);
$redis->hGetAll("concurrent_control_hash_on".$devSn);
$res = $redis->exec();
$concurrentArr = $res[2];
  • @multi()
  • @exec()

Keeping the atom in operation has worked better so far.

summary

Even if the above has been modified several times according to the process, the modification of docking process has been made. In a word, there is no absolute situation, it needs to be optimized and tested concurrently.