Photo by Mathew Schwartz on Unsplash
What is concurrency?
Concurrency occurs when many tasks can begin, run, and finish at the same time. For example, websites handle several concurrent users everyday whereby several instruction sequences are carried out simultaneously. Concurrency basically takes place when several process threads are running in the background of an operating system which assists to not obtrude on the user's experience.
What methods can be used to implement concurrency?
Concurrency as a whole can be implemented in a variety of ways, such as by implementing each computational execution as an operating system process or by implementing the computational processes as a collection of threads inside a single operating system process. What we are saying here is that within concurrent programming, two techniques are executed namely, processes and threads. Similar to a virtual computer, a process is a running instance of a program that is separate from other processes and has access to its own private memory. The memory or objects of one process cannot be accessed by another process. Every process in a computer system has at least one thread. The memory and open files of a process are shared by all threads.
In the Java programming language, concurrent programming is mostly concerned with multi-threads (enables us to write in a way where multiple activities can proceed concurrently in the same program) whereas in JavaScript, concurrent programming is mostly concerned with single-threads (only one activity can proceed at a time). Simply to start implementing concurrency, we can start with just one thread, called the main thread which will later have the ability to create additional threads.
How is concurrency implemented with Node.js?
JavaScript is the scripting language that is used by the open-source virtual machine known as Node.js. Despite being single-threaded, Node.js is still widely used because of its asynchronous nature, which enables it to handle concurrency and handle numerous input and output activities at once. To sustain concurrency and carry out non-blocking input and output operations, Node.js makes use of an event loop. Before, we discuss the event loop, let's first look at asynchronous programming in concurrency.
What role does asynchronous programming play in concurrency?
The best way to understand asynchronous programming is to visualize an example. Let's imagine that you would like to prepare your morning cup of coffee but you also would like to wash your dishes in the dish washer. With this in mind, you can easily prepare your coffee after setting up your dish washer without having to wait for the dish washer to finish the job. These two tasks were carried out here in asynchronous fashion.
From understanding this scenario, we can say that concurrency is made possible via the asynchronous programming approach which enables the execution of multiple operations at once without having to wait for each one to finish. Operations with synchronous services are carried out one at a time. In other words, one operation is carried out following the conclusion of another. Thus, asynchronous operations combine the results in order to calculate a total execution time that only includes the longest-running operations plus the time necessary to combine the results.
How are web APIs related to implementing concurrency?
As you may know, we frequently need to make API calls to distant servers when developing the backend of a web application. It is understood that the more API calls made, the longer it will need to serve the data. For example, imagine that you want to respond to a request that calls for four API calls. To do this, you clean, format, and integrate the data once it is all accessible, then you transmit it to the front end. Let's assume that it takes five seconds for all of the API requests to return data. Although five seconds may seem reasonable, we can make this process as quick and effective as possible.Therefore, we can make all the API calls at once to fulfill the request more quickly. This is where the relation between web API's and concurrency comes into play. Each API call's data will arrive independently of the others. When the data is ready, we may process it and send the results to the front end. As you can see, we are not executing the following API call after each API call in this instance. Simply put, we concurrently call every API. This procedure is effective and quick because of this.
What is the event-loop in JavaScript and how does it relate to concurrency?
Photo by Md Faishal on Medium
The concurrency model used by JavaScript is built on an event loop, which executes the code, gathers and processes events, and completes queued subtasks. JavaScript is a single-threaded programming language, which means that all of the code can only be executed on a single thread. So, you may be thinking as to how will we be able to async calls with JavaScript since the language is synchronous. Well, this is where the event loop comes into play, whereby the processes or requests can be executed concurrently without the need to construct additional threads.
We can also say that JavaScript is built to be concurrent as it features a built-in event loop-based concurrency model that manages code execution and management as well as gathers and processes events. In comparison to models written in languages like Java, this model is really unique. The JavaScript event loop uses concurrency to run code blocks positioned on its call stack in a way that gives the impression that it is non-blocking, even when it briefly blocks the process.
Overall, the procedure is straightforward. Every time the JavaScript run process is launched, the event loop examines the call-stack to see if any functions or activities are accessible. If none are, it pushes things from the queue into it, where they are later executed on the stack.
How does Oracle and MongoDB compare with each other when supporting database concurrency?
Due to the fact that multiple users can access data continuously and perform CRUD operations, it is important for databases to enable concurrency. This is where MongoDB and Oracle are taken into consideration.Oracle
Oracle which acts as a relational database management system maintains data consistency in a multiuser environment. This is done using a multi-version consistency architecture with various locks and transactions. Oracle offers non-blocking queries and row-level locking, both of which reduce read/write contention, in contrast to alternative serializable isolation implementations that lock blocks for both read and write operations. The serializable mode of transaction behavior makes an effort to make sure that transactions proceed so that they appear to be carried out sequentially as opposed to concurrently.MongoDB
In contrast, multiple clients can read and write the same data simultaneously using MongoDB which is a non relational database program. It employs locking and other concurrency control techniques to stop many clients from simultaneously changing the same piece of data in order to assure consistency. With the help of this concurrency control, database operations can be run concurrently without the fear of enabling any inaccuracies.Conclusion
From what we have discussed in this article today, we can say that overall concurrency is essential to the way we create software. It is crucial to understand this concept since it explains how everything operates. This is just the beginning as we will explore more about this topic soon so stay tuned!
I hope you have found this article helpful.
Thank you for reading.
Sources
Dev Community - How to make concurrent API calls in nodejs by Hasin Apurbo
Geeks for Geeks - If Node.js is single threaded then how to handles concurrency?
JavaScript Works - The Event Loop in JavaScript | What is it? by King Somto
MongoDB Documentation - FAQ: ConcurrencyOracle Database Online Documentation - Data Concurrency and Consistency
Tutorialspoint - What is concurrency control in DBMS?
Tutorialspoint - What is concurrency control in DBMS?
Comments
Post a Comment