Projects with synchronous and asynchronous Apis

Asked

Viewed 1,117 times

2

I am developing a small project and have, for now, two entities:

  • Product and Customer

Product API is asynchronous (with Task and async) and client API is synchronous.

I would like to know if there is a problem in using these two approaches in the same project? What are the problems, if any?

Another question is also when it is actually recommended to use asynchronous Apis in projects?

  • 1

    I believe not, you have the code to check .

1 answer

5


In principle no problem. Of course, the moment they intertwine the synchronous will always prevail and the synchronous stop will cause waiting at that point, but if you are inside something being called asynchronously that part will still maintain competition. If you synchronously call a code in an asynchronous method this call will be synchronous, and only one call to this calling method will be asynchronous, which may not be relevant.

Remember that async is useful for IO, so the last case I mentioned should not have an advantage under normal conditions. The IO operation has to be originally asynchronous to have gains. If the execution is too fast it doesn’t pay to use such a heavy mechanism. It establishes a minimum of 50ms of running time (not cake recipe), which is relatively rare to have such operations. Only with large volumes or some very slow IO will give gain.

In processing, competition or even parallelism with thread and not with async.

I answered in more detail in other questions:

  • The answer is interesting because it is correct for . NET Core but not necessarily for . Net Framewrok (I’m talking about deadlocks that may occur due to synchronization contexts...). https://stackoverflow.com/q/15021304 https://stackoverflow.com/q/48606313

  • @Brunocosta In general I see no difference, in specific code it may be, but the question is not specific. It is possible to get bad in any of them, this mechanism is quite specious.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.