November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Redis is a shallow introductory tutorial

Redis is a shallow introductory tutorial

1. Introduction to Redis

Redis is an open source using ANSI C language-based, memory-based Key-Value database.

It supports storing more value types, including string, list, set, zset (sorted collection), and hash (hash type).

Redis supports master-slave synchronization, and data can be synchronized from the master server to any number of slave servers. Since the publish / subscribe mechanism is fully implemented so that the tree can be synchronized from anywhere in the database, a channel can be subscribed and a complete message is received by the master server Release record.

Compared to memcached, Rdeis has the following advantages:

1. redis native support for more data types.
2. redis has a persistence mechanism, you can regularly store the data in memory to the hard disk.
3. redis support master-slave mode of data backup.

4. performance. Redis author’s argument is to average the performance of a single core, in a single data is not the case of Redis better.

Why do you say that the reason is that Redis is a single-threaded operation. Because it is single-threaded operation, so and Memcached multi-threaded compared to the overall performance will be low. Because it is a single-threaded operation, IO is serialized, network IO and memory IO, so when a single piece of data is too large, due to the need to wait for a command all IO to complete the subsequent order, so the performance will be affected.

2. Install Redis

2. Install Redis

2.1 Redis installation is very simple, with yum or apt-get can be installed directly

# yum install epel-release
# yum install redis

2.2 start / stop Redis

# redis-server /etc/redis.conf

# systemctl start redis
# systemctl stop redis

3. use Redis

3.1 Redis-cli Command Line Operation KV

Connect to Redis

# redis-cli -p port
# redis-cli
Ping

127.0.0.1:6379> ping
PONG
Set key

127.0.0.1:6379> set testkey “hello”
OK
Query key

127.0.0.1:6379> get testkey
“hello”
Delete key

127.0.0.1:6379> del testkey
(integer) 1
Set the validity period

127.0.0.1:6379> setex test 10 111
OK
Use EXPIRE key s to set the expiration time milliseconds with PEXPIRE

127.0.0.1:6379> EXPIRE test11 300
(integer) 1
Use the TTL key to view the expiration time in milliseconds with PTTL

127.0.0.1:6379> TTL test11
(integer) 288
Cancel expiration time with PERSIST key

127.0.0.1:6379> PERSIST test11
(integer) 1

3.2 Advanced features

3.2.1 self-increment, self-decreasing, INCR, DECR, INCRBY, SORT

127.0.0.1:6379> set counter 100
OK
127.0.0.1:6379> incr counter
(integer) 101
127.0.0.1:6379> incr counter
(integer) 102
127.0.0.1:6379> decr counter
(integer) 101

3.2.2 Transactions

127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set test11 111111
QUEUED
127.0.0.1:6379> set test12 121212
QUEUED
127.0.0.1:6379> incr counter
QUEUED
127.0.0.1:6379> EXEC
1) OK
2) OK
3) (integer) 102

3.2.3 HyperLogLogs

Redis adds the HyperLogLog algorithm to version 2.8.9.

3.2.4 publish / subscribe function

Redis publish a subscription (pub / sub) is a message communication mode: the sender sends a message and the subscriber receives the message. Redis clients can subscribe to any number of channels.

Subscribe to the channel redisChat on a client

127.0.0.1:6379> SUBSCRIBE redisChat
Reading messages… (press Ctrl-C to quit)
1) “subscribe”
2) “redisChat”
3) (integer) 1
On another client, send a message to the channel redisChat, the subscriber will be able to receive the message

Release:

127.0.0.1:6379> PUBLISH redisChat “redis haha”
(integer) 1
Subscriptions:

127.0.0.1:6379> SUBSCRIBE redisChat
Reading messages… (press Ctrl-C to quit)
1) “subscribe”
2) “redisChat”
3) (integer) 1
1) “message”
2) “redisChat”
3) “redis haha”
3. 3 View the Redis status

