Concurrency in Web Development
INTRODUCTION
In this article, we will take a look at different models of concurrency, how to achieve them in various programming languages. Describes what concurrency is and briefly describes various methods of implementing concurrency, and how concurrency is implemented with Node.js, and the role asynchronous programming play in concurrency Moreover interrogating how are web APIs related to implementing concurrency? Also, what is the event-loop in JavaScript and how does it relate to concurrency? Lastly, compare the way Oracle and MongoDB support database concurrency.
DESCRIPTION OF CONCURRENCY AND A BRIEF DESCRIPTION OF VARIOUS METHODS OF IMPLEMENTING CONCURRENCY
Concurrency means multiple computations are happening at the same time. Concurrency is everywhere in modern programming, whether we like it or not:
- Multiple computers in a network
- Multiple applications running on one computer
- Multiple processors in a computer (today, often multiple processor cores on a single chip)
Concurrency is essential in modern programming:
- Web sites must handle multiple simultaneous users.
- Mobile apps need to do some of their processing on servers (“in the cloud”).
- Graphical user interfaces almost always require background work that does not interrupt the user. For example, Eclipse compiles your Java code while you’re still editing it.
Being able to program with concurrency will still be important in the future. Processor clock speeds are no longer increasing. Instead, we’re getting more cores with each new generation of chips. So in the future, to get a computation to run faster, we’ll have to split up a computation into concurrent pieces.
Process and Threads
Process
A process is a program that has been stacked into memory alongside all the resources it needs to run. Each program needs certain fundamental assets, for example, registers, a program counter, and a stack. A register is part of the CPU and is utilized to hold information. It can hold an instruction, a storage address, or some other sort of data required by the process. The program counter monitors where a computer is in its program sequence and the stack is an information structure that stores data about the active subroutines of a computer program. It is utilized as scratch space for the process.
Thread
Thread is the segment of a process meaning a process can have multiple threads and these multiple threads are contained within a process. A thread has 3 states: running, ready, and blocked. Concurrency can be implemented in various ways. Multiprocessing and multithreading or an approach that uses both are common ways of implementing concurrency.
A process that contains more than one thread is known as a multi-threaded process. Multi-threaded processes accomplish several things at (almost) the same time.
Threads are known as lightweight processes because they have their own stack but can access shared data in the heap. The operational cost of communication between the threads is low because threads share the same address space as the process and other threads within the process. However, unlike a process, a problem with one thread in a process will affect other threads and the viability of the process as a whole.
ASYNCHROUNOUS PROGRAMMING'S ROLE IN JAVASCRIPT
Thread is the segment of a process meaning a process can have multiple threads and these multiple threads are contained within a process. A thread has 3 states: running, ready, and blocked. Concurrency can be implemented in various ways. Multiprocessing and multithreading or an approach that uses both are common ways of implementing concurrency.
A process that contains more than one thread is known as a multi-threaded process. Multi-threaded processes accomplish several things at (almost) the same time.
Threads are known as lightweight processes because they have their own stack but can access shared data in the heap. The operational cost of communication between the threads is low because threads share the same address space as the process and other threads within the process. However, unlike a process, a problem with one thread in a process will affect other threads and the viability of the process as a whole.
HOW ARE WEB API'S RELATED TO IMPLEMENTING CONCURRENCY
Asynchronous programming has two primary benefits. The first benefit is for end-user GUI programs: asynchronous programming enables responsiveness. Everyone has used a program that temporarily locks up while it’s working; an asynchronous program can remain responsive to user input while it’s working. The second benefit is for server-side programs: asynchronous programming enables scalability. A server application can scale somewhat just by using the thread pool, but an asynchronous server application can usually scale an order of magnitude better than that.
Both benefits of asynchronous programming derive from the same underlying aspect: asynchronous programming frees up a thread. For GUI programs, asynchronous programming frees up the UI thread; this permits the GUI application to remain responsive to user input. For server applications, asynchronous programming frees up request threads; this permits the server to use its threads to serve more requests.
Modern asynchronous applications use two keywords: async and await. The async keyword is added to a method declaration, and performs a double purpose: it enables the await keyword within that method and it signals the compiler to generate a state machine for that method, similar to how yield return works. An async method may return Task<TResult> if it returns a value, Task if it doesn’t return a value, or any other “task-like” type, such as Value Task. Also, an async method may return Async Enumerable<T> or Async Enumerator<T> if it returns multiple values in an enumeration. The task-like types represent futures; they can notify the calling code when the async method completes.
WHAT IS THE EVENT LOOP IN JAVASCRIPT AND HOW DOES IT RELATE TO CONCURRENCY?
The vast majority of modern JavaScript environments work according to an event loop. This is a common concept in computer programming which essentially means that your program continually waits for new things to happen, and when they do, reacts to them. The host environment calls into your program, spawning a "turn" or "tick" or "task" in the event loop, which then runs to completion. When that turn has finished, the host environment waits for something else to happen, before all this starts.
You might think that how concurrent processes can be executed given that JavaScript is single-threaded. Although this is true that JavaScript runtime can do only one thing at a time, the browser in itself is a lot more than just the runtime. The browser consists of other things like Web Apis, a Callback Queue, and an Event Loop. The Web Apis are threads that you can request and have them perform any process while keeping the Call Stack clear.
This is how the complete JavaScript environment looks like. In the context of node, the above picture remains the same except that instead of Web Apis you've C++ APIs like threads, file system, etc. Let's get back to the code we executed in the previous section and see how it fits in the bigger picture.
Apis like setTimeout, XHR, etc are not present in the runtime but are rather provided by the Web Apis. When you call the setTimeout function, it registers a timer function along with the callback. When the timer expires, it sends the callback to the event queue which is then pushed to the Call Stack by the Event Loop when the Call Stack is empty.
The Event Loop has a single job - it watches the Call Stack and the Callback Queue. When the Call Stack is empty, it takes the first event in the queue and pushes it to the stack which effectively runs it. Such an iteration is called a tick in the Event Loop. Each event is just a function callback.
COMPARE THE WAY ORACLE AND MONGODB SUPPORT CONCURRENCY?
According to the MongoDB documentation on FAQ about concurrency, MongoDB permits various clients to write the same data. To guarantee consistency, it utilizes locking and other concurrency control measures to keep different clients from altering a similar piece of data at the same time. Together, these mechanisms guarantee that all writes to a single document occur either in full or not at all and that clients never see an inconsistent view of the data. MongoDB utilizes multi-granularity locking that permits operations to lock at the global, database or collection level, and allows for individual storage engines to implement their own concurrency control below the collection level MongoDB uses reader-writer locks that allow concurrent readers shared access to a resource, such as a database or collection.
In contrast, Oracle maintains data consistency in a multiuser environment by using a multiversion consistency model and various types of locks and transactions.
Multiversion Concurrency Control
Oracle automatically provides read consistency to a query so that all the data that the query sees comes from a single point in time (statement-level read consistency). Oracle can also provide read consistency to all of the queries in a transaction (transaction-level read consistency).Oracle uses the information maintained in its rollback segments to provide these consistent views. The rollback segments contain the old values of data that have been changed by uncommitted or recently committed transactions
CONCLUSION
Understanding the environment in which your program runs can significantly increase your efficiency and effectiveness as a developer. It highlights the logic as to why a given program works the way it does. If you felt there was something missing or if you like the post, do let me know in the comments
Comments
Post a Comment