Concurrency in Web development

Concurrency

Concurrency, put simply, is the running of multiple things at the same time. i.e. concurrently. The

There are a couple of ways to deal with concurrency and different programming languages solve similar problems in their own unique way. Concurrency is an interesting problem to solve and a very important one. Sometimes the solution to the concurrency problem is solved by using a multi-process architecture or a multi-threaded architecture.

A multi-process architecture makes use of preforking, where it isolates different requests which get assigned their own portion of memory on the CPU. A multi-threaded architecture has a difference to multi-processing in that threads share the same process' address space, while each of them can have different individual tasks. Due to multiple threads having access to the same state it is important to coordinate any changes. two threads might increase a state variable by 1 at the same time. Which might result in the state variable only being incremented to 2, and not to 3 if they aren't synchronized well

An event driven architecture is another way of dealing with concurrency. Event emitters push events onto the queue for processing. The event loop then processes each of these until completion and then moves onto the next event All of these can be combined to solve concurrently too.

Node.js and Javascripts chosen solution to concurrency is an event-driven architecture. The way the event loop works is that the next message in the queue gets processed. In Javascript environments asynchronicity can be used to allow a message to get processed in the event loop, but not necessarily to completion. Javascript can use different web apis to handle asynchronous requests. The message gets handled in the event loop and sent to the web api for processing. The next message in the queue can then be processed even though the previous one has not been completed yet. Then once the web api has finished processing the asynchronous request, it's result gets put back into the message queue to be processed once it reaches the event loop.

In databases concurrency is a very important topic. It is good to be able to have some sort of concurrency so that many people can be working with the same data at the same time but when this is happening it is even more important to make sure that the data stays consistent.

One person might update a piece of data that might be accessed by another user. It is important the other user gets the correct data. MongoDB uses multi-granularity locking. MongoDB allows multiple clients to read and write the same data. It uses locking and other concurrency control measures to prevent multiple clients from modifying the same piece of data simultaneously. When locking at a specific granularity, all higher levels are locked too ensuring data stays consistent for every user accessing the data.

Oracle implements statement level and transaction level read consistency to ensure data consistency. Oracle always enforces statement-level consistency which makes sure the user making the query gets the data at its state at the point of time when the user made the select query. Any commits that occur between the initiation of the query and receiving the data will not be seen. Transaction level read consistency means that all accesses to the data will reflect the state of the DB when the transaction began. They do however allow for seeing the changes made in the transaction itself.

resources used: