Optimizing image requests served through cfm pages...
# cfml-general
a
Optimizing image requests served through cfm pages - I have pages that include 10-40 images that are dynamically served through cfm pages on a legacy site. Where possible (like if there is no security/resizing needed) I am moving towards putting them on a CDN, but they are part of complex workflows and it is not trivial... and there are 700k of them. Anyway until then - I was thinking I would create a separate application.cfc in the appropriate images dir that turns off sessions and has pretty barebones almost empty app.cfc - instead of the main (complex) app.cfc having to run on every call. We get a lot of bot activity (no cookies) so definitely don't want sessions enabled for these image requests. Does that sound a reasonable approach? I could alternatively setup a separate web context for this if that is better, which I guess might also help client-side with max parallel http connections if hitting a different host. Basically trying to lower the overhead of these calls. Using IIS and Lucee so might turn on IIS output caching too. Maybe someone solved similar issues in the past
m
to use a "buzzword", what you are wanting to develop is a "microservice". and for massive image processing, I would recommend doing a small node.js app - just avoid CF image tags... or if you are going to use CF, use a separate service/server to ensure it doesn't clog up your normal server.
but you are on the right track - definitely do an isolated app with a barebones framework (this is one case where a custom framework is easily justified)
m
I had a client who used Azure Functions for this. Basically everything was in blob storage and CF just called out to the endpoint and got what it needed. Cheap, fast. But that's probably more your end-game and not the stop-gap you're looking for here.
m
@Mark Takata (Adobe) sounds like there's some image processing involved. if he had to resize/crop/etc 700k images, would you recommend cf as a standalone service, or something else? (my experience processing many/large images/PDF documents has not been favorable)
m
If doing either image processing or PDF generation at scale, I recommend a stand-alone server/service, as those can do a number on your CPU. I consider this a great option for serverless, as it is a high requirement/low uptime situation. AWS Lambda is a good option here (though the actual lambda itself would not be available using ACF, you'd need Lucee in Fuseless or a different langauge) and you could use the build-in AWS Lambda authentication/object support in CF2021: https://helpx.adobe.com/coldfusion/using/get-started-aws-lambda.html
z
Lots of memory helps, a 10mb jpeg can be 100 of mb uncompressed
m
"lots of memory helps" I keep forgetting that. 😉
sadpanda 1
a
Thanks all. Excellent input.
t
you can simply bypass cf and use the servlet response
Copy code
pc = getpagecontext().getresponse();
        pc.reset();
        pc.resetBuffer();
        pc.getresponse().reset();
        pc.getresponse().setcontenttype('image/png');
        pc.getresponse().setcontentlength('#arraylen(binData)#');
        pc.getresponse().getOutputStream().write(binData);
r
Just to chime in with others, I build AWS Lambda async functions on S3 buckets of images using Node.js for exactly this sort of thing. Works great for such a scenario. We also have such Lambda functions running all image processing on upload to CF apps - it avoids all the heavy load on CPU/memory that image processing can have in ACF/Lucee.
e
1. Most enterprise load balancers let you set rules for "bots", AWF calls it WAF Bot Control Google has "cloud armor", F5 has bot defense, so on and so forth. Even if you only have a SINGLE site, its well worth putting your session control in the hands of a load balancer. You can then direct BOT traffic and other crap someplace else, while good traffic you can pass to your application. 2) if you are manipulating thousands of images in a standard way, upload the images, then execute a function on the OS to manipulate the files and return the result. It is far less resource intensive. Imagemagik comes to mind, there are cross platform binaries. You can either run a cron / scheduled task to do something, or you can write a simple cfsript that builds the needed OS script to do what you want with the file.
a
@thisOldDave - thanks. that doesn't bypass application.cfc though, right? just bypasses CF processing of the page itself. Still definitely faster though. Across the board - some really interesting ideas here.
t
@Anders Lars no you will still go through application.cfc but CF will not try and manipulate the image on the way out.
m
Also remember that applications.cfc works differently than application.cfm. .cfm gets run, in its entirety, on every template load. .cfc only runs the portions of the template that matter to the load's scope (so if the application is starting, on application start fires, etc. This can make a big difference to CPU load and the like (and is why .cfc is the preferred, modern method).
👍 1
a
got it. I started CF dev in 2022, after a life of C#, so I have barely even heard of application.cfm .. Cfc is all I know.. unless google sends me off to a 2005 ben.nadel blog post 😉
m
Welcome to the wild and crazy world of CF! I spent 10 years in C# in the middle of my career (26 years+ of CF) so if you have any questions, please feel free to reach out. Happy to point you at resources as well. I do a CFQuickTip on LinkedIn every week that might help you out as well (always under 5 minutes). If you shoot me an email I can send you over a list of other resources as well 🙂
👍 1
I should also point out, I've done .net forms, MVC4.5 and Core, but not Entity Framework, so if that was your jam I might not be as helpful.