Info output a lot of information, you can specify the output part

1
127.0.0.1:6379> info stats

1
127.0.0.1:6379> info memory

Used_memory: The total amount of memory allocated by the Redis allocator, in bytes.

Used_memory_rss: Returns the amount of memory that Redis has allocated from the operating system (commonly known as resident set size). This value and top, ps and other command output consistent.

Rss> used, and the difference between the two values ??is large, that there is (internal or external) memory fragmentation.

The ratio of memory fragmentation can be seen by the value of mem_fragmentation_ratio.

Used> rss, that part of the memory of Redis is replaced by the operating system swap space, and in this case, the operation may produce significant delay.

Used_memory_peak: peak, set the maximum memory to be greater than the peak

3. 4 other orders

View the number of records

127.0.0.1:6379> dbsize

View all KEYs

127.0.0.1:6379> KEYS *

List all client connections

127.0.0.1:6379> CLIENT LIST

Close the ip: port client

127.0.0.1:6379> CLIENT KILL 127.0.0.1:11902
Clear all keys for all databases

127.0.0.1:6379> FLUSHALL
Empty all keys in the current database

127.0.0.1:6379> FLUSHDB
Returns the last time the data was successfully saved to disk, expressed in UNIX timestamp format

127.0.0.1:6379> LASTSAVE
Returns the current server time, expressed in UNIX timestamp format

127.0.0.1:6379> TIME
Connect to other databases (default database is 0 )

127.0.0.1:6379> SELECT 1
OK
Moves the key of the current database to the specified database

127.0.0.1:6379> MOVE test2 1
(integer) 1

4. Set the file

4.1 /etc/redis.conf

daemonize no
timeout 0
maxclients 10000

maxmemory
maxmemory-policy volatile-lru
maxmemory-samples 3
hash-max-ziplist-entries 512 Map
hash-max-ziplist-value 64 Map
list-max-ziplist-entries 512?list-max-ziplist-value 64
slowlog-log-slower-than 10000 slow log ?microseconds?1000000?
slowlog-max-len 128 slow log

4.2 View the maximum number of connections

127.0.0.1:6379> config get maxclients
1) “maxclients”
2) “10000”
Adjust the parameters during operation

127.0.0.1:6379> config set maxclients 10001
4.3 View slow log

127.0.0.1:6379> SLOWLOG get
127.0.0.1:6379> SLOWLOG get 10
1) 1) (integer) 0
2) (integer) 1448413479
3) (integer) 124211
4) 1) “FLUSHALL”
Confirm the number of slow log settings

127.0.0.1:6379> SLOWLOG len
Clear the slow log

127.0.0.1:6379> SLOWLOG reset
5. Data persistence

5.1 Snapshot ( Snapshot )

5.1.1 Setting up a snapshot in the settings file

save
rdbcompression yes/no
dbfilename dump.rdb ?Append Only File?
dir /var/lib/redis/
5.1.2 Manually create snapshots

Execute the save or bgsave command at the command line

127.0.0.1:6379> SAVE
OK
5.2 Log Backup ( Append Only File )

Similar to the mysql binlog, the operation will be recorded in the log Lane. Snapshot does not require the need to save the accuracy, and snapshots in combination, is not recommended to use alone. The default interval is 1 second and can be modified.

5.2.1 Setting AOF in the setup file

appendonly yes Append Only File
appendfilename “appendonly.aof”
appendfsync always/everysec/no
no-appendfsync-on-rewrite no
bgsave
AOF ppendfsync
none

5.3 Restore

To restore Redis data simply move Redis’s backup file (dump.rdb, appendonly.aof) to the Redis directory and then start the server.

To get your Redis directory, use the command as follows:

127.0.0.1:6379> config get dir
1) “dir”
2) “/var/lib/redis”
6. Postscript

This article briefly describes the installation and use of Redis, followed by master and slave synchronization, load dispersion.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>