Playing Around with FreeBSD Memcached - How to implements an in memory data caching service

In 2003, while working at LiveJournal Brad Fitzpatrick succeeded in creating Memcached. The initial idea was to create Memcached, because at that time Fitzpatrick was struggling with slow database requests. Fitzpatrick needed a way to improve the performance of the sites he worked on. He successfully developed Memcached as a caching layer placed between databases and web applications, so that frequently accessed data is stored in memory and retrieved quickly.

Initially Memcached was written in Perl and then rewritten in C. In Memcached there is a Backend which has one job, namely storing key values in memory. This application is very simple, large companies such as Netflix, Wikipedia and Facebook have used Memcached to improve the performance of their websites.

Memcached is a free, open source, high-performance distributed memory object caching system. In dynamic web applications, using Memcached can reduce database load and increase site performance.

The Memcached design is able to store data in server memory, thereby speeding up website access times. This makes it an ideal choice for applications that need to store and retrieve large amounts of data quickly. So it's not wrong that many large companies use Memcached as a fast and efficient caching solution that can reduce the amount of time needed for a website or application to retrieve data from the database.

memcached can take memory from parts of the system that have a large amount of memory and can access areas that have less memory than necessary. Memcached can also make better use of memory capacity. Take a look at the diagram below which implements two Memcached scenarios.

1. Each node is completely independent (top).
2. Each node can use memory from other nodes (bottom).



The Memcached tutorial in this article explains how to install and use Memcached on a FreeBSD server. This tutorial is very good for those of you who are new to FreeBSD and want to improve website performance.


1. Installing Memcached

Memcached is an in-memory key-value data store that can be run on MacOS, BSD and Linux operating systems. In this section, you will install memcached onto FreeBSD via the PKG package manager or via the Ports system.

To install memcached on FreeBSD, first run the command below to update the FreeBSD PKG package index and continue by installing the Memcached dependencies.

Update packet PKG
proot@ns3:~ # pkg update -f
proot@ns3:~ # pkg upgrade -f
Dependencies
root@ns3:~ # root@ns3:~ # pkg install cyrus-sasl libevent rfc libxml2 libmemcached php82-pecl-memcached msgpack-c php82-pecl-msgpack
Run the port command below and install the memcached package on FreeBSD with make install clean.

/usr/ports/databases/memcached
root@ns3:~ # cd /usr/ports/databases/memcached
root@ns3:/usr/ports/databases/memcached # make install clean
After the installation process is complete, you run the command below to activate the Memcached service, so that it can run automatically, namely by adding several Memcached scripts.

/etc/rc.conf
root@ns3:/usr/ports/databases/memcached # ee /etc/rc.conf
memcached_enable="YES"
memcached_user="nobody"
memcached_group="nobody"
memcached_flags="-l 192.168.5.2 -d -U 0 -m 512 -c 2048"
Memcached flags options used:
-l = 192.168.5.80: Run memcached service on IP address 192.168.5.2
-U 0 = Disable memcached on UDP port.
-d = Start memcached in the background.
-c = 2048: Set up max connections to 2048.
-m = Set up max memory to 512 MB.

You can check the Memcached service that you have created above.

/etc/rc.conf
root@ns3:/usr/ports/databases/memcached # sysrc -a | grep memcached
memcached_enable: YES
memcached_flags: -l 192.168.5.2 -d -U 0 -m 512 -c 2048
memcached_group: nobody
memcached_user: nobody
Once you have configured memcached, run Memcached with the service command.

Run memcached
root@ns3:~ # service memcached restart
root@ns3:~ # service memcached status
On FreeBSD by default Memcached runs on port 1121, run the sockstat command to verify memcached port 1121.

Verify memcached port 1121
root@ns3:~ # sockstat -4 | grep 11211
nobody   memcached  42237 16 tcp4   192.168.5.2:11211     *:*


2. Testing Memcached Connection

Now your FreeBSD server has memcached installed and is running normally. Next, verify memcached via the memcached tool and netcat utility. The memcached tool is a Perl-based application for checking statistics and managing memcached contained in the memcached package. You don't need to install the utility, because it is automatically installed when you install Memcached. Follow these commands to test and verify memcached using the memcached tool.

Test and verify memcached
root@ns3:~ # memcached-tool 192.168.5.2 settings
You can also check memcached statistics, such as current read and write statistics, connection statistics, and also authentication statistics.

Check memcached statistics
root@ns3:~ # memcached-tool 192.168.5.2 stats
Apart from using the command above, you can also verify memcached statistics via the netcat or nc command as in the following example.

Check memcached statistics
root@ns3:~ # echo stats | nc 192.168.5.2 11211


3. Testing With PHP pecl-memcache

After we checked the Memcached connection there were no problems or Memcached could run normally on FeeeBSD. You can also perform Memcached checks with PHP. This check aims to ensure applications running with PHP can connect to Memcached, such as WordPress, Joomla, Drupal and other websites.

In the PHP language, there are two independent PHP PECL extensions for working with Memcached servers, namely.

1. Memcache, a very light extension with minimal features, is known to be very fast and productive in operation.

2. Memcached, this extension has complete features, so it can provide full capabilities for working with Memcached servers. This extension has a slightly different syntax in some parameters.

In this article, we will use the PECL-Memcached extension syntax. Before starting the check, first install pecl-memcache by following the commands below.

/usr/ports/databases/pecl-memcache
root@ns3:~ # cd /usr/ports/databases/pecl-memcache
root@ns3:/usr/ports/databases/pecl-memcache # make install clean
With a command in the console you can check whether the PECL-Memcached extension is installed in PHP.

Check PECL-Memcached
root@ns3:~ # php -i | grep memcached
/usr/local/etc/php/ext-30-memcached.ini,
memcached
memcached support => enabled
libmemcached-awesome version => 1.1.4
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.default_binary_protocol => Off => Off
memcached.default_connect_timeout => 0 => 0
memcached.default_consistent_hash => Off => Off
memcached.serializer => php => php
memcached.sess_binary_protocol => On => On
memcached.sess_connect_timeout => 0 => 0
memcached.sess_consistent_hash => On => On
memcached.sess_consistent_hash_type => ketama => ketama
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => not set => not set
memcached.sess_lock_retries => 5 => 5
memcached.sess_lock_wait => not set => not set
memcached.sess_lock_wait_max => 150 => 150
memcached.sess_lock_wait_min => 150 => 150
memcached.sess_locking => On => On
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_persistent => Off => Off
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => Off => Off
memcached.sess_remove_failed_servers => Off => Off
memcached.sess_sasl_password => no value => no value
memcached.sess_sasl_username => no value => no value
memcached.sess_server_failure_limit => 0 => 0
memcached.store_retry_count => 0 => 0
Registered save handlers => files user memcache memcached
Now we will check the operation of the Memcached server with a PHP script. Create a PHP file and write the script, then you run it.

/usr/local/www/apache24/data
root@ns3:~ # cd /usr/local/www/apache24/data
root@ns3:/usr/local/www/apache24/data # ee test.php
<?php
$memcache = new Memcache;
$memcache->connect('192.168.5.2', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."
\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)
\n";

$get_result = $memcache->get('key');
echo "Data from the cache:
\n";

var_dump($get_result);
?>
Run test.php file
root@ns3:/usr/local/www/apache24/data # php test.php
Another example, write the following script.

/usr/local/www/apache24/data
root@ns3:/usr/local/www/apache24/data # ee test2.php
<?php
	
	$memcache = new Memcache;
	$memcache->connect('192.168.5.2', 11211) or die ("Can't connect");
	
	$time_start = microtime(true);
	
	for($i=0;$i<1000000;$i++) {
		$memcache->set('key' . $i, 'MyTestString', false, 3600) or die ("Storing error");
	}
	
	echo "Wrire: " . ( microtime(true) - $time_start ) . " seconds\n";
	
	$time_start = microtime(true);
	
	for($i=0;$i<1000000;$i++) {
		$get_result = $memcache->get('key' . $i);
	}
	
	echo "Read: " . ( microtime(true) - $time_start ) . " seconds\n";
	
?>

Memcached commands are very simple yet powerful. Its simple design means it runs quickly, provides ease of development, and solves many of the problems facing large data caches. Memcached APIs are also available for most popular languages such as Go and Python.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post