Redis {REmote DIctionary Server} 高性能数据库
- 1. What is Redis?
- 1.1. 基于内存的数据存储
- 2. Install Redis on Linux
- 3. Starting and stopping Redis in the background
- 3.1. `systemctl`
- 3.2. `service `
- 4. Connect to Redis
- 5. 退出 Redis 的命令行界面 (redis-cli)
- 6. redis-server 统计信息
- References
Redis (REmote DIctionary Server)
https://redis.io/
1. What is Redis?
Redis (REmote DIctionary Server) is an open source, in-memory, NoSQL key/value store that is used primarily as an application cache or quick-response database.
Redis (REmote DIctionary Server) 是一个开源的内存数据库,遵守 BSD 协议,它提供了一个高性能的键值 (key-value) 存储系统,常用于缓存、消息队列、会话存储等应用场景。Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。
Redis stores data in memory, rather than on a disk or solid-state drive (SSD), which helps deliver unparalleled speed, reliability, and performance.
Redis 将数据存储在内存中,而不是磁盘或固态硬盘 (SSD) 上,这有助于提供无与伦比的速度、可靠性和性能。
Redis 将数据存储在内存中,以提供快速的读写访问速度,并且能够通过异步的方式将数据持久化到磁盘上。
1.1. 基于内存的数据存储
Redis 是一个内存中的数据结构存储系统,意味着它使用计算机的主内存 (RAM) 来存储所有的数据。这种内存优先的设计使得 Redis 能够提供极高的性能,因为内存的数据访问速度远远超过了传统硬盘存储。
由于存储在内存中,Redis 能够以微秒级别的延迟对数据进行读写操作,这对于需要快速响应的应用来说至关重要,如缓存系统、实时分析平台和高频交易系统等。然而,内存资源相对有限且价格较高,因此 Redis 也提供了数据驱动的逐出策略和精细的内存管理功能,确保有效利用可用内存。
2. Install Redis on Linux
https://redis.io/docs/latest/operate/oss_and_stack/install/archive/install-redis/
sudo apt update
sudo apt install redis-server
Note there are redis-server and redis packages in the Ubuntu repository. Both will install the same software, so you can use either and have the same outcome.
(base) yongqiang@yongqiang:~$ sudo apt update
(base) yongqiang@yongqiang:~$ sudo apt install redis-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:libfwupdplugin1 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:libhiredis0.14 libjemalloc2 liblua5.1-0 lua-bitop lua-cjson redis-tools
Suggested packages:ruby-redis
The following NEW packages will be installed:libhiredis0.14 libjemalloc2 liblua5.1-0 lua-bitop lua-cjson redis-server redis-tools
0 upgraded, 7 newly installed, 0 to remove and 327 not upgraded.
Need to get 915 kB of archives.
After this operation, 4077 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
3. Starting and stopping Redis in the background
3.1. systemctl
You can start the Redis server as a background process using the systemctl
command. This only applies to Ubuntu/Debian when installed using apt
, and Red Hat/Rocky when installed using yum
.
sudo systemctl start <redis-service-name> # redis or redis-server depending on platform
To stop the server, use:
sudo systemctl stop <redis-service-name> # redis or redis-server depending on platform
适用于 Linux 的 Windows 子系统 (WSL) 默认不启用 systemd,因此 sudo systemctl start redis-server 可能无效。
3.2. service
通过服务命令启动,并查看服务状态。
(base) yongqiang@yongqiang:~$ sudo service redis-server start
Starting redis-server: redis-server.
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ sudo service redis-server status* redis-server is running
(base) yongqiang@yongqiang:~$
通过服务命令关闭,并查看服务状态。
(base) yongqiang@yongqiang:~$ sudo service redis-server stop
Stopping redis-server: redis-server.
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ sudo service redis-server status* redis-server is not running
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ sudo service redis-server status* redis-server is running
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ ps aux | grep redis
redis 882 0.1 0.1 60924 6272 ? Ssl 21:58 0:01 /usr/bin/redis-server 127.0.0.1:6379
yongqia+ 897 0.0 0.0 8172 2304 pts/0 S+ 22:08 0:00 grep --color=auto redis
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ redis-cli --version
redis-cli 5.0.7
(base) yongqiang@yongqiang:~$ sudo service redis-server stop
Stopping redis-server: redis-server.
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ ps aux | grep redis
yongqia+ 910 0.0 0.0 8172 2432 pts/0 S+ 22:11 0:00 grep --color=auto redis
(base) yongqiang@yongqiang:~$
4. Connect to Redis
Check the Redis command-line client version by entering the following to ensure it is configured properly:
redis-cli --version
(base) yongqiang@yongqiang:~$ redis-cli --version
redis-cli 5.0.7
(base) yongqiang@yongqiang:~$
Once Redis is running, you can test it by running redis-cli:
redis-cli
Test the connection with the ping command:
127.0.0.1:6379> ping
PONG
(base) yongqiang@yongqiang:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
5. 退出 Redis 的命令行界面 (redis-cli)
使用 exit
命令:
(base) yongqiang@yongqiang:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> exit
(base) yongqiang@yongqiang:~$
使用 quit
命令:
(base) yongqiang@yongqiang:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
(base) yongqiang@yongqiang:~$
6. redis-server 统计信息
(base) yongqiang@yongqiang:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> INFO
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:66bd629f924ac924
redis_mode:standalone
os:Linux 6.6.87.2-microsoft-standard-WSL2 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.3.0
process_id:939
run_id:036b682fce7ef9126ec09d7b6210f7df004ff9f0
tcp_port:6379
uptime_in_seconds:57
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:10352588
executable:/usr/bin/redis-server
config_file:/etc/redis/redis.conf# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0# Memory
used_memory:859360
used_memory_human:839.22K
used_memory_rss:5902336
used_memory_rss_human:5.63M
used_memory_peak:859360
used_memory_peak_human:839.22K
used_memory_peak_perc:100.12%
used_memory_overhead:845926
used_memory_startup:796232
used_memory_dataset:13434
used_memory_dataset_perc:21.28%
allocator_allocated:1575864
allocator_active:1880064
allocator_resident:10461184
total_system_memory:4029890560
total_system_memory_human:3.75G
used_memory_lua:41984
used_memory_lua_human:41.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.19
allocator_frag_bytes:304200
allocator_rss_ratio:5.56
allocator_rss_bytes:8581120
rss_overhead_ratio:0.56
rss_overhead_bytes:-4558848
mem_fragmentation_ratio:7.22
mem_fragmentation_bytes:5084984
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.2.1
active_defrag_running:0
lazyfree_pending_objects:0# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1755182995
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0# Stats
total_connections_received:1
total_commands_processed:2
instantaneous_ops_per_sec:0
total_net_input_bytes:45
total_net_output_bytes:11475
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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# Replication
role:master
connected_slaves:0
master_replid:23df661e5962e2a8c907cb36e19637d3d74662a2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0# CPU
used_cpu_sys:0.073876
used_cpu_user:0.052958
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000# Cluster
cluster_enabled:0# Keyspace
127.0.0.1:6379> exit
(base) yongqiang@yongqiang:~$
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] What is Redis? https://www.ibm.com/think/topics/redis