skip to content
Relatively General .NET

Using PathBase with .NET 6's WebApplicationBuilder

by Andrew Lock

posted on: June 07, 2022

In this post I describe the difficulties of adding calls to UsePathBase with .NET 6 WebApplication programs, and describe two approaches to work around it…

Building a Call for Papers website with Blazor & RavenDB

by Oren Eini

posted on: June 06, 2022

CodingAfterWork has published a three parts series (more than 5 hours!) showing how you can build the Next Tech Event using Blazor and RavenDB. Take a look, it’s interesting.

Using AV1 video codec to reduce web page size

by Gérald Barré

posted on: June 06, 2022

This post is part of the series 'Web Performance'. Be sure to check out the rest of the blog posts of the series!Website performance: Why and how to measure?Website performance: How I've improved the performance of this website?Using AV1 video codec to reduce web page size (this post)Using Avif cod

Challenge

by Oren Eini

posted on: June 03, 2022

The following code does not output the right value, can you tell why? This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters record Item(string Name, dynamic Extra = null); static void Main(string[] args) { var obj = new Item("Milk") { Extra = new Dictionary<string, object> { ["Skim"] = false, ["Viscosity"] = 39m } ["Type"] = "Food", }; Console.WriteLine(JsonConvert.SerializeObject(obj, Formatting.Indented)); } view raw strange.cs hosted with ❤ by GitHub This has been a real bug that we ran into (with only slightly more complicated code. The sort of things that make you just stare at the screen in disbelief when you realize what is going on…

re

by Oren Eini

posted on: June 02, 2022

I run into this fascinating blog post discussing the performance of BonsaiDb. To summarize the post, the author built a database and benchmarked it, but wasn’t actually calling fsync() to ensure durability. When they added the fsync() call at the right time, the performance didn’t degrade as expected. It turned out that they were running benchmarks on tmpfs, which is running in memory and where fsync() has no impact. Once they tested on a real system, there was a much higher cost, to the point where the author is questioning whether to continue writing the database library he has developed. Forgetting to call fsync() is an understandable issue (they called the flush() method, but that didn’t translate to an fsync() call). I recall that at one point the C#’s API once had a bug where a Flush() would not call fsync() if you were making writes with 4KB alignment (that is… super bad). After figuring out the issue, the author has out to figure exactly how to ensure durability. That is a journey that is fraught with peril, and he has some really interesting tidbits there. Including sync_file_range(), fdatasync() and how you cannot write a durable database for Mac or iOS. From my perspective, you cannot really trust anything beyond O_DIRECT | O_DSYNC or fdatasync() for durability. Almost a decade ago I wrote about performance testing that I did for various databases. My code was the 2nd fastest around for the tested scenarios. It was able to achieve almost 23,000 writes, almost 25% of the next slowest database. However, the fastest database around was Esent, which clocked at 786,782 writes. I dug deep into how this is done and I realized that there is a fundamental difference between how all other databases were working and how Esent was handling things. All other databases issued fsync() calls (or fdatasync()). While Esent skipped that altogether. Instead, it opened a file with FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_DIRECT (the Unix version is O_DIRECT | O_DSYNC). That change alone was responsible for a major performance difference. When using O_DIRECT | O_DSYNC, the write is sent directly to persistent medium, skipping all buffers. That means that you don’t have to flush anything else that is waiting to be written. If you are interested, I wrote a whole chapter on the topic of durable writes. It is a big topic. The other thing that has a huge impact on performance is whether you are doing transaction merging or not. If you have multiple operations running at roughly the same time, are you going to do a separate disk write for each one of them, or will you be able to do that in a single write. The best example that I can think of is the notion of taking the bus. If you send a whole bus for each passenger, you’ll waste a lot of time and fuel. If you pack the bus as much as possible, for almost the same cost, you’ll get a far better bang. In other words, your design has to include a way for the database to coalesce such operations into a single write. Yesterday there was an update to this task, which more or less followed that direction. The blog post covers quite a lot of ground and is going in the right direction, in my opinion. However, there are a few things there that I want to comment upon. First, pre-allocation of disk space can make a huge impact on the overall performance of the system. Voron does that by allocating up to 1GB of disk space at a time, which dramatically reduces the amount of I/O work you have to do. Just to give some context, that turns a single disk write to multiple fsyncs that you have to do, on both your file and the parent directory, on each write. That is insanely expensive. The storage engine discussed here used append only mode, which makes this a bit of a problem, but not that much. You can still preallocate the disk space. You have to scan the file from the end on startup anyway, and the only risk here is the latency for reading the whole pre-allocation size on startup if we shut down immediately after the preallocation happened. It’s not ideal, but it is good enough. Second, the way you manage writes shouldn’t rely on fsync and friends. That is why we have the log for, and you can get away with a lot by letting just the log handle the durability issue. The log is pre-allocated to a certain size (Voron uses dynamic sizes, with a max of 256MB) and written to using O_DIRECT | O_ O_DSYNC each time. But because this is expensive, we have something like this (Python code, no error handling, demo code, etc): This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters class WriteAheadLog: def __init__(self) -> None: self.queue = [] self.hasWrites = threading.Event() self.log = open("write-ahead.log", O_DIRECT | O_DSYNC) th = threading.Thread(target=self.backgroundThread) th.start() def writeToLog(self, pages): future = loop.create_future() self.queue.append((pages, future)) return future def backgroundThread(self): while self.hasWrites.wait(): self.hasWrites.clear() pending = self.queue self.queue = [] pagesToWrite = [] for (pages, _) in pending: pagesToWrite = pagesToWrite + pages dataToWrite = CompressPages(pagesToWrite) self.log.write(dataToWrite) for (_, future) in pending: future.set_result(True) view raw WriteAheadLog.py hosted with ❤ by GitHub The idea is that you can call writeToLog() each time and you’ll get a future on the write to the log file. You can continue with your transaction when the log holds the data. Note that in this model, if you have concurrent writes, they will be merged into a single disk write. You can also benefit significantly from reduced amount we write to disk by applying compression. Third, something that has been raised as an option here is a new storage format. I’m not sure that I 100% get what is the intention, but… what I understand I don’t like. I think that looking at how LMDB does things would help a lot here. It is a COW model (which the append only is very similar to). The key difference is that the new model is going to store every header twice. Probably with a CURRENT and NEXT model, where you can switch between the two configurations. That… works, but it is pretty complex. Given that you have a log involved, there is no need for any of this. You can just store the most recent changes in the log and use that to find what the current version is of the data is. I don’t like append only models, since they require you to do compaction at some point. A better model is what LMDB does, where it re-used the same pages (with copy on write). It requires you to manage a free list of pages, of course, but that isn’t that big a task.

Managing Up and Getting Ahead

by Ardalis

posted on: June 01, 2022

If you're an employee, you probably have a boss, manager, supervisor, or similarly titled person to whom you report. Normally, you figure it…Keep Reading →

Understanding PathBase in ASP.NET Core

by Andrew Lock

posted on: May 31, 2022

In this post I'll describe a lesser-known property on HttpRequest called `PathBase`. I describe what it does, when it's useful, and how to use it.…

Round-robin DNS support in .NET HttpClient

by Gérald Barré

posted on: May 30, 2022

Round-robin DNS is a load-balancing technique where the DNS returns multiple IP addresses for a host. The client can choose any of the IP addresses to connect to the server. For example, bing.com has 2 IPv4 addresses:In .NET, the HttpClient connects to the server using the first IP address in the l

InfoQ interview with me on RavenDB, database consistency and using C# as a system language

by Oren Eini

posted on: May 27, 2022

My podcast interview with Wesley Reisz from InfoQ has been published. I think it was a very interesting discussion.

Cloud security and college assignment

by Oren Eini

posted on: May 26, 2022

I’m teaching a college class about Cloud Computing and part of that is giving various assignments to build stuff on the cloud. That part is pretty routine. One of my requests for the tasks is to identify failure mode in the system, and one of the students that I’m currently grading had this doozy scenario: If you’ll run this code you may have to deal with this problem. Just nuke the system and try again, it only fails because of this once in a while. The underlying issue is that he is setting up a Redis instance that is publicly accessible to the Internet with no password. On a regular basis, automated hacking tools will scan, find and ransom the relevant system. To the point where the student included a note on that in the exercise. A great reminder that the Network is Hostile. And yes, I’m aware of Redis security model, but I don’t agree with it. I’m honestly not sure how I should grade such an assignment. On the one hand, I don’t think that a “properly” secured system is reasonable to ask from a student. On the other hand, they actually got hacked during their development process. I tried setting up a Redis honeypot to see how long it would take to get hacked, but no one bit during the ~10 minutes or so that I waited. I do wonder if the fact that such attacks are so prevalent, immediate and destructive means that through the process of evolution, you’ll end up with a secured system (since unsecured isn’t going to be working).