Why cfthread in a loop is a very bad idea

Share this post:

I just run into an issue where one of our servers was totally bogged down when users started to download 100 or more files. See, the problem is that we had to check that each file was downloaded successfully before processing with the rest of the code. In other words;

1) Query for files that need to be downloaded
2) Loop over the records
3) Within loop download the file
4) check if file has been downloaded successfully
5) continue with the code.

In Coldfusion (or CFML to be general) your code would look like:

<cfloop query…>
<cfthread…>
<cfhttp…>
<cfthread action=”join”…>

As I mentioned above, we need to check if the file has been downloaded successfully before continuing with the next download. That’s why  there is a cfthread in this code.  Now, while this might be a good idea with small download, it has a hazardous effect if you will trigger 1000 downloads, which again would create 1000 threads!!!

So, there are actually 2 things I’ve learned from this:

  1. There is no need for the cfthread. Since cfhttp will halt the code process until the file has been downloaded, creating the thread is not needed.
  2. Creating a thread within the iteration of your resultset (cfloop) is a VERY  bad idea!

Over 10 years in business.

Self-funded. No investors. No bullshit.

More than 3,000 customers worldwide.

Helpmonks - no bullshit customer engagement service

Growth starts with action

Empower your team and delight your customers.

Helpmonks - email management for small businesses