Dark Horse Comments 04 Concurrency Security under Cluster

Practical combat chapter-08. Coupon flash sale-Thread concurrency security issues under the cluster_bilibili_bilibili

In order to cope with high concurrency, the project needs to be deployed to multiple machines to form a cluster, so nginx needs to be configured.

1. How to simulate a cluster

Modify the configuration through idea's ctrl + d to implement multiple tomcat running simulation clusters

Then configure the node on nginx, 8080/api will proxy to the backend, and then the backend configures the information of each node (upstream)

Implement polling nodes to achieve load balancing.

2. Clusters introduce new concurrency issues

The previous article has achieved concurrency security in the case of a single node. However, if a user sends two flash sale requests and are assigned to two nodes by nginx, then the user lock on one node (using the user ID as the monitor) cannot be used on the node. It takes effect on different nodes, so it still cannot lock multiple flash sales actions of the same user, and it still cannot guarantee one person and one order. So distributed locks are needed.

reason:

        Why does the intern() function mentioned above return the same address even if you use new as long as the content is the same? Because it will find the address of the string from the string constant pool. No matter how many times you come, the address of the string in the constant pool will be returned, so it is the same. No matter which thread you run on a tomcat, there is only one constant pool when there is only one jvm.

        When deploying in a cluster, different servers have their own tomcat, so each server has an independent jvm, so the constant pools are also different. Therefore, there is no guarantee that the return value of intern() is the same, and there is no guarantee that the lock monitors on the two servers are the same, and the same user cannot be locked.

It can only guarantee that one jvm will lock the same user internally, but it cannot guarantee that all jvm will lock the same user.

Guess you like

Origin blog.csdn.net/m0_50973548/article/details/135019136