redis 面试问题问答Top 10
1)什么是Redis?
English:Redis is an open source (BSD licensed), in-memory?data structure store, used as a database, cache and message broker【1】.
中文Redis是一個開源的內存數據庫,支持多種數據結構的存取,常用來作為數據庫,緩存和消息隊列。
2)解釋一下Redis的特性?
? ? ?English:It supports data structures such as?strings,?hashes,?lists,?sets,?sorted sets?with range queries,?bitmaps,?hyperloglogs,?geospatial indexes?with radius queries and?streams. Redis has built-in?replication,?Lua scripting,?LRU eviction,?transactions?and different levels of?on-disk persistence, and provides high availability via?Redis Sentinel?and automatic partitioning with?Redis Cluster.? ?
Other features include:
- Transactions
- Pub/Sub
- Lua scripting
- Keys with a limited time-to-live
- LRU eviction of keys
- Automatic failover
? 中文:Redis支持多種數據結構:一方面,支持字符串,哈希表,數組,集合和支持范圍查詢的有序集合等基礎數據結構;另一方面也支持位圖,hyperloglog,帶半徑查找的地理信息定位和流式結構的高級數據結構。同時,它自帶了復制,lua腳本,lru緩存清除,事務和不同等級的持久化方式,更通過哨兵方式提供了高可用,通過集群方式提供了自動分片。
主要特性:
事務
發布/訂閱
? ? ? ? ? ? ? Lua腳本
? ? ? ? ? ? ? KEY有效期設置
? ? ? ? ? ? ? LRU緩存清除
? ? ? ? ? ? ? 自動故障轉移
3) Memcached和Redis的區別是什么?
?
| ?????????????????????? ????Redis | ????????????????????????? ?Memcached |
|
|
4) Redis的優勢是什么?
速度快
支持服務器端鎖定
有很多客戶端庫【6】
具有命令級原子操作(tx操作)
應用廣泛
5) Redis有哪些不足?
單線程
持久化開銷大
6) Redis的鍵操作有哪些?
DELDUMPEXISTSEXPIREEXPIREATKEYSMIGRATEMOVEOBJECTPERSISTPEXPIREPEXPIREATPTTLRANDOMKEYRENAMERENAMENXRESTORESCANSORTTOUCHTTLTYPEUNLINKWAIT7) 誰在使用Redis?
Twitter? GitHub? Weibo? Pinterest? Snapchat? Craigslist? Digg? StackOverflow? Flickr【2】
8) 使用Redis時需要注意什么?
1. key名設計
- (1)【建議】: 可讀性和可管理性
以業務名(或數據庫名)為前綴(防止key沖突),用冒號分隔,比如業務名:表名:id
ugc:video:1- (2)【建議】:簡潔性
保證語義的前提下,控制key的長度,當key較多時,內存占用也不容忽視,例如:
user:{uid}:friends:messages:{mid}簡化為u:{uid}:fr:m:{mid}。- (3)【強制】:不要包含特殊字符
反例:包含空格、換行、單雙引號以及其他轉義字符
2. value設計
- (1)【強制】:拒絕bigkey(防止網卡流量、慢查詢)
string類型控制在10KB以內,hash、list、set、zset元素個數不要超過5000。
反例:一個包含200萬個元素的list。
非字符串的bigkey,不要使用del刪除,使用hscan、sscan、zscan方式漸進式刪除,同時要注意防止bigkey過期時間自動刪除問題(例如一個200萬的zset設置1小時過期,會觸發del操作,造成阻塞,而且該操作不會不出現在慢查詢中(latency可查)),查找方法和刪除方法
- (2)【推薦】:選擇適合的數據類型。
例如:實體類型(要合理控制和使用數據結構內存編碼優化配置,例如ziplist,但也要注意節省內存和性能之間的平衡)
反例:
set user:1:name tom set user:1:age 19 set user:1:favor football正例:
hmset user:1 name tom age 19 favor football3.【推薦】:控制key的生命周期,redis不是垃圾桶。
建議使用expire設置過期時間(條件允許可以打散過期時間,防止集中過期),不過期的數據重點關注idletime。
9)?Redis的持久化方式和各種優缺點?
Redis持久化提供了4個選項
1)RDB(快照)方式【The RDB persistence performs point-in-time snapshots of your dataset at specified intervals】
2)AOF(命令式如mysql 的binlog)【the AOF persistence logs every write operation received by the server, that will be played again at server startup, reconstructing the original dataset. Commands are logged using the same format as the Redis protocol itself, in an append-only fashion. Redis is able to rewrite the log on background when it gets too big】
3)不持久化
4)AOF 和RDB混合方式
各種方式的優缺點
RDB advantages RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups. For instance you may want to archive your RDB files every hour for the latest 24 hours, and to save an RDB snapshot every day for 30 days. This allows you to easily restore different versions of the data set in case of disasters. RDB is very good for disaster recovery, being a single compact file can be transferred to far data centers, or on Amazon S3 (possibly encrypted). RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent instance will never perform disk I/O or alike. RDB allows faster restarts with big datasets compared to AOF. RDB disadvantages RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working (for example after a power outage). You can configure different save points where an RDB is produced (for instance after at least five minutes and 100 writes against the data set, but you can have multiple save points). However you'll usually create an RDB snapshot every five minutes or more, so in case of Redis stopping working without a correct shutdown for any reason you should be prepared to lose the latest minutes of data. RDB needs to fork() often in order to persist on disk using a child process. Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for some millisecond or even for one second if the dataset is very big and the CPU performance not great. AOF also needs to fork() but you can tune how often you want to rewrite your logs without any trade-off on durability. AOF advantages Using AOF Redis is much more durable: you can have different fsync policies: no fsync at all, fsync every second, fsync at every query. With the default policy of fsync every second write performances are still great (fsync is performed using a background thread and the main thread will try hard to perform writes when no fsync is in progress.) but you can only lose one second worth of writes. The AOF log is an append only log, so there are no seeks, nor corruption problems if there is a power outage. Even if the log ends with an half-written command for some reason (disk full or other reasons) the redis-check-aof tool is able to fix it easily. Redis is able to automatically rewrite the AOF in background when it gets too big. The rewrite is completely safe as while Redis continues appending to the old file, a completely new one is produced with the minimal set of operations needed to create the current data set, and once this second file is ready Redis switches the two and starts appending to the new one. AOF contains a log of all the operations one after the other in an easy to understand and parse format. You can even easily export an AOF file. For instance even if you flushed everything for an error using a FLUSHALL command, if no rewrite of the log was performed in the meantime you can still save your data set just stopping the server, removing the latest command, and restarting Redis again. AOF disadvantages AOF files are usually bigger than the equivalent RDB files for the same dataset. AOF can be slower than RDB depending on the exact fsync policy. In general with fsync set to every second performances are still very high, and with fsync disabled it should be exactly as fast as RDB even under high load. Still RDB is able to provide more guarantees about the maximum latency even in the case of an huge write load. In the past we experienced rare bugs in specific commands (for instance there was one involving blocking commands like BRPOPLPUSH) causing the AOF produced to not reproduce exactly the same dataset on reloading. This bugs are rare and we have tests in the test suite creating random complex datasets automatically and reloading them to check everything is ok, but this kind of bugs are almost impossible with RDB persistence. To make this point more clear: the Redis AOF works incrementally updating an existing state, like MySQL or MongoDB does, while the RDB snapshotting creates everything from scratch again and again, that is conceptually more robust. However - 1) It should be noted that every time the AOF is rewritten by Redis it is recreated from scratch starting from the actual data contained in the data set, making resistance to bugs stronger compared to an always appending AOF file (or one rewritten reading the old AOF instead of reading the data in memory). 2) We never had a single report from users about an AOF corruption that was detected in the real world10)?Redis數據庫RDBMS的不同點有哪些?
- Redis是非關系型數據庫的成員而RDBMS是關系型數據庫.
- Redis支持key-value的結構而RDBMS支持表的結構.
- Redis速度極快RDBMS相對較慢.
- Redis是內存型數據,將所有數據存放到內存 而RDBMS存儲數據到磁盤.
- Redis用來存儲小的頻繁使用的的文件而RDBMS 用來存放較大的文件
- Redis僅僅對Linux, BSD, Mac OS X, Solaris提供官方的支持. 當前對 Windows不提供官方支持,而RDBMS 支持所有的操作系統.
【1】?https://redis.io/topics/introduction
【2】https://redis.io/topics/whos-using-redis
【3】https://yq.aliyun.com/articles/531067
【4】https://redis.io/topics/persistence
【5】https://www.javatpoint.com/redis-interview-questions-and-answers
【6】https://redis.io/clients
轉載于:https://www.cnblogs.com/davidwang456/p/11259410.html
總結
以上是生活随笔為你收集整理的redis 面试问题问答Top 10的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redis internal【转】
- 下一篇: 35+ Top Apache Tomca