201: HTML: Caching Generated Output For Speed.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

Now that you can generate HTML, why would you ever want to go back to the old way of unchanging HTML? It takes some amount of work to generate HTML. I’m not talking about the work to program this. Make sure to listen to the previous episode for more information. I’m talking about the work that the computer itself needs to do in order to generate HTML. You might think your computer is busy when it’s starting up an application or when uploading or downloading files, but this is called input/output or just IO for short. The computer might be busy but the processor itself will usually be just sitting around waiting for the application to be read from your hard drive or for your files to be sent over the communication lines. IO can slow down what you’re trying to do but it still leaves the processor with time to cool down. For a normal request, the CMS will generate HTML and send to the visitor. The web server forgets all about the HTML that was just sent. If another visitor lands on the same page, then the same work needs to be done all over again. Anytime you find yourself writing or using code that does anything over and over again, that’s a good opportunity to cache the results. And setting up a reasonable cache system in your CMS will help your web server survive a massive increase in visitors. That extra work needed to generate HTML pages may not be much but if your site suddenly gets popular and you get millions of visitors, then your web server just won’t be able to keep up with the total extra work and your whole website will go down. Caching HTML pages for regular visitors will save a lot of work when they would have all gotten the same HTML files anyway. Listen to the full episode for more details including advice on when and how to invalidate the cache so that visitors will be able to get updated web pages. You can also read the full transcript below. Transcript It takes some amount of work to generate HTML. I’m not talking about the work to program this. Make sure to listen to the previous episode for more information. I’m talking about the work that the computer itself needs to do in order to generate HTML. We don’t feel this work because we’re not the ones running the code. The computer is doing all the work and other than sometimes noticing when the fan needs to turn on, we normally don’t think about how much stress we place on a computer. Maybe that’s because normally, we don’t require much from our personal computers. Just writing email or browsing the web leaves your computer idle most of the time. You can be the world’s fastest typist and the computer will still be sitting around waiting for each key to be pressed. Watching a DVD will put your computer under more pressure but it can still easily handle the work. Now, if you want to see your computer struggle, then try creating your own video. There’s a lot of work involved in rendering a video. You can also try compiling a large computer program. It all comes down to this. Your computer will be the most busy whenever it has to create something that needs a lot of calculations. You might think your computer is busy when it’s starting up an application or when uploading or downloading files, but this is called input/output or just IO for short. The computer might be busy but the processor itself will usually be just sitting around waiting for the application to be read from your hard drive or for your files to be sent over the communication lines. IO can slow down what you’re trying to do but it still leaves the processor with time to cool down. Generating HTML has the potential to be heavily IO bound but it does need some amount of work from the processor. It shouldn’t require too much work. And the time it needs to wait around for database calls to complete so it has the basic information it needs to put the HTML files together is more than enough to let