Does (Queue) CreateIfNotExistsAsync slow things down when doing lots of storage calls? e.g. lots of Add/Get/Delete messages?
See original GitHub issueHi 👋
Question: I’m wondering if calling CreateIfNotExistsAsync
does a single request to Azure Storage each time that is called. Leading to: wasted HTTP requests after the first time it’s checked.
Nearly all the examples I keep seeing generally follow the following pattern:
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a queue.
CloudQueue queue = queueClient.GetQueueReference("myqueue");
// Create the queue if it doesn't already exist.
queue.CreateIfNotExists();
// Now do stuff with the queue, like AddMessage, etc.
This is great if it’s a single operation (e.g. Add 1x message). But if we need to keep adding a heap of messages, over time (ie. not all via one call to this method, but as people “do stuff” with your website, like buy things, signup, whatever) … then these calls get called a lot and if feels like it’s wasted cycles and can slow things down … especially if the CreateIfNotExists()
is doing HTTP (or TCP?) calls to the Storage.
So, it’s pretty easy to change things around … like say …
// Inject the storage account credentials.
public QueueService(CloudStorageAccount cloudStorageAccount)
{
_cloudStorageAccount = cloudStorageAccount ?? throw new ArgumentNullException(nameof(cloudStorageAccount));
// Create a queue ONCE and only when first required...
_queueClient = new Lazy<CloudQueue>( () =>
{
var queueClient = _cloudStorageAccount.CreateCloudQueueClient();
var queue = queueClient.GetQueueReference("my-queue");
queue.CreateIfNotExists(); // DOESN'T EXIST ANYMORE. it's CreateIfNotExistsAsync();
return queue;
})
}
So now we can create the queue ONCE when the queue if first ‘requested’. But there are now no sync methods on the CloudQueue. They are all async. which means i need to do an async/await inside a Lazy (urgh, not easy, need 3rd party nuget) and then inside a ctor (urgh. death…).
So I’m stuck. I’m not sure what other people do/are doing.
Sure it’s easy for me to just add the CreateIfNotExistsAsync
just before i do my action (AddMessageAsync
, etc) but that comes down the crux of my issue -> If i call CreateIfNotExistsAsync
each time, that does a single HTTP/TCP request … which is expensive and-or an overkill after i’ve checked for the very first time).
So - any hints?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top GitHub Comments
Thank you for the discussion, we are closing the issue.
@PureKrome my project isn’t core yet so I’m running the sync versions real early in my owin startup.
To run asynchronous methods synchronously you should call
.GetAwaiter().GetResult()
(this ensures any exceptions that are thrown are not wrapped in anAggregateException
).In your webjobs you can call these asynchronously by making your
Main
methodasync
like so: