Concurrency in Web Development
As technology and the world moves forward, alot more people are now using the web. Because of this, web applications must be able to handle multiple users and operations at the same time. This ability is made possible through concurrency, a fundamental concept every web developer must understand.
What Is Concurrency?
Concurrency refers to the ability of a system to handle multiple tasks at the same time. This doesn’t always mean the tasks are literally running at the exact same moment (that's parallelism), but rather that they are in progress simultaneously, often by rapidly switching between them.
For example, a web server that handles many client requests at once is using concurrency to ensure that one user's request doesn’t block everyone else.
How Is Concurrency Implemented?
There are several approaches to implementing concurrency:
1. Processes
A process is an instance of a program that is running. It has its own memory space and resources. Processes are isolated from each other.
For example, Opening two browser windows creates two separate processes.
2. Threads
A thread is a lightweight unit of execution inside a process. Multiple threads in a process can run concurrently and share the same memory. This makes communication between them fast but also risky: if one thread crashes, it can affect the whole process.
For example, In a video app, one thread handles video while another handles audio, all in the same app (process).
Other methods of implementing Concurrency, include:
3. Multithreading
A single program runs multiple threads. Useful for doing several things at once (e.g., loading files, handling requests, updating UI).
4. Multiprocessing
Running multiple processes in parallel, each isolated and often on different CPU cores. Ideal for heavy computation where threads might not be enough.
5. Asynchronous Programming
Allows the program to perform tasks like file reads, database queries, or HTTP requests without waiting for the result before moving on. This is common in web development and essential for non-blocking, high-performance applications.
Concurrency in Node.js
Node.js is single-threaded, but it handles concurrency differently using an event-driven and asynchronous I/O model.
Asynchronous Programming in Node.js
Node.js doesn't block the main thread when waiting for I/O operations like reading files or accessing a database. Instead, it uses:
- Callbacks
- Promises
- async and await
setTimeout
, HTTP requests, file system calls) handle long-running tasks outside the main thread. Once complete, they send results back to the event loop, which processes them when ready.- Is the call stack empty?
- Are there tasks waiting in the message queue?
Oracle defines three major concurrency issues:
Common Database Concurrency Problems
-
Dirty Reads: Reading data from a transaction that hasn’t been committed.
-
Nonrepeatable Reads: Reading the same data twice and getting different results because another transaction modified it.
-
Phantom Reads: Getting different results from the same query because new rows were added by another transaction.
- Row-level locks prevent data conflicts.
- Supports various isolation levels like "
READ COMMITTED"
and "SERIALIZABLE"
to avoid dirty reads and other issues. - Ideal for systems where data consistency is critical.
- Uses document-level locking, allowing high throughput and concurrent access across different documents.
- Starting from version 4.0, MongoDB supports multi-document ACID transactions, making it more reliable.
- Uses write concern and read concern options to control consistency and durability.
- Designed for scalability and performance, especially in distributed systems.
Comments
Post a Comment