Hey, I'm a Web application developer! I'm wondering how to populate the database not only with lowest-cost but also high performance. Redis is a good idea.
Why should you add a Redis server into your system?
The basic model of using Redis is: Application -- Redis -- Database . Redis will store the authenticated data that matched from Main database (Oracle, MySQL ) with a specified expiration. The application will connect to Redis to get cached data first, if keys have expired, connect to database , store new keys into Redis. That will speed up your application most clearly.
How to setup a Redis server?
- Deploy a Linux server ( CentOS )
- Install required packages .
- Install and start Redis
- Check the redis-server service
- Tuning Redis database
To setup a linux server, I'll go to create a droplet (VPS) on DigitalOcean, the price is nice, only $5/month for a starter like me . I created a droplet using CentOS 6.9, location Singapore . Within 2 minutes, I got access to my redis server. You'll get a 100$ in Digital Ocean account credit, when you create your own server via my referral link : https://nguyenkhaclong.com/digitalocean100
# Install required Redis packages sudo yum install epel-release sudo yum update # Install Redis sudo yum install redis # Start Redis-server service redis start
How to monitor the services of Redis server?
To monitor how the redis-server is running, I'll install Redis-stat on Ruby-rail
#Installing Requirements yum install gcc-c++ patch readline readline-devel zlib zlib-devel -y yum libyaml-devel libffi-devel openssl-devel make yum bzip2 autoconf automake libtool bison iconv-devel sqlite-devel -y #Install RVM curl -sSL https://rvm.io/mpapis.asc | gpg --import - curl -L get.rvm.io | bash -s stable load the RVM environment cd /usr/local/rvm/archives/ tar -xvf rvm-1.29.10.tgz cd rvm-1.29.10 ./install source /etc/profile.d/rvm.sh rvm reload Verify Dependencies rvm requirements run #Install Ruby rvm install 2.5 #Install redis-stat gem install redis-stat #Monitor Redis Server daemon redis-stat 127.0.0.1:6379 --auth=strong-password --verbose --server=8081 5 --daemon
Launch the monitor website via url : http://your-digitalocean-droplet-IP:8081
Tuning your the services
Base on your server hardware performance ( RAM , CPU , Disks and Networks connection ), try to optimize your Redis configuration as well as possible. Here's the solution that could help your server running more stable.
Tuning kernel for networking
#Tuning kernel for networking net.core.somaxconn=65535 # up the number of connections per port vm.overcommit_memory = 1 fs.file-max=100000 #disable hugepage echo never > /sys/kernel/mm/transparent_hugepage/enabled #Optimize Redis through configuration file ( /etc/redis/6379.conf ) save 3600 10000 save 900 1000 save 300 100 #disable saving redis to disk ( disable AOF, enable RDB ) appendonly no #limit the number of clients connection maxclients 10000 #Setup tcp-backlog tcp_max_syn_backlog tcp-backlog 65536 #memory usage ( in bytes ) maxmemory = ½ RAMSIZE #memory release policy maxmemory-policy volatile-random
Backup and Restore Redis database
To keep the service running 24/7, I'll make a backup script ( call redis_backup.sh ), add it to the crontab.
#!/bin/sh RedisDIR="/var/lib/redis" redis-cli << EOF auth 1322018 #password of Redis bgsave EOF cp $RedisDIR/dump.rdb /root/redis/backup/dump.$(date +%Y%m%d%H%M).rdb
Run redis after server rebooting
sh -c "ulimit -n 100032" && redis-server /etc/redis/6379.conf #add above config to your /etc/rc.local file or just run the following command sudo chkconfig --level 2345 redis on
Some basic & useful commands to monitor Redis services
# check total keys $redis-cli $127.0.0.1:6379> auth strong-password $127.0.0.1:6379> DBSIZE #monitor realtime $redis-cli $127.0.0.1:6379> auth strong-password $127.0.0.1:6379> monitor #check server info ( memory , CPU, keyspace ) $redis-cli $127.0.0.1:6379> auth strong-password $127.0.0.1:6379> info #find some keys with pattern $ redis-cli -p 6379 -a "strong-password" --scan --pattern '*keyword*' >/tmp/scan-keyword.txt
Redis's easy to setup , maintain. Redis is faster than SQL by using hashing mechanism. It can withstand failures and provide uninterrupted service. It could deal the popular programming languagesHuy Tran - IT Master