Redis study notes (1) redis basics

Basic concepts of redis

redis is a high-performance NOSQL series of non-relational databases

NOSQL

NoSQL (NoSQL = Not Only SQL), which means "not just SQL", is a brand new database concept, which refers to non-relational databases. With the rise of Internet web2.0 websites, traditional relational databases have been unable to cope with web2.0 websites, especially the super-large-scale and highly concurrent SNS-type web2.0 pure dynamic websites, which have exposed many insurmountable problems. However, non-relational databases have developed rapidly due to their own characteristics. NoSQL database was created to solve the challenges brought by multiple data types in large-scale data collections, especially big data application problems.

The difference between NOSQL and SQL

NOSQL features

Features:

  1. Cost: Nosql database is simple and easy to deploy. It is basically open source software. It does not need to spend a lot of money to buy and use like using oracle. It is cheaper than relational databases.
  2. Query speed: Nosql database stores data in the cache, and relational databases store data on the hard disk. Naturally, the query speed is far lower than that of nosql database.
  3. Data storage format: unstructured storage structure, nosql storage format is key, value format, document format, picture format, etc., so it can store various formats such as basic types and objects or collections, while databases only support basic Types of.
  4. Scalability: Relational databases have the limitation of multi-table query mechanisms like join, which makes it difficult to expand.
  5. Satisfying the BASE theory is a shorthand for the three phrases Basically Available, Soft state and Eventually consistent.
  6. Mass data, support high concurrency.

Disadvantages:

  1. The tools and materials to maintain are limited, because nosql is a new technology and cannot be compared with the technology of relational databases for more than 10 years.
  2. No support for sql is provided. If industry standards such as sql are not supported, a certain amount of user learning and use costs will be incurred.
  3. Does not provide relational database for transaction processing.

SQL features

Features:

  1. Relational databases are based on table, which is a two-dimensional model
  2. Is a structured storage mode, all stored data needs to adapt to the table structure
  3. Table and table can be associated
  4. Support structured operating language
  5. Relational databases support transactions and also have the ACID nature of transactions.
    Disadvantages:
  6. It can only be expanded upward, without dynamic expansion technology, and can only be expanded by adding hardware and some sub-databases and tables.
  7. When the data is stored in the table, it will be very difficult to change the table structure again.
  8. The read and write pressure on the disk is high

The advantages of non-relational and relational databases

Relational Database

  1. Complex queries can use SQL statements to conveniently do very complex data queries between one table and multiple tables.
  2. Transaction support enables the realization of data access requirements with high security performance. For these two types of databases, the other's advantage is its own weakness, and vice versa.

Non-relational database

  1. Performance NOSQL is based on key-value pairs, which can be imagined as the correspondence between primary keys and values ​​in the table, and does not need to be parsed by the SQL layer, so the performance is very high.
  2. Scalability is also because based on key-value pairs, there is no coupling between data, so it is very easy to scale horizontally.

Mainstream NOSQL products

Key-Value storage database

Related products: Tokyo Cabinet/Tyrant, Redis, Voldemort, Berkeley DB.
Typical applications: Content caching, which is mainly used to process large amounts of data with high access load.
Data model: A series of key-value pairs
Advantage: Fast query
Disadvantage: The stored data lacks structure

Column store database

Related products: Cassandra, HBase, Riak
Typical application: Distributed file system
Data model: Column cluster storage, storing the same column data together
Advantages: Fast search speed, strong scalability, easier to carry out distributed expansion
Disadvantages: Relatively limited functions

Document database

Related products: CouchDB, MongoDB
Typical applications: Web applications (similar to Key-Value, Value is structured)
Data model: A series of key-value pairs
Advantages: Data structure requirements are not strict
Disadvantages: query performance is not high, and lack of uniformity Query syntax

Graph database

Related databases: Neo4J, InfoGrid, Infinite Graph.
Typical applications: social networks.
Data model: graph structure.
Advantages: use graph structure related algorithms.
Disadvantages: It is necessary to calculate the whole graph to get the result, and it is not easy to make a distributed cluster solution.

What is redis

Redis is an open source high-performance key-value database developed in C language. The official test data is provided. 50 concurrent executions of 100,000 requests, the read speed is 110000 times/s, and the write speed is 81000 times. /s, and Redis adapts to storage needs in different scenarios by providing multiple key-value data types. The key-value data types supported by Redis so far are as follows:

  1. String type string
  2. Hash type hash
  3. List type list
  4. Collection type set
  5. Sortedset

Application scenarios of redis

Cache (data query, short connection, news content, product content, etc.)
online friend list of chat room
task queue. (Spike, panic buying, 12306, etc.)
application rankings,
website visit statistics,
data expiration processing (accurate to the millisecond
) session separation in distributed cluster architecture

