Redis Study Notes series consists of six articles:

  1. Redis Study Notes: Quick Start
  2. Redis study Notes: Core concepts
  3. Redis Study Notes: Performance optimization
  4. Redis learning Notes: Distributed solutions
  5. Redis learning notes: SpringBoot actual combat (with package complete code)
  6. Redis study notes: Often meet questions and answers

I wrote this series of articles in order to check and fill in the gaps, and to help more partners quickly master the core knowledge of Redis. This article is the first one, so let’s start now.

What is a Redis

Redis is a very fast, non-relational database that stores mappings from keys to five different types of values. Redis supports in-memory persistence on disk, replication to extend read performance, and client sharding to extend write performance.

Redis vs. other databases

The name of the type Data storage options Types of queries Additional functions
Redis Non-relational databases String, list, set, hash, sort set Commands for each data type of the common access pattern, with bulk operations and partial transaction support Publish/subscribe, master/slave replication, disk persistence, scripts (stored procedures)
MySQL Relational database Row table databases, table views, Spaces, and third-party extensions SELECT, INSERT, UPDATE, DELETE, functions, stored procedures ACID compatible (using InnoDB), master/slave and master/master replication
PostgreSQL Relational database Row table databases, table views, spatial and third-party extensions, customizable types SELECT, INSERT, UPDATE, DELETE, built-in functions, custom stored procedures ACID compatibility, master/slave replication, multi-master replication (third party)
MongoDB Non-relational document storage A database of schema-free BSON document tables Commands for creating, reading, updating, deleting, querying conditions, etc Supports Map-Reduce operations, master/slave replication, sharding, and spatial indexing

Redis features and benefits

The characteristics of

  • Redis supports data persistence, saving data in memory to disk, which can be reloaded for use upon restart.
  • Redis not only supports simple key-value type data, but also provides the storage of list, set, zset, hash and other data structures.
  • Redis supports data backup, namely, data backup in master-slave mode.

advantages

  • High performance – Redis can read 110,000 times /s and write 81,000 times /s.
  • Rich data types – Redis supports binary case Strings, Lists, Hashes, Sets and Ordered Sets data type operations.
  • Atomic – All operations in Redis are atomic, meaning that they either succeed or fail at all. Individual operations are atomic. Multiple operations also support transactions, namely atomicity, wrapped in MULTI and EXEC instructions.
  • Rich features – Redis also supports publish/subscribe, notifications, key expiration, and more.

Redis data structure

String (String)

The picture above is an example of a string, value: world for key: Hello.

Common operations:

instruction describe
GET Gets the data stored on the specified key
SET Stores value on the specified key
DEL Deletes the value stored on the specified key (for all types)

Example:

redis 127.0. 01.:6379> set hello world
OK

redis 127.0. 01.:6379> get hello
"world"

redis 127.0. 01.:6379> del hello
(integer) 1

redis 127.0. 01.:6379> get hello
(nil)
Copy the code

List

A Redis list is a simple list of strings, sorted by insertion order. You can add an element to either the head (left) or the tail (right) of the list.

Common operations:

instruction describe
RPUSH Adds one or more values to the list
LRANGE Gets the elements in the specified range of the list
LINDEX Gets the elements in the list by index
LPOP Removes and gets the first element of the list

Example:

Redis 127.0.0.1:6379> rpush list-key item (integer) 1 redis 127.0.0.1:6379> rpush list-key item2 (integer) 2 redis 127.0.0.1:6379> rpush list-key item (integer) 3Copy the code
Redis 127.0.0.1:6379> lrange list-key 0-1 1) "item" 2) "item2" 3) "item"Copy the code
Redis 127.0.0.1:6379> lpop list-key "item" redis 127.0.0.1:6379> lrange list-key 0-1 1) "item2" 2) "item"Copy the code

Set

Redis’ Set is an unordered collection of type String. Collection members are unique, which means that no duplicate data can occur in the collection.

The encoding of a collection object can be intSet or HashTable.

