Actually my question is not only targeting for play framework, but a general question for async programming.
I know that in play, we can use:
WS.url(url).get()
to make a rest call async by return something like promise/future. (I guess it might be supported by underlying NIO library like netty or spray.io)
So, my question is: to build a fully async web application, every I/O bound operation has better to be async, right? Otherwise, it will be a bottle neck?
Let's say I have a very simple web application which only calls a JDBC and return query value to browser.
In this case, suppose I don't have an async/reactive JDBC driver, will play2 performs better than a servlet solution?
Related
I encoutered a question here and need your help.
I had a .net web api project using PushStream to do async downloading, something like
new PushStreamContent(async (outputStream, httpContent, transportContext))=>{}
In this way, I can do multiple parts downloading in the action.
However, now I want to move the project into .net core and I cannot find a replacement in .net core for pushstream.
Could you please let me know is there something like pushstream in .net core or any methods to implement it?
Thanks a lot.
PushStreamContent works by essentially setting a callback to be invoked when the output stream is being processed. This was necessary in ASP.NET Web Api because there was no direct access to OutputStream.
However, in ASP.NET Core, you do have direct access to the output stream via HttpContext.Response.Body. As a result, you can just directly write to that stream without needing something like PushStreamContent.
FWIW, while this can save some server resources, it's actually pretty bad, especially with something like an API. Since you're writing directly to the output stream, the headers are already sent, including the response status code. This means if there's any exception raised while you're writing, there's no opportunity to handle it appropriately. The response is simply aborted, and the client is left hanging with a supposedly "successful" response, and only a partial body. Personally, I would say avoid this practice altogether and take the resource hit. Unless you're streaming gigs of data, any good server setup will have a plentiful amount of RAM to handle what you're doing. Even then, there's probably better methods to handle delivering that data.
Is it worthwhile to use Task based Asynch action methods in an ASP.NET MVC project to retrieve a bunch of EntityFramework queries which use LINQ-to-EF. The website is hosted in Azure as is the database. There are quite a few queries that load up a bunch of arrays to send via JSON to an ajax call from the client.
It is worthwhile as long as you are using truly async api (such as EntityFramework async methods) and not creating an async wrappers for example by wrapping your synchronous core in Task.Run or Task.FromResult.
Asynchronous method doesn't suppose to block any thread while it executes (Great article about this).
By the way, because both your application and database are cloud based, it actually makes sense to look into async approach since both your application and you DB can scale.
Technically, it's all three. There's network latency in sending the query/receiving the results. There's CPU-bound work when SQL Server or whatever is actually running the query against your database, and there's I/O as the database itself is a file on the filesystem.
However, from the perspective of your application, interacting with an external database via Entity Framework is network-bound, and is eligible for async as a result.
However, async is not a magical pill that will make your app run faster. If anything using async can actually slow your application down, as there's a lot of overhead to make async work. It's only purpose is to allow the thread doing work to return to the pool while it waits on some task outside of its control to finish. In the case of a database query, the query is sent off to the database which takes some period of time depending on network latency. The database must run the query and construct a result which takes some period of time depending on the resources available on that server. Then, the result is sent back to your application, which again incurs network latency. In a synchronous scenario, your application would just sit there and wait, holding onto the thread. In an async scenario, the thread is released to do other work (server other requests for the web server, etc.) and then when the database response is received the thread is requested back to finish whatever was going on in the application.
In case it's not obvious, the purpose of this is to allow the web server or whatever else you're working with to handle additional load by utilizing periods of what otherwise would be downtime doing other work. In that regard, async is important for using resources efficiently, but it's not necessarily quicker or more performant.
I am working in WPF now...here we use Background_Worker,TaskFactory.StartNew functions and async/await.
In web apps I know that we have AJAX, MVC partial views etc.
My question is do we really use above mentioned async methods (Tasks, sync/await) in Web apps ? If we use, could you give me some examples ?
I have a document generation module in my web app in which I have to fetch data from DB and create documents out of it. Is it a good scenario for async calls? (to call the function and throw..and never care about it).
I am working in WPF now...here we use Background_Worker,TaskFactory.StartNew functions and async/await.
You really should be using Task.Run instead of BackgroundWorker and Task.Factory.StartNew. Task.Run has much better defaults than StartNew, and is more composable and type-safe than BackgroundWorker.
My question is do we really use above mentioned async methods (Tasks, sync/await) in Web apps ?
On ASP.NET, you should almost never use your own background threads (BackgroundWorker, StartNew, Task.Run). However, you certainly can use async and await.
If we use, could you give me some examples ? I have a document generation module in my web app in which I have to fetch data from DB and create documents out of it.
Yes, database queries are one example of an I/O-based operation. So is "writing" documents - whether as uploads to cloud storage or to the local file system.
I have an MSDN article on async ASP.NET that you may find useful.
Async is useful any time you have disk/database/network I/O. It allows you to do other things while you wait for the I/O to complete, which can result in significant savings. Async isn't really useful for normal CPU bound operations, because of the increased overhead of making it async.
This holds true in WPF as well web applications.
If you want to see a concrete example, Scott Hanselman has a blog entry entitled The Magic of using Asynchronous Methods in ASP.NET 4.5 plus an important gotcha which you may find very interesting.
I am using WebApi with Entity Framework to implement a REST service. I need to log the usage to the database. I can do this in the controller before it returns or in an ActionFilterAttribute. I want to be able to fire off a call to update the database, BUT I don't want to wait for completion. I want the response to return to the user without waiting for the update.
I was thinking about using a BackgroundWorker and passing in the objects.
Anyone have thoughts on if this is the good way to do this?
I think the question you should ask yourself is how much latency the database update can add to your overall response time. In most cases, it will be a lot simple to do the update as part of the request processing. Background worker is generally not a good solution but here is a good read related to that topic. If you absolutely need to do the database update outside of request processing, it will be better to consider a queueing mechanism like MSMQ, RabbitMQ, etc from the perspective of reliability.
I believe my performance problem is related to running in VS2012 debugger. When running the site directly, performance increased drastically. So I won't need to do any 'tuning tricks' if this proves out when deployed to test server.
Have you tried exploring NLog's async capabilities? We use NLog's async wrappers by wrapping a database logging target like so:
<targets async="true">
<target name="dbLog" xsi:type="Database" dbProvider="sqlserver" connectionString=".." />
.
.
</targets>
A quick startup on logging to MS SQL DB using NLog is found here
Moreover, you could make your logging method call async (.NET 4.5 required) and if you do not care for the result, choose not to await the method execution.
Look at this SO Thread for a sample NLog.config.
I started to use silverlight/flex and immediately bumped into the asynchronous service calling. I'm used to solving the data access problems in an OO-way with one server fetch mechanism or another.
I have the following trivial code example:
public double ComputeOrderTotal(Order order)
{
double total = 0;
// OrderLines are lazy loaded
foreach (Orderline line in order.Orderlines)
{
// Article,customer are lazy loaded
total = total + line.Article.Price - order.Customer.discount;
}
return total;
}
If I understand correctly, this code is impossible in Flex/Silverlight. The lazy loading forces you to work with callbacks. IMO the simple expample above will be a BIG mess.
Can anyone give me a structured way to implement the above ?
Edit:
The problem is the same for Flex/Silverlight, pseudo code would
do fine
Its not really ORM related but most orms use lazy loading so i'll remove
that tag
The problem is lazy loading in the model
The above example would be very doable of all data was in memory but
we assume some has to be fetched from
the server
Closueres dont help since sometimes data is already loaded and no asynchronous fetch is needed
Yes I must agree that O/R mapping is usually done on the server-side of your application.
In SilverLight asynchronous way of execution is the desired pattern to use when working with services. Why services? Because as I said before there is no O/R mapping tool at the moment that could be used on the client-side (SilverLight). The best approach is to have your O/R mapped data exposed by a service that can be consumed by a SilverLight application. The best way at the moment is to use Ado.Net DataServices to transport the data, and on the client-side to manage the data using LINQ to Services. What is really interesting about ADS (former Astoria project) is that it is designed to be used with Entity Framework, but the good folks also implemented support for IQueriable so basically you can hook up any data provider that support LINQ. To name few you can consider Linq To Sql, Telerik OpenAccess, LLBLGen, etc. To push the updates back to the server the data source is required to support the ADS IUpdateable.
You can look just exactly how this could be done in a series of blogposts that I have prepared here: Getting Started with ADO.NET Data Services and Telerik Open Access
I can't speak to Silverlight but Flex is a web browser client technology and does not have any database driver embedded in the Flash runtime. You can do HTTP protocol interactions to a web server instead. It is there in the middle-tier web server where you will do any ORM with respect to a database connection, such as Java JDBC. Hibernate ORM and iBATIS are two popular choices in the Java middle-tier space.
Also, because of this:
Fallacies of Distributed Computing
You do not do synchronous interactions from a Flex client to its middle-tier services. Synchronous network operations have become verboten these days and are the hallmark signature of a poorly designed application - as due to reasons enumerated at the above link, the app can (and often will) exhibit a very bad user experience.
You instead make async calls to retrieve data, load the data into your client app's model object(s), and proceed to implement operations on the model. With Flex and BlazeDS you can also have the middle-tier push data to the client and update the client's model objects asynchronously. (Data binding is one way to respond to data being updated in an event driven manner.)
All this probably seems very far afield from the nature of inquiry in your posting - but your posting indicates you're off on an entirely incorrect footing as to how to understand client-side technologies that have asynchronous and event-driven programming baked into their fundamental architecture. These RIA client technologies are designed this way completely on purpose. So you will need to learn their mode of thinking if you want to have a good and productive experience using them.
I go into this in more detail, and with a Flex perspective, in this article:
Flex Async I/O vs Java and C# Explicit Threading
In my direct experience with Flex, I think this discussion is getting too complicated.
Your conceptual OO view is no different between sync and asynch. The only difference is that you use event handlers to deal with the host conversation in the DAL, rather than something returned from a method call. And that often happens entirely on the host side, and has nothing to do with Flex or Silverlight. (If you are using AIR for a workstation app, then it might be in client code, but the same applies. As well if you are using prolonged AJAX. Silverlight, of course, has no AIR equivalent.)
I've been able to design everything I need without any other changes required to accomodate asynch.
Flex has a single-threaded model. If you make a synchronous call to the web server, you'd block the entire GUI of the application. The user would have a frozen application until the call completes (or times out on a network error condition, etc.).
Of course real RIA programs aren't written that way. Their GUI remains accessible and responsive to the user via use of async calls. It also makes it possible to have real progress indicators that offer cancel buttons and such if the nature of the interaction warrants such.
Old, bad user experience web 1.0 applications exhibited the synchronous behaviour in their interactions with the web tier.
As my linked article points out, the async single-threaded model coupled with ActionScript3 closures is a good thing because it's a much simpler programming model than the alternative - writing multi-thread apps. Multi-threading was the approach of writing client-server Java Swing or C# .NET WinForm applications in order to achieve a similarly responsive, fluid-at-all-times user experience in the GUI.
Here's another article that delves into this whole subject matter of asynchronous, messaging/event-driven distributed app architecture:
Building Effective Enterprise Distributed Software Systems
data-driven communication vs behavior-driven communication
Silverlight is a client technology and the Object - Relational mapping happens completely in the server. So you have to forgot about the ORM in Silverlight.
Following your example what you have to do is to create a webservice (SOAP, REST...) that can give your silverlight client the complete "Order" object.
Once you have the object you can work with it with no communication with the server in a normal - synchronous way.
Speaking about Silverlight, you should definitely check RIA services.
Simply, it brings the DataContext from the server to the client from where you can asynchronously query it (there is no need to write WCF services by hand, it's all done by RIA).
C# 5
async / await construct will almost exactly what I want..
watch presentation by anders hejlsberg