NoSQL database (1) - the development and characteristics of redis & Redis installation and configuration file redis.conf & redis command line client

NoSQL database (1) - the development and characteristics of redis & Redis installation and configuration file redis.conf & redis command line client

NoSQL database

Course Introduction

main content

  • Redis development and actual combat (memory type)
  • Getting started with Memcached (memory type)
  • Getting started with MongoDb (storage type)

Redis and memcached are the same type, memory type. MongoDb is storage type.

Focus on explaining redis , the storage database is still the world of mysql.

learning target

  • Master the characteristics and use of non-relational database
  • Master the application of NoSQL technology in actual development

Main application scenarios of NoSQL

It is designed to deal with high concurrency and high-speed read and write scenarios. As far as Redis technology is concerned, its performance is very superior, and it can support hundreds of thousands of read/write operations per second. Its performance far exceeds that of databases, and it also supports clusters , Distributed, master-slave synchronization and other configurations.

  • Tmall Double 11
  • Grab red envelopes, grab mobile phones, grab train tickets, grab tickets
  • ssr server side rendering

Chapter 1 Redis

  • features
  • api
  • Combined with nodejs application

History and Development

In 2008, an Italian start-up company Merzia[ http://merzia.com ] launched a MySQL-based real-time website statistics system LLOOGG[ http://lloogg.com ], but not long after the company's founder Salvatore Sanfilippo began to be disappointed with the performance of MySQL, so he decided to tailor a database for LLOOGG himself, and completed the development in 2009. This database is Redis. However, Salvatore Sanfilippo was not satisfied with only using Redis for the LLOOGG product, but hoped to allow more people to use it, so in the same year Salvatore Sanfilippo released Redis as open source, and began to work with another major Redis code Contributor Pieter Noordhuis continues Redis development to this day.

features

What is the charm of redis that has attracted so many users?

  • Storage structure special - dictionary
  • Memory Storage and Persistence - Cache
  • feature rich
  • Simple and stable - simple and reliable

storage structure

Redis is the abbreviation of REmote DIctionary Server (remote dictionary server), which stores data in a dictionary structure.

A dictionary is an object in js

The key in js can only be a string

Like dictionaries in most languages, the key values ​​of Redis dictionaries can be strings or other data types.

  • string
  • hash
  • the list
  • gather
  • ordered set

Dictionary: Similar to object in js is a typical dictionary structure.

ex:

We store the data of an article in the post variable (title, body, reading volume)

post.titile = 'hello'
post.content = 'balabala'
post.views = 0
post.tags = ['php', 'java', 'nodejs']

If you need to retrieve articles by tag, the relational database mysql needs to build 3 tables, and the query is very complicated.

But using Redis can perform set operations such as intersection and union of tags. Various query requirements for tags can be easily realized.

memory storage and persistence

All data in the Redis database is stored in memory. On an ordinary laptop, Redis can read and write more than 100,000 key-value pairs per second.

However, data stored in memory will result in data loss after the program exits. However, redis also provides support for data persistence.

Data persistence in the browser can be understood as: localStorage, cookie

feature rich

The application scenarios are rich, and redis is a veritable all-rounder.

  • cache
  • queue system
  1. Redis can set the lifetime for each key, and it will be automatically deleted when it expires. This function combined with excellent performance allows it to be used as a cache system. Because redis supports persistence and rich data types, it also becomes a competitor of Memcached.

insert image description here

  1. As a cache system, redis can also limit the maximum space occupied by data, and automatically delete unnecessary keys after exceeding.
  2. Redis's list type keys can also be used to implement queues, and support blocking reads, making it easy to implement a high-performance priority queue.
  3. Redis also supports "publish/subscribe", and systems such as chat rooms can be built based on this.

simple and stable

No matter how feature-rich it is, it's hard to be attractive if it's too complicated to use.

  1. The intuitive storage structure of Redis makes it very simple to interact with Redis through programs. In Redis, commands are used to read and write data. Command statements are to Redis what SQL language is to relational databases.
ex:

In a relational database, you can use the following SQL statement to obtain the value of the title field of the record whose id is 1 in the posts table:

SELECT title FROM posts WHERE id=1 LIMIT 1

redis reads like this

HGET post:1 title

Where HGET is a command. Redis provides more than one hundred commands (as shown in Figure 1-2), which sounds like a lot, but there are only a dozen commonly used ones, and each command is easy to remember, which is actually much simpler than SQL statements.

insert image description here

  1. Redis is developed in C language, and the code size is only more than 30,000 lines. This lowers the threshold for users to modify the Redis source code to make it more suitable for their own project needs. This is certainly a big draw for developers looking to "squeeze out" database performance.

Redis installation

"What is done on paper will eventually become shallow, and I will never know that this matter must be practiced."
——Lu You, "Reading on a Winter Night and Showing Ziyu"

  • MacOS
brew install redis

Brew is homebrew

start up

redis-server

The default port is 6379, modify the port

redis-server --port 6389

Initialize the configuration file

It will be read every time the redis service startsredis.conf

The path for mac os is in/usr/local/etc/redis.conf

# Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
# 启用守护进程后,Redis会把pid写到一个pidfile中,在/var/run/redis.pid
daemonize no

# 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
pidfile /var/run/redis.pid

# 指定Redis监听端口,默认端口为6379
# 如果指定0端口,表示Redis不监听TCP连接
port 6379

# 绑定的主机地址
# 你可以绑定单一接口,如果没有绑定,所有接口都会监听到来的连接
bind 127.0.0.1
# 也就是本机

# 当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 0

# 指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
# debug (很多信息, 对开发/测试比较有用)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel verbose

# 日志记录方式,默认为标准输出,如果配置为redis为守护进程方式运行,而这里又配置为标准输出,则日志将会发送给/dev/null
logfile stdout

# 设置数据库的数量,默认数据库为0,可以使用select <dbid>命令在连接上指定数据库id
# dbid是从0到‘databases’-1的数目
databases 16

################################ SNAPSHOTTING  #################################
# 指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   满足以下条件将会同步数据:
#   900秒(15分钟)内有1个更改
#   300秒(5分钟)内有10个更改
#   60秒内有10000个更改
#   Note: 可以把所有“save”行注释掉,这样就取消同步操作了

save 900 1
save 300 10
save 60 10000

# 指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
rdbcompression yes

# 指定本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb

# 工作目录.
# 指定本地数据库存放目录,文件名由上一个dbfilename配置项指定
# 
# Also the Append Only File will be created inside this directory.
# 
# 注意,这里只能指定一个目录,不能指定文件名
dir ./


easy to use redis-cli

Similar to the node command, an interactive command line client.

redis-cli

close connection

Considering that Redis may be synchronizing the data in the memory to the hard disk, forcibly terminating the Redis process may result in data loss. The correct way to stop Redis should be to send a SHUTDOWN command to Redis, the method is:

SHUTDOWN

redis command line client

When redis-cli is executed, it will automatically connect to Redis according to the default configuration (the server address is 127.0.0.1, and the port number is 6379). The address and port number can be customized through the -h and -p parameters:

redis-cli -h 127.0.0.1 -p 6379

ex:

  • ping

    PING
    
  • echo hi

    ECHO hi
    

command return value

status reply

PING

PONG

error reply

Randomly enter a command that does not exist

(error) ERR unknown command asdasd, with args beginning with:

integer reply

The Redis Incr command increments the numeric value stored in the key by one.

If the key does not exist, the value of the key will be initialized to 0 first, and then the INCR operation will be performed.

Returns an error if the value contains the wrong type, or if a value of type string cannot be represented as a number.

The value of this operation is limited to a 64-bit (bit) signed number representation.

incr abc
set abc 20
incr abc
string reply
127.0.0.1:6379> get foo
(nil)
multiline string reply
127.0.0.1:6379> keys *

configuration

/usr/local/etc/redis.confYou can also modify the configuration in the cli

config set loglevel warning

multiple databases

In fact, a Redis instance provides multiple dictionaries for storing data, and the client can specify which dictionary to store the data in. This is similar to the well-known fact that multiple databases can be created in a relational database instance, so each dictionary in it can be understood as an independent database.
Each database is externally named with an increasing number starting from 0. Redis supports 16 databases by default, and this number can be modified by configuring the parameter databases. After the client establishes a connection with Redis, it will automatically select database 0, but you can use the SELECT command to change the database at any time. If you want to select database 1:

redis>SELECT 1
OK
redis [1]>GET foo
(nil)

t loglevel warning


#### 多数据库

而实际上一个Redis实例提供了多个用来存储数据的字典,客户端可以指定将数据存储在哪个字典中。这与我们熟知的在一个关系数据库实例中可以创建多个数据库类似,所以可以将其中的每个字典都理解成一个独立的数据库。
每个数据库对外都是以一个从0开始的递增数字命名,Redis默认支持16个数据库,可以通过配置参数databases来修改这一数字。客户端与Redis建立连接后会自动选择0号数据库,不过可以随时使用SELECT命令更换数据库,如要选择1号数据库:

```shell
redis>SELECT 1
OK
redis [1]>GET foo
(nil)

However, these databases named after numbers are different from the databases we understand. First of all, Redis does not support the name of the custom database. Each database is named after a number. Developers must record which databases store which data. In addition, Redis does not support recording which databases store which data for each database. **In addition, Redis does not support setting different access passwords for each database, so a client can either access all databases, or even have no access to one database. **The most important point is that multiple databases are not completely isolated. For example, the FLUSHALL command can clear the data in all databases in a Redis instance. To sum up, these databases are more like a kind of namespace, not suitable for storing data of different applications. For example, you can use database 0 to store data in the production environment of an application, and database 1 to store data in the test environment, but it is not suitable to use database 0 to store data of application A and database 1 to store data of application B. Different applications should use different Redis instances to store data . Since redis is very lightweight, the memory occupied by an empty Redis instance is only about 1MB, so there is no need to worry that multiple Redis instances will occupy a lot of additional memory.

Guess you like

Origin blog.csdn.net/weixin_44867717/article/details/131628762