سؤال

I need to send approximately 10,000 small json strings from a C# client application to a web service which will then be inserted into a database.

My question: Is it better to send a large number of small requests or is there some way to send the whole thing /multiple large chunks?

I'm trying to avoid something like this:

List<Thing> things_to_update = get_things_to_update(); // List now contains 10,000 records
foreach (Thing th in things_to_update)
{
    //  POSTs using HttpWebRequest or similar
    post_to_web_service(th);
}
هل كانت مفيدة؟

المحلول

If you control the server and can change the code, it's definitely better to send them batched.

Just encode your objects into json object and put them in an array. So instead of sending data like

data={ id: 5, name: "John" }

make it an array

data=[{ id: 5, name: "John" }, { id: 6, name: "Jane" }, ...]

then parse it in your Controller action on the server side and insert it. You should create a new Action that handles more than 1 request for sake of cleaner and more maintainable code.

I'd suggest splitting it into smaller batches of 1000 or 2000 rather than sending all 10000 at once. Easy done with Linq:

int batchSize = 1000;
for(int i=0; i<things_to_update.Count; i+= batchSize) {
    List<Thing> batch = things_to_update.Skip(i).Take(batchSize);
    post_to_web_service(batch);
}

نصائح أخرى

Post the whole List<Thing>.

I believe a string in C# is generally 20 bytes + 2x the size of it's length.

This may not be exactly correct, but just to get an rough approximation:

Assuming your "small" (?) json strings are bout 100 chars long then, that should result in 10'000 string-objects, each of approximately 220 bytes, which results in a total size of 2'200'000 bytes, which is approximately 2 megabytes.

Not too much, in other words. I would definitely prefer that to 10'000 connections to your web-service.

If for some reason 2 mb is too much to accept at a go for your WS, you could always split it into a few smaller packages of say 1mb, or 200 kb, etc. I've no doubt you'll still get way better performance doing that than trying to send 10'000 strings, one at a time.

So yes, place your strings in one (or a handfull of) array(s), and send them in a batch.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top