Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core and Kestrel design decision: Why do they use libuv?

I just realized that ASP.NET Core apps are not pure CLR apps - they always depend on additional binary libraries: libuv through Kestrel or, probably way less used, http.sys.

I find this surprising as I would have thought there to be already enough networking api under .NET Core (and so .NET Standard) to make a decent performing web server with asynchronous IO purely in .NET - yet they didn't go that route.

So:

  • Why is using libuv making stuff faster? "Because async IO" alone can't be all there's to it, as .NET already has that.
  • Why does Kestrel not have the option to run on top of .NET's IO stack? In most cases the speed difference can't be that important, right?
  • What kind of IO exactly does go through the better performing libuv? I reckon that's only the inbound requests. Outgoing requests through WebClient, HttpRequest or HttpClient wouldn't use libuv, correct?

EDIT:

I had a look at the Kestrel sources and besides an assembly called Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv there also is an assembly called Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets. The corresponsing NuGet package for the sockets assembly is in preview only though. (The sockets assembly doesn't depend on libuv of course.)

like image 483
John Avatar asked Feb 01 '18 08:02

John


People also ask

What is the Libuv in ASP.NET Core?

Earlier versions of ASP.NET Core used Libuv as an implementation detail of how asynchronous input and output was performed. In ASP.NET Core 2.0, an alternative, Socket-based transport was developed. In ASP.NET Core 2.1, Kestrel switched to using the Socket -based transport by default.

Why do we need Kestrel server?

Kestrel used as an edge server without a reverse proxy server doesn't support sharing the same IP and port among multiple processes. When Kestrel is configured to listen on a port, Kestrel handles all of the traffic for that port regardless of requests' Host headers.

How does Kestrel work in NET Core?

It is included by default as internal server in ASP.NET Core. Kestrel can be used, by itself as an edge server i.e Internet-facing web server that can directly process the incoming HTTP requests from the client. In Kestrel, the process used to host the app is dotnet.exe.

Does Kestrel work on Linux?

With support for Unix sockets, Kestrel is compatible with most load balancers, including Nginx. This makes it ideal for use in containers, either under Linux or in a Windows container.

Is libuv still supported in Kestrel?

In ASP.NET Core 2.1, Kestrel switched to using the Socket -based transport by default. Libuv support was maintained for compatibility reasons. At this point, use of the Socket -based transport is far more common than the Libuv transport. Consequently, Libuv support is marked as obsolete in .NET 5 and will be removed entirely in .NET 6.0.

What is libuv in ASP NET Core?

Earlier versions of ASP.NET Core used Libuv as an implementation detail of how asynchronous input and output was performed. In ASP.NET Core 2.0, an alternative, Socket -based transport was developed.

What is ASP NET Core Kestrel?

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the web server that's included by default in ASP.NET Core project templates. Kestrel is supported on all platforms and versions that .NET Core supports.

Is libuv obsolete in NET 5?

At this point, use of the Socket -based transport is far more common than the Libuv transport. Consequently, Libuv support is marked as obsolete in .NET 5 and will be removed entirely in .NET 6.0.


Video Answer


1 Answers

The plan is to switch to Sockets but it may not make it in time for Net Core 2.1 but could be available for the public.

It seems that as of December 2017 they had to switch back from Sockets to Libuv due to performance issues:

see https://github.com/aspnet/KestrelHttpServer/issues/2220

like image 194
Max Quagliotto Avatar answered Oct 20 '22 09:10

Max Quagliotto