Multi-switch, multi-business line design thinking and summary

Assuming that there is such a business requirement, users need to add SMS, WeChat, and app push switches. Switch 0 means off, 1 means on. If this is you, how would you design the database table?

One field for each switch?

You might say this is simple. SMS switch is represented by one field, WeChat switch is represented by one field, and app push is represented by one field. If there are only these three switches, it’s okay. Assuming that this piece of new demand comes,

You need to add a switch for the user 5 minutes before class and 10 minutes before class. . . . , The development and testing must not be crazy. Is there a better solution for the above problems? Yes, multiple switches can be implemented with one field.

How to implement multiple switches in one field?

You might say this is simple, the SMS switch is turned on as 1, the WeChat switch is turned on as 2, and the app push is turned on as 3, is there a problem with this design? Yes, for example, the value of this field is 3.

How do you judge whether it is 1+2, that is, SMS open, WeChat open or 3, or app push open? That is to say, there are two switch states in the 3 field, which cannot uniquely indicate one switch state.

3. How to ensure that each value of the field represents the uniqueness of the switch?

It can be solved using 2 to the power of n. For example, the 0th power of 2 is 1, which means the SMS is open; the 1st power of 2 is 2, which means WeChat is open; the 2nd power is 4, which means the app push switch.

Uniqueness : In binary terms, 01 means 1, 10 means 2, 100 means 4, and we can see uniqueness from the binary. Take 3 as an example, it means that both SMS and WeChat are open at the same time.

Extensibility : 5 minutes before class and 10 minutes before class next time. . . , And so on, we can continue to expand into 8 (1000), 16 (10000). . .

In this business, if the SMS switch is turned on, you need to send a text message to the user, otherwise it will not be sent. Then we need to determine whether the SMS switch is turned on.

The enumeration values ​​of this switch are: SMS (1), WeChat (2), app push (4), 5 minutes before class (8), and 10 minutes before class (16). . . .

4. How to judge whether a certain switch is turned on?

You can AND the field value with the switch enumeration value . If the result obtained is the enumeration value itself, the switch is open; if the result of the AND is 0, the switch is closed. What do you mean, let's give an example:

For example, the field value is 7, which means that the SMS, WeChat, and app push switches are turned on.

  • 7 is ANDed with 1, that is, the result of the ANDing of 111 with 001 is 1, indicating that the SMS switch is turned on.
  • The AND of 7 and 2, that is, the result of the AND of 111 and 010 is 2, indicating that the WeChat switch is turned on.
  • 7 and 4 phase AND, that is, the result of 111 and 100 phase AND is 4, indicating that the WeChat switch is turned on.

For example, if the field is 4, it means that the app push switch is turned on.

  • 4 and 1 phase AND, that is, the result of the phase AND of 100 and 001 is 0, indicating that the SMS switch is closed.
  • The AND of 4 and 2, that is, the result of the AND of 100 and 010 is 0, indicating that the WeChat switch is closed.
  • 4 and 4 phase AND, that is, the result of 100 and 100 phase AND is 4, indicating that the WeChat switch is turned on.

5. How to ensure the correctness and robustness of the switch data?

Assuming that we have multiple threads to open the SMS switch, how do we ensure that the database will eventually add 1 to the current value?

Correctness : If the amount of concurrency is not high, optimistic locking can be used. The so-called optimistic locking is updated according to the version. If the concurrency is high, you can use distributed locks.

Robustness : Before changing the SMS switch, you must confirm whether the switch is turned on . If it is turned on , it is not necessary to turn it on, and there is no need to change it.

 

Multi-business lines are also similar in design. For example, if we have a phone, how do we know which business lines the phone is registered with? Similarly, suppose we have three business lines A, B, and C

It is also possible to define business line A as 1, business line B as 2, and business line C as 4, assuming that another business line is new, and so on.

Guess you like

Origin blog.csdn.net/jack1liu/article/details/112549578