Author Topic: redis vs memcache  (Read 8696 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
redis vs memcache
« on: เมษายน 13, 2017, 12:11:16 PM »
http://stackoverflow.com/questions/10558465/memcached-vs-redis

I got the opportunity to use both memcached and redis together in the caching proxy that i have worked on , let me share you where exactly i have used what and reason behind same....

Redis >

1) Used for indexing the cache content , over the cluster . I have more than billion keys in spread over redis clusters , redis response times is quite less and stable .

2) Basically , its a key/value store , so where ever in you application you have something similar, one can use redis with bothering much.

3) Redis persistency, failover and backup (AOF ) will make your job easier .

Memcache >

1) yes , an optimized memory that can be used as cache . I used it for storing cache content getting accessed very frequently (with 50 hits/second)with size less than 1 MB .

2) I allocated only 2GB out of 16 GB for memcached that too when my single content size was >1MB .

3) As the content grows near the limits , occasionally i have observed higher response times in the stats(not the case with redis) .

If you ask for overall experience Redis is much green as it is easy to configure, much flexible with stable robust features.

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: redis vs memcache
« Reply #1 on: พฤษภาคม 13, 2017, 07:44:37 PM »
###Reference install redis cache on ubuntu
https://www.digitalocean.com/community/tutorials/how-to-configure-redis-caching-to-speed-up-wordpress-on-ubuntu-14-04

Step 1 — Install Redis
In order to use Redis with WordPress, two packages need to be installed: redis-server and php5-redis. The redis-server package provides Redis itself, while the php5-redis package provides a PHP extension for PHP applications like WordPress to communicate with Redis.

Install the softare:

sudo apt-get install redis-server php5-redis
Step 2 — Configure Redis as a Cache
Redis can operate both as a NoSQL database store as well as a cache. For this guide and use case, Redis will be configured as a cache. In order to do this, the following settings are required.

Edit the file /etc/redis/redis.conf and add the following lines at the bottom:

sudo nano /etc/redis/redis.conf
Add these lines at the end of the file:

maxmemory 256mb
maxmemory-policy allkeys-lru
When changes are complete, save and close the file.

Step 3 — Obtain Redis Cache Backend Script
This PHP script for WordPress was originally developed by Eric Mann. It is a Redis object cache backend for WordPress.

Download the object-cache.php script. This download is from DigitalOcean's asset server, but this is a third-party script. You should read the comments in the script to see how it works.

Download the PHP script:

wget https://assets.digitalocean.com/articles/wordpress_redis/object-cache.php
Move the file to the /wp-content directory of your WordPress installation:

sudo mv object-cache.php /var/www/html/wp-content/
Depending on your WordPress installation, your location may be different.

Step 4 — Enable Cache Settings in wp-config.php
Next, edit the wp-config.php file to add a cache key salt with the name of your site (or any string you would like).

nano /var/www/html/wp-config.php
Add this line at the end of the * Authentication Unique Keys and Salts. section:

define('WP_CACHE_KEY_SALT', 'example.com');
You can use your domain name or another string as the salt.

Note: For users hosting more than one WordPress site, each site can share the same Redis installation as long as it has its own unique cache key salt.

Also, add the following line after the WP_CACHE_KEY_SALT line to create a persistent cache with the Redis object cache plugin:

define('WP_CACHE', true);
All together, your file should look like this:

 * Authentication Unique Keys and Salts.

. . .

define('NONCE_SALT',       'put your unique phrase here');

define('WP_CACHE_KEY_SALT', 'example.com');
define('WP_CACHE', true);
Save and close the file.

Step 5 — Restart Redis and Apache
Finally, restart redis-service and apache2.

Restart Redis:

sudo service redis-server restart
Restart Apache:

sudo service apache2 restart
Restart php5-fpm if you are using it; this is not part of the basic installation on DigitalOcean:

sudo service php5-fpm restart
That's it! Your WordPress site is now using Redis caching. If you check your page load speeds and resource use, you should notice improvements.

Monitor Redis with redis-cli
To monitor Redis, use the redis-cli command like so:

redis-cli monitor
When you run this command, you will see the real-time output of Redis serving cached queries. If you don't see anything, visit your website and reload a page.

Below is example output from a WordPress site configured per this guide using Redis:

