AJAX Made Easy - Learn AJAX
AJAX Home AJAX-Tips

Applying Mutual Exclusion in Ajax

 Category: AJAX Tips Add Comment





In building an Ajax based website, it could be possible that a single data would be processed at the same time but different functions. This function is enabled because of XMLHttpRequest could dispatch two or more functions at the same time.


However, this could be a problem especially when a function interprets the data based on the command from the client side. The result is a broken logic – creating an error which eventually would be an inconvenience to the user. In either DOM or MVC architecture, XMLHttpRequest could dispatch two or more function.


Fortunately, this type of problem is rare in Ajax and JavaScript based websites and applications. But even though this happens rarely, it should not be taken lightly. When this type of problem happens, developers would probably have to rework the entire coding. That is why precautions have to be done before implementing the application. Safely developing functions in Ajax is important to save time and avoid frustration.


A broken logic in any application could be addressed by properly implementing mutual exclusion. It is a type of coding logic that would prevent the function from conflict. Mutual exclusion could still access a single data but mutual exclusion ensures that the critical part of the data is accessed one at a time.


Lamport’s Bakery Algorithm

There are three ways of applying mutual exclusion in an Ajax based website. Developers are free to choose which algorithm they will be using as long as they are comfortable with the implementation.


The first option is the Lamport’s Bakery Algorithm. This type of algorithm is perfect for based Ajax based websites. It could work on shared memory so bandwidth is not a problem. This type of algorithm assigns a number to functions which will serve as a queue to access a single data. It is a simple and a very smart way of applying mutual exclusion to Ajax and JavaScript applications.


On the other hand, Lamport’s Bakery Algorithm could only be used in simple Ajax websites. JavaScript cannot easily read this type of algorithm and most browsers has a hard time reading this algorithm if the application is too long or too complicated.


Wallace Algorithm (Command Design Pattern)

This type of algorithm is a little bit different from Lamport as it has been “upgraded” to be properly applied to Ajax websites. The command design pattern develops the exclusion function as a class which means it has virtually become an API. As an API, it could be easily integrated into the application. The developer would just have to integrate the API to the entire application so that functions will have a queue in accessing critical areas of the data.


Complete Integration

In previous versions, you will notice that only the critical data are being protected through mutual exclusion. In this case, each aspect of the data is considered important. This is often used in Ajax which provides RIA to users. It could be complicated because it has to be integrated in each aspect of the data, but it ensures that mutual exclusion is implemented.






Next: Smart Caching with JavaScript



Post Comment


You need to Login to post your comment...

Click here to login

Title:
 
Comment:



Daily Email Updates

AJAX Updates delivered directly to your Inbox...

Enter your email address:

Latest AJAXwith Updates

Related AJAX Articles

Popular AJAX Articles