Original author, public number [programmer reading], welcome to pay attention to the public number, reprint the article please indicate the source oh.

There are many factors affecting server performance, such as the limitations of CPU, memory, hard disk and other hardware conditions, as well as the overall architecture of the server, the quality of the code of the programmer, the algorithm used and the quality of the data structure and other factors.

The individual feels the greatest influence on server performance, however, should be a database, the server in the face of high concurrent requests, often appears in the aspect of database performance bottlenecks, because the database carried not so high concurrency, and use the database cluster often also means higher cost of hardware and software, as a result, if the code layer and add a layer between the database cache, It can effectively relieve the pressure of database and greatly improve the performance of server.

When it comes to caching, the first thing we think of is Redis. Maybe this is the first impression Redis gives us. Although this understanding of Redis is not completely correct, because Redis can do more than this.

What is a Redis

Redis is an open source memory data structure storage system written by Salvatore Sanfilippo in ANSI C in 2009. The author of Redis developed Redis largely because the database could not meet his needs.

Of course, we can think of Redis simply as an in-memory database (NoSQL) or key-value server.

Redis provides a variety of data structures and different programming languages of the client API, in addition, also provides persistence, master-slave replication, transactions and other functions and features, just like a Swiss Army knife, is a sharp tool in server development.

While Redis is very powerful, the code is very simple, with 23,000 lines in the original version and over 50,000 lines in a recent version, so anyone interested should take a look at the Redis source code.

The characteristics of Redis

Here is a list of Redis features, let us have a more comprehensive understanding of Redis, in fact, from these features, also can see the power of Redis.

Speed is fast

The main reason why Redis is so fast is that all Redis operations on data are performed in memory. As you can see from the memory hierarchy below, memory reads and writes are much faster than hard disks (whether solid or mechanical), so Redis is naturally very fast.

persistence

We said that Redis stores data in memory, but Redis also provides a persistence mechanism, so that users can store data in memory, periodic persistence to disk and other storage devices, to ensure that when the server restarts, data will not disappear, Redis supports RDB and AOF two persistence mechanisms, there is a chance, We’ll talk about the differences between these two mechanisms in a future article.

Multiple data types

Redis supports multiple types of data structures. This is one of the differences between Redis and Memcached. Memcached only supports String, while Redis supports String, Hash, List, Set, and Sort Set. There are also advanced data types such as BitMaps, HyperLogLog and GEO.

Supports multiple programming language clients

Redis provides client apis for most programming languages, some official and some contributed by the open source community, so it’s easy to connect to Redis no matter what language you’re developing your server application in.

Below is the Redis website for a list of all programming language apis

# list of clients supported by Redis
https://redis.io/clients
Copy the code

Release subscription

Redis supports subscriptions and publishing, so you can use Redis to implement a simple message queue, with one end serving as a publisher to publish messages to Redis and the other as a consumer to process messages from Redis.

The Lua script

Redis in V2.6U added scripting functionality, allowing clients to use The Lua language to write scripts to Redis execution, the benefits of this is: reduce network overhead atomic operations and script reuse, about how to use Lua script in Redis, free again.

The transaction

Redis supports transactions. When we need to execute multiple commands, we can ensure the atomicity of multiple command execution, because in a transaction, the execution of multiple commands, either successful or failed. Of course, to execute multiple commands, using Lua script may be more convenient.

A master-slave replication

Redis supports master-slave replication, that is, data on the master server can be synchronized to multiple slave servers. Therefore, master-slave replication actually provides the basis for high availability and distribution of Redis.

Highly available distribution

Redis also supports high availability and distribution. Actual master-slave replication is only the foundation of actual high availability, but it is not possible to achieve high availability through master-slave replication. The high availability of Redis is achieved through Redis Sentinel(Sentinel), a feature added in V2.8. Distributed requires Redis Cluster support, Redis_cluster was added after V3.0.

Single thread

Redis is single-threaded, single thread here, refers to the Redis as a server in the receiving client network request is made of a single thread, so at the same time, Redis cannot handle two requests at the same time, there is only one request to deal with the next request processing is complete, but Redis in achieving a certain function, or use the multithreading, Persisting data to hardware, for example, is done by starting another thread.

Install and start Redis

The installation of Redis is very simple, here are two installation methods, one is to download the source code from the official Redis website, and then compile and install, the other is more convenient, directly pull the Docker image of Redis from the Docker Hub.

The following describes the process of installing Redis on a Linux system.

Compile and install using source code

Download the redis installation packageWget HTTP: / / http://download.redis.io/releases/redis-5.0.5.tar.gz# decompressionThe tar - ZXVF redis - 5.0.5. Tar. GzEnter the redis source directory
cdRedis - 5.0.5# compile & install
make && make install
Copy the code

Install and start Redis using the Docker image

Redis provides Docker images, so we can easily pull and run Redis images, but only if you have Docker installed on your server.

Pull official image from Docker Hub,redis:latest
docker pull redis

Start a container from the image
docker run --name my_redis -p 6379:6379 -d redis
Copy the code

It should be noted that the authors of Redis did not develop a Windows version of Redis. Perhaps the authors of Redis felt that Windows servers were not suitable for running Redis, or for some reason, but Microsoft provided a Windows version of Redis anyway.

# Redis for Windows
https://github.com/microsoftarchive/redis/releases
Copy the code

Several ways to start Redis

Start the server with default Settings without parameters

Start with the default configuration
redis-server
Copy the code

Specify command line arguments to start the server

Use command parameters to configure startup
redis-server --port 6380
Copy the code

Start the server using the configuration file

Start with configuration fileRedis server ~ / redis - 5.0.5 / redis. ConfCopy the code

Connect the Redis

After starting the Redis server using the methods described above, you can use the Redis client to connect to the server and write or read data to the server. As mentioned earlier, Redis supports most programming languages, so you can choose a different API depending on your situation.

Use the command line client to connect to Redis

After Redis is installed, you can use Redis -cli, a command line client, as shown in the following example:

# connect to local server with no parameters
redis-cli

# specify the address and port numberRedis -cli -h Server address -p port numberCopy the code

Connect to the Redis server using code

This demo uses golang’s developed Redigo/Redis package to connect to the Redis server.

Get a redis client library for go
go get github.com/gomodule/redigo/redis
Copy the code

Here is a simple code example connecting to Redis using the Redigo/Redis library:

package main

import (
    "fmt"
    "github.com/gomodule/redigo/redis"
)

var redisCon redis.Conn

func main() {
    var err error
    redisCon ,err = redis.Dial("tcp"."localhost:6379")
    
    iferr ! = nil{ panic(err) } defer redisCon.Close() rs,err := redisCon.Do("set"."test_key"."test_value")
    
    iferr ! = nil{ panic(err) } fmt.Println(rs) }Copy the code

While the above code may be relatively simple, we have a chance to see in detail how Golang can be used to better connect and manipulate Redis.

Application scenarios

Here are some Redis usage scenarios, you can also use Redis flexibly according to your needs to achieve the purpose of improving server performance.

Caching system

Redis is most commonly used for data cache. Due to its fast memory-based data read and write operation, the response speed is very fast. It can be used to cache some hot data or data that does not change much. Improves server data throughput and speed.

list

Redis sort set data type is very suitable for ranking line functions, such as [Denver] [recommended author list] in fact, the principle of implementation should be to calculate the author ranking weight in the Redis sort set to achieve, so Redis in the implementation of this kind of function is really very convenient and simple.

Implementing community functions

Redis and some community has a natural fit, use Redis, supported by the data structure can be easily stored community functions such as attention in number, number of fans, pay attention to the list, the fans list, to note such as data, such as attention between the fan and can be used to counter, pay attention to the list and fans can use seto types to store list.

counter

Redis built-in incr command can be easily under the single thread count, and the operation is atomic, don’t have to worry about a time when there are two requests to the count of the same key value, the counter function is very practical in some scenarios, such as the Denver nuggets, read the number, the number of comments, the thumb up can use Redis counting function to achieve, If this data were written directly to the database, every time someone liked it, read it, or commented on it, the database would have to be operated on, which would probably cause the database to hang. Using Redis counting, this could be done quickly and easily.

The message queue

Redis supports publish and subscribe, so we can think of Redis as a simple message queue. When we need to use message queue, we can consider using Redis. Of course, there are many mature message queue middleware systems, such as Kafka, RabbitMQ, RocketMQ, ActiveMQ, and then using these may require additional hardware and software costs and learning costs, so Redis message team function can be used instead.

Real-time systems

For some data that need to be processed in real time, it can also be directly written into Redis, and then processed by other systems. This is also the use of Redis publishing and subscription function to achieve the purpose of real-time data processing, such as some garbage mailbox processing systems.

summary

In this article, we briefly talk about what Redis is, what you can do, and understand its powerful features and functions, by demonstrating how to install, start, and connect the Redis, also let us know how to use Redis, of course, want to better understand and use Redis, still need further study, there are opportunities, We continue to learn about it.


Your attention is the biggest encouragement on my writing road!