0% found this document useful (0 votes)
9 views3 pages

API Concurrency Architecture

The document discusses techniques for ensuring API consistency, including efficient algorithms, caching, and load-balancing. It explains JavaScript concurrent programming with examples, highlights the importance of reducing Time to First Byte using CDNs, and addresses concurrency control methods in databases, specifically pessimistic and optimistic approaches to prevent race conditions. The document also provides SQL examples for implementing these concurrency control techniques.

Uploaded by

aharonno2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

API Concurrency Architecture

The document discusses techniques for ensuring API consistency, including efficient algorithms, caching, and load-balancing. It explains JavaScript concurrent programming with examples, highlights the importance of reducing Time to First Byte using CDNs, and addresses concurrency control methods in databases, specifically pessimistic and optimistic approaches to prevent race conditions. The document also provides SQL examples for implementing these concurrency control techniques.

Uploaded by

aharonno2020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1685875961170

To develop and be certain about your API about its consistency will include using efficient
algorithms, caching data, and load-balancing techniques.

Javascript concurrent programming:

// sequential
async function load() {
const users = await getUsers();
const posts = await getPosts();
const messages = await getMessages();
// do stuff
}

// concurrent
async function load() {
const [users, posts, messages] = await Promise.all([getUsers(), getPosts(),
getMessages()]);
// do stuff
}

Reduce TTFB (Time to First Byte): we can reduce the Time to First Byte by using a CDN and
caching content in local data centers across the globe. This helps users access the content with
minimal latency. Cloudflare is one of the CDN solutions you can use to start with.

Run Tasks in Parallel: promise.all / Use async.js to help you run tasks.

Don’t use too many callbacks: using too many nested async operations (also known as
“callback hell”) can make your code difficult to read and understand, and can also affect the
overall performance of your application.

There are other tools and libraries that can be used to manage async flows in Node.js. For
example, the async library provides a set of utility functions for working with async operations,
and the co-library allows you to write async code using a generator-based syntax.
ACID property - Isolation

We can solve race conditions either on a read step or on an update step.

Pessimistic concurrency control: Pessimistic concurrency control is a technique used to


prevent race conditions in a database by locking the data that is being accessed or updated.
This ensures that only one user can access the data at a time, and other users have to wait until
the lock is released before they can access it.

To implement pessimistic concurrency control for A table, a user can execute the following SQL
statement:

SELECT * FROM Room WHERE id = 123 FOR UPDATE;

COMMIT; -- releases the lock


ROLLBACK; -- releases the lock and discards any changes made to the
data
Optimistic concurrency control: Optimistic concurrency control, on the other hand, allows
multiple users to access and update the data concurrently, but checks for conflicts before
committing the changes. If a conflict is detected, the user is notified and the changes are not
applied. One way to implement optimistic concurrency control in a booking system is to use a
"version" column in the "Room" table. This column can be used to store a "version number" for
each booking, which is incremented each time the booking is updated.

UPDATE Room
SET available = FALSE, version = version + 1
WHERE id = 123 AND version = 1;

INSERT INTO Booking (room_id, start_date, end_date, version)


VALUES (123, '2022-01-01', '2022-01-07', 1);

UPDATE Room
SET available = FALSE, version = version + 1
WHERE id = 123 AND version = 1;

INSERT INTO Booking (room_id, start_date, end_date)


VALUES (123, '2022-01-03', '2022-01-10');

If both of these statements are executed concurrently, the first UPDATE


statement to be executed will increment the "version" of the room with ID 123 to
2, and the second UPDATE statement will fail, as the "version" in the WHERE
clause is 1 (so zero rows will be updated with the second transaction).
This will prevent the race condition from occurring, as only one booking will be
inserted into the "Booking" table, and the availability of the room will not be
incorrectly updated.

You might also like