OK
1412273195.815838 "monitor"
1412273198.428472 "EXISTS" "example.comwp_:default:is_blog_installed"
1412273198.428650 "GET" "example.comwp_:default:is_blog_installed"
1412273198.432252 "EXISTS" "example.comwp_:options:notoptions"
1412273198.432443 "GET" "example.comwp_:options:notoptions"
1412273198.432626 "EXISTS" "example.comwp_:options:alloptions"
1412273198.432799 "GET" "example.comwp_:options:alloptions"
1412273198.433572 "EXISTS" "example.comwp_site-options:0:notoptions"
1412273198.433729 "EXISTS" "example.comwp_:options:notoptions"
1412273198.433876 "GET" "example.comwp_:options:notoptions"
1412273198.434018 "EXISTS" "example.comwp_:options:alloptions"
1412273198.434161 "GET" "example.comwp_:options:alloptions"
1412273198.434745 "EXISTS" "example.comwp_:options:notoptions"
1412273198.434921 "GET" "example.comwp_:options:notoptions"
1412273198.435058 "EXISTS" "example.comwp_:options:alloptions"
1412273198.435193 "GET" "example.comwp_:options:alloptions"
1412273198.435737 "EXISTS" "example.comwp_:options:notoptions"
1412273198.435885 "GET" "example.comwp_:options:notoptions"
1412273198.436022 "EXISTS" "example.comwp_:options:alloptions"
1412273198.436157 "GET" "example.comwp_:options:alloptions"
1412273198.438298 "EXISTS" "example.comwp_:options:notoptions"
1412273198.438418 "GET" "example.comwp_:options:notoptions"
1412273198.438598 "EXISTS" "example.comwp_:options:alloptions"
1412273198.438700 "GET" "example.comwp_:options:alloptions"
1412273198.439449 "EXISTS" "example.comwp_:options:notoptions"
1412273198.439560 "GET" "example.comwp_:options:notoptions"
1412273198.439746 "EXISTS" "example.comwp_:options:alloptions"
1412273198.439844 "GET" "example.comwp_:options:alloptions"
1412273198.440764 "EXISTS" "example.comwp_:options:notoptions"
1412273198.440868 "GET" "example.comwp_:options:notoptions"
1412273198.441035 "EXISTS" "example.comwp_:options:alloptions"
1412273198.441149 "GET" "example.comwp_:options:alloptions"
1412273198.441813 "EXISTS" "example.comwp_:options:notoptions"
1412273198.441913 "GET" "example.comwp_:options:notoptions"
1412273198.442023 "EXISTS" "example.comwp_:options:alloptions"
1412273198.442121 "GET" "example.comwp_:options:alloptions"
1412273198.442652 "EXISTS" "example.comwp_:options:notoptions"
1412273198.442773 "GET" "example.comwp_:options:notoptions"
1412273198.442874 "EXISTS" "example.comwp_:options:alloptions"
1412273198.442974 "GET" "example.comwp_:options:alloptions"
Press CTRL-C to stop the output.

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: redis vs memcache
« Reply #2 on: ตุลาคม 06, 2017, 12:06:42 PM »
https://troubleshootguru.wordpress.com/2014/11/19/how-to-install-redis-on-a-centos-6-5-centos-7-0-server-2/

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

How To Install Redis on Centos 7
# wget -r –no-parent -A ‘epel-release-*.rpm’ http://dl.fedoraproject.org/pub/epel/7/x86_64/e/
# rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-*.rpm


It will create two epel’s repo file inside /etc/yum.repos.d
These are –
1. epel.repo
2.epel-testing.repo

Install Redis with Yum
# yum install redis php-pecl-redis

Enable Redis service to start on boot
# systemctl enable redis-server.service

Disable Redis service from start on boot
# systemctl disable redis-server.service

Start/Stop/Restart Redis
# systemctl start redis-server.service
# systemctl stop redis-server.service
# systemctl restart redis-server.service

Check if Redis is Running
#systemctl is-active redis-server.service

How To Install Redis on Centos 6.5
# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
# yum install redis php-pecl-redis
# service redis start
# chkconfig redis on

Now verify its set to start at boot
# chkconfig –list redis
redis 0:off 1:off 2:on 3:on 4:on 5:on 6:off

To make sure redis is working, run command “redis-cli ping” from command line. If you get result “PONG”, that shows redis is working.
Now install redis PHP extension, using following command.
# pecl install redis

