成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

學(xué)會這15點,讓你分分鐘拿下Redis數(shù)據(jù)庫

運維 數(shù)據(jù)庫運維 存儲軟件 Redis
Redis 是一個開源的使用 ANSI C 語言編寫、遵守 BSD 協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value 數(shù)據(jù)庫,并提供多種語言的 API。

REmote DIctionary Server(Redis) 是一個由 Salvatore Sanfilippo 寫的 Key-Value 存儲系統(tǒng)。

[[232977]]

Redis 簡介

Redis 是一個開源的使用 ANSI C 語言編寫、遵守 BSD 協(xié)議、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value 數(shù)據(jù)庫,并提供多種語言的 API。

它通常被稱為數(shù)據(jù)結(jié)構(gòu)服務(wù)器,因為值(value)可以是字符串(String),哈希(Map),列表(list),集合(sets) 和有序集合(sorted sets)等類型。

大家都知道 Redis 是基于 Key-Value 的 NoSQL 數(shù)據(jù)庫,因此,先來了解一下關(guān)于 Key 相關(guān)的知識點:

  • 任何二進制的序列都可以作為 key 使用
  • Redis 有統(tǒng)一的規(guī)則來設(shè)計 key
  • 對 Key-Value 允許的最大長度是 512MB

支持的語言

  1. ActionScript Bash  C  C#  C++  Clojure Common Lisp 
  2. Crystal  D  Dart  Elixir  emacs  lisp  Erlang   
  3. Fancy  gawk  GNU Prolog  Go  Haskell  Haxe  Io Java  Javascript   
  4. Julia  Lua  Matlab  mruby  Nim  Node.js  Objective-C   
  5. OCaml Pascal  Perl  PHP  Pure Data  Python  R  Racket   
  6. Rebol  Ruby  Rust Scala  Scheme  Smalltalk  Swift  Tcl  VB  VCL 

Redis 的應(yīng)用場景到底有哪些?

  • 最常用的就是會話緩存
  • 消息隊列,比如支付
  • 活動排行榜或計數(shù)
  • 發(fā)布、訂閱消息(消息通知)
  • 商品列表、評論列表等

Redis 安裝

關(guān)于 Redis 安裝與相關(guān)的知識點介紹請參考:Nosql數(shù)據(jù)庫服務(wù)之redis

安裝的大概步驟如下:

Redis 是 C 語言開發(fā)的,安裝 Redis 需要 C 語言的編譯環(huán)境,如果沒有 gcc 需要在線安裝:yum install gcc-c++

  • 第一步,獲取源碼包:wget http://download.redis.io/releases/redis-3.0.0.tar.gz
  • 第二步,解壓縮 Redis:tar zxvf redis-3.0.0.tar.gz
  • 第三步,編譯。進入redis源碼目錄(cd redis-3.0.0)。執(zhí)行 make
  • 第四步,安裝。make install PREFIX=/usr/local/redis

#PREFIX 參數(shù)指定 Redis 的安裝目錄

Redis 數(shù)據(jù)類型

Redis 一共支持五種數(shù)據(jù)類型:

  • string(字符串)
  • hash(哈希)
  • list(列表)
  • set(集合)
  • zset(sorted set 有序集合)

String(字符串):它是 Redis 最基本的數(shù)據(jù)類型,一個 Key 對應(yīng)一個 Value,需要注意是一個鍵值最大存儲 512MB。

  1. 127.0.0.1:6379> set key "hello world" 
  2. OK 
  3. 127.0.0.1:6379> get key 
  4. "hello world" 
  5. 127.0.0.1:6379> getset key "nihao" 
  6. "hello world" 
  7. 127.0.0.1:6379> mset key1 "hi" key2 "nihao" key3 "hello" 
  8. OK 
  9. 127.0.0.1:6379> get key1 
  10. "hi" 
  11. 127.0.0.1:6379> get key2 
  12. "nihao" 
  13. 127.0.0.1:6379> get key3 
  14. "hello" 

