[Useful for volume opening] record the process of solving deadlocks under a high concurrency, and give useful ideas on volume opening

Source: Internet
Author: User

[Useful for volume opening] record the process of solving deadlocks under a high concurrency, and give useful ideas on volume opening

It is helpful to open the exam. I haven't written a blog for a long time, and I have been busy working recently.


The deadlock was not far away from me, and I finally encountered it in high concurrency.


DeadLock Found!


Despite trying to avoid deadlocks in the programming style, I still ran into it. The article may not tell what I am doing, but just record my own deadlock elimination process.


It originated from two linked cache + redis + asynchronous database read/write operations.


Deadlock occurs in this sentence in the transaction:


Delete from table where from key = 'helloworld'


The idea for getting rid of deadlocks is as follows:


1) analyze the deadlock model. Suppose there are two threads, one or two, and two locks, A and B. In general:

1 hold lock A, wait for lock B -- 2 hold lock B, wait for lock

So it collapsed.

2) continue to think about it. It should be so simple, and I am sorry to say that I am an engineer. In actual development, I have encountered a deadlock loop. What does it mean? Continue hypothesis

1 hold A, wait for B,

2 hold B, wait for C,

3 Hold C, wait for

Anyway, you can assume there are 4, 5, 6, 7...

3) continue to think about the deadlock. The first step must be to review your code. At this time, be sure to be confident in using mature tools, but be sure to keep a questioning attitude. How can we review them, record my Processes

A. the simpler the code, the less error-prone it is. Therefore, adhering to the DRY principle, we also need to eliminate repeated code. If we can reuse the code, we will never have to look at the number of lines.

B. After reconstruction, the data layer and the logic layer must be separated. do not include logical processing in the data layer, or do not do what the data layer does in the logic layer. (At this time, we will find that there is a lot of difference between the idea of design patterns and those who do not have this concept)

C. Back to the question: the deadlock in my code is a high-concurrency environment with hotspot areas. After refactoring the code, we must pre-process the hotspot data before writing it to the database asynchronously. What is a hotspot zone? My own name is a mess, that is, in many cases, logs or external data, many times there will be 100 pieces of 99% similarity data within Ms.

D. Continue the test. When the deadlock persists, a new cache is created immediately to determine what went wrong. The logic is correct.

E. With the attitude of questioning tools, the retrieval tools and high concurrency occur in the asynchronous read/write database, since the logic is no problem, the data operated in the read/write will not be faulty, that is, the locking mechanism of the database is faulty. Read the database lock mechanism documentation and find that there is a problem with the page lock and record lock in transaction operations.

F. The problem is that since the transaction is multi-threaded and highly concurrent, it is impossible to prevent the transaction from holding the record lock and page lock. What should I do at this time?

Solution: discard deleting and updating indexes. Use the primary key to delete and update indexes.

G, test, normal.


Throughout the process, I read a lot of documents, among which the most useful:

Http://hedengcheng.com /? P = 844

After writing the article, I felt that I was writing something. I haven't written the article for a long time. I came out with a bubble and said I was still there.

When you are awake, make a high-concurrency topic and summarize your work. At the same time, we will also translate a highly concurrent foreign article.
This is my word.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.