LoginSignup
1

More than 5 years have passed since last update.

初めての Redis の巻

Last updated at Posted at 2018-07-03

目的

ナウいアプリケーションに触れ、アプリ屋さんと小話する

目標

MORI さんから、インメモリー処理ってどんな?と聞かれて、困らない状態になる
RDS で苦手なランキングを作る

成果イメージ

インメモリー処理に必要な環境構築
redis のソート機能を試すアプリケーションの構築

利用の仕方

[セールス向け]PoC の環境構築や顧客と共育

サーバエンジニアかプログラマか、顧客が不足している箇所を補足しましょう
特に、GCP など、サービス固有の仕様がからむサーバエンジニアのパートを補うと良いでしょう

[プログラマ向け]インメモリー処理の実態を学習できます

php のライブラリを使って、メモリーでの読み書き、ディスクへの書き込みの実態を学べます。
sudo cat redis.conf
下のソースを見て、redis へのデータ追加と引き出し方を確認しましょう
https://raw.githubusercontent.com/HaHatake/tesstRedis/master/rankingtest.php

[サーバエンジニア向け]GCP が学習できます

下の作業をこなすことで、GCP のIaaS 活用方法を実践できます。
また、サーバの環境設定(OS とミドル[php や redis]) 、簡単なスクリプト(ランキングサイト構築)ができます。
特に、tips に書いた redis の設定や特性はおさえておきましょう。
http://www.denet.ad.jp/technology/2017/11/redis-centos7.html

作業

GCP で CentOS7 を作成
ssh でログイン
WS000010.JPG

yum の更新
sudo yum -y update

redis のインストール
sudo yum -y install redis --enablerepo=epel
※epel がレポジトリにない場合は、個別に追加して下さい。GCP上のデフォルトCentOS7 は、2018年7月3日現在は、登録済みの様子

php のインストール
sudo yum -y install php

php-redis のインストール
sudo yum -y install php-pecl-redis --enablerepo=epel

php のコンフィグ設定
まずは、場所を確認
php -r "echo phpinfo();" | grep "php.ini"
私の場合、

/etc/php.ini

バックアップのフォルダとini コピーファイル作成
sudo mkdir /usr/backup
sudo cp -p /etc/php.ini /usr/backup/php.ini.org

php にphp-redis を使う設定を行う
下のサイトを参照
https://github.com/phpredis/phpredis/blob/master/README.markdown
sudo vi /etc/php.ini
最終行へ下の内容を追加
extension=redis.so
vi の使い方が分からない場合は、個別にググって下さい。
 一連の流れで言うと下の通り。
 a > paste > esc > :wq

redis の確認
php -m | grep redis

redis
上のように出力されればok

redis へインプットとソートしてアウトプットさせる。
下のサイトを参照
https://sys-guard.com/post-16250/

cd /var/www/html/
sudo curl https://raw.githubusercontent.com/HaHatake/tesstRedis/master/rankingtest.php

php -f rankingtest.php

確認すること

入力は、ランダムなスコアで行っている。
出力は、redis によって、ソートされたスコアになっている。

ログ(本編とは無関係のものもあり)

redis のインストール
下のサイトを参照
https://redis.io/download

cd /usr/local/src/
sudo wget http://download.redis.io/releases/redis-4.0.10.tar.gz
sudo tar xzf redis-4.0.10.tar.gz
cd redis-4.0.10
sudo apt install make

コンフィグを確認
sudo cat redis.conf

150 ライン付近に非常に特徴的なオプションが。。。
デフォルト設定は下記の通り
・少なくとも 一つのキーが変更された場合は 900秒後にスナップショット取得
・少なくとも 十個(回)のキーが変更された場合は 300秒後にスナップショット取得
・少なくとも 一万個(回)のキーが変更された場合は 60秒後にスナップショット取得

save 900 1
save 300 10
save 60 10000

509 ライン付近にも、、、
データ更新の際は常にディスクに保存するか
ディスク保存すると当然遅くなる

