Redis Introduction, Installation, and Data Structure

Introduction to Redis

What is Redis

Redis is an open source (BSD licensed) high-performance key-value memory database developed in C that can be used as a database, cache, and messaging middleware.It is a NoSQL (NOT-Only Sql, generically referred to as a non-relational database) database

  • Excellent performance, data in memory, very fast read and write, support concurrent 10W QPS
  • Single-process, single-threaded, thread-safe, with IO multiplexing
  • Rich data types: strings, hashes, lists, sets, sorted sets, etc.
  • Supports data persistence by saving in-memory data on disk and loading on restart
  • Master-slave replication

Redis application scenarios

  • Memory database (login information, shopping cart information, user browsing records, etc.)
  • Cache servers (commodity data, advertising data, etc.) (most used)
  • Solve session separation in distributed cluster architecture (session sharing)
  • Task queue (seconds killing, snapping, 12306, etc.)
  • Implementation of Distributed Lock
  • Message mode to support publishing subscriptions
  • Application Charts (Ordered Collection)
  • Website Access Statistics
  • Data expiration processing (accurate to milliseconds)

Redis Installation Start (Single)

Redis does not have an official version of Windows and is recommended to run on Linux

Installation Start Under Linux

install

Redis'official website is redis.io , the latest version is available on the official website

Download Unzip

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

Catalog entry, compilation

$ cd redis-5.0.5
$ make

The compiled binary is in the src directory, where redis-server is the server program and redis-cli is the client program

install

You can also install to a specified directory using the make install command and specify an installation directory using PREFIX

$ make install PREFIX=/usr/local/redis

Command Description

  • redis-server: Start the redis service
  • redis-cli: Enter the redis command client
  • redis-benchmark: a tool for performance testing
  • Tools for checking redis-check-aof:aof files
  • Tool for checking redis-check-dump:rdb files
  • redis-sentinel: Start Sentinel Monitoring Service

start-up

The command to start Redis is redis-server, run redis-server under the installation directory

$ /usr/local/redis/bin/redis-server

Close ctrl+c, start this way, our connection window closes, Redis closes, we want it to run in the background all the time, so we need to start the back end

Backend startup (daemon startup)'

1) Copy the redis.conf configuration file from the redis-5.0.5 unzipped file to the bin directory under the installation directory

$ cp /root/redis-5.0.5/redis.conf /usr/local/redis/bin/ 

2) Modify the redis.conf configuration file

$ vim redis.conf
# Change `daemonize` from `no` to `yes`
daemonize yes
# The default binding is a loopback address, which cannot be accessed by other machines by default
# bind 127.0.0.1
# Whether to turn on protection mode, yes should be no
protected-mode no

3) Start the redis-server service

./redis-server redis.conf 

4) Shut down services

./redis-cli shutdown

Command Line Client

The command to start the client is redis-cli, which has two parameters

  • -h: ip address of redis server (default 127.0.0.1)

  • -p:redis instance port number (default is 6379)

./redis-cli -h 127.0.0.1 -p 6379 

Windows setup

Although there is no Windows version officially available, Microsoft has 64-bit Reedis developed and maintained for download at https://github.com/microsoftarchive/redis/releases

Download Redis-x64-xxx.zip here, unzip it, and enter the unzipped directory. The following command is to start redis

redis-server redis.windows.conf

Client connection is

redis-cli.exe -h 127.0.0.1 -p 6379

Redis data structure

Redis stores data in key-value format, key is a binary secure string, where value supports multiple data types. We need to understand and master the following five data types

  • String: A binary-safe string that can be a string, an integer, or a floating point number
  • Hash: A mapping table of field and value of String type suitable for storing objects
  • List: List of strings, sorted by insertion order
  • Set: Unordered set of strings, de-duplicate
  • Sorted Set (zset): Same as Set, but sorted by score

String

command

set/get assignment/value

grammar