The collection in Redis is realized by hash table, so the complexity of adding, deleting and searching is O(1).

Common operations:

instruction describe
SADD Adds one or more members to a collection
SMEMBERS Returns all members of the collection
SISMEMBER Check whether the member element is a member of the collection key
SREM Removes one or more members of a collection

Example:

Redis 127.0.0.1:6379> sadd set-key item (integer) 1 redis 127.0.0.1:6379> sadd set-key item2 (integer) 1 redis 127.0.0.1:6379> sadd set-key item3 (integer) 1 redis 127.0.0.1:6379> sadd set-key item (integer) 0Copy the code
Redis 127.0.0.1:6379> smembers set-key 1) "item" 2) "item2" 3) "item3"Copy the code
Redis 127.0.0.1:6379> sismember set-key item4 (integer) 0 redis 127.0.0.1:6379> sismember set-key item (integer) 1Copy the code
Redis 127.0.0.1:6379> srem set-key item2 (integer) 1 redis 127.0.0.1:6379> srem set-key item2 (integer) 0Copy the code
Redis 127.0.0.1:6379> smembers set-key 1) "item" 2) "item3"Copy the code

Hash

Redis hash is a mapping of string fields and values, and hash is especially useful for storing objects.

Common operations:

instruction describe
HSET Set the value of field in hash table key to value.
HGET Gets the value of the specified field stored in the hash table.
HGETALL Gets all the fields and values of the specified key in the hash table
HDEL Deletes one or more hash table fields, if present

Example:

Redis 127.0.0.1:6379> hset hash-key sub-key1 Value1 (integer) 1 redis 127.0.0.1:6379> hset hash-key sub-key2 value2 (INTEGER) 1 redis 127.0.0.1:6379> hset hash-key sub-key1 Value1 (integer) 0Copy the code
Redis 127.0.0.1:6379> hgetall hash key1) "sub-key1" 2) "value1" 3) "sub-key2" 4) "value2"Copy the code
Redis 127.0.0.1:6379> hdel hash-key sub-key2 (integer) 1 redis 127.0.0.1:6379> hdel hash-key sub-key2 (integer) 0Copy the code
Redis 127.0.0.1:6379> hget hash-key sub-key1 "value1"Copy the code
Redis 127.0.0.1:6379> hgetall hash-key 1) "sub-key1" 2) "value1"Copy the code

Sorted set (sorted set)

Redis ordered collections, like collections, are collections of string elements and do not allow duplicate members.

The difference is that each element is associated with a double score. Redis uses scores to sort the members of a collection from smallest to largest.

The members of an ordered set are unique, but the score can be repeated.

Collections are implemented by hashing tables, so adding, deleting, and searching are O(1) complexity.

Common operations:

instruction describe
ZADD Adds one or more members to an ordered collection, or updates the scores of existing members
ZRANGE Returns an ordered collection of members within a specified interval by indexing the interval
ZRANGEBYSCORE Returns an ordered set of members within a specified interval by a fraction
ZREM Removes one or more members of an ordered collection

Example:

Redis 127.0.0.1:6379> zadd zset-key 728 member1 (integer) 1 redis 127.0.0.1:6379> zadd zset-key 982 member0 (integer) 1 Redis 127.0.0.1:6379> zadd zset-key 982 member0 (integer) 0Copy the code
Redis 127.0.0.1:6379> zrange zset-key 0 -1 withscores 1) "member1" 2) "728" 3) "member0" 4) "982"Copy the code
Redis 127.0.0.1:6379> zrangebyscore zset-key 0 800 withscores 1) "member1" 2) "728"Copy the code
Redis 127.0.0.1:6379> zrem zset-key member1 (integer) 1 redis 127.0.0.1:6379> zrem zset-key member1 (integer) 0Copy the code
Redis 127.0.0.1:6379> zrange zset-key 0-1 withscores 1) "member0" 2) "982"Copy the code

Common application

  • Login token, cookie cache
  • The shopping cart
  • Web caching
  • Json data caching