Concurrency in Web Development



What is concurrency?

Concurrency can be defined as the ability to handle or execute more than one task at the same time. The two units of concurrency in programming are processes and threads. 

According to a reading by HyperionDev on concurrency (HyperionDev, 2021), processes are the instances of programs being executed and each process relies on: a register which is a part of the CPU used to hold data; a program counter which keeps track of the program sequence; and a stack which is responsible for storing information about the active subroutines in a computer program. Threads are units of execution within processes - they can be also referred to light-weight processes with their own threads.

There are various methods in implementing concurrency however the traditionally these approaches are usually threaded or event based. Here's a brief list of these approaches (Erb, n.d.):

  • Threaded-Based Server Architecture - this approach is supported by many programming languages, each incoming connection is given a thread and synchronous blocking I/O (input/output) is implemented to handle I/O.



  • Multi-Process Architecture - in this approach "preforking" is used where the main server forks child processes to be handled later. A socket is created as a two way communication link between processes in the main server and each request handler blocks for new connections and handles the current connection then waits for the next connection.



  • Multi-Threaded Architecture - this approach replaces the heavy-weight processes with light-weight threads for each connection. This allows for a smaller memory footprint when executing processes. I/O is handled through acceptor threads which block for new socket connections and sends them to a set of threads handling incoming requests also known as worker threads. These worker threads are either handling requests or waiting for new requests to process.

 

  • Event-driven Sever Architecture - is the alternative approach to synchronous blocking I/O and uses asynchronous non-blocking calls instead. Each event is consumed one after the other in the event queue by a single threaded event loop and each time an event is consumed, event handler code gets executed. Event handlers can also be responsible for a chain in events where I/O actions can trigger new events.

 

  • Staged Event-driven Sever Architecture - here threaded and event based approaches are combined where requests are processed through stages to resolve the scalability issues found in the examples mentioned above.  Each stage contains a queue of incoming events (which also connect the stages), a controller to monitor resources, an event handler, and a thread/thread pool for processing instructions.

 

Concurrency in Node.js?

Concurrency in Node.js is implemented through the use of I/O (input/output) operations model which uses asynchronous non-blocking I/O calls to improve speed in responsiveness to requests through handling I/O through events and call-backs.

Most JavaScript runtime environments don't run asynchronous code so they corporate with and a low-level I/O API with a single-threaded non-blocking event loop, as a mediator between the two. The Web API provides a space where asynchronous JavaScript can be executed while making way for other synchronous JavaScript to be executed.

The event loop is a key component in JavaScript’s concurrency model that executes tasks in the task queue, collects code and processes events. The Model can be illustrated this according to a presentation by Robert Philip (Philip, 2014):

1-     The main function is called and carries out the first code in the function which is pushed to the stack and executed.



2-     Then comes in the asynchronous code which gets pushed to the web API to be processed there.

3-     Now we don’t have to wait for the asynchronous code to be executed so we move on to our last code and it get pushed to the stack and get executed. Notice how the single threaded architecture is implemented through the stack.



4-     Now that the stack is cleared and our asynchronous code has been processed the event loop kicks in and pushes it to the stack to be executed.




 

Concurrency in database Interaction?

With the demand for data-driven dynamic websites where multiple users have access to it at the same time, concurrency is crucial in being implemented in database interaction. Here are some the issues that might be encountered due to multiple user database interaction according to Oracle (Oracle, 2021):

  • Dirty reads – where a transaction reads uncommitted data created by another author.
  • Non-repeatable reads – where a transaction rereads data it has previously read which is an outdated version of data.
  • Phantoms – where when searching for data a transaction reruns the query but then later finds that there’s new data committed that matches the search conditions.

Oracle and MongoDB are the most popular database technologies that both implement locks to prevent the issues mentioned above in a multiuser database however slightly different.

  • MonogDB allows multiple users to read and create the same data and for better reliability in ensuring that the same pieces of data are not being modified at the exact same time through granularity locking (MongoDB, n.d.). This locking allows operations to lock at different levels of the database and the following modes of locking:
    • The Shared (S) locking mode for reading operations.
    • The Exclusive (X) mode for writing operations.
    • The Intent Shared (IS) and Intent Exclusive (IX) modes for intentional reading or writing operations and implemented at higher levels of the database.
  • According to Oracle (Oracle, 2021), it uses two mode of locking:
    • The Exclusive lock mode where the first transaction to lock a resource exclusively will be the only one given access to update the data until the lock is released.
    • The Share lock mode where data is shared among multiple users reading the data on the same resource.

Both technologies offer great reliability in multiuser databases however the choice on which one is best suitable depends on whether an SQL or NoSQL based database would suite your needs. Oracle is SQL based and a relational database while MongoDB is a cloud, NoSQL based database that offers great convenience, is open source, provides great scalability and with Mongo shell you can easily perform your CRUD operations in JavaScript.

To conclude we see the value in concurrency in web development and how JavaScript provides high availability in implementing it.



References

Erb, B., n.d. Concurrent Programming for Scalable Web Architectures - 4.2 Server Architectures. [Online]
Available at: http://berb.github.io/diploma-thesis/original/042_serverarch.html
[Accessed 9 June 2021].

HyperionDev, 2021. Interview Preparation: Concurrency, s.l.: s.n.

MongoDB, n.d. FAQ: Concurrency. [Online]
Available at: https://docs.mongodb.com/manual/faq/concurrency/
[Accessed 9 June 2021].

Mozilla , 2021. https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop. [Online]
Available at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
[Accessed 9 June 2021].

Oracle, 2021. 13 Data Concurrency and Consistency. [Online]
Available at: https://docs.oracle.com/cd/B19306_01/server.102/b14220/consist.htm
[Accessed 9 June 2021].

Philip, R., 2014. What the heck is the event loop anyway? | Philip Roberts | JSConf EU. [Online]
Available at: https://youtu.be/8aGhZQkoFbQ
[Accessed 9 June 2021].

 

Philip, R., 2014. What the heck is the event loop anyway? | Philip Roberts | JSConf EU. [Online]
Available at: https://youtu.be/8aGhZQkoFbQ
[Accessed 9 June 2021].

Comments