相關(guān)命令介紹:

  • set 為一個 Key 設(shè)置 Value(值) 
  • get 獲得某個 Key 對應(yīng)的 Value(值)
  • getset 為一個 Key 設(shè)置 Value(值)并返回對應(yīng)的值
  • mset 為多個 Key 設(shè)置 Value(值)

Hash(哈希):Redis Hash是一個鍵值對的集合, 是一個 String 類型的 Field 和 Value 的映射表,適合用于存儲對象。

  1. 127.0.0.1:6379> hset redishash 1 "001" 
  2. (integer) 1 
  3. 127.0.0.1:6379> hget redishash 1 
  4. "001" 
  5. 127.0.0.1:6379> hmset redishash 1 "001" 2 "002" 
  6. OK 
  7. 127.0.0.1:6379> hget redishash 1 
  8. "001" 
  9. 127.0.0.1:6379> hget redishash 2 
  10. "002" 
  11. 127.0.0.1:6379> hmget redishash 1 2 
  12. 1) "001" 
  13. 2) "002" 

相關(guān)命令介紹:

  • hset 將 Key 對應(yīng)的 Hash 中的 Field 配置為 Value,如果 Hash 不存則自動創(chuàng)建
  • hget 獲得某個 Hash 中的 Field 配置的值
  • hmset 批量配置同一個 Hash 中的多個 Field 值
  • hmget 批量獲得同一個 Hash 中的多個 Field 值

List(列表):是 Redis 簡單的字符串列表,它按插入順序排序。

  1. 127.0.0.1:6379> lpush word  hi 
  2. (integer) 1 
  3. 127.0.0.1:6379> lpush word  hello 
  4. (integer) 2 
  5. 127.0.0.1:6379> rpush word  world 
  6. (integer) 3 
  7. 127.0.0.1:6379> lrange word 0 2 
  8. 1) "hello" 
  9. 2) "hi" 
  10. 3) "world" 
  11. 127.0.0.1:6379> llen word 
  12. (integer) 3 

相關(guān)命令介紹:

  • lpush 向指定的列表左側(cè)插入元素,返回插入后列表的長度
  • rpush 向指定的列表右側(cè)插入元素,返回插入后列表的長度
  • llen 返回指定列表的長度
  • lrange 返回指定列表中指定范圍的元素值

Set(集合):是 String 類型的無序集合,也不可重復(fù)。

  1. 127.0.0.1:6379> sadd redis redisset 
  2. (integer) 1 
  3. 127.0.0.1:6379> sadd redis redisset1 
  4. (integer) 1 
  5. 127.0.0.1:6379> sadd redis redisset2 
  6. (integer) 1 
  7. 127.0.0.1:6379> smembers redis 
  8. 1) "redisset1" 
  9. 2) "redisset" 
  10. 3) "redisset2" 
  11. 127.0.0.1:6379> sadd redis redisset2 
  12. (integer) 0 
  13. 127.0.0.1:6379> smembers redis 
  14. 1) "redisset1" 
  15. 2) "redisset" 
  16. 3) "redisset2" 
  17. 127.0.0.1:6379> smembers redis 
  18. 1) "redisset1" 
  19. 2) "redisset3" 
  20. 3) "redisset" 
  21. 4) "redisset2" 
  22. 127.0.0.1:6379> srem redis redisset 
  23. (integer) 1 
  24. 127.0.0.1:6379> smembers redis 
  25. 1) "redisset1" 
  26. 2) "redisset3" 
  27. 3) "redisset2" 

相關(guān)命令介紹:

  • sadd 添加一個 String 元素到 Key 對應(yīng)的 set 集合中,成功返回 1,如果元素存在返回 0
  • smembers 返回指定的集合中所有的元素
  • srem 刪除指定集合的某個元素

Zset(sorted set 有序集合):是 String 類型的有序集合,也不可重復(fù)。