Application of redis

This note mainly uses some steps of redis in windows.

  1. Official website: https://redis.io
  2. Chinese website: http://www.redis.net.cn/
  3. Decompression can be used directly:
    redis.windows.conf: configuration file
    redis-cli.exe: redis client
    redis-server.exe: redis server

Basic operation commands

The data structure of redis:

Redis stores data in the format of key and value, where the key is a string, and the value has 5 different data structures. The data structure of
value:

  1. String type string
  2. Hash type hash: map format
  3. List type list: linkedlist format. Support for repeated elements
  4. Collection type set: duplicate elements are not allowed
  5. Sorted set type sortedset: duplicate elements are not allowed, and the elements are in order

String type string

  1. Storage: set key value
127.0.0.1:6379> set username zhangsan
OK
  1. Get: get key
127.0.0.1:6379> get username
"zhangsan"
  1. Delete: del key
127.0.0.1:6379> del age
(integer) 1

Hash type hash

  1. Storage: hset key field value
127.0.0.1:6379> hset myhash username lisi
(integer) 1
127.0.0.1:6379> hset myhash password 123
(integer) 1
  1. Get:
    hget key field: get the value corresponding to the specified field
127.0.0.1:6379> hget myhash username  "lisi"

hgetall key: get all the field and value

127.0.0.1:6379> hgetall myhash
1) "username"
2) "lisi"
3) "password"
4) "123"
  1. Delete: hdel key field
127.0.0.1:6379> hdel myhash username
(integer) 1

Linked list

List type list: You can add an element to the head (left) or tail (right) of the list

  1. Add:
    lpush key value: add the element to the left table of the list
    rpush key value: add the element to the right of the list
127.0.0.1:6379> lpush myList a
(integer) 1
127.0.0.1:6379> lpush myList b
(integer) 2
127.0.0.1:6379> rpush myList c
(integer) 3
  1. Get:
    lrange key start end: range get
127.0.0.1:6379> lrange myList 0 -1
1) "b"
2) "a"
3) "c"
  1. Delete:
    pop key: delete the leftmost element of the list, and return the element to
    rpop key: delete the rightmost element of the list, and return the element

Set

Collection type set: duplicate elements are not allowed

  1. Storage: sadd key value
127.0.0.1:6379> sadd myset a
(integer) 1
127.0.0.1:6379> sadd myset a
(integer) 0
  1. Get: smembers key: get all elements in the set collection
127.0.0.1:6379> smembers myset
1) "a"
  1. Delete: srem key value: delete an element in the set collection
127.0.0.1:6379> srem myset a
(integer) 1

Sorted set type sortedset:

Repeating elements are not allowed, and the elements are in order. Each element will be associated with a double type score. Redis uses scores to sort the members of the collection from small to large.

  1. Storage: zadd key score value
127.0.0.1:6379> zadd mysort 60 zhangsan
(integer) 1
127.0.0.1:6379> zadd mysort 50 lisi
(integer) 1
127.0.0.1:6379> zadd mysort 80 wangwu
(integer) 1
  1. 获取:zrange key start end [withscores]
127.0.0.1:6379> zrange mysort 0 -1
1) "lisi"
2) "zhangsan"
3) "wangwu"

127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "wangwu"
4) "80"
5) "lisi"
6) "500"
  1. Delete: zrem key value
127.0.0.1:6379> zrem mysort lisi
(integer) 1

General order

  1. keys *: Query all keys
  2. type key: Get the type of value corresponding to the key
  3. del key: delete the specified key value

redis persistence

Redis is a memory database. When the redis server restarts and the computer restarts, the data will be lost. We can persist the data in redis memory to files on the hard disk.

Redis persistence mechanism:

RDB

RDB is the default mode, no configuration by default uses this mechanism
in certain time intervals, the change of key is detected, then the persistent data

  1. Edit the redis.windwos.conf file
#   after 900 sec (15 min) if at least 1 key changed
save 900 1
#   after 300 sec (5 min) if at least 10 keys changed
save 300 10
#   after 60 sec if at least 10000 keys changed
save 60 10000
  1. Restart the redis server and specify the configuration file name
G:\JAVAEE\JAVAEE2\day22\redis\windows-64\redis-2.8.9>redis-server.exe redis.windows.conf	

AOF:

The log record method can record the operation of each command. Data can be persisted after each command operation

编辑redis.windwos.conf文件
appendonly no(关闭aof) --> appendonly yes (开启aof)		
# appendfsync always : 每一次操作都进行持久化
appendfsync everysec : 每隔一秒进行一次持久化
# appendfsync no	 : 不进行持久化

Need to load related configuration files at startup.

It is not recommended because of the greater impact on performance.

Guess you like

Origin blog.csdn.net/xueshanfeitian/article/details/110928085