一、Redis的单线程和高性能

Redis是单线程吗?

Redis的单线程主要是指Redis的网络IO和键值对读写是由一个线程来完成的,这也是Redis对外提供键值存储服务的主要流程。但Redis的其他功能,比如持久化、移步删除、集群数据同步等,其实是由额外的线程执行的。

Redis单线程为什么还能这么快?

因为他所有的数据都在内存中,所有的运算都是内存级别的运算,而且单线程避免了多线程的切换性能损耗问题。正因为Redis的单线程,所以要小心使用Redis指令,对于那些耗时的指令(比如keys),一定要谨慎使用,一不小心就可能会导致Redis卡顿。

Redis单线程如何处理那么多的并发客户端连接?

Redis的IO多路复用:redis利用epoll来实现IO多路复用,将连接信息和事件放到队列中,依次放到文件事件分派器将事件分发给事件处理器。

# 查看redis支持的最大连接数,在redis.conf文件中可修改,# maxclients 10000 2 127.0.0.1:6379>CONFIGGETmaxclients
 ##1) "maxclients"
 ##2) "10000"

其他高级命令

keys:全量遍历键,用来列出所有满足特定正则字符串规则的key,当redis数据量比较大时,性能比较差,要避免使用。

scan:渐进式遍历键

SCAN cursor [MATCH pattern] [COUNT count]

scan参数提供了三个参数,第一个是cursor整数指(hash桶的索引值),第二个是key的正则模式,第三个是一次遍历key的数量(参考值,底层遍历的数量不一定),并不是符合条件的结果数量。第一次遍历时,cursor值位0,如何将返回结果中的第一个整数值作为下一个遍历的cursor。一直遍历到返回的cursor值为0时结束。

注意:但是scan并非完美无瑕,如果在scan的过程中如果有键的变化(增加、删除、修改),那么遍历效果可能会碰到如下问题:新增的键可能没有遍历到,遍历出了重复的键等情况,也就是说scan并不难保证完整的遍历出来所有的键,这些事我们在开发时需要考虑的。

info:查看redis服务运行信息,分为9大块,每个块都有非常多的参数,这9个块分别是:

Server 服务器运行的环境参数

Clients 客户端相关信息

Memory 服务器运行内存统计数据

Persistence 持久化信息

Stats 通用审计数据

Replication 主从复制相关信息

CPU CPU使用情况

Cluster 集群信息

KeySpace 键值对统计数量信息

> info
# Server
redis_version:5.0.5
redis_git_sha1:166174bc
redis_git_dirty:1
redis_build_id:b7513c4d56e56404
redis_mode:standalone
os:Linux  
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:0.0.0
process_id:126247
run_id:e2b16df313e8af397fa51758045cee7d2b65b44e
tcp_port:6379
uptime_in_seconds:26025536
uptime_in_days:301
hz:10
configured_hz:10
lru_clock:323345
executable:
config_file:
support_ptod:1

# Clients
connected_clients:3 # 正在连接的客户端数量
client_recent_max_input_buffer:4
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:40433600 #Redis分配的内存总量(byte),包含redis进程内部的开销和数据占用的内 存
used_memory_human:38.56M #Redis分配的内存总量(Kb,human会展示出单位)
used_memory_rss:41226240
used_memory_rss_human:39.32M # 向操作系统申请的内存大小(Mb)(这个值一般是大于used_memo
y的,因为Redis的内存分配策略会产生内存碎片)
used_memory_peak:77650384 #redis的内存消耗峰值(byte)
used_memory_peak_human:74.05M #redis的内存消耗峰值(KB)
used_memory_peak_perc:52.07%
used_memory_overhead:38694630
used_memory_startup:5002280
used_memory_dataset:1738970
used_memory_dataset_perc:4.91%
allocator_allocated:40682688
allocator_active:41201664
allocator_resident:51437568
used_memory_lua:39936
used_memory_lua_human:39.00K
used_memory_scripts:880
used_memory_scripts_human:880B
number_of_cached_scripts:2
maxmemory:268435456 # 配置中设置的最大可使用内存值(byte),默认0,不限制
maxmemory_human:256.00M # 配置中设置的最大可使用内存值
maxmemory_policy:volatile-lru # 当达到maxmemory时的淘汰策略
allocator_frag_ratio:1.01
allocator_frag_bytes:518976
allocator_rss_ratio:1.25
allocator_rss_bytes:10235904
rss_overhead_ratio:0.80
rss_overhead_bytes:-10211328
mem_fragmentation_ratio:1.02
mem_fragmentation_bytes:793680
mem_not_counted_for_evict:1350
mem_replication_backlog:33554432
mem_clients_slaves:17042
mem_clients_normal:117982
mem_aof_buffer:1350
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
oom_err_count:0

# Stats
total_connections_received:43041166
total_commands_processed:233044593
instantaneous_ops_per_sec:6 # 每秒执行多少次指令
instantaneous_write_ops_per_sec:0
instantaneous_read_ops_per_sec:0
instantaneous_other_ops_per_sec:6
total_net_input_bytes:8267065214
total_net_output_bytes:356557207062
instantaneous_input_kbps:0.25
instantaneous_output_kbps:8.54
rejected_connections:0
rejected_connections_status:0
sync_full:0
sync_partial_ok:1
sync_partial_err:0
expired_keys:11
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
evicted_keys_per_sec:0
keyspace_hits:14387
keyspace_misses:44
hits_per_sec:0
misses_per_sec:0
hit_rate_percentage:0.00
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:503
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
traffic_control_input:0
traffic_control_input_status:0
traffic_control_output:0
traffic_control_output_status:0
stat_avg_rt:16
stat_max_rt:118
pacluster_migrate_sum_rt:0
pacluster_migrate_max_rt:0
pacluster_migrate_qps:0
pacluster_import_sum_rt:0
pacluster_import_max_rt:0
pacluster_import_qps:0
pacluster_migrate_start_time:0
pacluster_importing_start_time:0
slot_psync_ok:0
slot_psync_err:0

# Replication
role:master

# CPU
used_cpu_sys:22283.150641
used_cpu_user:50403.064312
used_cpu_sys_children:0.003128
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0
databases:256
nodecount:1

# paCluster
pacluster_enabled:0

# Keyspace
db0:keys=12,expires=0,avg_ttl=0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alchemy_Ding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值