sorted set 中的每個元素都需要指定一個分數(shù),根據(jù)分數(shù)對元素進行升序排序,如果多個元素有相同的分數(shù),則以字典序進行升序排序,sorted set 因此非常適合實現(xiàn)排名。

  1. 127.0.0.1:6379> zadd nosql 0 001 
  2. (integer) 1 
  3. 127.0.0.1:6379> zadd nosql 0 002 
  4. (integer) 1 
  5. 127.0.0.1:6379> zadd nosql 0 003 
  6. (integer) 1 
  7. 127.0.0.1:6379> zcount nosql 0 0  
  8. (integer) 3 
  9. 127.0.0.1:6379> zcount nosql 0 3 
  10. (integer) 3 
  11. 127.0.0.1:6379> zrem nosql 002 
  12. (integer) 1 
  13. 127.0.0.1:6379> zcount nosql 0 3 
  14. (integer) 2 
  15. 127.0.0.1:6379> zscore nosql 003 
  16. "0" 
  17. 127.0.0.1:6379> zrangebyscore nosql 0 10 
  18. 1) "001" 
  19. 2) "003" 
  20. 127.0.0.1:6379> zadd nosql 1 003 
  21. (integer) 0 
  22. 127.0.0.1:6379> zadd nosql 1 004 
  23. (integer) 1 
  24. 127.0.0.1:6379> zrangebyscore nosql 0 10 
  25. 1) "001" 
  26. 2) "003" 
  27. 3) "004" 
  28. 127.0.0.1:6379> zadd nosql 3 005 
  29. (integer) 1 
  30. 127.0.0.1:6379> zadd nosql 2 006 
  31. (integer) 1 
  32. 127.0.0.1:6379> zrangebyscore nosql 0 10 
  33. 1) "001" 
  34. 2) "003" 
  35. 3) "004" 
  36. 4) "006" 
  37. 5) "005" 

相關(guān)命令介紹:

  • zadd 向指定的 sorteset 中添加 1 個或多個元素
  • zrem 從指定的 sorteset 中刪除 1 個或多個元素
  • zcount 查看指定的 sorteset 中指定分數(shù)范圍內(nèi)的元素數(shù)量
  • zscore 查看指定的 sorteset 中指定分數(shù)的元素
  • zrangebyscore 查看指定的 sorteset 中指定分數(shù)范圍內(nèi)的所有元素

鍵值相關(guān)的命令

  1. 127.0.0.1:6379> exists key 
  2. (integer) 1 
  3. 127.0.0.1:6379> exists key1 
  4. (integer) 1 
  5. 127.0.0.1:6379> exists key100 
  6. (integer) 0 
  7. 127.0.0.1:6379> get key 
  8. "nihao,hello" 
  9. 127.0.0.1:6379> get key1 
  10. "hi" 
  11. 127.0.0.1:6379> del key1 
  12. (integer) 1 
  13. 127.0.0.1:6379> get key1 
  14. (nil) 
  15. 127.0.0.1:6379> rename key key0 
  16. OK 
  17. 127.0.0.1:6379> get key 
  18. (nil) 
  19. 127.0.0.1:6379> get key0 
  20. "nihao,hello" 
  21. 127.0.0.1:6379> type key0 
  22. string 
  • exists:#確認 Key 是否存在
  • del:#刪除 Key
  • expire:#設(shè)置 Key 過期時間(單位秒)
  • persist:#移除 Key 過期時間的配置
  • rename:#重命名 Key
  • type:#返回值的類型

