No-sql's Redis

Source: Internet
Author: User
Tags cas echo message memcached message queue redis redis labs

No-SQL Redis Introduction
Redis is a key-value high-performance storage system based on memory storage, similar to memcached, but redis supports rich data structure types, and it also supports data persistence to disk.

Redis is a data structure server. It is open-source, networked,
in-memory, and stores keys with optional durability. The
development of Redis has been sponsored by Redis Labs since June
2015. Before that, it was sponsored by Pivotal Software and
by VMware. According to the monthly ranking by DB-Engines.com,
Redis is the most popular key-value database. Redis has also
been ranked the # 1 NoSQL (and # 4 database) in User Satisfaction
and Market Presence based on user reviews, the most popular
NoSQL database in containers, and the # 1 NoSQL among Top 50
Developer Tools & Services. The name Redis means REmote
DIctionary Server.
Remarks:

The NO-SQL (specifically key-value) database is actually relatively simple, and is comparable to the traditional relational database
The learning cost is very low, but the learning gain is much greater than the learning cost, so it should take some time
Learn a little between them. NO-SQL databases themselves are not difficult, their difficulty lies in clearing their application field
King, know when to use NO-SQL to get the most benefit.
Redis commonly used commands
I think the best command is

help command
Refer to redis official website: http://redis.io
Connection command
select index (change the selected database for current connection, an integer with index starting from 0.)
quit (close the connection) // Key control
auth password (authenticate to the server)
echo message (echo the given string)
ping [message] (ping the server, this command is often used to test if a connection is still alive, is a test connectivity command) // understand
Server commands
info [section] (get information and statistics about the server) // Key to master
client list (get the list of client connections) // Master
dbsize (return the number of keys in the selected database) // Master
flushall (remove all keys form all databases) // Master with caution
flushdb (remove all key from the current database) // Master with caution
role (return the role of the instance in the context of replication) // Master
save (Synchronously save the dataset to disk) // Master
slaveof host port (make the server a slave of another instance, or promote it as master) // understand
bgrewriteaof (Asynchronously rewrite the append-only file)
bgsave (Asynchronously save the database to disk)
client setname (set the current connection name)
client getname (get the current connection name)
client pause timeout (stop processing commands from clients for some time)
client reply on | off | skip (instruct the server whether to reply to commands)
command (get array of Redis command details)
command count (get total number of Redis commands)
command getkeys (Extract keys given a fullRedis command)
command info command-name [command-name ...] (get array of specifi Redis command details)
config get parameter (get the value of a configuration parameter)
config rewrite (rewrite the configuration file with the in memory configuration)
config set parameter (set a configuration parameter to the give value)
config resetstat (reset the stats returned by info)
debug object key (get debugging information about a key)
debug segfault (make the server crash)
lastsave (get the unix timestamp of the last successful save to disk)
monitor (listen for all requests received by server in real time)
shutdown [nosave | save] (Synchronously save dataset to disk and then shut down the server)
slowlog subcommand [argument] (manages the redis slow queres log)
sync (internal command used for repliation)
time (return the currect server time)
keys command
del key [key ...] (delete a key or some keys) // Key control
exists key [key ...] (determine if a key exists) // Key control
expire key seconds (set a key ‘s time to live in seconds) // focus on mastering
persist key (remove the expiration form a key) // Key control
randomkey (return a random key from the keyspace) // Key control
rename key newkey (rename a key) // Key control
keys pattern (find all keys matching the given pattern) // Key control
ttl key (get the time to live for a key) // Key control
type key (determine the type stored at key) // Key control
expireat key timestatmp (set the expiration for a key as a unix timestatmp) // Key control
sort key [by pattern] [limit offset count] // TODO to be studied (sort the elements in a list, set or sorted set) // focus on
move key db (move a key to another database) // Understand
migreate // TODO to be understood and used
object subcommand [argument [argument ...]] (inspect the internals of Redis objects)
pexpire key milliseconds (set a key ‘s time to live in milliseconds)
pexpireat key milliseconds (set the expiration for a key as a unix time specified in milliseconds)
ptll key (get the time to live for a key in milliseconds)
renamenx key newkey (rename a key, only if the new key does not exist)
restore key ttl serialized-value [replace] (create a key using the provided serialized value, previously obtained using dum)
wait numslaves timeout (wait for the Synchronous replication of all the write commands sent in the context of the current connection)
scan cursor [match pattern] [count count] (incrementally iterate the keys space)
dump key (return a serialized verison of the value stored at the specified eky)
String data type related commands
append key value (append a value to a key) // Key control
set key value [EX seconds] [PX milliseconds] [NX | XX] (set the string value of a key)
setex key seconds vlaue (set the value and expiration of a key) // Key control
setnx key value (set the value of a key, only if the key does not exist) // Key control
get key (get the value of a key) // Important grasp
getset key value (set the string value of a key and return its old value) // Master understand
mset key value [key value ...] (set multiple keys to multiple values) // Key control
msetnx key value [key value ...] (set multiple keys to multiple values, only if none of the keys exist) // Key control
mget key [key ...] (get the values of all the given keys) // Key control
strlen key (get the length of the value stored in a key) // Key control
incr key (increment the integer value of a key by one) // Key control
incr key increment (increment the integer value of a key by the given increment) // Key control
decr key (decrement the integer value of a key by one) // Key control
decrby key decrement (decrement the integer value of a key by the given decrement) // Key control
getrange key start end (get a sbustring of the string stored at a key)
setrange key offset value (overwrite part of a string at key starting at the specified offset)
incrbyfloat key increment (increment the float value of a key by the given increment)
psetex key milliseconds value (set the value and expiration in milliseconds of a key)
Summary: The main operations of the string type data structure are assignment and value operation, and the setting is invalid during assignment
time. The time complexity of most operations in redis is O (1), because redis stores data
The hash code of the key is first calculated and stored according to this code value.
Hash data structure related commands
hset key field value (set the string value of a hash field) // focus on mastering
hget key field (get the value of a hash field) // Key control
hmset key field value [field value ...] (set multiple hash fields to multiple values) // Key control
hmget key field [field ...] (get the values of all the given hash fields) // Key control
hsetnx key field value (set the value of a hash field, only if the field does not exist) // Understand
hexists key field (determine if a hash field exists) // Master understand
hkeys key (get all the fields in a hash) // Master understand
hvals key (get all the values in a hash) // Master understand
hdel key field [field ...] (delte one or moew hash fields) // Mastery
hlen key (get the number of fields in a hash)
hgetall key (get all the fields and values in a hash)
hincrby key field increment (increment the integer value of a hash field by the given increment)
hincrbyfloat key field increment (increment the float value of a hash field by the given amount)
hstrlen key field (get the length of the value of a hash field)
hscan key cursor [match pattern] [count count] (incrementally iterate hash fields and associated values) // TODO to be studied
How to understand the Hash data structure in Redis

