> 技术文档 > 【零基础掌握Redis】第二弹——天工特性赋

【零基础掌握Redis】第二弹——天工特性赋

 

目录

一、前言

二、正文

1.Redis特性介绍

1.1 In-memory data structures

1.2 Programmaability

1.3. Extensibility

1.4 Persistence

1.5 Clustering

1.6 High avaliablity

2.Redis的安装

● Ubuntu 22.04

● Centos8

3.Redis为什么快(常考面试题)

三、结语


一、前言

        本文会为大家打来零基础掌握Redis第二弹的学习,在本篇文章中会为大家讲解Redis的特性,安装以及一道常考Redis面试题,希望读者们能够从中有所收获!!!

二、正文

1.Redis特性介绍

    通过Redis第一弹的介绍,相信小伙伴们已经认识到Redis是一个在内存中存储数据的中间件,可以用于作为数据库,也可以用于作为数据缓存,往往使用在分布式系统,下面我们就来介绍Redis的一些特性(优点)

1.1 In-memory data structures

        我们都知道MySQL是通过“表”的方式来存储组织数据的,是一种关系型数据库。

        但是Redis主要是通过“键值对”的方式来存储组织数据的,是一种非关系型数据库。而这种键值对存储,即key-value式,key的类型都是string,value的类型有很多,可以是strings,hashes,lists,sets等

注:Redis中的strings,hashes等和C++中的字符串,哈希表虽然名称类似,但是细节上还是有一些略微差异的,会在后面的文章中具体介绍到

1.2 Programmaability

Server-side scripting with rua and server-side stored procedures with Redis Functions

针对Redis的操作,可以直接通过简单的交互式命令进行操作,也可以通过一些脚本的方式,批量执行一些带有逻辑的操作

1.3. Extensibility

A module API for building custom extensions to Redis in C ,C++, and Rust

Redis具有拓展性,可以在Redis原有的功能基础上再进行拓展,Redis中提供了一组API

可以通过C,C++,Rust语言来编写Redis拓展,比如,Redis自身已经提供了很多的数据结构和命令,通过拓展可以让Redis支持更多的数据结构以及支持更多的命令

1.4 Persistence

Keeps the dataset in memory for acccess but can also persist all writes to permanent storage to survive reboots and failures

Redis把数据存储在内存上,但是,我们都知道内存的数据时“易失”的,当进程退出或者是系统重启的时候。而Redis的持久化,会把数据存储在硬盘上,即内存为主,硬盘为辅。如果Redis重启了,就会在重启时加载硬盘中的备份数据,使Redis的内存恢复到重启前的状态

1.5 Clustering

Horizonta scalability wish hash-based sharding,scaling to millions of nodes with automatic re-partition when growing the cluster

Redis支持集群,其水平拓展类似于“分库分表”。

一个Redis能存储的数据时有限的(内存空间有限),但是我们引入多个主机,部署多个Redis节点,每个Redis存储数据的一部分,就能存储大量的数据

1.6 High avaliablity

Redis的高可用性支持“主从”结构,从节点相当于主节点的备份,一旦主节点出现问题后,还有从节点中的数据来进行访问和使用

2.Redis的安装

● Ubuntu 22.04

①  使用apt安装

apt install redis -y

②支持远程连接

当我们执行上面指令后,redis就安装好了,但是Redis默认的配置文件中是支持本地连接,的,但是如果小伙伴想要其他的主机也能够使用Redis服务,就需要更改配置来支持远程连接

修改 /etc/redis/redis.conf

● 修改bind 127.0.0.1 为 bind 0.0.0.0 

● 修改 protected-mode yes 为 protected-mode no

57 # 58 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the 59 # internet, binding to all the interfaces is dangerous and will expose the 60 # instance to everybody on the internet. So by default we uncomment the 61 # following bind directive, that will force Redis to listen only on the 62 # IPv4 loopback interface address (this means Redis will only be able to 63 # accept client connections from the same host that it is running on). 64 # 65 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES 66 # JUST COMMENT OUT THE FOLLOWING LINE. 67 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 68 bind 0.0.0.0 ::1 69  70 # Protected mode is a layer of security protection, in order to avoid that 71 # Redis instances left open on the internet are accessed and exploited. 72 # 73 # When protected mode is on and if: 74 # 75 # 1) The server is not binding explicitly to a set of addresses using the 76 # \"bind\" directive. 77 # 2) No password is configured. 78 # 79 # The server only accepts connections from clients connecting from the 80 # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain 81 # sockets. 82 # 83 # By default protected mode is enabled. You should disable it only if 84 # you are sure you want clients from other hosts to connect to Redis 85 # even if no authentication is configured, nor a specific set of interfaces 86 # are explicitly listed using the \"bind\" directive. 87 protected-mode no

配置完以后,我们还需要重启Redis服务来让配置生效,下面介绍一下控制Redis启动的指令

③控制Redis启动的指令

启动Redis服务

service redis-server start

停止Redis服务

service redis-server stop

重启Redis服务

service redis-server restart

● Centos8

①使用yum安装

yum install -y redis

②通过systemd管理Redis

⼀旦安装完成,我们可以将redis设置为开机⾃动启动:

Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

③支持远程连接

修改Redis的配置⽂件:/etc/redis.conf

● 定位到bind127.0.0.1开头的⼀⾏,修改为bind 0.0.0.0 以添加全接⼝⽀持:

● 关闭保护模式, protected-mode no

④通过 systemd 控制Redis

启动Redis服务

systemctl start redis

停止Redis服务

systemctl stop redis

重启Redis服务

systemctl restart redis

3.Redis为什么快(常考面试题)

1. Redis数据在内存中,就比访问硬盘的数据库,要快很多~~

2.Redis核心功能都是比较简单的逻辑,都是对比较键槽的操作内存的数据结构

3.从网络角度,Redos使用了IO多路复用的方式(epoll),即使用一个线程就能够管理多个socket

4.Redis使用的是单线程模型,减少了不必要的线程之间的竞争开销

三、结语

        到此为止,本文关于【从零掌握Redis】第二弹——天工特性赋 的内容到此结束了,如有不足之处,欢迎小伙伴们指出呀!

         关注我 _麦麦_分享更多干货:_麦麦_-CSDN博客

         大家的「关注❤️ + 点赞👍 + 收藏⭐」就是我创作的最大动力!谢谢大家的支持,我们下期见!!