Wednesday 3 December 2014

Using positive feedback in software

There is something like a positive feedback loop in society panic. Someone says something horrible to a small group of people, they all panic and tell the story to their friends and relatives and each of them does the same ... then suddenly the whole society panics. Another example which seems have positive feedback in its nature is nuclear fusion, they way the sun generates energy or the atomic bomb works which even Einstein had been underestimated it. Big bang itself has had some positive feedback loop in its nature.

The above examples show that positive feedback naturally likes to spread or increase something as fast as possible, does this feature have any usage in software? Sure it does, let us see an example.

Positive feedback example
Have you ever worked with systems which have many connected online users (or any other processes) and because of a software upgrade, power outage, maintenance or ... you need to restart the system. In this situations, we usually can't instantly connect all the users to the system.



An analogy for this is like having a power outage for the entire cities of a country. You can't turn all power plants on and the suddenly put all country load on them. Without any doubt, the country gets instantly dark again. You need to connect the cities or even the districts step by step gradually. The reason is very obvious, the initial connection needs some more power (we don't want to get into electrical engineering stuff, just accept this fact) and the total power of the power plants can't stand for instant connection of all districts.

Let us have an easier analogy, consider a shop or a store, Walmart for example. At 10 o'clock you count all the customers shopping there, say there are 1000 customers. Do you think this store can handle entering all these 1000 customers instantly? Of course not, but gradually, yes. The main reason exactly is like the power plants, there are always some limitation or constraints (in this example number of doors).

Solution 
OK, now I assume we all know how it looks like restarting a system with thousands or millions of users. The system has limited resources that don't let it accept all the users instantly. What can we do? Accept users one by one? This surely takes too much time and the owner company will not be happy with this.

Did you notice what the problem is here? The problem is we don't know how much resource the system requires for each individual user or connection, it may depend on the user's configuration and so many other parameters, we don't know the power of the hosting machine or machines. Here is what we can do, it is just a pseudo code:

userCount := 1;
startUserIndex := 0;
tempUserList := userList.getSubList(startUserIndex, startUserIndex+userCount);

while (tempUserList != null) {
  result := connectUsers(tempUserList); // result contains the status of the processed list
  startUserIndex += userCount; 
  if (result.getResourceUtilization() < 90) 
    ++userCount;
  tempUserList := userList.getSubList(startUserIndex, startUserIndex+userCount);
}

As you see at first we assume we can just handle one user at a time, so we connect it to the system. The we check the resource utilization during the connection, if it still doesn't hit our threshold which is 90%, we increase the number of users we can connect at a time. This process keeps going until we reach a point in which the resource utilization reaches to 90%, then we stop and don't increase the list length.

You see in this example we wrote a code which finds itself the best performance it can give to connect all the users to the system. Don't forget if we don't stop increasing userCount, the system faces the 100% utilization and nobody knows what happens after that.

No comments:

Post a Comment