CityHost.UA
Help and support

Caching services for sites: Memcached, OPCache, Redis

 6370
18.10.2021
article

What is data caching?

Caching sites is one of the ways to speed up the server by reducing the load on computing power.

The main difference between cache stores is that they are an order of magnitude faster than the main storage, data comes from them and is processed instantly.

How does it work? When a user opens a website page, he sends a request to the server, it is processed and a response is returned in the form of a compiled page.

Every minute, thousands of visitors perform actions on web resources, and all these processes are processed on the server, creating a constant load.

If the processor does not have time to quickly process all tasks, the speed of loading sites may decrease. This harms traffic (without waiting for a quick response, the user simply closes the tab) and worsens the position in the search engine (speed is one of the key metrics when ranking sites).

In order not to do the same manipulations every time, repeated actions are stored in temporary files in the intermediate storage. Access to it is easier and faster, it is not necessary to repeatedly create a sample of data located on the main drives.

For example, already viewed pictures are stored in the cache of the user's browser. When he visits the site for the second time, a request to the server is no longer created, but the image is simply extracted from the computer's memory.

Entering the domain name of your favorite site in the address bar and receiving a ready-made page, most users do not even suspect that part of this page (or maybe all) is already saved on their computer.

Interestingly, our brain also uses caching. Having met for the first time with an object (an apple, a house, a cat), he spends time and resources on studying it and forming a block of information (properties, purpose, dangerous / safe). The next time we see an object, we don't need to reprocess all available data about it. A ready-made compiled image is obtained from the cache, and we immediately restore all the previously received information. In this way, the brain saves a lot of time and effort by creating stable scenarios (scripts), and the main processor is unloaded for more complex and new tasks.

Data caching methods

There are different ways of caching, depending on what is being cached and how, where the results are stored.

Browser caching (client side)

  • pictures and files,
  • https protocol,
  • web pages (in full or their fragments).

Server caching

  • PHP code,
  • database requests and server responses.

Cache as a method of working with data has existed since the 1960s, so not all known methods are relevant. There are many obsolete ones - some have gone into oblivion, others are still used today.

Services for server caching

Today we will tell you about three modern and effective methods of server caching — OPCache, Memcached and Redis.

OPCache

OPCache is designed specifically for PHP. To understand how it works, let's first understand how PHP scripts work with client requests. The system searches for files, opens them, compiles the answer and gives it to the user. In the process, opcodes (compiled scripts) are created and immediately destroyed after execution.

OPCache allows you to store these scripts in memory, thanks to which the site's performance is several times faster. The very process of creating opcodes is quite resource-intensive, so saving scripts in the cache allows you to seriously unload the server.

Memcached

Memcached is software that runs on hash tables. The service is open source and supports all major programming languages. The technology allows information to be stored in a key-value format and is mainly used for session caching, but other data can also be stored in this way, such as small sections of the HTML code of a page.

Caching looks like this: first, the client's request goes to the Memcached repository and looks for a ready compilation there. If it is not there, then the request is sent to the server, a page is created, the result is sent to the user and immediately stored in the RAM using the Memcached service. In subsequent similar sessions, the result will be served immediately from the cache, the process will not affect the main disks. Thanks to this, the response time is reduced to milliseconds, repeatedly unloading server capacities. This contributes to fast loading of sites.

One of the features of Memcached is that it takes up very little space and consumes a minimum of resources. It has no data types, only strings with keys. Due to this, Memcached saves memory.

The service keeps data only in the RAM and does not save them after the system is restarted. If there is not enough memory, it starts overwriting new data over the old ones.

Memcached is multi-threaded and scales well vertically by increasing memory and number of cores. This method has limitations because it is implemented within the framework of one server.

Redis

Redis is a database management system that is similar to Memcached and also works on a key-value basis, both of which are suitable for sites with a lot of database queries. But it has a much wider functionality, so the service is used not only for caching.

What Redis can do:

  • Caches user sessions, intermediate data, fragments of HTML pages;
  • Smoothes the blocking of sessions on PHP so that the site loads evenly for all users, and not in turn;
  • It is used as a DBMS for small sites;
  • Works as a message broker;
  • Caches PHP code.

If we talk about the differences between Memcached and Redis further, Redis has data types; it not only stores information in RAM, but also periodically sends it to disk.

Redis is single-threaded and scales well horizontally by adding additional servers. Thanks to this, scaling can be carried out almost indefinitely - as long as there is money to buy new equipment.

Offers from Cityhost. Which of the methods does the client choose?

Cityhost offers all three services as part of hosting . Extensions are available in the hosting control panel, in the "Additional services" section.

Customers are allocated 16 MB of RAM for OPCache free of charge. Memcached will cost 10 kopecks per day of using the service, and Redis will cost 53 kopecks (the minimum amount of RAM that can be ordered is 32MB).

You can find all services and instructions for their installation here.

OPCache, Memcached and Redis are used for high-load projects with a large number of visitors to the site and frequent access to the database. Which one should I install on my hosting?

OPCache is a minimalistic and efficient service that is suitable for all projects that use PHP as a backend language. It can be installed along with the other caching apps mentioned.

Since Memcached and Redis are quite similar, it is often a dilemma to choose between them.

Usually, decisions are made in favor of Redis, because it is newer, has a large functionality and allows you to solve more tasks, it is scalable by expanding the server base. This option is used by large projects or with the prospect of rapid growth.

But Memcached solves the caching task no worse and has its advantages — it is economical in terms of resources, suitable for compact projects, does not take up much space and scales within the limits of one server.

A logical question arises: if these technologies speed up work so much, isn't it better to put all three extensions on your hosting - then the site will probably fly like a rocket? Not at all. The connections OPCache + Redis and OPCache + Memcached work effectively, because each component of the pair performs its tasks.

But Redis and Memcached are similar to each other and may conflict when enabled in parallel. You can, of course, fine-tune both services to perform different functions. But usually this is not done - one of them is quite enough. Which of the two to choose should be determined based on the needs of the site, testing the operation of the services.

So the answer is simple: Redis and Memcached can be combined with OPCache, but you should not include all three services.


Like the article? Tell your friends about it: