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