Redis is a non-relational database (NoSql), generally refers to the database does not store data in the form of tables

Why do non-relational databases exist?

Which brings us to relational databases (where data is stored in tables and relationships are maintained by keys). Mysql, which stands for Oracle)

And relational databases in today’s problems

1. Data is saved in tables, and there are constraints among data.

2 The high-speed Development of the Internet leads to the following three problems: a High concurrency: massive concurrent requests B High performance: querying or operating a piece of data from massive data

C: High scale: multiple database nodes, data migration (involving clusters, load balancing)

Hence the birth of Redis

Redis stores data in the form of key-value and stores data in the content. Redis as a cache library, can effectively improve the web application

Access efficiency. Port number default 6379 can be baidu under merz personality program ape

5 data types String, list,set, Hash, sortedSet the most common is String

List is used when a queue is needed. Like sending a text message or something

Hash stores information after the user logs in.

Set as the name implies is not repeated

Sortedset has scenarios that need to be sorted without repetition. List. Ha ha

Redis features

By default, a Jedis provides up to 16 databases: 0 to 15, and we are working with library 0 by default. You can change the number of databases by changing the configuration file redis.conf to change the value of database

Select 3– to switch to library 3

2. Move database: move data from database 0 to database 3: move key 3

3. Quit

4. Query the number of keys in a library: dbsize

5. Flushes the current library: flushdb

6. Flushall libraries: flushhall

Message subscription and publishing

Subscribe to messages: Subscribe Channel

Bulk subscribe messages: psubscribe Channel *

Publish message: Publish Channel message

The transaction

Transactions in Redis are pseudo transactions. In the Redis database, after a transaction is started, all commands executed are placed in a queue. When the transaction is committed, fetch the commands from the queue and execute them one by one. If one of the commands fails, pass and continue with the others.

Start transaction: multi

Commit transaction: exec execute

Rollback transaction: discard Discard all commands to be executed in a queue

Redis persistence (Understand)

Redis can be persisted in two ways: RDB and AOF. RDB is enabled by default, while AOF is manually enabled.

RDB mode – Snapshot mode

Performance is good, but data may be lost

The Redis database automatically backs up all the data in the database periodically and generates a “snapshot file”. The generated file is called dump. RDB by default and is stored in the Redis installation directory.

RDB persistence time:

Save900 1 If a data operation is performed within 900 seconds, a snapshot is saved

Save300 10 If you perform 10 operations within 300 seconds, save one snapshot

Save60 10000 If the operation is performed 10000 times within 60 seconds, a snapshot is saved

AOF mode – Append only file

The performance is slightly lower and the possibility of data loss is lower

Every time there is a data change to Redis, Redis saves the command executed in a file. When the Redis server restarts, it automatically reads the files and reconstructs the database. Manually enable: Change the appendonly value in redis. Conf to yes to generate the appendone.aof file automatically.

 

AOF mode persistence time:

Appendfsync always: Stores data once as soon as it changes.

Appendfsync everysec: saves once at second

Appendfsync no: not saved

\

Usage scenarios of Redis in Web applications

In Web applications, Redis is usually used as a cache database. Cache the data that is frequently queried but not frequently changed in Redis. When you need to query this data again, you can get it quickly from Redis without having to manipulate a relational database to get it

Matters needing attention:

If the data changes, the cache needs to be cleared. To keep the data in the cache consistent with the data in the database.

\

\