Several methods of writing user information to database with high concurrency in Java

Suppose such a situation exists

Multiple users write to the database. Our business logic stipulates that each user can only write once, and most users only send requests once.

However, there is a case (in 1% of cases), that is, some users will send write requests twice or more times (because of database restrictions, it is not convenient for us to write on the primary key).

If the time interval between two requests sent by this special user is relatively large, it is simple. Each time you write, write it to the database to see if this person has written it. If so, discard the request directly.

However, the biggest problem is that if the user sends two writes almost instantaneously.

Moreover, assuming that our do something is time-consuming, the above strategy may fail.

Why failed? I don't have to explain.

So what?

Method 1

Synchronized for ten thousand years.

Of course, we have to admit that with the above method, it will not appear. There are two three records in the database

But the granularity of the above lock is too large. When Zhang San writes, Li Si can't write either.

In fact, all we want is that Zhang San himself can't write many times at the same time.

Method 2

The string class maintains a string pool. When the intern method is called, if the pool already contains a string equal to this string object (determined by the equals (object) method), the string in the pool is returned. It can be seen that when strings are the same, string Intern () always returns the same object, so it implements locking the same user. Because the granularity of lock is limited to specific users, the system obtains the maximum concurrency.

The above idea ensures that when Zhang San writes, Li Si can write, but not two Zhang San together.

Method 3

In fact, I personally think that method 2 is already very good. If there are any problems with method 2, I can only say:

String. The defect of inter() is that a string pool maintained by the class string is placed in the JVM perm area. If there are too many users, the string placed in the string pool is uncontrollable, which may lead to oom errors or too many full GCs.

So what?

As for the strategy of getting suffixes, we think about it ourselves.

With this strategy, I can guarantee 100 million users, maybe only 10000 different suffixes.

It is possible that the suffixes of Zhang San Li Si are the same, but the probability of Zhang San Li Si sending requests at the same time should not be too large. Even if it's sent at the same time, can't you wait a minute?

Method 4

Personal feeling is similar to the core of method 3.

Method 5

In the case of a cluster, if two Zhang sans enter two servers almost instantaneously, the Java language level locks will have to be scrapped.

Redis distributed locks can be used

Method 6

Using zookeeper

I just heard that there is such an idea, but I haven't used zookeeper, so I won't say much about this method.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>