It ’s exactly this type
    Map <String key, Map <String field, String value >>
    Map <String key, JavaBean javaBean>
    userinfo [id: 1, name: xxx, friends: 10 ...]
This data structure is used in redis, a bit like the property name and property value of the object in java to store, that is, key-value key value.
Remarks: Similar to the overall dictionary structure of HashMap <String, Object> and Python in Java
List data structure related commands
lpush key value [value ...] (prepend one or multiple values to a list) // Important
lpushx key value (prepend a value to a list, only if the list exists)
rpush key value [value ...] (append one or multiple to a list) // Important
rpushx key value [append a value to a list, only if the list exists]
lpop key (remove and get the first element in a list) // Important
rpop key (remove and get the last element in a list) // Important
rpoplpush source destination (remove the last element in a list, prepend it to another list and return it) // Important
brpoplpush source destination timeout (pop a value from a list, push it to another list and return it; or block until one is available) // Importantly, you can use this method to make a blocking queue
llen key (get the length of a list)
lrange key start stop (get a range of elements form a list) // Important
blpop key [key ...] timeout (remoce and get the first element in a list, or block until one is available)
brpop key [key ...] timeout (remove and get the last element in a list, or block until one is available)
lindex key index (get an element form a list by its index)
linsert key before | after pivot value (insert an element before or after another element in a list)
lset key index value (set the value of an element in a list by its index)
ltrim key start stop (trim a list to the specified range)
lrem key count value (remove elements from a list)
Remarks: The list data structure in redis is more like a queue structure, and it can implement the blocking queue function.
So it may be used as a simple implementation of message queue, but I personally do not recommend using redis as message queue,
You can use more standard message middleware for queue services, such as RabbitMQ.
Redis transaction related commands
multi (mark the start of a transaction block, similar to starting a transaction) // emphasis on mastering
exec (execute all commands issued after multi, similar to committing a transaction) // emphasis on mastering
discard (discard all commands issued after multi, similar to rollback transaction) // emphasis on mastering
watch key [key ...] (watch the given keys to determine executio of the multi / exec block) // Key control
unwatch (forget about all watched keys) // Key control


