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

What Are Web APIs?

In the browser or Node.js runtime, Web APIs (like 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.

The JavaScript Event Loop

The event loop is the heart of JavaScript's concurrency model. It constantly checks:
  • Is the call stack empty?
  • Are there tasks waiting in the message queue?
If so, it pushes them onto the stack and runs them. This allows JavaScript to appear to do multiple things at once, even with a single thread.


Concurrency with Databases

Databases must also handle concurrent users, think of hundreds of users trying to book an Airbnb at once. If not handled correctly, concurrent access can lead to data integrity problems.

Oracle defines three major concurrency issues:

Common Database Concurrency Problems

  1. Dirty Reads: Reading data from a transaction that hasn’t been committed.

  2. Nonrepeatable Reads: Reading the same data twice and getting different results because another transaction modified it.

  3. Phantom Reads: Getting different results from the same query because new rows were added by another transaction.

Oracle vs MongoDB: Database Concurrency

Oracle

Oracle is a relational (SQL-based) database. It uses ACID-compliant transactions and controls concurrency using locks and isolation levels.

  • 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.
MongoDB

MongoDB is a NoSQL document database.

  • 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.

Conclusion

Concurrency is what makes modern web apps fast, responsive, and scalable. Whether you’re writing JavaScript code using asynchronous functions, designing multi-threaded systems, or building apps that talk to a database, understanding concurrency helps you build reliable and efficient applications.

Comments

Popular posts from this blog

Big O Notation Basics for Web Developers

Interfaces in Object-Oriented Programming Languages and Prototype-Based Languages

How JavaScript Uses Hashing