Redis 服務(wù)相關(guān)的命令

  1. 127.0.0.1:6379> select 0 
  2. OK 
  3. 127.0.0.1:6379> info 
  4. # Server 
  5. redis_version:3.0.6 
  6. redis_git_sha1:00000000 
  7. redis_git_dirty:0 
  8. redis_build_id:347e3eeef5029f3 
  9. redis_mode:standalone 
  10. os:Linux 3.10.0-693.el7.x86_64 x86_64 
  11. arch_bits:64 
  12. multiplexing_api:epoll 
  13. gcc_version:4.8.5 
  14. process_id:31197 
  15. run_id:8b6ec6ad5035f5df0b94454e199511084ac6fb12 
  16. tcp_port:6379 
  17. uptime_in_seconds:8514 
  18. uptime_in_days:0 
  19. hz:10 
  20. lru_clock:14015928 
  21. config_file:/usr/local/redis/redis.conf 
  22. -------------------省略N行 
  23. 127.0.0.1:6379> CONFIG GET 0 
  24. (empty list or set
  25. 127.0.0.1:6379> CONFIG GET 15 
  26. (empty list or set
  • slect:#選擇數(shù)據(jù)庫(數(shù)據(jù)庫編號 0-15)
  • quit:#退出連接
  • info:#獲得服務(wù)的信息與統(tǒng)計
  • monitor:#實時監(jiān)控
  • config get:#獲得服務(wù)配置
  • flushdb:#刪除當(dāng)前選擇的數(shù)據(jù)庫中的 Key
  • flushall:#刪除所有數(shù)據(jù)庫中的 Key

Redis 的發(fā)布與訂閱

Redis 發(fā)布與訂閱(pub/sub)是它的一種消息通信模式,一方發(fā)送信息,一方接收信息。

下圖是三個客戶端同時訂閱同一個頻道:

下圖是有新信息發(fā)送給頻道 1 時,就會將消息發(fā)送給訂閱它的三個客戶端:

Redis 事務(wù)

Redis 事務(wù)可以一次執(zhí)行多條命令:

  • 發(fā)送 exec 命令前放入隊列緩存,結(jié)束事務(wù)
  • 收到 exec 命令后執(zhí)行事務(wù)操作,如果某一命令執(zhí)行失敗,其它命令仍可繼續(xù)執(zhí)行
  • 一個事務(wù)執(zhí)行的過程中,其它客戶端提交的請求不會被插入到事務(wù)執(zhí)行的命令列表中

一個事務(wù)經(jīng)歷三個階段:

  • 開始事務(wù)(命令:multi)
  • 命令執(zhí)行
  • 結(jié)束事務(wù)(命令:exec)
  1. 127.0.0.1:6379> MULTI 
  2. OK 
  3. 127.0.0.1:6379> set key key1 
  4. QUEUED 
  5. 127.0.0.1:6379> get key 
  6. QUEUED 
  7. 127.0.0.1:6379> rename key key001 
  8. QUEUED 
  9. 127.0.0.1:6379> exec 
  10. 1) OK 
  11. 2) "key1" 
  12. 3) OK 

Redis 安全配置

可以通過修改配置文件設(shè)備密碼參數(shù)來提高安全性:#requirepass foobared。

去掉注釋#號就可以配置密碼,沒有配置密碼的情況下查詢?nèi)缦拢?/p>

  1. 127.0.0.1:6379> CONFIG GET requirepass  
  2. 1) "requirepass"  
  3. 2) "" 

配置密碼之后,就需要進行認證:

  1. 127.0.0.1:6379> CONFIG GET requirepass 
  2. (error) NOAUTH Authentication required. 
  3. 127.0.0.1:6379> AUTH foobared #認證 
  4. OK 
  5. 127.0.0.1:6379> CONFIG GET requirepass 
  6. 1) "requirepass" 
  7. 2) "foobared" 

Redis 持久化

Redis 持久有兩種方式:

  • Snapshotting(快照)
  • Append-only file(AOF)

Snapshotting(快照):

  • 將存儲在內(nèi)存的數(shù)據(jù)以快照的方式寫入二進制文件中,如默認 dump.rdb中。
  • save 900 1,#900 秒內(nèi)如果超過 1 個 Key 被修改,則啟動快照保存。
  • save 300 10,#300 秒內(nèi)如果超過 10 個 Key 被修改,則啟動快照保存。
  • save 60 10000,#60 秒內(nèi)如果超過 10000 個 Key 被修改,則啟動快照保存。

