4 ms·
Lesson 0: Don't do premature optimization.
by mseebach 18y ago
Lesson 0: Don't do premature optimization.
- tdavis 18y agoLesson N: Use a system that doesn't require you to manually remove cache files. Seriously.
- patio11 18y agoSo being that this is Rails there are a couple of ways I could have done this (assuming, again, that I cached): 1) Cache the page in memcached, which would give me access to an easy time-based expiry syntax. 2) Page cache. Instead of cleaning the cache with a one-line cron script, write a 10 line cache sweeper. Then, install a plugin to periodically execute the sweeper... or execute it with a cron script. 3) Page cache. Delete with a cronjob. Why I didn't do #1: Memcached is overkill for my needs, and keeping another process running on the VPS just gobbles its 256MB of RAM even faster, while giving me another potential security/uptime headache. Why I didn't do #2: Doesn't eliminate the dependency on cron, it just moves the actual delete operation from a single line in the crontab to more complicated Sweeper logic in the app. That is logic I'd then have to actually maintain and whatnot. (And doing #2 wouldn't have helped me if I had executed the Sweeper job as the same user I executed rm -rf as, because it would similarly have been unable to remove the file.) I might be careless but I don't make choices totally at random. ;)
- henning 18y ago"keeping another process running on the VPS just gobbles its 256MB of RAM even faster" Why not upgrade? On Slicehost, going from 256 to 512 MB is only $20 extra. If you're running a business on it, I think that's a pretty reasonable expense, especially if it can save you even a little time (within reason).
- tdavis 18y agoI never said you made it at random, just that even if it were the best option it was still a poor one. From the options you laid out, it appears none of them were particularly suited to your situation. If it were me, upon realizing that, I would have just waited to see if caching were really necessary before instituting a cron hack.
- subwindow 18y agoIt doesn't: http://api.rubyonrails.org/classes/ActionController/Caching/Pages.html#M000294 http://api.rubyonrails.org/classes/ActionController/Caching/... Removing them with a cron task is usually the worst way to solve this problem. Using the expire_page method is the correct solution when the updates are being done as a result of a user's action (updating/deleting/creating a record). The problem here was that they weren't being done by a user, but automatically by the system.
- jonursenbach 18y agoThere's nothing wrong with premature optimization. Who knows if your site will ever get hit hard by Slashdot, Digg or the like. If your site isn't up to par, you'll crash and all of those people, and possible ad revenue, will turn away. The problem here is when you don't test your code properly.
- abstractbill 18y agoThere's nothing wrong with premature optimization. Surely there's always something wrong with premature anything, by definition of the word "premature".
- swombat 18y agoThere's everything wrong with premature optimisation. Optimisation isn't free. It costs in terms of time and code elegance and versatility - often once you've optimised a piece of code to be fast, it's more complex, harder to understand, and harder to change. Premature optimisation is bad because of this. Taking things one step further, what is premature? Well, since your production architecture (if you're a start-up on a shoestring) may well be vastly different from your development architecture or your staging architecture (you may run your production site on a clustered EngineYard set-up that costs hundreds of dollars a month and your staging site on a VM that costs $20). And even if the architecture is identical, the data volumes and patterns may be different. And even if you can afford an exact duplicate of your production environment, and keep the data exactly in sync with your production system, usage patterns may be different. And even if you can afford to simulate the usage patterns in a realistic manner, there will be differences in how the traffic gets routed on an IP level which will also cause the bottlenecks to shift. All this to say, if you're a start-up on a budget, do not optimise until you see a problem in production. You may well be surprised by how it's not the bottlenecks you expected that actually slow you down. My application has no indexes in the database, because so far, database access has never been a bottleneck. On the other hand, ruby parsing of objects, and AMF rendering of them, and complex aggregation operations on the objects - all those have caused problems that I only found once I was able to observe them happening in a production environment. Simple solution: don't optimise before you see the problem begin to develop in production.