分享

redis只作为缓存,不做持久化的配置

 KILLKISS 2017-03-29
[plain] view plain copy
  1. 1.配置缓存内存限制和清理策略  
  2. 作为缓存服务器,如果不加以限制内存的话,就很有可能出现将整台服务器内存都耗光的情况,可以在redis的配置文件里面设置:  
  3.   
  4. example:  
  5. # 限定最多使用1.5GB内存  
  6. maxmemory 1536mb  
  7. 如果内存到达了指定的上限,还要往redis里面添加更多的缓存内容,需要设置清理内容的策略:  
  8. 默认为0,没有指定最大缓存,如果有新的数据添加,超过最大内存,则会使redis崩溃,所以一点要设置。  
  9. 设置maxmemory之后,配合的要设置缓存数据回收策略。  
  10.   
  11.   
  12. # 设置策略为清理最少使用的key对应的数据  
  13. maxmemory-policy allkeys-lru  
  14.   
  15.   
  16. 下面为redis官网上的几种清理策略:  
  17. noeviction: return errors when the memory limit was reached and the client is trying to execute commands that could result in more memory to be used (most write commands, but DEL and a few more exceptions).  
  18. allkeys-lru: evict keys trying to remove the less recently used (LRU) keys first, in order to make space for the new data added.  
  19. volatile-lru: evict keys trying to remove the less recently used (LRU) keys first, but only among keys that have an expire set, in order to make space for the new data added.  
  20. allkeys-random: evict random keys in order to make space for the new data added.  
  21. volatile-random: evict random keys in order to make space for the new data added, but only evict keys with an expire set.  
  22. volatile-ttl: In order to make space for the new data, evict only keys with an expire set, and try to evict keys with a shorter time to live (TTL) first.  
  23.   
  24.   
  25.    
  26. redis回收算法,实际不是严谨的LRU算法,而是抽样回收数据,这样算是为了减少消耗内存使用,但是抽样回收的缓存和全部数据回收缓存差异非常小,或者根本就没有。  
  27.   
  28.    
  29. 先预测好系统所需要的内存高峰,部署相对应内存的缓存服务器。  
  30. 设置maxmemory和相对应的回收策略算法,设置最好为物理内存的3/4,或者比例更小,因为redis复制数据等其他服务时,也是需要缓存的。  
  31. 以防缓存数据过大致使redis崩溃,造成系统出错不可用。牺牲一部分缓存数据,保存整体系统可用性。  
  32.   
  33.   
  34. 如果数据是 幂律分布  也就是某些数据访问频率比较高 则适合allkeys-lru  
  35. 如果数据是 平等分布  也就是访问频率比较平均 则适合allkeys-random  
  36.   
  37. 配对的配置:  
  38. maxmemory-samples 3  
  39.   
  40. 默认值3,上面LRU和最小TTL策略并非严谨的策略,而是大约估算的方式,因此可以选择取样值以便检查  
  41.   
  42. 2.我的需求是只把redis当作缓存来用,所以持久化到硬盘对我的需求来说没有意义。配置 save ""  
  43.   
  44. 3.增加ip绑定(可选)  
  45. redis 不够安全,# bind 192.168.1.100 10.0.0.1  
  46. # bind 127.0.0.1  
  47. 指定Redis 只接收来自于该IP 地址的请求,如果不进行设置,那么将处理所有请求,系统默认是注释掉的,不开启。在生产环境中为了安全最好设置该项。  
  48. 4.maxclients 10000  
  49.   
  50. 限制同时连接的客户数量。当连接数超过这个值时,redis 将不再接收其他连接请求,客户端尝试连接时将收到error 信息。默认为10000,要考虑系统文件描述符限制,不宜过大,浪费文件描述符,具体多少根据具体情况而定  
  51.   
  52. 5.当一个redis支撑不了的时候再考虑Master-Slave模式  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多