Append-only file(AOF):

  • 使用 AOF 持久時,服務(wù)會將每個收到的寫命令通過 write 函數(shù)追加到文件中(appendonly.aof)
  • AOF 持久化存儲方式參數(shù)說明:

appendonly yes,#開啟 AOF 持久化存儲方式 

appendfsync always,#收到寫命令后就立即寫入磁盤,效率最差,效果最好

appendfsync everysec,#每秒寫入磁盤一次,效率與效果居中

appendfsync no,#完全依賴 OS,效率最佳,效果沒法保證

Redis 性能測試

自帶相關(guān)測試工具:

  1. [root@test ~]# redis-benchmark --help 
  2. Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>] 
  3.  
  4.  -h <hostname>      Server hostname (default 127.0.0.1) 
  5.  -p <port>          Server port (default 6379) 
  6.  -s <socket>        Server socket (overrides host and port) 
  7.  -a <password>      Password for Redis Auth 
  8.  -c <clients>       Number of parallel connections (default 50) 
  9.  -n <requests>      Total number of requests (default 100000) 
  10.  -d <size>          Data size of SET/GET value in bytes (default 2) 
  11.  -dbnum <db>        SELECT the specified db number (default 0) 
  12.  -k <boolean>       1=keep alive 0=reconnect (default 1) 
  13.  -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD 
  14.   Using this option the benchmark will expand the string __rand_int__ 
  15.   inside an argument with a 12 digits number in the specified range 
  16.   from 0 to keyspacelen-1. The substitution changes every time a command 
  17.   is executed. Default tests use this to hit random keys in the 
  18.   specified range. 
  19.  -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline). 
  20.  -q                 Quiet. Just show query/sec values 
  21.  --csv              Output in CSV format 
  22.  -l                 Loop. Run the tests forever 
  23.  -t <tests>         Only run the comma separated list of tests. The test 
  24.                     names are the same as the ones produced as output
  25.  -I                 Idle mode. Just open N idle connections and wait. 
  26.  
  27. Examples: 
  28.  
  29.  Run the benchmark with the default configuration against 127.0.0.1:6379: 
  30.    $ redis-benchmark 
  31.  
  32.  Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1: 
  33.    $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20 
  34.  
  35.  Fill 127.0.0.1:6379 with about 1 million keys only using the SET test: 
  36.    $ redis-benchmark -t set -n 1000000 -r 100000000 
  37.  
  38.  Benchmark 127.0.0.1:6379 for a few commands producing CSV output
  39.    $ redis-benchmark -t ping,set,get -n 100000 --csv 
  40.  
  41.  Benchmark a specific command line: 
  42.    $ redis-benchmark -r 10000 -n 10000 eval 'return redis.call("ping")' 0 
  43.  
  44.  Fill a list with 10000 random elements: 
  45.    $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__ 
  46.  
  47.  On user specified command lines __rand_int__ is replaced with a random integer 
  48.  with a range of values selected by the -r option

實際測試同時執(zhí)行 100 萬的請求:

  1. [root@test ~]# redis-benchmark -n 1000000 -q 
  2. PING_INLINE: 152578.58 requests per second 
  3. PING_BULK: 150308.14 requests per second 
  4. SET: 143266.47 requests per second 
  5. GET: 148632.58 requests per second 
  6. INCR: 145857.64 requests per second 
  7. LPUSH: 143781.45 requests per second 
  8. LPOP: 147819.66 requests per second 
  9. SADD: 138350.86 requests per second 
  10. SPOP: 134282.27 requests per second 
  11. LPUSH (needed to benchmark LRANGE): 141302.81 requests per second 
  12. LRANGE_100 (first 100 elements): 146756.67 requests per second 
  13. LRANGE_300 (first 300 elements): 148104.27 requests per second 
  14. LRANGE_500 (first 450 elements): 152671.75 requests per second 
  15. LRANGE_600 (first 600 elements): 148104.27 requests per second 
  16. MSET (10 keys): 132731.62 requests per second 