Now open php.ini ( /usr/local/lib/php.ini on cPanel servers ) file and add the following line to it
extension=redis.so

Now restart Apache to apply the changes to php.ini file.
Now that was pretty simple, but lets say you want to add a second instance to your server on a different port

Add a second instance of Redis
# cp redis.conf redis2.conf && cp /etc/init.d/redis /etc/init.d/redis2

Change
# vim /etc/init.d/redis2
pidfile=”/var/run/redis/redis.pid” to pidfile=”/var/run/redis/redis2.pid”
REDIS_CONFIG=”/etc/redis.conf” to REDIS_CONFIG=”/etc/redis2.conf”

Change
# vim /etc/redis2.conf
port 6379 to port 6380
unixsocket /tmp/redis.sock to unixsocket /tmp/redis2.sock

# service redis2 start

To install a web interface for redis
# cd /downloads/
# git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git
# cd phpRedisAdmin/includes
# cp config.sample.inc.php config.inc.php

Make sure the setting are correct
# vim config.inc.php

Lets add the RedisAdmin configuration file to Apache
# vim /etc/httpd/conf.d/redisadmin.conf

Now add the following
#
# Web Interface for RedisAdmin
#
<Directory “/downloads/phpRedisAdmin/”>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from <your ipaddress>
</Directory>
Alias /redisAdmin /downloads/phpRedisAdmin
Alias /redisadmin /downloads/phpRedisAdmin
View your RedisAdmin Section
http://example.tld/redisadmin

Create a Bash Script to make sure Redis is Running
vim /scripts/redis-check.sh
Add the following for the above installation

#!/bin/bash
PS=$(which ps)
GREP=$(which grep)
WHEN=$(date +”%Y-%m-%d-%H:%M:%S”)
if ! $PS aux | $GREP “redis.conf” | $GREP -v grep 2>&1 > /dev/null; then
/etc/init.d/redis restart
echo ‘Restarted Redis @’ $WHEN
fi
#Check Second instance
if ! $PS aux | $GREP “redis2.conf” | $GREP -v grep 2>&1 > /dev/null; then
/etc/init.d/redis2 restart
echo ‘Restarted Redis2 @’ $WHEN
fi
Make the Script executable

# chmod +x /scripts/redis-check.sh
Add your script to your cron to run every 3 minutes or so

# vim /var/spool/cron/root
*/3 * * * * /bin/bash /script/redis-check.sh >> /var/log/redis-check.log
« Last Edit: ตุลาคม 06, 2017, 12:10:53 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2145
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: redis vs memcache
« Reply #3 on: พฤศจิกายน 17, 2017, 08:33:00 PM »
การติดตั้ง redis-server แบบ compile + make install เองบน debian 8.x
sudo apt-get update
sudo apt-get install build-essential -y

wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz

wget http://download.redis.io/releases/redis-3.2.11.tar.gz
tar zxf redis-3.2.11.tar.gz

cd redis-stable
make
sudo apt-get install tcl8.5 -y
# optionally run "make test" to check everything is ok
sudo make install

ถ้าใช้งานกับพวก debian เขาจะมี script บน utils ให้รันครับ
#cd utils
#./install_server.sh

วิธีการ start ก็
#/etc/init.d/redis_6379 restart

##Check version โดย
redis-server --version

##Verify the Installation
Verify Redis is up by running redis-cli:
redis-cli

Your prompt will change to 127.0.0.1:6379>. Run the command ping, which should return a PONG:
127.0.0.1:6379>ping
PONG
Enter exit or press Ctrl-C to exit from the redis-cli prompt.

ส่วนพวก tunning basic ก็แนะนำให้ปรับใน sysctl.conf นะครับ
To improve Redis performance, make the following adjustment to the Linux system settings.

Set the Linux kernel overcommit memory setting to 1:
sudo sysctl vm.overcommit_memory=1
This immediately changes the overcommit memory setting. To make the change permanent, add vm.overcommit_memory = 1 to /etc/sysctl.conf:

/etc/sysctl.conf
vm.overcommit_memory = 1
#sysctl -p

####Reference
http://www.cvedetails.com/cve/CVE-2015-4335/  => critical bug on version lower than redis2.8.21 need upgrade.
https://fortiguard.com/encyclopedia/ips/41921
https://redis.io/download
http://blog.programster.org/debian-8-install-redis-server