# assignment
SET key value [EX seconds] [PX milliseconds] [NX|XX]
# Value 
GET key
# Set new value, return old value
GETSET key value
  • EX seconds: Key expiration time
  • PX milliseconds: Set millisecond expiration time for keys
  • NX: Key must not exist to set successfully for adding
  • XX: The key must exist before it can be set successfully for updating

Example:

127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> get test
"123"

incr/incrby/decr/decrby value increase or decrease

The Value Increase/Decrease command can only be used when the value is an integer, incr/decr increases/decreases by 1, incrby/decrby increases/decreases by 1, and numeric increments or decreases are atomic operations.

127.0.0.1:6379> set counter 100
OK
127.0.0.1:6379> incr counter
(integer) 101
127.0.0.1:6379> incrby counter 20
(integer) 121
127.0.0.1:6379> decr counter
(integer) 120
127.0.0.1:6379> decrby counter 10
(integer) 110

mset/mget sets/gets multiple key values simultaneously

Setting/getting multiple key values in a single command can reduce latency

127.0.0.1:6379> mset k1 10 k2 20 k3 30
OK
127.0.0.1:6379> mget k1 k2 k3
1) "10"
2) "20"
3) "30"

Use scenarios

  • General key-value cache

  • Count: Number of microblogs, fans, etc.

  • Production of Globally Unique id or Single Number Using INCR Atomic Count

Hash

command

hset/hget/hmset/hmget settings/values

# Set a field value
HSET key field value 
# Setting multiple field values
HMSET key field value [field value ...]
# Assignment when field does not exist
HMSETNX key field value
# Get a field value
HGET key field 
# Get multiple field values
HMGET key field [field ...] 
# Get all field values
HGETALL key
127.0.0.1:6379> hmset user name zou age 23
OK
127.0.0.1:6379> hset user location Guangzhou
(integer) 1
127.0.0.1:6379> hget user name
"zou"
127.0.0.1:6379> hgetall user
1) "name"
2) "zou"
3) "age"
4) "23"
5) "location"
6) "Guangzhou"

hincrby increase number

# add number
HINCRBY key field increment 
127.0.0.1:6379> hincrby user age 1
(integer) 24

Other Commands

# Determine whether a field exists
HEXISTS key field
# Get only field names
HKEYS key
# Get only field values
HVALS key
# Get the number of fields
HLEN key
# Get all fields and values
HGETALL key

The difference between String and Hash

The Hash type is suitable for storing data about objects, especially those whose attributes are subject to frequent add-delete operations.String types can also store object data, converting Java objects to json strings or serialization for storage, which is suitable for [query] operations

Use scenarios

  • Store structured data objects, such as user information, commodity information

List

Redis's list is a linked list structure that stores an ordered list of strings, often by adding elements to either end of the list or by getting a fragment of the list

List types are implemented internally using a two-way linked list, so adding elements to both ends of the list has a time complexity of 0 (1), and the closer elements get to both ends, the faster.This means that even if you have a list of tens of millions of elements, it's extremely fast to get 10 records at the top or bottom

command

lpush/rpush Add a new element to the list

# Add elements to the left of the list
LPUSH key value [value ...]
# Add elements to the right of the list
RPUSH key value [value ...]
127.0.0.1:6379> lpush list:1 1 2 3 
(integer) 3 
127.0.0.1:6379> rpush list:1 4 5 6 
(integer) 3 

lrange Get List Fragment

Gets a fragment in the list, returns all the elements between start and stop (including the elements at both ends). The index starts at 0 and the index can be negative, indicating that the count starts at the end: so -1 is the last element and -2 is the second last element in the list

LRANGE key start stop
127.0.0.1:6379> lrange  list:1 0 2 
1) "2" 
2) "1" 
3) "4"

lpop/rpop ejects elements from both ends of the list

Pop up an element from both ends of the list and complete it in two steps:

  • The first step is to remove the elements to the left of the list from the list
  • The second step is to return the removed element value
