Skip to main content

Consensus and Paxos in Distributed Systems

Before discussing Consensus and Paxos, we need to know some basic information. So, do you know that we use various distributed systems in day-to-day life even without knowing it? Let me give you an example. You probably have to use google docs, google slides, or any similar kind of cloud service. So if you think about google docs, can it be a single application executed on a single machine? You need to know that real-time processing and concurrent processing are very expensive for performance. Therefore, definitely, it should be a supercomputer or something that can handle those expensive computations in an efficient and effective way. In reality, it is a network of a large number of connected machines and devices. A distributed system is a system that has multiple components located on different machines that communicate and coordinate actions in order to appear as “a single coherent system” to the end-user. Consensus addresses a fundamental problem in the distributed system and Paxo

JavaScript Infinite Loops that work on browsers


What's the big deal about JavaScript infinite loops? That's the common question that gets into your mind if you are not aware of the usefulness of infinite loops or the working context of the JavaScript inside a browser. So, let's dig into points and facts.

First of all, we need to understand what an infinite loop is. In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached. The only difference in an infinite loop is it will never reach the terminating condition or there is no one. However practical aspects are a little bit different. That means even it seems infinite, it will terminate at some point due to some reason or by a condition. Let's see about that.

In general, you can achieve infinite loops using "for", "while" or other keywords as below in most of the programming languages. (Consider this as a pseudo code)
while(True) {}

The condition is always "True" and therefore the loop will execute infinitely. But still, you can terminate this loop by adding a break-point for some conditions.
while(True) {
    ...
    if(condition) {
        break;
    }
}

This concept is very helpful for implementing some algorithms and applications. You need to worry about this when you work with C, C++, or Java kinds of languages. But dealing with this in JavaScript is a different story. Before that, I will tell you why I needed to think about such a thing at first. I hope you know about the lifecycle of frameworks such as Angular, React, or Vue. The lifecycle is the mechanism that how handles the initialization, existence, and destruction of the components of a web application.


As you can see, this process is iterative and seems infinite. So, I just need to implement a custom lifecycle for an HTML/jQuery project of mine. But now I ended up here explaining to you what I figure out.

1st Attempt - While loop

First I tried the same way as mentioned before with a while loop for JavaScript. 
while (1) {}
    
/* Similarly, you can use,
while (true) {}
    or
while ([a condition that returns true]) {}
    or
for(;;){}
*/

What do you think the result is? The browser waits for a few seconds. Then returns a popup saying that the script is slowing down the browser.

The browser popup for an infinite while loop

Now let's think about the reason behind this. To understand the problem you need to know some background information.
JavaScript is a single-threaded, non-blocking, and asynchronous programming language.
If we think about it in a simple way, JavaScript code is executed in a single thread. This seems like JavaScript can't do concurrent processing but actually, it can handle it in a different way via asynchronous executions using a special component in the browser called "Web APIs".

Non-blocking means the execution of a process will not be blocked by another. As an example think there is a computational task and between that, we need to get some user input too. In languages like C or Java, the execution of the computational task will block until gets the user inputs. But in JavaScript that will not happen.

Okay, let's go back to our problem. The reason behind the slow-downing popup is the single-thread case. When we try an infinite loop like that, it will try to access the thread and main stack over and over again without giving any chance to execute any other process. So the browser will get to know about it and inform us. So, we need to give some breaks to the thread and main stack to overcome this issue.

2nd Attempt - JavaScript infinite loops using "setInterval()"

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). And it will continue calling the function until the clearInterval() method is called, or the window is closed. We use the ID value returned by setInterval() as the parameter for the clearInterval() method.
var timer = setInterval(() => {
    ...
    if([termination condition]) {
        clearInterval(timer);
    }
}, [interval in miliseconds]);

If we execute this script with relevant values and methods, the browser will execute it infinitely until reaching the termination condition. The difference here with the previous method is that setInterval() will not block the main stack and thread. It uses the queue of WebAPIs and only executes when the main stack is free for the queue. However, still, there are some drawbacks. Because the setInterval() executes repeatedly, some of the not performed actions might load in the queue. That load might be caught to unexpected outcomes.

Final Attempt - JavaScript infinite loops using "setTimeout()"

This was another way to implement the JavaScript infinite loops without blocking the browser. Basically, the setTimeout() is kind of a simplified method of setInterval(). It calls a function or evaluates an expression after a specified number of milliseconds and it will execute only once. If we need, we can use the clearTimeout() method to prevent the function from running.
function life() {
    ...
    if ([negation of termination condition]) {
        setTimeout(() => life(), [timeout in miliseconds]);
    }
}

life();

This will ensure to release of the load before executing the next setTimeout() method.

Conclusion

In this post, we discussed how to implement JavaScript infinite loops that work on browsers using setInterval() and setTimeout() methods. However, these are special cases, and highly recommend reducing infinite loops as much as possible in your applications. Because they might cause various performance and memory-related issues. I hope you have found something useful here. For any clarification or sharing of knowledge, please leave a comment. Let's meet from another post. Thank you.

Comments