Technology Sharing

NoSQL REDIS configuration and optimization

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Introduction to Redis

Redis (Remote Dictionary Server) is an open source NoSQL database written in C language. It runs on memory and supports persistence, using key-value storage. Redis is widely used in caching, real-time analysis systems, rankings and other scenarios due to its high performance, rich data type support and atomic operations.
The Redis server program is a single-process model, which means that multiple Redis processes can be started on a server at the same time, and the actual processing speed of Redis depends entirely on the execution efficiency of the main process. If only one Redis process is run on the server, when multiple clients access it at the same time, the processing capacity of the server will decrease to a certain extent; if multiple Redis processes are started on the same server, Redis will put a lot of pressure on the server's CPU while improving the concurrent processing capacity. That is: in the actual production environment, it is necessary to decide how many Redis processes to start based on actual needs. If the requirements for high concurrency are higher, you may consider starting multiple processes on the same server. If CPU resources are relatively tight, a single process is sufficient.

2. Installation of Redis on Linux

There are several ways to install Redis on Linux:

1. Install using a package manager

For most Linux distributions, Redis can be installed directly using the package manager. For example, on Ubuntu or Debian, you can useapt-getOrder:

bash复制代码

sudo apt-get update
sudo apt-get install redis-server

On CentOS or Red Hat, useyumOrder:

bash复制代码

sudo yum update
sudo yum install redis

2. Compile and install from source code

Another way to install is to download the source code from the official website of Redis, and then compile and install it. The steps are as follows:

  1. Install the compilation tools

    First, you need to install gcc and other compilation tools. On CentOS or Red Hat, you can useyumInstall:

    bash复制代码

    sudo yum install gcc-c++ make
  2. Download Redis source code

    Download the latest compressed package (such as redis-6.xxtar.gz) from the Redis official website.

  3. Compile and install

    Unzip the downloaded compressed package, enter the unzipped directory, and execute the compilation and installation commands:

    bash复制代码

    tar zxvf redis-6.x.x.tar.gz
    cd redis-6.x.x
    make && make PREFIX=/usr/local/redis install
  4. Configure Redis Service

    You can use the Redisinstall_server.shScript to set up the Redis service, or configure it manually.

3. Redis configuration and optimization

1. Configuration File

Redis configuration files are usuallyredis.conf, you can configure various parameters of Redis by modifying this file.

  • Binding Address: By default, Redis only accepts connections from the local interface. If you want Redis to accept connections from other hosts, you need to modifybindoption or comment out the line.
  • Protected Mode: Redis runs in protected mode by default and only accepts connections from 127.0.0.1. Turning off protected mode allows remote connections.
  • Password authentication: To improve security, you can set a password for Redis.
  • Persistence: Redis supports two persistence mechanisms: RDB and AOF. You can set relevant options in the configuration file to enable persistence.
  • Maximum memory limit: You can set the maximum memory usage of Redis and the elimination strategy when the maximum memory is reached.

2. Performance Optimization

Redis performance optimization mainly includes memory allocation control, swappiness setting, network connection optimization and other aspects.

  • Memory allocation control

    Linux operating systemvm.overcommit_memoryThe parameter controls the memory allocation strategy. Redis recommends setting it to 1 so that fork operations can be performed successfully even in low memory situations.

    bash复制代码

    echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
    sysctl vm.overcommit_memory=1
  • swappiness setting

    The swappiness parameter determines the operating system's tendency to use swap. For applications that require high concurrency and high throughput, it is recommended to set swappiness to a lower value to reduce the possibility of disk IO becoming a system bottleneck.

    bash复制代码

    echo "vm.swappiness=10" >> /etc/sysctl.conf
    sysctl vm.swappiness=10
  • Network connection optimization

    Ensure stable network connection and reduce network delay and packet loss. At the same time, properly configure Redis TCP parameters, such astcp-keepaliveetc. to improve the stability and reliability of network connections.

3. Monitoring and debugging

  • Check Redis status

    can useredis-cliCommand to connect to the Redis server and executeINFOcommand to view the current status of Redis, including memory usage, persistence status, number of connections, etc.

  • Viewing log files

    Redis log files are usually located in/var/log/redis/redis-server.log, you can get error information and running status by viewing the log file.