LPOP key
RPOP key
127.0.0.1:6379>lpop list:1 
"3" 
127.0.0.1:6379>rpop list:1 
"6"

Other Commands

# Gets the number of elements in the list
LLEN key
# Delete a specified number of values from the list
# -When count > 0, LREM is deleted from the left of the list.
# -When count <0, LREM is deleted from the back of the list.
# -When count=0, LREM deletes all elements with value
LREM key count value
# Gets the element value of the specified index
LINDEX key index
# Only list specified fragments are retained, specified ranges are consistent with lrange s
LTRIM key start stop
# Insert an element into the list
LINSERT key BEFORE|AFTER pivot value
# Transfer elements from one list to another
RPOPLPUSH source destination

Use scenarios

  • Various lists: attention list, fan list, comments
  • Message Queuing, which can use the PUSH operation of Lists to store tasks in Lists, then the worker thread can use the POP operation to remove tasks from execution
  • LRANGE makes it easy to page list s
  • Operation to fetch the latest N data

Set

Set is an unordered collection of strings, where the data is not repeated and there is no order. Common operations for a collection type are adding or deleting elements to the collection, determining whether an element exists, etc. Since Redis for a collection type is implemented internally using a Hash list with empty values, the time complexity of all these operations is 0 (1)Redis also provides operations for intersections, unions, and differences between sets

command

sadd/srem Add/Remove Elements

SADD key member [member ...]
SREM key member [member ...]
127.0.0.1:6379> sadd set a b c 
(integer) 3 
127.0.0.1:6379> sadd set a 
(integer) 0 
127.0.0.1:6379> srem set c d 
(integer) 1 

smembers takes all elements

SMEMBERS key 
127.0.0.1:6379> smembers set 
1)  "b" 
2)  "a"

sismember determines if an element is in a set

# Determines whether an element is in a set, returns 1 as in, 0 as not
SISMEMBER key member
127.0.0.1:6379>sismember set a 
(integer)  1 
127.0.0.1:6379>sismember set h 
(integer)  0 

sdiff/sinter/sunion difference/intersection/union operation command

  • SDIFF-Difference Set Operations A-B: A set of elements belonging to A and not to B

    SDIFF key [key ...] 
    
    127.0.0.1:6379> sadd setA 1 2 3
    (integer) 3
    127.0.0.1:6379> sadd setB 2 3 4
    (integer) 3
    127.0.0.1:6379> sdiff setA setB
    1) "1"
    127.0.0.1:6379> sdiff setB setA
    1) "4"
    
  • SINTER-intersection of sets A_B: A set of elements belonging to A and B

    SINTER key [key ...]
    
    127.0.0.1:6379> sinter setA setB
    1) "2"
    2) "3"
    
  • SUNION - Union of sets A_B: A set of elements belonging to A or B

    SUNION key [key ...]
    
    127.0.0.1:6379> sunion setA setB
    1) "1"
    2) "2"
    3) "3"
    4) "4"
    

Other Commands

# Gets the number of elements in the collection
SCARD key
# Random pop-up of an element from a set
SPOP key

Use scenarios

  • List of weights to be removed
  • Provides operations such as seeking intersection, union, difference and so on, which can be very convenient to achieve such functions as common concern, common preferences, second degree friends, etc.

Ordered Set (zset)

An ordered Set (zset) is based on the Set type. The ordered Set type associates a score with each element in the Set, which allows us to not only insert, delete, and determine whether an element exists in the Set, but also get the first N elements with the highest or lowest score and get the fingerElements etc. within a given fraction range are related to fractions Operation

The difference between SortedSet and List

Similarity points

  • Both are ordered, lists are in data insertion order, and SortedSet s are in natural order based on score
  • Both get elements of a certain range

