Memcached

Introducing Memcached for High-Performance Web Applications on Linespace

In the fast-paced digital world, speed is king. Our customers expect their websites and applications to load in milliseconds, and even a slight delay can lead to a loss in engagement and revenue. At Linespace, we’re committed to helping developers and businesses optimize their web applications for high performance. One key technology we recommend for achieving this is Memcached, the free & open source, high-performance, distributed memory object caching system,

What is Memcached?

Memcached is an open-source, high-performance, in-memory caching system designed to reduce database load and accelerate dynamic web applications. By storing frequently accessed data in memory, Memcached minimizes the need for repeated database queries, significantly improving response times.

Key Features of Memcached

  1. Distributed Architecture: Scales horizontally by adding more nodes.
  2. In-Memory Storage: Uses RAM for lightning-fast data retrieval.
  3. Simple Protocol: Employs a lightweight, text-based protocol.
  4. Language Agnostic: Supports multiple programming languages via client libraries.
  5. Low Latency: Designed for sub-millisecond response times.

How Memcached Works

At its core, Memcached stores key-value pairs in memory. When a web application needs a piece of data, it checks Memcached first:

  1. Cache Hit: If the data exists in Memcached, it’s retrieved instantly.
  2. Cache Miss: If the data isn’t found, the application fetches it from the database and stores it in Memcached for future requests.

This approach dramatically reduces database queries, leading to faster response times and improved scalability.

Why Use Memcached for your Application on Linespace?

Here are some compelling reasons to integrate Memcached into your web application:

  1. Enhanced Performance: Reduce latency and speed up dynamic applications.
  2. Scalability: Handle higher traffic without overloading your database.
  3. Cost Efficiency: Reduce database server costs by offloading read operations.
  4. Flexibility: Cache any type of data, from database query results to session information and API responses.

Implementing Memcached in Your Application from Your Control Panel

To use Memcached with PHP, please enable memcached extension from the control panel at Advanced -> PHP Settings -> Edit php.ini -> memcached On.

To have Memcached enabled your plan needs to have Memcached instances available. In case you need to add one or more, you can purchase it as an upgrade from the Add/Upgrade service(s) section in the hosting control panel.

Each instance goes with 16 MB of memory which is why the Memcached instances go with Memcached memory.

Important: many applications require host and port to link them with Memcached. On our system the Memcached works on a Socket instead of Port.

In this case you need to use:

Memcached hostname(IP): unix:///home/sys/memcached.sock
Memcached Port: 0

Example: $memcached->addServer('unix:///home/sys/memcached.sock', 0);

You can speed up your Joomla site by using memcached.

To enable memcached on Joomla, you need to modify a few settings in your Joomla configuration.php file first.

You can find this file in your Joomla site’s root folder. Using your favorite FTP software, edit this file and find the following lines:

public $caching = '0';
public $cache_handler = 'file';

Change these lines to the following:

public $caching = '2';
public $cache_handler = 'memcache';

Then, immediately after the two lines you edited, add these two lines:

public $memcache_server_host = ‘/home/sys/memcached.sock’;
public $memcache_server_port = ‘0’;

After you are done making all the changes, save the configuration file.

Configure Memcached in Joomla Settings

Now, we will configure memcached on our Joomla-based website.

Login to your Joomla site’s admin panel and navigate to System -> Global Configuration.

Click on the server tab at the top and scroll down to Cache Settings. From the drop-down menu select Persistent(Memcached).

Note: Make sure to enter the correct Memcached server host and port for your server.

Memcache(d) Server Host: /home/sys/memcached.sock
Memcache(d) Server Port: 0

Save the settings.

You’ve successfully enabled memcached on your Joomla website.

For your convenience we would like to provide you with a sample working Memcached test script:

<?php
$memcached = new Memcached();
$memcached->addServer('unix:///home/sys/memcached.sock', 0);
$name = 'testkey';
$ttl = 10;
$data = sha1(time());
$memcached->set($name, $data, $ttl);
echo date('His') . ': key "' . $name . '" set to "' . $data . '" with ttl ' . $ttl . PHP_EOL;
for ($i = 0; $i < ($ttl + 5); $i ++) {
$res = $memcached->get($name);
echo date('His') . ': key "' . $name . '" data is "' . $res . '" and that is ' . ($res == $data ? 'a match' : 'not a match') . PHP_EOL;
sleep(1);
}
?>

Please note that instead of $memcached->addServer('127.0.0.1", 11211); we use socket: $memcached->addServer('/home/sys/memcached.sock', 0);

Memcached is an invaluable asset for building high-performance web applications. By offloading database read operations and storing frequently accessed data in memory, it ensures faster response times and improved scalability.

One response to “Introducing Memcached for High-Performance Web Applications on Linespace”

  1. Russia including even a thousand shreds Do the first to us Nor Avatar

    Russia including even a thousand shreds Do the first to us Nor

Leave a Reply

Your email address will not be published. Required fields are marked *