Skip to content

Instantly share code, notes, and snippets.

@gr33ngr33n
Forked from montanaflynn/CONCURRENCY.md
Created January 17, 2021 08:08
Show Gist options
  • Select an option

  • Save gr33ngr33n/50a2fa627926fa6b84778fb7df36beb2 to your computer and use it in GitHub Desktop.

Select an option

Save gr33ngr33n/50a2fa627926fa6b84778fb7df36beb2 to your computer and use it in GitHub Desktop.
Examples of sequential, concurrent and parallel requests in node.js

// Concurrency in JavaScript

Javascript is a programming language with a peculiar twist. It's event driven model means that nothing blocks and everything runs concurrently. This is not to be confused with the type of concurrency as running in parallel on multiple cores. Javascript is single threaded so each program runs core on one core.

Every line of code executes without waiting for anything to return. This sounds weird but it's true. If you want to have any type of sequential ordering you can use events, callbacks, or as of late promises. Let's see an example:

function delayedLog(index, delay){
  setTimeout(function(){ 
    console.log("Logging from function call #"+index)
  }, delay)
}

delayedLog(1, 2000)
delayedLog(2, 1000)

Now you might be wondering what setTimeout is doing and it's basically saying Run this function after this much time. Javascript is a Higher-order programming language which means you can pass functions as arguments. The first delayedLog will print to stdout after 2 seconds. The second one will print after 1 second. What you might expect to see is:

// ...wait for 2 seconds
Logging from function call #1
// ...wait for 1 second
Logging from function call #2

But that is not the case. Instead both will be fired at the same time one after the other and the output would instead be like this:

// ...wait for 1 second
Logging from function call #2
// ...wait for 2 seconds
Logging from function call #1
// CONCURRENT REQUESTS
var http = require('http')
function concurrentRequests(){
for (var i = 0; i < 5; i++) {
var request = {
hostname: 'httpbin.org',
headers: {'request-id': i},
path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
}
http.get(request, (res) => {
var body = ''
res.on('data', function (chunk) {
body += chunk
})
res.on('end', function () {
console.log(JSON.parse(body).headers['Request-Id'])
})
}).end()
}
}
console.log("Running concurrent requests!")
concurrentRequests()
// PARALLEL REQUESTS
var http = require('http')
var cluster = require('cluster')
function getResponse(i, cb){
var request = {
hostname: 'httpbin.org',
headers: {'request-id': i},
path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
}
http.get(request, (res) => {
var body = ''
res.on('data', function (chunk) {
body += chunk
})
res.on('end', function () {
cb(JSON.parse(body).headers['Request-Id'])
})
}).end()
}
if(cluster.isMaster) {
console.log("Running concurrent requests!")
var numWorkers = require('os').cpus().length;
for(var i = 0; i < 5; i++) {
var worker = cluster.fork()
worker.on('message', function(message) {
console.log(message.data.result)
})
}
var count = 0
for(var wid in cluster.workers) {
count++
if (count > 5) {return}
cluster.workers[wid].send({
type: 'request',
data: {
number: count
}
})
}
} else {
process.on('message', function(message) {
if(message.type === 'request') {
getResponse(message.data.number, function(res){
process.send({
data: {
result: res
}
})
process.exit(0)
})
}
})
}
// SEQUENTIAL REQUESTS
var http = require('http')
function sequentialRequest(count){
if (count == undefined) {count = 0}
count++
if (count > 5) {return}
var request = {
hostname: 'httpbin.org',
headers: {'request-id': count},
path: '/delay/.'+ Math.floor(Math.random() * (5 - 1 + 1)) + 1
}
http.get(request, (res) => {
var body = ''
res.on('data', function (chunk) {
body += chunk
})
res.on('end', function () {
console.log(JSON.parse(body).headers['Request-Id'])
sequentialRequest(count)
})
}).end()
}
console.log("Running sequential requests!")
sequentialRequest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment