1

I send multiple requests using jquery ajax call to my asp.net mvc application. the method that handles this requests is async and it executes a query on my oracle database. but these queries are not sent to database in parallel. why doesnt it send them all in parallel? this is the async controller method:

public async Task<int> GetVoltageRowCount(pLayerName)
{
    DataWrapper featuresDataWrapper = new DataWrapper(pLayerName);
    int rowCount = await Task.Factory.StartNew<int>(() => featuresDataWrapper.GetRowCount());
    return rowCount;
}

this is client request. (are sent parallel) jquery ajax request and this is V$SQL oracle table. about 34 seconds takes to send all requests. V$SQL table

3 Answers 3

1

Most likely, ASP.NET has session state enabled, which will deliberately serialize all requests for a given user. Try changing the session state to ReadOnly or disabled.

Sign up to request clarification or add additional context in comments.

2 Comments

that did it. thank you. but with disabling sessions and even without 'await async' all requests are sent at the same time. now its much faster. is this the only way? I have to disable sessions?
You can make the sessions readonly, if you do need to read data from them in your action. If sessions are enabled (read/write), then they automatically serialize, preventing parallel execution.
1

You should not use Task.Run or Task.Factory.StartNew. It seems you don't actually understand what async is. Namely, it's not parallel processing. All async does is allow the running thread to be returned to the pool if it enters a wait-state. Threads are generally a limited commodity; they have overhead and consume system resources. In something like a web server, the thread pool is usually capped at 1000, which means you can have 1000 active threads at any given time. Async simply allows threads which are idle to be returned to the pool, so that they can be used to do additional work, rather than just sitting there idle.

In the context of a web server, each request is assigned a thread. This is why the thread-cap on the server is usually referred to as the "max requests". One request always equals at least one thread. Doing something like creating a new thread, takes another thread from the pool, so now your single request is sitting on two threads (effectively halving your server's potential throughput). Now, if the action is async, the first thread will be returned to the pool, because the new thread is now active and the original is now idle. However, that means you've bought yourself nothing: you've simply traded one thread for another and added a bunch of unnecessary overhead. Creating a new thread is not the same as background processing; that's a common and dangerous misconception.

Plain and simple, the server cannot return a response until all work has completed on the action. Whether you use one thread or a thousand to get there, makes no difference in terms of response time (except that using more than one thread actually increases your response time, causing more delays, not less). Even async done properly will actually increase your response time, even if only by microseconds. This is because async has inherent overhead. Sync is always quicker. The benefit from async is that you can utilize resources more efficiently, not that anything happens "faster".

Comments

0

You should not mix Task.Factory.StartNew with async-await. Use Task.Run instead.

But, since it's an ASP.NET application, by doing async over sync, you're just switching from one thread pool thread to another and then to another which will make the overall system slower.

3 Comments

so is there a way to improve this? make asp.net send requests to database in parallel?
that did not work. still not sending in parallel. @Paulo Morgado
Does the database allow parallel access? If it's parallelism instead of asynchrony that you want, try ParallelForEach from the TPL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.