Application Caching

March 7, 2012 Pavel Novitsky Tips&Tricks

                                                                   «We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil»

Donald Knuth

In previous articles I have dealt with the implementation of caching on the side of the web server. Caching allows you to save frequently used information on disk or in the memory and save time when it is retrieved. Now we’ll examine how the application caching is used to reduce loading and decrease the response time of Web server.

What is the application caching?

Caching stores previously received information in the memory for subsequent use by one or more users. Modern web applications become more complex, resource-intensive and, therefore, slower. Application caching allows you to reduce loading and to save extra seconds of web server response generation.

Usually a Web application can be divided into several layers. Generally we can distinguish a client side (a set of HTML pages and related technologies) and a server component (utilities responsible for the data processing and the web server response generation). The server component of most applications is made of a few more layers. Typically these layers include UI or View block (application response generation), the business logic block (rules responsible for correlation of application parts) and data block (storage and retrieval of data from one or more data sources).

If we take for example an ordinary online store, the application layers can be divided as follows:

• User interface block: shopping cart, menu, static pages
• Business logic block: processing order, taxes calculations, delivery parts
• Data block: directory, order registries, etc.

Obviously, online store is a highly dynamic application, depending on individual choice and customization of each specific user account, the availability of products in stock and the order final country. The data exchange between all application layers and generation of blocks work results upon processing each request increase the site response time. Not every application part must be generated every time all over again. For example, each page contains header and footer, which can be divided into static components and store applications in separate storage. When it comes to a webstore, you can see that such things as delivery rules, taxes and currency exchange rates are changed relatively rare and can be calculated and stored once. Even the state of the user shopping cart can be placed in cache, because there is no direct need to constantly update the shopping cart structure until the user makes order changes.

Saving pre-generated data in memory allows you to eliminate many complicated requests for the system (database reading requests, many arithmetic operations, text conversion, etc.) and get a significant increase in terms of page generation.

Caching-related questions

Most of the caching occurs on the server side. You should consider several factors, organizing application caching.

Application scope. What application parts can use cache? Some site components, for example, shopping carts are different for each visitor. Other components, such as the delivery rules, are common for all users and can be saved generally. Processing rarely changed or static data in the application, running on a distributed server environments, the developer should create a caching mechanism that can share data among several Web servers. It can be implemented either with centralized caching storage or through some mechanism of synchronizing data in server storage.

Server resources. Data caching reduces the request processing time and the number of disk read-write operations. But do not forget that the speed gain takes place due to the aggressive use of free memory on the Web server. Not only the process of storing data in memory, but also the way to clear the cache of unused or outdated information must be implemented in the application. Also, you should be aware that your caching mechanism does not consume all available memory on the server.

Cache expiration. Like the client and server caching, application caching also needs the mechanism of checking the stored data relevance. When information is placed in cache, the application can add the expiration date for each element to the stored data (like the HTTP-server). Each block can be considered expired and be deleted after a certain period.

Let’s come back to webstore example: shopping cart cache should be updated upon adding / removing products therefrom and changing the number of ordered items. Currency exchange rates can be updated every few hours, and VAT (Value Added Tax) may not vary from several months to several years.

Application Caching and Server Side Applications

In modern applications caching is often implemented in software and depends on used software environment. In most server platforms caching tools are already built-in: Alternative PHP Cache (APC) in PHP, Java Caching System (JCS) in Java and Cache class in ASP.NET.

The danger of excessive caching

Despite the fact that caching can significantly speed up the web application, there is a risk to load the web server too much and encounter unplanned downtimes and unexpected breakdowns. Developers need to plan the application accordingly and carry out load testing to determinate the performance bottlenecks before the start of the cache system engineering. You should make it a rule to begin cache system development only after all inefficient algorithms and recurrent or redundant database requests are eliminated from the application.

We have reviewed web server caching, browser caching, and application caching, but it’s not the end. Expect to read about databases and related ways of caching soon.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *