Redis Quick Start

Source: Internet
Author: User
Tags benchmark redis desktop manager install redis redis server redis tutorial



Redis is an open source, advanced key-value Storage and a perfect solution for building high-performance, scalable Web applications.



The three main features that Redis inherits from its many competitions are:


    • The Redis database is completely in memory and uses disk for persistence only.

    • Redis has a rich set of data types compared to many key-value data stores.

    • Redis can replicate data to any number of slave servers.

Redis Benefits
    • Exceptionally fast: Redis is very fast and can perform about 110,000 episodes per second, about 81000 + records per second.

    • Support for rich data types: Redis support Most developers already know like lists, collections, ordered collections, hash data types. This makes it very easy to solve a wide variety of problems because we know which issues are better than the data types that can be handled through it.

    • Operations are atomic: All Redis operations are atomic, which ensures that Redis servers accessed by two clients at the same time will get the updated values.

    • Multifunction utility: Redis is a versatile tool that can be used in multiple uses such as cache, message, queue (Redis native support publish/subscribe), any transient data, applications such as Web application sessions, Web page hits count etc.

Redis-Environment


Install Redis on Ubuntu, open terminal, and type the following command:


$sudo apt-get Update$sudo apt-get install Redis-server


This will install Redis on your computer.



Start Redis


$redis-server


Check to see if Redis is working?


$redis-CLI


This will open a redis hint, as shown in:


Redis 127.0.0.1:6379>


The above tip 127.0.0.1 is the IP address of this machine, and 6379 is the port that the Redis server is running on. Now enter the ping command as shown in.


Redis 127.0.0.1:6379> PingPONG


This means that you have successfully installed Redis on your machine.


Installing Redis Desktop Manager on Ubuntu


To install Redis Desktop Manager on Ubuntu, simply open the download package from Http://redisdesktop.com/download and install it.



Redis Desktop Manager gives you the user interface to manage Redis keys and data.


Redis-Data type


Redis supports 5 types of data types, which are described in the following ways:


String


A Redis string is a sequence of bytes. Redis strings are binary safe, which means they have a known length without any special characters terminating, so you can store anything, 512 megabytes for the upper limit.


Example
 
 
 
redis 127.0.0.1:6379> SET name "yiibai"
OK
redis 127.0.0.1:6379> GET name
"yiibai"


Above is an example of Redis's set and get commands, a redis name called Yiibai used by a key stored in a Redis string value.


Hash


A Redis hash is a collection of key-value pairs. A Redis hash value is a mapping between a string field and a string value, so they are used to represent the object


Example
 
redis 127.0.0.1:6379> HMSET user:1 username yiibai password yiibai points 200
OK
redis 127.0.0.1:6379> HGETALL user:1

1) "username"
2) "yiibai"
3) "password"
4) "yiibai"
5) "points"
6) "200"


In the example above, the hash data type is used to store the user's object that contains the user's basic information. Here the Hmset,hegtall user command user:1 is the key.


List


The Redis list is a simple list of strings, sorted in order of insertion. You can add elements to the head or tail of the Redis list.


Example
 
 
redis 127.0.0.1:6379> lpush tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> lpush tutoriallist mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutoriallist rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutoriallist 0 10

1) "rabitmq"
2) "mongodb"
3) "redis"


The maximum length of the list is 232-1 elements (4294967295, which can hold more than 4 billion elements in each list).


Collection


A collection of Redis is an unordered collection of strings. In Redis you can add, delete, and test whether a file exists, in member O (1) time complexity.


Example
 
redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist

1) "rabitmq"
2) "mongodb"
3) "redis"


Note: In the example above, the RABITMQ collection is added two times, but because the collection element has a unique property.



The maximum number of elements in the collection is 232-1 (4294967295, which can hold more than 4 billion elements).


Ordered collection


An ordered collection of Redis is similar to a collection of Redis, a collection of strings that are not duplicates. The difference is that each member of an ordered set uses fractions in order to take an ordered set command, from the smallest to the largest member score. Although members are unique, the scores may be duplicated.


Example
 
redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000

1) "redis"
2) "mongodb"
3) "rabitmq"
Redis-keys


The Redis keys command is used in the Redis admin key. The Redis Keys command uses the following syntax:


