Concurrency in Web Development


What is Concurrency?

Concurrency means the occurrence of multiple events or computations running at the same time in parallel with one another in a system. Concurrency is prevalent in most modern programming and a vital concept to grasp. Concurrency is advantageous as it allows for the running of multiple applications, better response times and enables a better overall operating system performance in some instances. 

That being said, it is not without its downsides. Common issues that can occur with concurrency is starvation (when a process cannot obtain service to continue), deadlock (when two processes are blocked and neither of the two can be executed) and race conditions (when two or more processes accessing shared data, may try to change or delete shared data at the same time).  

Concurrency has various methods of implementation such as 'Thread-based Server Architectures', Event-based Server Architectures and combined approaches. Thread-based approaches use a process-per-connection model that is easy to implement and use preemptive scheduling, however uses large amounts of memory and limits scalability. Event-based approaches use a thread-per-connection model and new events are scheduled in a nice single flow to be executed. A combined approach separates the logic into well-defined stages that are queued. A downside can arise of increased latencies, but countermeasures are available to solve this. 

Node.js and Concurrency

Node.js uses an asynchronous event-driven, non-blocking I/O model that makes it lightweight and efficient. This means that several actions are taken at the same time while executing a program and the server does not have to wait for the first request to return data before moving on to the next. In asynchronous JavaScript, one or more tasks will be executed at a time and this creates async callbacks. Callbacks can be difficult to execute correctly and are not very intuitive and in some cases can result in "Callback Hell". A way to resolve "Callback Hell" is with Promises. Essentially your code is promising to return a value later in the program, thus providing a somewhat of a queue and ensuring there is no "Callback Hell". The async and await keywords enable asynchronous, promise-based behaviour and the await expression suspends the progress and the only resumes when the awaited promise-based asynchronous function is either successful or rejected. 

Web APIs and Concurrency

Concurrency plays a big part in the successful usage of APIs. If two users request Source A via GET request at the same time and both try to save their respective changes via PUT request, you'll run into the issue that one user will overwrite the other users data. Concurrency control measures that can be implemented to resolve this potential issue is Pessimistic locking or Optimistic locking. Pessimistic locking is when a users starts making changes to the resource, the resource is locked and only that user can make changes to the resource. Optimistic locking, the preferred choice, allows all users to retrieve the data in parallel, however when user A saves their data it will save, but when user B saves their data it will say that the data has been changed and will not allow them to overwrite user A's edits. 

What is the JavaScript Event-Loop?

The Event Loop is basically a "manager" to be able to handle asynchronous operations in a program. With the Event Loop, once your operation has completed, instead of immediately calling the callback it'll be placed in a special queue. Only when it's possible to correctly handle, the callbacks will be pushed to the stack and executed one by one. Something to keep in mind is that each time-consuming JavaScript operation will block the Event Loop.

Concurrency - MongoDB vs Oracle?

Let's look at support for concurrency in MongoDB and Oracle. MongoDB exhibits strong consistency and relies on locks for this. MongoDB has a DB Level Lock for all operations. It uses multi-granularity locking that allows locking at global, database or collection level and allows custom concurrency control below collection level.

Oracle provides control at the operation level for consistency and durability. It maintains data consistency in a multiuser environment by implementing a multiversion consistency model and numerous types of locks. Oracle allows each user to see a consistent view of data, including changes made by the user themselves and the other users. Read consistency is tied to concurrency, controlled by quorum, version, timestamp.


Sources: 

Docs.mongodb.com. 2021. [online] Available at: <https://docs.mongodb.com/manual/faq/concurrency/> [Accessed 27 September 2021].

Docs.oracle.com. 2021. Data Concurrency and Consistency. [online] Available at: <https://docs.oracle.com/cd/B19306_01/server.102/b14220/consist.htm> [Accessed 27 September 2021].

Erb, B., n.d. 4.2 Server Architectures. [online] Berb.github.io. Available at: <http://berb.github.io/diploma-thesis/original/042_serverarch.html> [Accessed 25 September 2021].

GeeksforGeeks. 2020. Concurrency in Operating System. [online] Available at: <https://www.geeksforgeeks.org/concurrency-in-operating-system/> [Accessed 25 September 2021].

Hope, C., 2019. What is Concurrency?. [online] Computerhope.com. Available at: <https://www.computerhope.com/jargon/c/concurre.htm> [Accessed 25 September 2021].

Muia, J., 2021. Parallelism, concurrency, and async programming in Node.js - LogRocket Blog. [online] LogRocket Blog. Available at: <https://blog.logrocket.com/parallelism-concurrency-and-async-programming-in-node-js/> [Accessed 27 September 2021].

Ostrowski, R., 2019. A simple guide to JavaScript concurrency in Node.js | TSH.io. [online] The Software House. Available at: <https://tsh.io/blog/simple-guide-concurrency-node-js/> [Accessed 27 September 2021].

Viguier-Just, G., 2020. Handling Concurrent Requests in a RESTful API. [online] Medium. Available at: <https://medium.com/swlh/handling-concurrent-requests-in-a-restful-api-5a25a4b81a1> [Accessed 27 September 2021].

Web.mit.edu. 2014. Reading 17: Concurrency. [online] Available at: <https://web.mit.edu/6.005/www/fa14/classes/17-concurrency/> [Accessed 25 September 2021].

Popular Posts