Go Redis Quick Start

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

This article transferred from: http://www.yiibai.com/redis/redis_quick_guide.html

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:

    1. $sudo apt-get Update
    2. $sudo Apt-get Install Redis-server
Copy Code

This will install Redis on your computer.

Start Redis

    1. $redis-server
Copy Code


Check to see if Redis is working?

    1. $redis-CLI
Copy Code


This will open a redis hint, as shown in:

    1. Redis 127.0.0.1:6379>
Copy Code


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.

    1. Redis 127.0.0.1:6379> Ping
Copy Code


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

    1. Redis 127.0.0.1:6379> SET name "Yiibai"
    2. Ok
    3. Redis 127.0.0.1:6379> GET Name
    4. "Yiibai"
Copy Code




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

    1. Redis 127.0.0.1:6379> hmset user:1 username Yiibai password Yiibai points 200
    2. Ok
    3. Redis 127.0.0.1:6379> Hgetall user:1
    4. 1) "username"
    5. 2) "Yiibai"
    6. 3) "Password"
    7. 4) "Yiibai"
    8. 5) "Points"
    9. 6) "200"
Copy Code

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

    1. Redis 127.0.0.1:6379> Lpush tutoriallist Redis
    2. (integer) 1
    3. Redis 127.0.0.1:6379> Lpush tutoriallist MongoDB
    4. (integer) 2
    5. Redis 127.0.0.1:6379> Lpush tutoriallist RABITMQ
    6. (integer) 3
    7. Redis 127.0.0.1:6379> lrange tutoriallist 0 10
    8. 1) "RABITMQ"
    9. 2) "MongoDB"
    10. 3) "Redis"
Copy Code



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

    1. Redis 127.0.0.1:6379> Sadd tutoriallist Redis
    2. (integer) 1
    3. Redis 127.0.0.1:6379> Sadd tutoriallist MongoDB
    4. (integer) 1
    5. Redis 127.0.0.1:6379> Sadd tutoriallist RABITMQ
    6. (integer) 1
    7. Redis 127.0.0.1:6379> Sadd tutoriallist RABITMQ
    8. (integer) 0
    9. Redis 127.0.0.1:6379> smembers tutoriallist
    10. 1) "RABITMQ"
    11. 2) "MongoDB"
    12. 3) "Redis"
Copy Code

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

    1. Redis 127.0.0.1:6379> zadd tutoriallist 0 Redis
    2. (integer) 1
    3. Redis 127.0.0.1:6379> zadd tutoriallist 0 MongoDB
    4. (integer) 1
    5. Redis 127.0.0.1:6379> zadd tutoriallist 0 RABITMQ
    6. (integer) 1
    7. Redis 127.0.0.1:6379> zadd tutoriallist 0 RABITMQ
    8. (integer) 0
    9. Redis 127.0.0.1:6379> zrangebyscore tutoriallist 0 1000
    10. 1) "Redis"
    11. 2) "MongoDB"
    12. 3) "RABITMQ"
Copy Code



Redis-keys

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

Grammar

    1. Redis 127.0.0.1:6379> COMMAND Key_name
Copy Code



Example

    1. Redis 127.0.0.1:6379> SET Yiibai Redis
    2. Ok
    3. Redis 127.0.0.1:6379> DEL Yiibai
    4. (integer) 1
Copy Code



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

    1. Redis 127.0.0.1:6379> COMMAND Key_name
Copy Code



Example

    1. Redis 127.0.0.1:6379> SET Yiibai Redis
    2. Ok
    3. Redis 127.0.0.1:6379> GET Yiibai
    4. "Redis"
Copy Code

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

    1. Redis 127.0.0.1:6379> hmset yiibai name "Redis Tutorial" description "Redis basic commands for caching" likes Visito RS 23000
    2. Ok
    3. Redis 127.0.0.1:6379> Hgetall Yiibai
    4. 1) "Name"
    5. 2) "Redis Tutorial"
    6. 3) "description"
    7. 4) "Redis Basic commands for caching"
    8. 5) "Likes"
    9. 6) "20"
    10. 7) "Visitors"
    11. 8) "23000"
Copy Code

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

    1. Redis 127.0.0.1:6379> Lpush Tutorials Redis
    2. (integer) 1
    3. Redis 127.0.0.1:6379> Lpush Tutorials MongoDB
    4. (integer) 2
    5. Redis 127.0.0.1:6379> Lpush Tutorials MySQL
    6. (integer) 3
    7. Redis 127.0.0.1:6379> Lrange Tutorials 0 10
    8. 1) "MySQL"
    9. 2) "MongoDB"
    10. 3) "Redis"
Copy Code

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

    1. Redis 127.0.0.1:6379> Sadd Tutorials Redis
    2. (integer) 1
    3. Redis 127.0.0.1:6379> Sadd Tutorials MongoDB
    4. (integer) 1
    5. Redis 127.0.0.1:6379> sadd Tutorials MySQL
    6. (integer) 1
    7. Redis 127.0.0.1:6379> sadd Tutorials MySQL
    8. (integer) 0
    9. Redis 127.0.0.1:6379> Smembers Tutorials
    10. 1) "MySQL"
    11. 2) "MongoDB"
    12. 3) "Redis"
Copy Code

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

    1. Redis 127.0.0.1:6379> Zadd Tutorials 1 Redis
    2. (integer) 1
    3. Redis 127.0.0.1:6379> Zadd Tutorials 2 MongoDB
    4. (integer) 1
    5. Redis 127.0.0.1:6379> zadd Tutorials 3 MySQL
    6. (integer) 1
    7. Redis 127.0.0.1:6379> zadd Tutorials 3 MySQL
    8. (integer) 0
    9. Redis 127.0.0.1:6379> Zadd Tutorials 4 MySQL
    10. (integer) 0
    11. Redis 127.0.0.1:6379> Zrange Tutorials 0 withscores
    12. 1) "Redis"
    13. 2) "1"
    14. 3) "MongoDB"
    15. 4) "2"
    16. 5) "MySQL"
    17. 6) "4"
Copy Code

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:

    1. Redis 127.0.0.1:6379> Pfadd Tutorials "Redis"
    2. 1) (integer) 1
    3. Redis 127.0.0.1:6379> Pfadd Tutorials "MongoDB"
    4. 1) (integer) 1
    5. Redis 127.0.0.1:6379> pfadd Tutorials "MySQL"
    6. 1) (integer) 1
    7. Redis 127.0.0.1:6379> Pfcount Tutorials
    8. (integer) 3
Copy Code



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

    1. Redis 127.0.0.1:6379> SUBSCRIBE Redischat
    2. Reading messages ... (Press Ctrl-c to quit)
    3. 1) "Subscribe"
    4. 2) "Redischat"
    5. 3) (integer) 1
Copy Code

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

    1. Redis 127.0.0.1:6379> PUBLISH redischat "Redis is a great caching technique"
    2. (integer) 1
    3. Redis 127.0.0.1:6379> PUBLISH redischat "Learn Redis by Tutorials Point"
    4. (integer) 1
    5. 1) "Message"
    6. 2) "Redischat"
    7. 3) "Redis is a great caching technique"
    8. 1) "Message"
    9. 2) "Redischat"
    10. 3) "Learn Redis by Tutorials Point"
Copy Code



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.

    1. Redis 127.0.0.1:6379> MULTI
    2. Ok
    3. List of commands here
    4. Redis 127.0.0.1:6379> EXEC
Copy Code



Example

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

    1. Redis 127.0.0.1:6379> MULTI
    2. Ok
    3. Redis 127.0.0.1:6379> SET Tutorial Redis
    4. QUEUED
    5. Redis 127.0.0.1:6379> GET Tutorial
    6. QUEUED
    7. Redis 127.0.0.1:6379> INCR Visitors
    8. QUEUED
    9. Redis 127.0.0.1:6379> EXEC
    10. 1) OK
    11. 2) "Redis"
    12. 3) (integer) 1
Copy Code



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:

    1. Redis 127.0.0.1:6379> EVAL script Numkeys key [key ...] arg [arg ...]
Copy Code



Example

Here's an example of how Redis scripts work:

    1. Redis 127.0.0.1:6379> EVAL "return {keys[1],keys[2],argv[1],argv[2]}" 2 Key1 Key2 First Second
    2. 1) "Key1"
    3. 2) "Key2"
    4. 3) "First"
    5. 4) "Second"
Copy Code



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.

    1. Redis 127.0.0.1:6379> AUTH "Password"
    2. Ok
    3. Redis 127.0.0.1:6379> PING
    4. PONG
Copy Code



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:

    1. 127.0.0.1:6379> SAVE
Copy Code



Example

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

    1. 127.0.0.1:6379> SAVE
    2. Ok
Copy Code

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:

    1. 127.0.0.1:6379> CONFIG Get dir
    2. 1) "Dir"
    3. 2) "/user/yiibai/redis-2.8.13/src"
Copy Code

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

    1. 127.0.0.1:6379> BGSAVE
    2. Background Saving started
Copy Code



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.

    1. 127.0.0.1:6379> CONFIG Get Requirepass
    2. 1) "Requirepass"
    3. 2) ""
Copy Code

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

    1. 127.0.0.1:6379> CONFIG Set Requirepass "Yiibai"
    2. Ok
    3. 127.0.0.1:6379> CONFIG Get Requirepass
    4. 1) "Requirepass"
    5. 2) "Yiibai"
Copy Code

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:

    1. 127.0.0.1:6379> AUTH Password
Copy Code



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:

    1. Redis-benchmark [option] [option value]
Copy Code



Example

The example given below examines the Redis call 100000 command.

    1. Redis-benchmark-n 100000
    2. ping_inline:141043.72 Requests per second
    3. ping_bulk:142857.14 Requests per second
    4. set:141442.72 Requests per second
    5. get:145348.83 Requests per second
    6. incr:137362.64 Requests per second
    7. lpush:145348.83 Requests per second
    8. lpop:146198.83 Requests per second
    9. sadd:146198.83 Requests per second
    10. spop:149253.73 Requests per second
    11. Lpush (needed to benchmark Lrange): 148588.42 Requests per second
    12. LRANGE_100 (first elements): 58411.21 requests per second
    13. lrange_300 (first elements): 21195.42 requests per second
    14. lrange_500 (first elements): 14539.11 requests per second
    15. lrange_600 (first elements): 10504.20 requests per second
    16. MSET (keys): 93283.58 Requests per second
Copy Code



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:

    1. Config get maxclients
    2. 1) "MaxClients"
    3. 2) "10000"
Copy Code

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.

    1. Redis-server--maxclients 100000
Copy Code



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.

    1. $ (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
    2. +pong
    3. +ok
    4. Redis
    5. : 1
    6. : 2
    7. : 3
Copy Code

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.

Go Redis Quick Start

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.