Cache: a use case
If you are dealing with the internet you always see articles related to the cache and the benefits that your implementation can bring to a website, however some websites never really needed to use this feature. For some, the cache would make the website outdated. Well, here's a real use case.
Cache what is it?
By default, caching would be a way to access something that is already processed, without the need to reprocess again.
Imagine you need a page where you bring a list of information from the database. If you have to request this page, wait for processing and then have the result, a second user who needs that same information would not have to wait for a new processing, once the processing has already been delivered once. Therefore, this information is stored and when someone needs delivery from the cache.
In this perspective, the benefit would be the performance, but the disadvantage (obvious perhaps) of delivering outdated information.
After all, when was it really necessary?
An XPTO site receives about 2,000 daily visits, and is updated hourly with texts and images. This site consumes between 60 and 80GB of traffic per month.
In this model, the site started to present some characteristics: When the visits began to increase, the server became unavailable for a few moments, this because the amount of concurrent users became bigger and the resources of the server happened to be in maximum use, consequently the website also began to slow down.
At first, some optimizations were performed on querys and indexes in the database, but in the data there was not much to do, and there was not much result. In this way the website would not have more space to grow and if it wanted to grow, it would need to invest more in servers, here comes the need to use the cache, and validate if it really could make any difference.
What was done?
The site is updated every hour, so all visitors from 0 to 59 minutes of each hour consumes the same information. Therefore, in this period it would not be necessary to re-process anything. To do this, caching was enabled in a specific folder and once an hour the cache files were deleted so that a new processing could occur.
The simplest thing is that this cache was only enabled. That is, the resource already existed. But how so? The website uses a template component called twig which is nothing more than a template component that the Symphony framework uses. So the option already existed and that, out of stubbornness, was always disabled.
And the results?
The information below is the comparisons of Jul / 2017 and Sep / 2017. August was the transition period and so is not valid for comparison. Finally, the results were much more than satisfactory. The XPTO site consumed 63GB of traffic in July and 12GB in September. 51GB reduction. See the charts for each month and your average traffic consumption per minute:
And as if that were not enough, the results were positive in several respects. See the following information from the same period, in which the number of sessions for each day, according to Google Analytics information:
Still the percentages related to the months in comparison. Note that there were significant increases in the number of sessions, users and pageviews:
This last graph in "red" shows that more users have returned to the site.
What about technologies?
The site in both periods has not changed its structure: It uses PHP 7.1 and MySQL. It does not use a specific framework only by option, but uses some components that meet the need.
In composer repositories:
According to the graphs presented, it is possible to analyze a considerable reduction in traffic consumption, which was the main objective, and there was a growth of views and visitors, this increase may have been driven by the greater availability of the site due to the use of caching.
Therefore, for the website in question, the use of caching was much more expressive and positive, and met the goal assertively. However, the use or not of this resource must be analyzed and used according to the need of each website.