Why Redis Why so popular

Most developers will now have heard of Redis. Redis is one of the best on the market open-source NoSQL database memory. It is a front-end and back-end services (such as key-value lookup, queues, hash, etc.) provides a lot of help.

First, what is Redis?

The official description Redis, Redis is an open source (BSD license), memory data structures stored as a database, cache and message broker, which supports strings, hash tables, lists, sets, ordered set, bitmaps, hyperloglogs etc. type of data.

Two, Redis and Memcached

Redis server is a data structure. As a key data storage, Redis like Memcached, but it has two major advantages over Memcached: support for more data types and persistence.

Persistence allows you Redis can operate as a legal database, rather than a temporary cache unstable. If you restart, Memcached information will be lost; but Redis data still exists.

Redis supports multiple types of data storage. Memcached can be used just like a string. And you can also deal with hash (hash), set (not sorted all the different values), zset (sort all the different values) and a list (including a possible repeat of the sort).

Three, Redis How does it work?

Database data stored on disk or SSD, and Redis data is resident in memory. By eliminating the need to access the disk, Redis and other data storage memory to find time to avoid delays and can access data in a few microseconds. Redis offers a variety of data structures, high availability, geospatial, Lua scripts, transactions, persistence, and disk cluster support, allowing real-time Internet-level application building easier.

Four, Redis persistence

Can now be achieved in two different ways Persistence: called snapshot, a semi-persistent mode, generating a data set in a specified interval time snapshots, written in RDB (Redis DataBase) dump format. Starting with version 1.1, there are safer alternatives AOF (Append Only File), it appears to make up for the lack of RDB (data inconsistency), in the form of a log to record each write operation and append the file.

Five, RDB and AOF, which one should I use?

In general, if you want to achieve comparable data security PostgreSQL, you should use RDB and AOF two kinds of persistence function.

AOF default case, the Redis every 2 seconds to write data to the file system, set the time on demand. If the system fails in default, it will only lose a few seconds of data. If you can afford the loss of data within a few minutes, then you can just use RDB persistence.

There are a lot of people will use the AOF alone, but we do not encourage this, since it is often very easy to be a snapshot of RDB database backups, faster than the speed start, but also to avoid the bug AOF engine.

Six common case

Cache - Because of its high performance, when read and write operations exceeds traditional database functionality, can be resolved by Redis. Since Redis data can easily be persisted to disk and rich data types, so it is an excellent alternative to traditional Memcached caching solutions.

Publish and Subscribe - Starting with version 2.0, Redis provides a publish / subscribe messaging paradigm distribution function of the data.

Queue - items such as Resque use Redis as the back end of a background job queue.

Game Chart - Redis is seeking to build a popular choice for game developers of real-time charts. It can be used directly ordered set Redis data structure that implements elements of uniqueness, while at the same maintaining a list sorted by user score. You can also use the timestamp as the score, using an ordered set of time series data.

Real-time analysis -Redis as a data storage memory, and stream processing platform (e.g. Apache Kafka) with the use of sub-millisecond delay to extract, process and analyze data in real time. Redis is an ideal choice for real-time analysis use cases, such as social media analytics, advertising, personalization.

Seven examples of Redis basic commands

Now I simply explain Redis-CLI basic commands operate, by default, Redis-CLI runs on port 6379.

SET (Settings key)

127.0.0.1:6379> SET foo "Hello World"
OK // 设置密钥

GET (get key)

127.0.0.1:6379> GET foo
"Hello World" // 获取密钥

DEL (Delete key)

127.0.0.1:6379> GET foo 
"Hello World" 
127.0.0.1:6379> DEL foo
(integer) 1 // 密钥刚被删除
127.0.0.1:6379> GET foo
(nil) // 密钥被删除,结果为nil。

SETEX (key set expiration date)

127.0.0.1:6379> SETEX foo 40 "I said, Hello World!"
OK //密钥已设置为40秒到期

The TTL (time remaining key)

127.0.0.1:6379> TTL foo
(integer) 36 //36秒超时

PERSIST (key deletion deadline)

127.0.0.1:6379> PERSIST foo
(integer) 1 //将密钥转为持久化(密钥不会过期)

The RENAME (rename the current existing)

127.0.0.1:6379> RENAME foo bar
OK // 将键'foo'重命名为 'bar'

FLUSHALL (emptied of all content so far saved)

127.0.0.1:6379> flushall
OK // 删库跑路

VIII Summary

Redis has an extremely rich data types and very high performance, reading performance rate is 110,000 times / s, write speed is 81000 times / s. Redis is not a panacea, however, limited by the physical memory, reading and writing can not be used as high-performance mass data, so Redis appropriate scene mainly confined to the small amount of data and high-performance computing operations.

Guess you like

Origin www.cnblogs.com/TFengStorm/p/10946899.html
Recommended