What is the response of Express when it encounters numerous identical asynchronous requests from the same origin?

Currently, I am utilizing Express.js for my project. There is an async function that performs a task that can take anywhere from 20 to 30 seconds to complete. Once the task is done, it increases a user's counter in the database. However, users are required to wait at least 24 hours before making another request.

The function first checks the last time the user's counter was updated before commencing the lengthy 30-second task. It only executes if the previous update occurred more than 24 hours ago.

But what happens if a user submits multiple requests almost simultaneously (let's say, five requests within the same second)? Will the function start the 30-second task five times and consequently increase the user's counter fivefold? This could potentially occur as all requests sent within the same second may not be recorded in the database during that time frame. Or will the function handle requests sequentially, processing one after the other, waiting for a response before moving on to the next one?

I aim for the API to manage asynchronous requests from the same user in a sequential manner.

Answer №1

Is the function set to start work and increment the counter five times in a row?

Yes, that is correct.

Will it handle requests one after the other or can it process multiple requests simultaneously?

No, nodejs operates asynchronously, allowing for the handling of multiple requests at once as long as the request handler itself is asynchronous, like in your scenario.

What steps can be taken to avoid this issue?

To prevent this problem, ensure that the counter is checked and incremented before carrying out the work. Additionally, make sure this check and update process is an atomic transaction within the database to eliminate any race conditions between multiple requests.

Can the API be configured to handle asynchronous requests from the same user sequentially?

You have the option of implementing a queue for each user to achieve this. However, this approach may complicate horizontal scaling and could lead to timeouts on the client side. It may be better practice to immediately respond with a 409 (Conflict) or 429 (Too many request) HTTP status code if another request is already being processed, ensuring that requests are sent one at a time from the client side.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Manual mocking in Jest is only effective for the initial function call

In my project, I have created a custom XHR wrapper in utils/xhr.js and I am using Jest manual mocking feature to mock it. However, I am running into an issue where only the first XHR call is being tracked: utils/xhr.js let xhr = { get: function(par ...

What is causing TypeScript to compile and remove local variables in my Angular base controller?

I am trying to develop a base controller in Typescript/Angular for accessing form data, but I'm encountering an issue where the form member seems to be getting removed during compilation and is not present in the generated JavaScript code. Could you ...

Enhancing the aesthetic appeal of a form

I have created a form in HTML that utilizes JavaScript to pull data from a database. I am looking to style the form, but I'm unsure of how to proceed. Below is the form along with some CSS code. How can I integrate the two together? <form id=" ...

What is the best way to optimize a search for objects with extensive field arrays?

Recently, I've been working with an object schema that includes an array field to store ids for objects from a separate collection. This array has the potential to contain thousands of ids. Up until now, I have been excluding this field using .select( ...

What is the process for creating the token?

I've encountered an issue when generating a token while creating a user account. Despite my efforts, I keep getting an empty set. What could be causing this problem? Could there be an error in the syntax? Take a look at the controller file: import m ...

What is the best way to ensure that a global variable is populated after subscribing to an asynchronous event?

Recently, I encountered an async firebase call that looks something like this: this.fbProvider.fbGetProfile() .subscribe(p => { this.profile = p; ...

Interactive Navigation Bar in JavaScript

Is there a way to create a horizontal menu with an arrow that follows the mouse cursor? I saw this effect on a website (http://cartubank.ge) and I'm curious if anyone has the source code for it? Your help would be greatly appreciated. ...

Error message: Requesting server-side file requires JavaScript to be enabled

This issue has been quite a challenge for me, as it appears to be straightforward but has turned into a complex problem. I have a vue application that utilizes canvas to draw images. I want an API to determine the 'type' of image to display (fil ...

Is npm installation specifically for a node server?

I'm in the process of developing a React app with webpack and utilizing npm to install different front end packages such as tabs, D3, and more. As I plan for production, I'm wondering if I specifically need to run my server as a Node server given ...

What is the best way to retrieve a specific number of documents from MongoDB?

I am currently facing an issue with my code that is returning all news related to a company. However, I only want the first 15 elements. Is there a way to achieve this? The following code snippet retrieves all news for a company using the google-news-jso ...

transition effect of appearing and disappearing div

Having trouble creating a fade out followed by a fade in effect on a div element. The fade out happens too quickly and the fade in interrupts it abruptly. Here is the JavaScript code: $('#fillBg').stop(true,false).fadeTo(3000, 0); $("#fillBg"). ...

Unable to run Express server and socket.io on the same port simultaneously

Having trouble getting socket server to work## I have tried different solutions, but cannot get the socket server to work with Express on the same port number. If I use any other port number instead of the server, it works fine. Can anyone explain why th ...

What's the rationale behind receiving the second before the first?

I've been digging into ES6 promises, and I thought I had a handle on it for a moment. However, it seems like I may have hit a roadblock. What I'm aiming to do is handle a series of file operations where each operation depends on the completion o ...

Performing a map or foreach function on an array of objects limited to the first 5 objects

I need to iterate through an array of objects, but I only want to loop through the first 5 objects and then stop. What is the most efficient way to achieve this? [ {"visibility": 10000,}, {"visibility": 10000,}, {"visibilit ...

Concealing functions within Accessors

I've recently started working on a website project utilizing the node.js/express stack and I am experimenting with developing in the functional programming style, which is quite new to me. One issue I encountered was related to the express method res. ...

When utilizing div.load, jQuery and other JavaScript sources may not be defined

When you load a page using jQuery load: $("#myDiv").load("page.php",{foo: bar}); The head section is included in the index: <head> <script src="/assets/plugins/jQuery/jQuery-2.1.4.min.js"></script> <script src="/assets/plugi ...

Icon button not found

I have created a reusable hook component for input fields. The TextField renders perfectly, but the IconButton is not showing up. const InputHookComponent = (props) =>{ const [val, setval]=useState(""); const cmp = <TextField type={ ...

Styling Javascript Objects

[ [ {"path":"path2","value":"kkk"}, {"path":"path0","value":"uuu"}, {"path":"path1","value":"ppp"} ] ] The result obtained from my manipulation is shown above. However, I require the format to be as depicted below: ["path":"pat ...

Using node.js with express to connect and link CSS and JS files

I am facing an issue with linking my CSS/JS files on a specific page. The linking works fine on localhost:5000/books/, but does not work on localhost:5000/books/1. app.use(express.static(__dirname + "/public")); bookRouter.route('/') ...

I created a chat application using Node.js and Socket.io, but for some reason, messages are not being sent between two browser windows

My server-side JavaScript code seems to have an issue as it is not sending messages when I click the button. var express = require('express'); var socket = require('socket.io'); var app = express(); var server = app.listen(8000,functio ...