Redis 的備份與恢復(fù)

Redis 自動備份有兩種方式:

  1. 通過 dump.rdb 文件實現(xiàn)備份
  2. 使用 aof 文件實現(xiàn)自動備份

dump.rdb 備份

Redis 服務(wù)默認的自動文件備份方式(AOF 沒有開啟的情況下),在服務(wù)啟動時,就會自動從 dump.rdb 文件中去加載數(shù)據(jù)。

具體配置在 redis.conf:

  • save 900 1 
  • save 300 10 
  • save 60 10000

也可以手工執(zhí)行 save 命令實現(xiàn)手動備份。

  1. 127.0.0.1:6379> set name key 
  2. OK 
  3. 127.0.0.1:6379> SAVE 
  4. OK 
  5. 127.0.0.1:6379> set name key1 
  6. OK 
  7. 127.0.0.1:6379> BGSAVE 
  8. Background saving started 

Redis 快照到 dump 文件時,會自動生成 dump.rdb 的文件。

  1. # The filename where to dump the DB 
  2. dbfilename dump.rdb 
  3. -rw-r--r-- 1 root root   253 Apr 17 20:17 dump.rdb 
  • SAVE 命令表示使用主進程將當(dāng)前數(shù)據(jù)庫快照到 dump 文件
  • BGSAVE 命令表示,主進程會 fork 一個子進程來進行快照備份
  • 兩種備份不同之處,前者會阻塞主進程,后者不會。

恢復(fù)舉例

  1. # Note that you must specify a directory here, not a file name
  2. dir /usr/local/redisdata/ 
  3. 備份文件存儲路徑 
  4. 127.0.0.1:6379> CONFIG GET dir 
  5. 1) "dir" 
  6. 2) "/usr/local/redisdata" 
  7. 127.0.0.1:6379> set key 001 
  8. OK 
  9. 127.0.0.1:6379> set key1 002 
  10. OK 
  11. 127.0.0.1:6379> set key2 003 
  12. OK 
  13. 127.0.0.1:6379> save 
  14. OK 

將備份文件備份到其他目錄:

  1. [root@test ~]# ll /usr/local/redisdata/ 
  2. total 4 
  3. -rw-r--r-- 1 root root 49 Apr 17 21:24 dump.rdb 
  4. [root@test ~]# date 
  5. Tue Apr 17 21:25:38 CST 2018 
  6. [root@test ~]# cp ./dump.rdb /tmp/ 

刪除數(shù)據(jù):

  1. 127.0.0.1:6379> del key1 
  2. (integer) 1 
  3. 127.0.0.1:6379> get key1 
  4. (nil) 

關(guān)閉服務(wù),將原備份文件拷貝回 save 備份目錄:

  1. [root@test ~]# redis-cli -a foobared shutdown 
  2. [root@test ~]# lsof -i :6379 
  3. [root@test ~]# cp /tmp/dump.rdb /usr/local/redisdata/ 
  4. cp: overwrite ‘/usr/local/redisdata/dump.rdb’? y 
  5. [root@test ~]# redis-server /usr/local/redis/redis.conf & 
  6. [1] 31487 

登錄查看數(shù)據(jù)是否恢復(fù):

  1. [root@test ~]# redis-cli -a foobared 
  2. 127.0.0.1:6379> mget key key1 key2 
  3. 1) "001" 
  4. 2) "002" 
  5. 3) "003" 

AOF 自動備份

Redis服務(wù)默認是關(guān)閉此項配置:

  1. ###### APPEND ONLY MODE ########## 
  2. appendonly no 
  3.  
  4. # The name of the append only file (default"appendonly.aof"
  5. appendfilename "appendonly.aof" 
  6.  
  7. # appendfsync always 
  8. appendfsync everysec 
  9. # appendfsync no 

