Home Database Mysql Tutorial 30G 的redis 如何优化

30G 的redis 如何优化

Jun 07, 2016 pm 05:38 PM
redis optimization how

突然发现我们的redis 已经用了30G了,好吧这是个很尴尬的数字因为我们的缓存机器的内存目前是32G的,内存已经告竭。幸好上上周公司采购了90G的机器,现在已经零时迁移到其中的一台机器上了。(跑题下,90G的内存太爽了是我除了koding.com 之外第二次用到90G的

突然发现我们的redis 已经用了30G了,好吧这是个很尴尬的数字因为我们的缓存机器的内存目前是32G的,内存已经告竭。幸好上上周公司采购了90G的机器,现在已经零时迁移到其中的一台机器上了。(跑题下,90G的内存太爽了是我除了koding.com 之外第二次用到90G的机器,koding 是个好网站,在线编程IDE。) 但是随着数据量越来越大单机始终无法承受的,改造势在必行。经过初步思考我们得出了很简单的方案 概括起来就是    "内外兼修"

1.内功修炼

先从我们的应用层说起 看看redis 使用情况 ,有没有办法回收一些key ,先进入redis 服务器执行 info ,有删减

1: redis 127.0.0.1:6391> info

2: used_memory_human:35.58G

3: keyspace_hits:2580207188

4: db0:keys=2706740,expires=1440700

目前我们只使用了1个DB 但是key 太多了 有270W个key,已经过期的有144W。第一个想到的就是我勒个去,怎么会有这么多key ,第二个想法就是可能存在过大的key

看看能不能针对过大的key 做优化?可是遗憾的是官方并没有命令显示db 的key 大小,我们只能自己想办法了

Google 一番,发现国外友人已经写好了shell

传送门: https://gist.github.com/epicserve/5699837

可以列出每个key 大小了。可是这并不适用我们,因为我们key 太大了 执行了9个小时都没跑完,无力吐槽了。 其实还有一个选择就是用另外一个工具

传送门:https://github.com/sripathikrishnan/redis-rdb-tools

可惜这个太重了 ,不想麻烦ops ,我们就只能撩起袖子,造轮子。

把shell 代码简单看了下发件DEBUG OBJECT 是个好东西啊 ,google 下发现官网

已经有简单的调试信息了,剩下的就好处理了

1: #coding=utf-8 2: import redis 3:  4: COLOR_RED = "\033[31;49;1m %s \033[31;49;0m" 5:  6: COLOR_GREED = "\033[32;49;1m %s \033[39;49;0m" 7:  8: COLOR_YELLOW = "\033[33;49;1m %s \033[33;49;0m" 9:  10: COLOR_BLUE = "\033[34;49;1m %s \033[34;49;0m" 11:  12: COLOR_PINK = "\033[35;49;1m %s \033[35;49;0m" 13:  14: COLOR_GREENBLUE = "\033[36;49;1m %s \033[36;49;0m" 15:  16:  17: def getHumanSize(value): 18: gb = 1024 * 1024 * 1024.0 19: mb = 1024 * 1024.0 20: kb = 1024.0 >= gb: 22: return COLOR_RED % (str(round(value / gb, 2)) + " gb") 23: elif value >= mb: 24: return COLOR_YELLOW % (str(round(value / mb, 2)) + " mb") 25: elif value >= kb: 26: return COLOR_BLUE % (str(round(value / kb, 2)) + " kb") 27: else: 28: return COLOR_GREED % (str(value) + "b") 29:  30:  31: month = 3600 * 24 * 30 32: result = [] 33: client = redis.Redis(host="XXXXX", port=XXXX) 36: client.info() 37:  38: count = 0 39: for key in client.keys('*'): 40: try: 41: count += 1 42: idleTime = client.object('idletime', key) 43: refcount = client.object('refcount', key) 44: length = client.debug_object(key)['serializedlength'] 45: value = idleTime * refcount 46: print "%s key :%s , idletime : %s,refcount :%s, length : %s , humSize :%s" % (count, key, idleTime, refcount, length, getHumanSize(length)) 47: except Exception: 48: pass

写了个简单的python 脚本输出每个key 的大小和idle time,和refer count 。有了这么多数据结合awk 就可以很好的统计每个key 的使用情况。有一点要注意的是这个size 是key 在redis 中的大小,并非实际的大小,这个是经过redis 压缩的。经过分析之后发现不存在过大的key ,但是存在有些key 半年都没有被访问过 Orz 。

接下来就很好处理了,我们为每个key 设置的过期时间,若key 被hit 上则更新这个expire time 。这样可以逐步淘汰冷数据,达到冷热分离

 

2. 外功修炼

我们对内清理了无效的key,对外我们要做到水平扩展,单机的承载始终有限,于是我们开始了传说中的分布式改造

分布式这东西看起来很唬人做起来更唬人,幸好我们是缓存服务 CAP约束有限。 缓存服务做分布式最好的当然是一致性hash 咯。其实当我们改造完成之后,才发现官方已经准备做这个分布式的缓存体系了(流口水啊) 只是现在还在开发中 给了个备用的响当当的  Twemproxy  奈何我们已经做好了,就先用着,坐等官方测试之后再说

传送门:

我们实现了数据的平滑迁移,而且对server 的修改实现了最小影响。 因为原来是用的是phpredis 所以就扩展了下,代码可以平滑过渡。

我们自己的实现:https://github.com/trigged/redis_con_hash

其实扯了这么多就是要把redis 的数据分散开,单机的承载始终是个瓶颈,但是redis 在这方面没有Memcached 完善,不过以后会越来越好

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to solve data loss with redis How to solve data loss with redis Apr 10, 2025 pm 08:24 PM

Redis data loss causes include memory failures, power outages, human errors, and hardware failures. The solutions are: 1. Store data to disk with RDB or AOF persistence; 2. Copy to multiple servers for high availability; 3. HA with Redis Sentinel or Redis Cluster; 4. Create snapshots to back up data; 5. Implement best practices such as persistence, replication, snapshots, monitoring, and security measures.

How to use the redis command line How to use the redis command line Apr 10, 2025 pm 10:18 PM

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

See all articles