Difference

  • List types are implemented through a chain table, which provides fast access to data near both ends, but as more elements are added, access to intermediate data becomes slower.
  • Ordered collection types are implemented using hash lists, and all data in the middle can be read quickly
  • Lists cannot simply adjust the position of an element, but ordered collections can (by changing scores)
  • Ordered collections consume more memory than list types

command

zadd/zrem Add/Remove Elements

ZADD key score member [score member ...]
ZREM key member [member ...] 
127.0.0.1:6379> zadd scoreboard 80 zhangsan 89 lisi 94 wangwu
(integer) 3
127.0.0.1:6379> zadd scoreboard 97 lisi
(integer) 0
127.0.0.1:6379> zrem scoreboard lisi
(integer) 1

zrange/zrevrange Gets a list of range elements

Gets a list of elements ranked in a range.

  • ZRANGE: Returns all elements (including elements at both ends) indexed from start to stop in the order of element fraction from smallest to largest
  • ZREVRANGE: Returns all elements (including elements at both ends) indexed from start to stop in the order of element fraction from large to small
# Add the WITHSCORES parameter at the end of the command if you want to get the fraction of the element
ZRANGE key start stop [WITHSCORES] 
ZREVRANGE key start stop [WITHSCORES]
127.0.0.1:6379> zrange scoreboard 0 2
1) "zhangsan"
2) "wangwu"
3) "lisi"
127.0.0.1:6379> zrevrange scoreboard 0 2
1) "lisi"
2) "wangwu"
3) "zhangsan"

zscore gets the fraction of an element

ZSCORE key member
127.0.0.1:6379> zscore scoreboard lisi
"97"

Other Commands

# Gets the elements of the specified score range
ZRANGEBYSCORE key min max [WITHSCORES] 
# Increase the fraction of an element
ZINCRBY key increment member
# Gets the number of elements in the collection
ZCARD key
# Gets the number of elements in the specified score range
ZCOUNT key min max
# Delete elements by rank range
ZREMRANGEBYRANK key start stop
# Delete elements by score range
ZREMRANGEBYSCORE key min max
# Get ranking of elements 
# - ZRANK: from small to large
# - ZREVRANK: from big to small
ZRANK key member
ZREVRANK key member

Use scenarios

  • Store an ordered and non-repeating list of collections

  • Table related, take TOP N operation

Common Command

keys returns the key value that satisfies the rule

# Return key-value that satisfies the rule
KEYS pattern
127.0.0.1:6379> keys k*
1) "k1"
2) "k3"
3) "k2"
4) "key"

Delete

DEL key [key ...] 
127.0.0.1:6379> del k1 k2 k3
(integer) 3

Exists exists

EXISTS key [key ...]
127.0.0.1:6379> exists test
(integer) 1
127.0.0.1:6379> exists mytest
(integer) 0

expires lifetime setting (important)

Redis is used more as a cache in the actual process, and the cached data is generally set to a lifetime, after expiration the data is automatically destroyed

# Set expiration time (in seconds)
EXPIRES key seconds  
# Set expiration time (in milliseconds)
PEXPIRES key milliseconds 
# View key remaining lifetime
TTL key
PTTL key
# Clear lifetime
PERSIST key
127.0.0.1:6379> expire test 5
(integer) 1
127.0.0.1:6379> ttl test
(integer) 2
127.0.0.1:6379> ttl test
(integer) -2
127.0.0.1:6379> get test
(nil)

Rename rename

RENAME key newkey
127.0.0.1:6379> keys *
1) "counter"
127.0.0.1:6379> rename counter coounter_new
OK
127.0.0.1:6379> keys *
1) "coounter_new"

Typee View Data type

TYPE key
redis 127.0.0.1:6379> type addr
string
redis 127.0.0.1:6379> type myzset2
zset
redis 127.0.0.1:6379> type mylist
list

Keywords: Programming Redis Database Windows Fragment

Added by davey10101 on Sun, 11 Aug 2019 22:57:35 +0300