配置文件的相關(guān)參數(shù),前面已經(jīng)詳細介紹過。AOF 文件備份,是備份所有的歷史記錄以及執(zhí)行過的命令,和 MySQL binlog 很相似,在恢復(fù)時就是重新執(zhí)次一次之前執(zhí)行的命令。

需要注意的就是在恢復(fù)之前,和數(shù)據(jù)庫恢復(fù)一樣需要手工刪除執(zhí)行過的del或誤操作的命令。

AOF 與 dump 備份不同:

  • AOF 文件備份與 dump 文件備份不同
  • 服務(wù)讀取文件的優(yōu)先順序不同,會按照以下優(yōu)先級進行啟動

如果只配置 AOF,重啟時加載 AOF 文件恢復(fù)數(shù)據(jù)

如果同時 配置了 RBD 和 AOF,啟動是只加載 AOF 文件恢復(fù)數(shù)據(jù)

如果只配置 RBD,啟動時將加載 dump 文件恢復(fù)數(shù)據(jù)

注意:只要配置了 AOF,但是沒有 AOF 文件,這個時候啟動的數(shù)據(jù)庫會是空的。

Redis 生產(chǎn)優(yōu)化介紹

內(nèi)存管理優(yōu)化 

  • hash-max-ziplist-entries 512
  • hash-max-ziplist-value 64
  • list-max-ziplist-entries 512
  • list-max-ziplist-value 64

#list 的成員與值都不太大的時候會采用緊湊格式來存儲,相對內(nèi)存開銷也較小

在 Linux 環(huán)境運行 Redis 時,如果系統(tǒng)的內(nèi)存比較小,這個時候自動備份會有可能失敗,需要修改系統(tǒng)的 vm.overcommit_memory 參數(shù),這個參數(shù)是 Linux 系統(tǒng)的內(nèi)存分配策略。

  • 0 表示內(nèi)核將檢查是否有足夠的可用內(nèi)存供應(yīng)用進程使用;如果有足夠的可用內(nèi)存,內(nèi)存申請允許;否則,內(nèi)存申請失敗,并把錯誤返回給應(yīng)用進程。
  • 1 表示內(nèi)核允許分配所有的物理內(nèi)存,而不管當(dāng)前的內(nèi)存狀態(tài)如何。
  • 2 表示內(nèi)核允許分配超過所有物理內(nèi)存和交換空間總和的內(nèi)存。

Redis官方的說明是,建議將 vm.overcommit_memory 的值修改為 1,可以用下面幾種方式進行修改:

  • 編輯 /etc/sysctl.conf 改 vm.overcommit_memory=1,然后 sysctl -p 使配置文件生效
  • sysctl vm.overcommit_memory=1
  • echo 1 > /proc/sysm/overcommit_memory 

內(nèi)存預(yù)分配

持久化機制

  • 定時快照:效率不高,會丟失數(shù)據(jù)
  • AOF:保持數(shù)據(jù)完整性(一個實例的數(shù)量不要太大2G最大)

優(yōu)化總結(jié):

  • 根據(jù)業(yè)務(wù)需要選擇合適的數(shù)據(jù)類型
  • 當(dāng)業(yè)務(wù)場景不需持久化時就關(guān)閉所有持久化方式(采用ssd磁盤來提升效率)
  • 不要使用虛擬內(nèi)存的方式,每秒實時寫入 AOF
  • 不要讓 Redis 所在的服務(wù)器物理內(nèi)存使用超過內(nèi)存總量的 3/5
  • 要使用 maxmemory
  • 大數(shù)據(jù)量按業(yè)務(wù)分開使用多個 Redis 實例

Redis 集群應(yīng)用

集群是將多個 Redis 實例集中在一起,實現(xiàn)同一業(yè)務(wù)需求,或者實現(xiàn)高可用與負載均衡到底有哪些集群方案呢?

