1

I have a .NET Core service. We want to implement fire and forget call for one of its endpoint. When request comes to this end point it should fire a method and immediately returns response as Ok. The method then processes that requests by calling other services, and some DB operations.

The above works fine. But, when I tried to do perf testing with thousands of requests, sent back to back, we noticed that some requests are not getting processed at all. It is working fine up to 4000 requests but more than that it is not processing some(approx 20+) requests. There are no exceptions in the logs

How can I identify the issue?

await Task.Factory.StartNew(() => FireAndForgetMethod()); return Ok(); 
4
  • thank you, you too stay safe :) Commented Jun 20, 2020 at 9:23
  • 1
    You may find this interesting: Fire and Forget on ASP.NET Commented Jun 20, 2020 at 10:08
  • You are probably exhausting the thread pool. You should not start new threads to perform a fire-and-forget. Actually, in general, you shouldn't start new threads in an asp.net app at all. Commented Jun 20, 2020 at 10:48
  • 3
    "Fire & forget" == "Fire & don't care" Commented Jun 21, 2020 at 17:15

1 Answer 1

2

"Fire and forget" literally means that you don't care if the completes and you're perfectly fine with ignoring exceptions. Since you do care that it completes and you do want to see exceptions, then you do not have a situation that is appropriate for "fire and forget".

When request comes to this end point it should fire a method and immediately returns response as Ok. The method then processes that requests by calling other services, and some DB operations.

Your API endpoint should serialize the work to be done into a message and place it on a reliable message queue (e.g., Azure Queue, Amazon Simple Queue, etc), after which it may return an HTTP result to its caller. This queue should then be read and each message executed by a background processor (e.g., ASP.NET Core Worker Service).

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

5 Comments

Thanks for responding. I have been referring your articles on this topic :). Based on your article I broke the architecture and now have a consumer which reads the message, calls other services and do DB operations. Now it could be more than 10K messages; and I am noticing a slowness in processing. It almost takes more than 10mins to process all this messages. I have caching, chunk processing in place. Any thoughts on how I can improve the performance?
The easiest approach is to add multiple consumers (assuming your DB can handle the demand). One nice side effect about splitting the architecture like this is that the consumers can scale separately from the API.
All these records are part of a batch. Now API taking this record from a batch, putting them into a queue and processing them via consumer. I tried Parallel processing using TPL library but it is inconsistent. Multiple consumers will lead to duplication of code. Correct me if I am wrong. Or do you mean one consumer for external service call and another for DB operations?
@arpymastro: Sorry, I mean to have multiple instances of your consumer - no code duplication. E.g., if your consumer is a Win32 service, run it on multiple boxes.
Thanks Stephen. I will try implementing scaling rules on consumers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.