Grammar
Redis 127.0.0.1:6379> COMMAND Key_name
Example
Redis 127.0.0.1:6379> SET yiibai redisOKredis 127.0.0.1:6379> DEL yiibai(integer) 1  


In the above example del is the command, and Yiibai is key. If the key is deleted, then the output of the command will be (integer) 1, otherwise it will be (integer) 0


Redis-strings


The Redis strings command is used to manage string values in Redis. The use syntax for the Redis Strings command is as follows:


Grammar
Redis 127.0.0.1:6379> COMMAND Key_name
Example
Redis 127.0.0.1:6379> SET yiibai redisokredis 127.0.0.1:6379> GET yiibai"Redis"  


In the example above, set and get are commands, while Yiibai is key.


Redis-Hash


A Redis hash is a mapping between a string field and a string value, so they are the perfect data type to represent the object



In the Redis hash value, up to 400 billion field-value pairs can be stored.


Example
 
redis 127.0.0.1:6379> HMSET yiibai name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000
OK
redis 127.0.0.1:6379> HGETALL yiibai

1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"


In the example above, the Redis collection named Yiibai in the hash name is tutorials (name, description, likes, visitors)


Redis-List


The Redis list is a simple list of strings, sorted in order of insertion. You can add Redis elements at the end of the list header or list.



The maximum length of a list is 232-1 elements (more than 4294967295 per list element).


Example
 
redis 127.0.0.1:6379> LPUSH tutorials redis
(integer) 1
redis 127.0.0.1:6379> LPUSH tutorials mongodb
(integer) 2
redis 127.0.0.1:6379> LPUSH tutorials mysql
(integer) 3
redis 127.0.0.1:6379> LRANGE tutorials 0 10

1) "mysql"
2) "mongodb"
3) "redis"


In the above example, the three values are inserted in the Redis list named Lpush Command Tutorial.


Redis-Collections


A Redis collection is an unordered collection of unique strings. The uniqueness of the collection does not allow duplicate keys for the data.



In the Redis collection, add, delete, and test whether the file exists members in O (1) (constant time regardless of the number of elements contained within the collection). The maximum length of a collection is 232-1 elements (more than 4294967295 elements per collection).


Example
 
redis 127.0.0.1:6379> SADD tutorials redis
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mongodb
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mysql
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS tutorials

1) "mysql"
2) "mongodb"
3) "redis"


In the above example, the three values are ordered Sadd to insert the Redis collection name tutorials.


Redis ordered set


An ordered collection of Redis similar to Redis's collection store is unique in setting values. The difference is that each member of an ordered set uses fractions in order to take an ordered set command, from the smallest to the largest fraction.



The ordered set of Redis adds, deletes and tests the presence of Members O (1) (fixed time, regardless of the number of elements contained within the collection). The maximum length of a list is 232-1 elements (more than 4294967295 elements per collection).


Example
 
redis 127.0.0.1:6379> ZADD tutorials 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD tutorials 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE tutorials 0 10 WITHSCORES

1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"


In the above example, the three values are ordered by Zadd to insert their scores in the Redis's ordered set named tutorials.


Redis-hyperloglog


Redis's Hyperloglog uses an algorithm that is randomized to provide an approximation of the number of unique elements using only one constant, and small size, small amount of memory.



Hyperloglog provides that even if each uses a very small amount of memory (12,000 bytes), the standard error is very similar to the cardinality of the set, with no limit on the number of entries that can be specified unless close to 264 entries.


Example


The following example shows how Redis's Hyperloglog works:


 
redis 127.0.0.1:6379> PFADD tutorials "redis"

1) (integer) 1

redis 127.0.0.1:6379> PFADD tutorials "mongodb"

1) (integer) 1

redis 127.0.0.1:6379> PFADD tutorials "mysql"

1) (integer) 1

redis 127.0.0.1:6379> PFCOUNT tutorials

(integer) 3
Redis-Subscribe


Redis subscriptions implement mail systems, senders (referred to as publishers in Redis terminology), and receivers (users) receive them. The link sent by this message is called a channel.



The Redis client can subscribe to any number of channels.


Example


The following example shows how to publish a user's conceptual work. In the following example, a client subscription is given a channel named Redischat


 
redis 127.0.0.1:6379> SUBSCRIBE redisChat  Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "redisChat"
3) (integer) 1 


Now, two clients are published in the same named Channel Redischat message, and the above subscribed clients receive the message.


 
redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique"  (integer) 1 redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by tutorials point"  (integer) 1  1) "message"
2) "redisChat"
3) "Redis is a great caching technique"
1) "message"
2) "redisChat"
3) "Learn redis by tutorials point"
Redis-Transactions


Redis transactions let a set of commands execute in a single step. There are two properties in the transaction, described below:


    • All the commands in a transaction are executed sequentially as a single isolated operation. It is not possible to execute a request made by another client in the process of a redis transaction.

    • The Redis transaction is atomic in nature. An atom means that either all commands are executed or not executed.

Example


The Redis transaction is initiated by the instruction multiple, and then needs to be passed in the transaction, and the entire transaction is executed by executing the command EXEC command list.


Redis 127.0.0.1:6379> MULTIOKList of commands hereRedis 127.0.0.1:6379> EXEC  
Example


The following examples illustrate how Redis transactions are started and executed.


Redis 127.0.0.1:6379> MULTIOKredis 127.0.0.1:6379> SET tutorial RedisQUEUEDredis 127.0.0.1:6379 > GET tutorialQUEUEDredis 127.0.0.1:6379> INCR visitorsQUEUEDredis 127.0.0.1:6379> EXEC1 ) OK2) "Redis" 3) (integer) 1        
Redis-Scripts


Redis scripts Use the LUA interpretation script to evaluate calculations. It has built-in Redis, starting with the 2.6.0 version using the script command eval.


Grammar


The basic syntax for the eval command is as follows:


Redis 127.0.0.1:6379> EVAL script Numkeys key [key ...] arg [arg ...]
Example


Here's an example of how Redis scripts work:


Redis 127.0.0.1:6379> EVAL "return {keys[1],keys[2],argv[1],argv[2]}" 2 Key1 key2 First second1) "Key1" 2) "Key2" 3) "First" 4) "second"
Redis-Connect


The Redis connection commands are basically used to manage Server client connections to Redis.


Example


The following example shows how a customer can authenticate themselves with a Redis server and check if the server is running.


Redis 127.0.0.1:6379> AUTH "password"OKredis 127.0.0.1:6379> PINGPONG  
Redis-Backup


The Redis Save command is used to create the current Redis database backup.


Grammar


The basic syntax for the Redis Save command is as follows:


127.0.0.1:6379> SAVE
Example


The following example shows how the Redis current database creates a backup.


127.0.0.1:6379> SAVEOK


This command will create the Dump.rdb file in the Redis directory.


Restore Redis Data


To recover Redis data simply move the Redis backup file (DUMP.RDB) to the Redis directory and start the server. To get your Redis directory, use the configuration command as follows:


127.0.0.1:6379> CONFIG get dir1) "dir" 2) "/USER/YIIBAI/REDIS-2.8.13/SRC"


In the output of the above command in the/USER/YIIBAI/REDIS-2.8.13/SRC directory, install the Redis Server installation location.


Bgsave


To create a backup alternate command for Redis, Bgsave can also. This command will start the backup process and run in the background.


Example
127.0.0.1:6379> BGSAVEBackground Saving started
Redis-Security


Redis databases are more secure, so any client involved needs to be authenticated before executing the command. Client input password matching requires a password that is set in the configuration file using Redis.


Example


The following example shows the steps to ensure that your Redis instance is secure.


127.0.0.1:6379> CONFIG get requirepass1) "Requirepass" 2) ""


By default, this property is empty, which indicates that there is no password set for this instance. You can change this property by executing the following command


127.0.0.1:6379> config set requirepass "Yiibai"OK127.0.0.1:6379> CONFIG get requirepass1) " Requirepass "2)" Yiibai   "


Set the password, if the client Run command is not verified, will prompt (error) Noauth, need to pass the authentication. The error will be returned to the client. Therefore, the client needs to use Authcommand for authentication.


Grammar


The basic syntax for the AUTH command is as follows:


127.0.0.1:6379> AUTH Password
Redis-Benchmark


The Redis benchmark is a common tool that runs the Ñ command to check Redis performance.


Grammar


The basic syntax for Redis baselines is as follows:


Redis-benchmark [option] [option value]
Example


The example given below examines the Redis call 100000 command.


Redis-benchmark-n 100000ping_inline:141043.72 requests per secondping_bulk:142857.14 requests per secondset:14144  2.72 requests per secondget:145348.83 requests per secondincr:137362.64 requests per secondlpush:145348.83 requests per secondlpop:146198.83 requests per secondsadd:146198.83 requests per secondspop:149253.73 requests per SecondLPUSH (nee Ded to Benchmark Lrange): 148588.42 requests per secondlrange_100 (first elements): 58411.21 requests per Secondlrange _300 (first elements): 21195.42 requests per secondlrange_500 (first elements): 14539.11 requests per Secondlrange _600 (first elements): 10504.20 requests per secondmset (keys): 93283.58 requests per second
Redis-Client Connection


Redis accepts the configuration to listen for connections to the TCP port and the UNIX socket client, if enabled. When a new client connection is accepted, the following operations are performed:


    • Client sockets are placed in a non-blocking state because Redis uses multiplexed and nonblocking I/O operations.

    • The Tcp_nodelay option is set to ensure that we are not delayed at the time of connection.

    • When creating a readable file, Redis is able to collect the client's query as soon as the new data is available for the read socket.

Maximum number of clients


Called MaxClients on the Redis configuration (redis.conf) property, which describes the maximum number of clients that can connect to Redis. The basic syntax of the command is:


Config get maxclients1) "MaxClients" 2) "10000"


By default, this property is set to 10000 (which depends on the maximum number of file descriptor limits for the operating system), but you can change this property.


Example


In the example given below, we set the maximum number of clients to 100,000 when starting the server.


Redis-server--maxclients 100000
Redis-Pipeline transport


Redis is a TCP server and supports the request/response protocol. On the Redis one request complete the following steps:


    • The client sends a query to the server and reads from the socket, usually in a blocking manner, to the server's response.

    • The server processes the command and returns the response to the client.

Meaning of pipeline transmission


The basic implication of the pipeline is that the client can send multiple requests to the server without waiting for the reply all and finally reading the reply in a single step.


Example


To check the Redis pipeline, simply start the Redis instance and type the following command at the terminal.


$ (echo-en "ping\r\n SET tutorial redis\r\nget tutorial\r\nincr visitor\r\nincr visitor\r\nincr visitor\r\n"; Sleep 10) | NC localhost 6379+pong+okredis:1:2:3


In the above example, we must use the ping command to check the Redis connection, and after that we have set the value of the Redis string named Tutorial, and then get the value of the key and the increment of three times times the traffic. In the results, we can check that all the commands are submitted once to Redis,redis is the output of all the commands in one step.


The benefits of piping


The benefit of this technique is to greatly improve the performance of the Protocol. The connection speed of slow Internet connection from 5 times to localhost is increased to at least hundred times by pipeline.


Redis-Partitioning


Partitioning is the process of splitting data into multiple redis cases, so that each instance will contain only a subset of your keys.


Benefits of Partitioning
    • It allows a larger database to use the sum of the memory of multiple computers. If you do not partition, the amount of memory available to a single computer is limited.

    • It allows for large-scale computing power to multiple cores and multiple computers, as well as network bandwidth to multiple computers and network adapters.

Disadvantages of partitioning
    • Operations involving multiple keys are not usually supported. For example, you cannot perform intersections between two collections because they are stored in keys that are mapped to different Redis instances.

    • Redis transactions involving multiple keys cannot be used.

    • Partition granularity is key, so it is impossible to fragment a DataSet with a huge key that is a very large ordered set.

    • When partitioning, data processing is more complex, such as processing multiple rdb/aof files, so that data backups need to aggregate persistent files from multiple instances and hosts.

    • The ability to add and remove can be complex. For example, Redis's cluster support has the ability to add and remove nodes at runtime that do not support this feature, but most of the data for other systems, such as client partitions and proxies, is transparently rebalanced. But there is a technique called presharding that helps in this respect.

Types of partitions


There are two types of partitions available for Redis. Suppose we have four Redis instances R0,r1,r2,r3 and many keys on behalf of the user such as: user:1, User:2, ... Wait a minute


Range Partitioning


The range partition is implemented within the scope of the mapped object that is translated into a specific Redis instance. Suppose the user ID0 in this example? ID10000 will enter the instance R0, and the user form ID10001 to No. 20000 will enter the instance R1 and so on.


Hash partition


In this type of partition, a hash function (for example, a modulus function) is used to convert the key into a number, and then the data is stored in different instances of Redis.



Redis QuickStart


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.