If you are trying to speed up your website, it’s important to first know the difference between a dynamic website, and a static website. These are the two types of websites that currently exist on the Internet.
A quick break down would be that dynamic websites require more server resources, but provide more flexibility when it comes to updating them. While static websites are fast in nature, because they don’t ask the server to do extra work, other than just handing off already completed files to the visitor.
Each one has its own advantages and disadvantages, and in this guide I’ll discuss why you might want to use one over the other in your specific case.
Probably the most frequently used type of website today would be a dynamic website. In order to be dynamic, the website runs a server-side scripting language to dynamically create the page being requested by each visitor.
This type of software uses scripts to connect to databases that pull in info to piece together a complete website. This gives the user a front-end to easily adjust some of the dynamic variables of the site.
In its simplest form this example below would be considered a dynamic website. This is because the config.php file sets up variables to use, and the index.php script then includes these variables to create the actual page.
<?php $title = "My dynamic website"; $body = "This is an example dynamic website"; ?>
<?php include('config.php'); ?> <html> <head> <title><? echo $title;?></title> </head> <body> <? echo $body;?> </body> </html>
Now every time a visitor comes to the index.php script, the server has to fire up a PHP process to run the script.
That PHP process also includes the config.php script, and so when it gets down to the line where it prints out the $title variable, instead of printing out that actual word, it prints My dynamic website.
The same thing happens with the $body variable printing out This is an example dynamic website.
Now lets say that you have 50 different PHP scripts, and you wanted each title to start with the same text. This is where having a dynamic website can come in handy because now you could do this:
My dynamic website – Page 1
<title><? echo $title;?> - Page 1</title>
My dynamic website – Page 2
<title><? echo $title;?> - Page 2</title>
My dynamic website – Page 3
<title><? echo $title;?> - Page 3</title>
Now if you all the sudden wanted to change the title across your 50 different PHP scripts, all you’d need to do is go back and just edit the config.php file and change the $title variable to use the new text.
Because each of your PHP pages is generated dynamically, the server will first check the config.php script every single time one of the pages is requested to see what the title should be.
You might be thinking at this point that this sounds great, why don’t I just always make my website dynamic?
Static websites save on resources
While there are seemingly a lot of benefits to having a dynamic website, it can also cause your website to load slowly if you’re trying to dynamically load up too much at once.
Also if you have one particular page getting requested again and again, and nothing is changing on the page itself, all that dynamic creation of that page over and over can lead to resource usage problems on the server.
Let’s say for instance my page1.php page from the dynamic website examples above is getting requested 1,000 times an hour because it got really popular all of the sudden.
Each time that page is requested, a PHP script has to dynamically fill in the variables for $title and $body, and if the average CPU time, or time that the server has to run that script for, is equal to 0.2 CPU seconds then that means that for 1,000 requests it would take about 200 CPU seconds of computing time to handle all the requests.
If that page keeps getting hit steadily with that level of traffic, it kinda seems like a waste of computing power to just keep creating the same page again and again. This is where a static website can easily take the advantage over a dynamic website.
With a static website, you simply have the raw HTML that creates your page in a file. A visitor’s web-browser will just directly request that page, and all the server has to due is hand it off as is.
Dynamic vs Static website
Depending on your needs, either a dynamic website or a static website could do just fine. The best of both worlds would be accomplished by using a cache or caching plugin for your dynamic site, so that frequently accessed dynamic pages can be automatically turned into static pages.
A great example of this flexibility would be using a WordPress cache plugin. These can easily turn your WordPress dynamic website, into a site that still handles dynamic requests, but at the same time saves on resources by still serving up static pages when dynamic ones aren’t needed.