Haproxy+Keepalived+Redis 集群

  • 通過 Redis 的配置文件,實現(xiàn)主從復(fù)制、讀寫分離
  • 通過 Haproxy 的配置,實現(xiàn)負載均衡,當(dāng)從故障時也會及時從集群中T除
  • 利用 Keepalived 來實現(xiàn)負載的高可用

Redis 官方 Sentinel 集群管理工具

Redis集群生產(chǎn)環(huán)境高可用方案實戰(zhàn)過程:

  • Sentinel 負責(zé)對集群中的主從服務(wù)監(jiān)控、提醒和自動故障轉(zhuǎn)移
  • Redis 集群負責(zé)對外提供服務(wù)
  • 關(guān)于 Redis Sentinel Cluster 集群配置可參考

Redis Cluster 

Redis Cluster 是 Redis 的分布式解決方案,在 Redis 3.0 版本正式推出的,有效解決了 Redis 分布式方面的需求。

當(dāng)遇到單機內(nèi)存、并發(fā)、流量等瓶頸時,可以采用Cluster架構(gòu)達到負載均衡的目的。

  • 官方推薦,毋庸置疑。
  • 去中心化,集群最大可增加 1000 個節(jié)點,性能隨節(jié)點增加而線性擴展。
  • 管理方便,后續(xù)可自行增加或摘除節(jié)點,移動分槽等等。
  • 簡單,易上手。

責(zé)任編輯:武曉燕 來源: 民工哥Linux運維
相關(guān)推薦

2018-05-14 09:05:40

Redis數(shù)據(jù)庫String

2018-06-21 15:15:05

數(shù)據(jù)庫Redis書籍

2019-03-11 12:50:51

offer書單面試

2015-12-22 11:48:50

javascript閉包

2017-07-17 14:17:37

閉包匿名函數(shù) 作用域

2020-04-14 14:30:43

Redis數(shù)據(jù)庫開源

2020-02-15 16:48:28

機器學(xué)習(xí)算法人工智能

2019-12-31 14:10:58

Excel文章SQL

2021-05-08 12:10:49

Python代碼抓點圖片

2017-06-13 10:08:19

AI交通識別

2019-08-20 14:40:35

Redis數(shù)據(jù)庫

2017-06-28 09:26:28

人臉識別AIFR技術(shù)

2024-08-21 08:27:30

擴展數(shù)據(jù)庫服務(wù)器

2023-07-03 07:20:50

2023-03-10 22:08:20

2021-01-13 11:03:20

Python數(shù)據(jù)代碼

2009-11-20 17:06:49

Oracle數(shù)據(jù)庫字符

2013-12-19 09:20:59

2020-12-08 09:52:22

數(shù)據(jù)庫工具技術(shù)

2015-10-29 18:05:36

點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 久久伊 | 99久久婷婷国产综合精品电影 | 国产精品高潮呻吟久久久久 | 一区二区三区欧美 | 国产精品久久久久9999鸭 | 国产精品国产a | 久久久久久久久久久久久9999 | 99久久久国产精品免费消防器 | 天堂综合网 | 中文字幕在线观 | 国产sm主人调教女m视频 | 成人av一区二区三区 | 久久99精品久久久久 | 久久久91| 成人av在线播放 | 在线国产一区 | www.se91| 在线观看黄免费 | 一区二区免费 | 99久久久国产精品 | 国产精品污www在线观看 | 国际精品鲁一鲁一区二区小说 | 日本高清中文字幕 | 欧美一级三级在线观看 | 精品久久久久久久人人人人传媒 | 一区二区三区四区电影视频在线观看 | 欧美最猛性xxxxx亚洲精品 | 日本理论片好看理论片 | 日韩精品视频在线播放 | 色婷婷综合在线观看 | 日韩精品一区二区在线 | 国产成人精品视频在线观看 | 国产成人99久久亚洲综合精品 | 91国产精品 | 久久精品国产一区 | 香蕉视频一区二区 | 日韩一区二区三区在线视频 | 欧美中文字幕在线观看 | 亚洲资源在线 | 一区二区高清 | 色资源av|