Creating 5,000 background threads in my ASP.NET Web app
Threading is ridiculously complex to get right, and the idea of having 5,000 background worker threads is of course madness, unless you're using Hyperlambda, at which point it's just another Saturday
Join the DZone community and get the full member experience.Join For Free
I once saw a guy speaking about multi threaded programming, and he asked his audience how many of the people amongst had audience had created multiple threads, at which point half the room lifted their arms. Then he asked how many had used dozens of threads running simultaneously, at which point 25% of the room kept their arms up. When he reached the question "how many have created millions of concurrently executed threads" the speaker was the only person left with his arm raised. The reasons for this of course, is because the idea of creating millions of concurrently executed background threads is simply ridiculous to even imagine.
However with Hyperlambda, at least in theory, this is actually quite easily achieved. To understand how this is possible, realise that multi threading is just a state machine, allowing you to perform context switches, at the CPU level, with an interrupt scheduling work for each individual thread.
Once you understand the above, you realise you can create "multiple threads" using alternative mechanisms, that scales much better than CPU threads, allowing you to create millions of these buggers, without clogging your Web API backend in any ways what so ever. To bring home the point I am demonstrating how to create 5,000 background worker threads in Hyperlambda in the following video, each running in an "infinite loop", repeating some task over and over again. To further the insult, each individual worker thread is writing to a MySQL database, and everything is barely noticeable for my backend in any ways, and my backend is still almost as responsive as it would be without these background threads concurrently executing. And in fact, any performance loss is probably due to my MySQL server being hammered, and not my backend in any ways what so ever. Implying if I exchanged the default logger in Magic, I wouldn't even notice I've got 5,000 concurrently executed threads executed in parallel. And as a final touch, each thread's workload is persisted into my database, implying I can reboot my server without loosing any threads ...
Sounds like Magic? That's because it IS Magic ;)
In the above video I am dealing with 5,000 logical threads. However, at the operating system level, for all practical concerns, I am consuming zero threads, 99% of the time.
This is easily achieved with some "intelligent scheduling", allowing me to create 5,000 concurrently running background tasks, that for all practical concerns becomes the equivalent of 5,000 "logical background worker threads", while never consuming more than maximumly 8 threads from my operating system, and due to the async nature of Hyperlambda, consume zero threads 99% of the time. Implying my backend is still perfectly responsive, almost at the level it would normally be, without any of these background tasks running at all.
Of course, creating 5,000 background worker threads in for instance C# or something would completely choke my web server, making it beg for mercy at the operating system level, and have everything fall down and crash. With Hyperlambda, it's just another walk in the park :)
The code I am using can be found below. First how to create 5,000 scheduled tasks.
// Create 5,000 scheduled tasks .no:int:1 while lt get-value:x:@.no .:int:5000 .lambda guid.new convert:x:- type:string tasks.create:x:@convert repeats:120.seconds .lambda log.info:Scheduled task executed math.increment:x:@.no
Then how to delete the tasks afterwards.
tasks.list limit:5000 for-each:x:-/*/*/id tasks.delete:x:@.dp/#
If you want to reproduce what I am doing in the above video, you'll need at least version 10.0.15 of Magic and Hyperlambda. You can download it below.
Opinions expressed by DZone contributors are their own.