appendonly no

ソースからコンパイルするため、コンパイル環境を整える

sudo apt-get update
sudo apt-get -y install gcc
sudo service apparmor stop

redis のソースをゲットして、コンパイル

sudo wget http://download.redis.io/releases/redis-4.0.10.tar.gz
cd /redis-4.0.10
make test

redis の実行
src/redis-server

redis のアスキーアートが出たら完了。
ctl + Ckey で中止。

トラブルシュート

redis のmake 時にエラー発生

cd src && make install
make[1]: Entering directory '/usr/local/src/redis-4.0.10/src'
CC adlist.o
/bin/sh: 1: cc: not found
Makefile:228: recipe for target 'adlist.o' failed
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory '/usr/local/src/redis-4.0.10/src'
Makefile:9: recipe for target 'install' failed
make: *** [install] Error 2

sudo apt-get update
sudo apt-get -y install gcc
sudo service apparmor stop
sudo wget http://download.redis.io/releases/redis-4.0.10.tar.gz
cd /redis-4.0.10
make test

cd src && make test
make[1]: Entering directory '/home/h1d34h4t/redis-4.0.10/src'
You need tcl 8.5 or newer in order to run the Redis test
Makefile:242: recipe for target 'test' failed
make[1]: *** [test] Error 1
make[1]: Leaving directory '/home/h1d34h4t/redis-4.0.10/src'
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 2

sudo apt-get -y install tcl
make test
(10分ほど待つ)

\o/ All tests passed without errors!
Cleanup: may take some time... OK

make
src/redis-server

1301:C 03 Jul 00:28:31.985 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1301:C 03 Jul 00:28:31.985 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=1301, just started
1301:C 03 Jul 00:28:31.985 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1301:M 03 Jul 00:28:31.986 * Increased maximum number of open files to 10032 (it was originally set to 1024).
.

.-``_ ''-._

.-`. `. ''-._ Redis 4.0.10 (00000000/0) 64 bit
.-.-.\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.
-.|'_.-'| Port: 6379
|
-.
._ / _.-' | PID: 1301
-._ -._-./ .-' _.-'

|`-.
-._-..-' .-'.-'|

| -._-._ .-'.-' | http://redis.io

-._-._`-.
.-'.-' _.-'

|`-.
-._-..-' .-'.-'|

| -._-._ .-'.-' |

-._-._`-.
.-'.-' _.-'

`-.
-.__.-' _.-'
-._ .-'

`-.
_.-'

1301:M 03 Jul 00:28:31.987 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1301:M 03 Jul 00:28:31.987 # Server initialized
1301:M 03 Jul 00:28:31.987 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1301:M 03 Jul 00:28:31.987 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1301:M 03 Jul 00:28:31.988 * DB loaded from disk: 0.000 seconds
1301:M 03 Jul 00:28:31.988 * Ready to accept connections

いくつか、Warning があるが、立ち上がった様子。
Warning の内容は、下を参照
http://www.denet.ad.jp/technology/2017/11/redis-centos7.html

・somaxconn が、511 より低い値だけど大丈夫?問題
 OSレベルの接続キューの最大値として、TCPソケットで待ち受けるサービス全般に影響する
 redis はデフォルトで、511 で待ち受けようとするため、somaxconn が128 と少ない値になってるよ?
 と、心配してくれている。
 今回は、テストだからいいっしょ。無視。

・overcommit_memory は、セット0 より1 の方がいいよ?問題
 下を参照
 https://qiita.com/h-imaoka/items/9c0dc9e07bf39175b7ea
 セットを"1" にするとオーバーコミットを制御しない様子。
 他のApp も同居させたいから、今回は、Warning 無視する。

・Transparent Huge Pages (THP) support enabled になってるけど、disable の方が早いよ?問題
 数GB のサーバだと、THP ON の方が遅い様子。
 うるせぇ!今回のサーバは、1G なんだよ!!無視する。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1