Regarding the watch description, since redis is single-threaded, before redis2.2, no watch function was introduced
Yes, if client_x starts a transaction, update the a variable, but the transaction has not been submitted at this time.
During this process, if client_y also performed an update operation on the variable a,
Then for clinet_x the a variable it reads has changed, but it does n’t know it by itself, just guide
The business process is wrong, so the watch function was introduced after redis2.2, which can monitor the key
Whether it has changed, if other threads have performed an update operation on this key after the transaction is started and before the transaction is submitted, then
The commit transaction will fail this time, and it will be rolled back.
Redis positioning (GEO) related commands
Remarks: redis geo function is only supported after redis3.2, please note
geoadd locationSet longitude latitude name [longitude latitude name ...] (add address coordinates to use latitude and longitude and positioning)
geopos locationSet name [name] (display the coordinates of an address)
geodist locationSet location_x location_y [unit] (calculate the distance between two points of geographic location)
georadius locationSet longitude latitude radius m | km | ft | mi [WITHCOORD] [WITHDIST] [ASC | DESC] [COUNT count] (range count)
georadiusbymemer location-set location radius m | km | ft | mi [WITHCOORD] [WITHDIST] [ASC | DESC] [COUNT count] (range checkFind)
To be added
1. The remaining set and zset data structures of redis
2. Redis configuration file
3. Redis persistence
4. Redis master-slave replication
5. Redis message publish and subscribe
6. Pooled redis connection
Application scenario

Usually social software will have similar geolocation functions for nearby people, we can get the user's geography first
The coordinates are then recorded in redis to calculate the distance between geographic coordinates. Actually don't use redis for me
They can also be achieved, we can write a method to calculate the distance between two points.
Redis watch and memcached CAS implement distributed lock
They are all to solve the distributed concurrency problem, and redis is implemented based on transactions and watch optimistic locking
Yes, memecached is implemented based on CAS (check and set).

Redis implementation steps
1. Watch the key first
2. Start the transaction
3. Perform a series of command operations
4. Submit the transaction (need to check whether the watched key has changed, if there is a change, roll back the transaction)
Redis application scenarios
1.Counter (for example: SNS likes, group purchase spike participants)

Remarks: Data that changes frequently but can be used in scenes that do not require high real-time display
2. Ranking of hotspot list data (e.g. top 10, leaderboard and other scenarios)

Note: The zset data structure is used, and it must have a score attribute to rank
3.Distribution lock based on redis atomic operation (in distributed concurrent scenarios, I use it personally)

Note: The operation of redis is an atomic operation and can be used to implement distributed locks
4. Deal with overdue items / businesses (e.g. such as special order effective time, activity effective time)

Remarks: Based on redis expire key seconds
5. Implement simple pub / sub and queue functions (I personally do not recommend using redis to implement, using a more standard AMQP is more appropriate)

Remarks: realization of list data structure based on redis
6.Cache function (this is also the function of redis to replace memcached)

Remarks: Used as a cache for relational databases (frequently accessed data, data that is not updated frequently. Usually the data is mostly displayed)
7, redis3.2 began to support, positioning function redis geo (for example: shake, people nearby)

Remarks: Its implementation is calculated based on the longitude and latitude provided by the geographic location
Reids and Memcached comparison
1. Redis supports rich data structure types (memcached only supports string type data structures)
2. Redis supports simple transaction mechanism
3. Redis supports data persistence (memcached does not support)
4. Memcached is relatively simple as a distributed cluster
5. They are all cache databases based on memory storage (redis will be based on disk for persistence)
6. It is recommended to use redis instead of memcached (for expansion)
Redis is different than other database solutions in many ways: it uses memory as main storage support and disk only for persistence, the data model is pretty unique, it is single threaded and so forth. I think that another big difference is that in order to take advantage of Redis in your production environment you don't need to switch to Redis. You can just use it in order to do new things that were not possible before, or in order to fix old problems.

Switching to Redis is of course an option, and many users are using Redis as primary database since they need features or write speed or latency or some other feature, but as you can guess switching is a big step if you have an already running application in production. Also for some other kind of applications Redis may not be the right database: for instance a Redis data set can't be bigger than available memory, so if you have some big data application and a mostly-reads access pattern, Redis is not the right pick.

to sum up
Redis is a memory-based key-value high-performance cache server that supports rich data structure classes
Type, we want to use redis in appropriate scenarios will get great benefits.
reference
1. http://redis.io/
2. https://en.wikipedia.org/wiki/Redis
3. https://www.ibm.com/developerworks/cn/java/j-javadev2-22/

No-SQL Redis


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.