A large number of concurrent contingency processing schemes and practices 1--asynchronous processing

Source: Internet
Author: User

First of all, I think that most of the application system performance problems in the database application, such as SQL statement design, indexing design problems. The methods mentioned in this article are more suitable for beginners.

Often we get the problem that a system that has been running well has crashed because of a sudden encounter with a large number of concurrent accesses, and your clients and bosses are all in a hurry to go to the house. It is difficult to reconstruct the system structure in an emergency. Then you regret that if you were using a distributed architecture then it would be nice to just increase the level of application or data server, and all existing data and applications would be unaffected. About the system architecture refer to my other article: "Opening restaurants and doing software-how to improve the performance of large Web sites" http://blog.csdn.net/Dreamcode/archive/2009/09/10/4540153.aspx

Now we have to adopt some contingency solutions. For example, a room can only accommodate 500 people, and now there are 1000 of people suddenly come to what we should do.

There are two ways: one is to increase the size of the room, this approach, like the level of application or data server, need to redesign the architecture, so will involve a lot of problems, perhaps not a specific proportion of the new province time. Another way is to let the guests in line, when someone out of the room, and then let others come in, that is, asynchronous processing.

Asynchronous processing, when there is not enough resources to deal with the incoming requests, but to queue the requests until there is a resource available for processing. The cost of this approach is time and cannot be returned to the processing results in time. But that's the only way to do it when we don't have the conditions to remodel the house.
Now there is a question as to where we can queue the guest, the lobby (the hard drive) or the doorway (RAM). The answer seems obvious, where it is best to stay close to the room, but the speed of the place is often not rich space. It is a common method to save user requests in some way on the hard disk, and of course it takes a step to load the data into memory.

The way to queue data in memory is to use arrays, hash tables, lists, and other data structures, as well as off-the-shelf components such as ACTIVEMQ, such as Message Queuing. If we use a map object of a single instance pattern, we save requests from multiple concurrent.

Import Java.util.Map;

public class TestMap {

Private volatile static TestMap Singleton=null;

private static MAP TestMap = null;

Private TestMap () {}

public static TestMap getinstance ()
{

             if (singleton==null) {
                   synchronized ( Testmap.class)
                   {
                        singleton=new TestMap ();
                 }
            }
   
             return Singleton
       }

Public Map Gettestmap () {
return testmap;
}

public void Settestmap (Map testmap) {
Testmap.testmap = TestMap;
}

}

Queuing on a hard drive means writing data directly to your hard disk, for example, in Java, where you can save objects directly to your hard disk, and the code is as follows:

public static Boolean writeobject (String FilePath, Object entity)
{
FileOutputStream fos = null;
try {
FOS = new FileOutputStream (FilePath);
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return false;
}

ObjectOutputStream Oos;
try {
Oos = new ObjectOutputStream (FOS);
Oos.writeobject (entity);
Oos.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return false;
}

return true;
}

public static Object ReadObject (String filePath)
{
Object entity = NULL;
FileInputStream FIS = null;
try {
FIS = new FileInputStream (FilePath);
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

ObjectInputStream Ois;
try {
OIS = new ObjectInputStream (FIS);
try {
entity = Ois.readobject ();
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Ois.close ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

return entity;
}

Finally, we need a monitoring module (such as a thread) for scheduling, for example:

public class Testthread implements runnable{

private static long interval = 3000; Cycle interval

@Override
public void Run () {
while (true)

{
...//Here is the code that needs to be executed.

try {

Thread.Sleep (interval);
catch (Interruptedexception e) {

TODO auto-generated Catch block
E.printstacktrace ();
}

}

}

}


   

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.