突然发现我们的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 完善,不过以后会越来越好
,
InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool