"Go" Java multithreaded programming (10)-Concurrent programming principles (concurrency issues in distributed environments)

Source: Internet
Author: User

Reprint Address: http://blog.csdn.net/leicool_518/article/details/42268947

In a distributed environment, there is no way to deal with concurrency problems through the operating system and JVM tools, then in a distributed environment, you can take a strategy and methods to deal with:

    • Avoid concurrency
    • Time stamp
    • Serialization
    • Database
    • Row lock
    • Unified Trigger Path

Avoid concurrency

In a distributed environment, if there is a concurrency problem, it is difficult to solve by technology, or the cost of the solution is very high, so let's first think about whether there are some policies and business design to avoid concurrency. For example, through reasonable time scheduling, avoid the access conflict of shared resources. In addition, in parallel task design can be through the appropriate policies to ensure that there is no shared resources between tasks and tasks, such as the example mentioned in the previous blog post, we need to use multi-threaded or distributed cluster to calculate a bunch of customer related statistics, because the customer's statistical value is shared data, so there will be concurrency potential. But from the business we can analyze the customer and customer data is not shared, so you can design a rule to ensure that a customer's computing work and data access will only be a single thread or a machine to complete, rather than the calculation of a customer to assign a number of threads to complete. This rule is easy to design, for example, a hash algorithm can be used.

Time stamp

Concurrency in a distributed environment is not guaranteed to be sequential, whether through synchronous calls or asynchronous messages over remote interfaces, so it is easy to create errors in high concurrency for some business that requires time-series requirements. For example, system a needs to synchronize changes of a value to system B, because the timing problem of the notification causes an expired value to be overwritten with a valid value. For this problem, the commonly used method is to use a timestamp, each time the system A to send a change to system B need to bring a time stamp can be marked timing, System B will be notified after the time stamp and the existence of the timestamp comparison, only if the time stamp of the notification is greater than the time stamp of the existence of the update. This is a simpler approach, but the key is that the caller generally guarantees time-stamp timing validity.

Serialization

There are times when serialization can create concurrency problems, sacrificing performance and scalability to meet the requirements for data consistency. For example, the distributed message system can not guarantee the order of the message, but it can guarantee the order of the message by changing the distributed message system into a single system. In addition, when the receiver is unable to handle the call ordering, it is possible to cache the call information through a queue before processing the calls serially.

Database

The shared resources in the distributed environment can not be secured by the synchronization method or lock in Java, but the database is the shared point of the distributed servers, which can satisfy the demand through the high reliable consistency mechanism of the database. For example, a unique index can be used to solve the concurrency process of duplicate data production or repetitive task execution, and some update calculation operations as far as possible through the SQL to complete, because the program section is calculated after the update is likely to occur dirty replication problems, However, the database lock mechanism can be used to ensure the consistency of the update operation through a SQL to complete the calculation and update.

Row lock

Some transactions are more complex, can not solve the problem through a SQL, and there is a concurrency problem, then need to be resolved through row locks, the general row of locks may be implemented in the following ways:

    • For Oracle databases, you can use the Select ... for update mode. There is a potential danger that the data will be locked if there is no commit, and other tasks involving this data will be suspended and should be used with caution.
    • In the table to add a marked lock field, before each operation, the first through the Update Lock field to complete a similar competition lock operation, after the operation is completed in the Update Lock field Reset, indicating that the lock has been returned. This is a safer way, and the downside is that the operation of these update lock fields is an additional performance drain

Unified Trigger Path

When a data may be more than one trigger point or multiple services involved, there is a problem of concurrency problems, it can be through the pre-architecture and business design, as far as possible to unify the trigger path, the trigger path is less one is to reduce the likelihood of concurrency, but also conducive to the analysis and judgment of concurrency problems.

"Go" Java multithreaded programming (10)-Concurrent programming principles (concurrency issues in distributed environments)

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.