使用redis出现性能问题,可以使用redis的slowlog命令 来查看哪些命令阻塞了redis。
使用redis作为数据库时,系统出现少量超时,通过日志信息发现,超时发生在bgsave时。bgsave命令会fork一个子进程,子进程会将redis数据库信息dump到rdb文件中。因此不能确定使用bgsave命令时,是fork一个子进程引起超时,还是dump文件时与主进程的sync同步同时写磁盘引起的超时。
这时就可以使用redis自带的slowlog命令了,但是需要在redis 配置文件中加入slowlog使用的参数。
################################## SLOW LOG ################################### # The Redis Slow Log is a system to log queries that exceeded a specified # execution time. The execution time does not include the I/O operations # like talking with the client, sending the reply and so forth, # but just the time needed to actually execute the command (this is the only # stage of command execution where the thread is blocked and can not serve # other requests in the meantime). # # You can configure the slow log with two parameters: one tells Redis # what is the execution time, in microseconds, to exceed in order for the # command to get logged, and the other parameter is the length of the # slow log. When a new command is logged the oldest one is removed from the # queue of logged commands. # The following time is expressed in microseconds, so 1000000 is equivalent # to one second. Note that a negative number disables the slow log, while # a value of zero forces the logging of every command. #如果一个命令执行操作10ms,需要记录下这个命令和执行时间 slowlog-log-slower-than 10000 # There is no limit to this length. Just be aware that it will consume memory. # You can reclaim memory used by the slow log with SLOWLOG RESET. #保留最近的1024个超时的命令 slowlog-max-len 1024
下面开始使用slowlog查看超时的命令有哪些,先使用redis-cli连接服务器,在使用slowlog命令查看超时的命令有哪些。
redis 127.0.0.1:7771> SLOWLOG GET 3 1) 1) (integer) 174 //编号 2) (integer) 1413914462 //发生日期,UnixTime类型,需要使用工具转换成日期 3) (integer) 724446 //命令次持续时间,单位是微妙 4) 1) "bgSave" //执行的命令和参数 2) 1) (integer) 173 2) (integer) 1413828062 3) (integer) 740908 4) 1) "bgSave" 3) 1) (integer) 172 2) (integer) 1413741662 3) (integer) 737333 4) 1) "bgSave"
问题发现了,bgsave命令执行时间用了700多ms,原来是fork时使用了太长时间,从而阻塞了主进程。
-------------------------------------------------showlog命令实现细节------------------------------------------------------
执行时间的记录。所有redis命令都会调用call函数来执行,而call函数就在执行前后记录下时间,并放在slowlog时间队列中,队列的最大长度就是配置文件定义的长度。
/* Call() is the core of Redis execution of a command */ void call(redisClient *c, int flags) { long long dirty, start, duration; int client_old_flags = c->flags; 。。。 /* Call the command. */ c->flags &= ~(REDIS_FORCE_AOF|REDIS_FORCE_REPL); redisOpArrayInit(&server.also_propagate); dirty = server.dirty; start = ustime(); //记录命令执行开始时间 c->cmd->proc(c); //执行命令 duration = ustime()-start; //记录命令执行时间 dirty = server.dirty-dirty; /* When EVAL is called loading the AOF we don't want commands called * from Lua to go into the slowlog or to populate statistics. */ if (server.loading && c->flags & REDIS_LUA_CLIENT) flags &= ~(REDIS_CALL_SLOWLOG | REDIS_CALL_STATS); /* If the caller is Lua, we want to force the EVAL caller to propagate * the script if the command flag or client flag are forcing the * propagation. */ if (c->flags & REDIS_LUA_CLIENT && server.lua_caller) { if (c->flags & REDIS_FORCE_REPL) server.lua_caller->flags |= REDIS_FORCE_REPL; if (c->flags & REDIS_FORCE_AOF) server.lua_caller->flags |= REDIS_FORCE_AOF; } /* Log the command into the Slow log if needed, and populate the * per-command statistics that we show in INFO commandstats. */ if (flags & REDIS_CALL_SLOWLOG && c->cmd->proc != execCommand) slowlogPushEntryIfNeeded(c->argv,c->argc,duration); //保存命令执行时间 。。。 }
执行时间的查看。slowlogCommand 函数对应slowlog命令。
/* The SLOWLOG command. Implements all the subcommands needed to handle the * Redis slow log. */ void slowlogCommand(redisClient *c) { if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"reset")) { slowlogReset(); addReply(c,shared.ok); } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"len")) { addReplyLongLong(c,listLength(server.slowlog)); } else if ((c->argc == 2 || c->argc == 3) && !strcasecmp(c->argv[1]->ptr,"get")) { long count = 10, sent = 0; listIter li; void *totentries; listNode *ln; slowlogEntry *se; if (c->argc == 3 && getLongFromObjectOrReply(c,c->argv[2],&count,NULL) != REDIS_OK) return; listRewind(server.slowlog,&li); totentries = addDeferredMultiBulkLength(c); //打印count数量的超时命令 while(count-- && (ln = listNext(&li))) { int j; se = ln->value; addReplyMultiBulkLen(c,4); addReplyLongLong(c,se->id); addReplyLongLong(c,se->time); addReplyLongLong(c,se->duration); addReplyMultiBulkLen(c,se->argc); for (j = 0; j < se->argc; j++) addReplyBulk(c,se->argv[j]); sent++; } setDeferredMultiBulkLength(c,totentries,sent); } else { addReplyError(c, "Unknown SLOWLOG subcommand or wrong # of args. Try GET, RESET, LEN."); } }
相关推荐
redis又被称作是NOsql,先不管它叫啥,总之就是可以解决关系型数据库在应用场景所面临的一些性能问题,因为它是基于内存存储的,查找速度很快,号称是关系型数据库查找速度的50倍,当然了,redis也有持久化存储,当你...
Spring boot基于Redis Hash数据结构实现附近的人Demo,框架由Spring-boot实现,压缩包含源码以及部署jar包。代码清晰,有注释,考虑性能优化
redis集成 mybatis缓存实现和redis session共享问题,详细代码。 结合项目中优点: (1) 因为数据存在内存中,类似于 HashMap ,HashMap 的优势就是查找和操作的时间复杂度都是O (1) 。 (2) Redis 本质上是一个 Key-...
高性能 Redis 系列教程实战入门基于 Redis 实现高级限流器及其在 Laravel 队列任务处理中的应用基于 Redis HyperLogLog 实现全站用户 UV 统计中间件基于 Redis 布隆过滤器实现数据去重及其在 Laravel 中的应用示例...
1.基于redis,性能高效 2.实时更新索引 3.支持Suggest前缀、拼音查找(AutoComplete 功能) 4.支持单个或多个分词搜索 5.可根据字段进行结果排序 环境 1.jdk 1.6+ 2.redis 2.2+ 依赖包 1.Jedis-2.1.0 2.commons-pool-...
Redis Keys 命令用于查找所有符合给定模式 pattern 的 key 尽管这个操作的时间复杂度是 O(N), 但是常量时间相当低。例如,在一个普通笔记本上跑Redis,扫描100万个key只要40毫秒。 命令格式 KEYS pattern Warning:...
KEYS 的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,如果你需要从一个数据集中查找特定的 key ,你最好还是用 Redis 的集合结构(set)来代替。 DEL 最早可用版本1.0.0 删除指定的键值对,如果指定...
3. redis常见性能问题和解决方案: (1) Master最好不要做任何持久化工作,如RDB内存快照和AOF日志文件 (2) 如果数据比较重要,某个Slave开启AOF备份数据,策略设置为每秒同步一次 (3) 为了主从复制的速度和连接的...
特点:内存数据引擎,性能极为高效List块链结构,精简内存,优化查找效率Node数据项可定义,支持多种过滤操作支持redo-log,数据持久化,非Cache模式分布式,主从同步 Memlink主要应用于天涯论坛类型产品(论坛、来...
nessDB是一个小巧、高性能的、支持Redis协议的K-V... * 可分布存储的B Tree索引文件 * 支持范围查找 * Redis协议支持(PING,GET,SET,DEL):SET相同的key即更新;DEL后面跟多key,即批量删除 标签:nessDB
Redis:一个高性能的键值对数据库,用于缓存数据以提高系统性能。 RabbitMQ:一个开源的消息队列,用于实现系统中的异步通信。 Elasticsearch:一个分布式搜索和分析引擎,用于实现项目的全文检索功能。 Vue.js:一...
[第十例 Redis集群连接][十一例 Zookeeper操作][十二例 Kafka操作][十三例 NSQ操作][十四例 二分查找][十五例 交换排序 - 冒泡排序][十六例 插入排序 - 直接插入排序][十七例 插入排序 - 希尔排序][十八例 交换排序 ...
version2.0为web服务性能提升:tomcat集群与页面分离版本; version3.0为分布式架构web系统:实现了分布式锁,队列等。版本与1.0,2.0区别较大,另起一个名为“SpaceX-Web-Distributed”项目。 maven依赖 version1.0 ...
覆盖带 •• •••••用来衡量生产代码使用情况的gem,它显示一个计数器,用于指示每行... 按以下顺序查找Redis端点: ENV['COVERBAND_REDIS_URL'] ENV['REDIS_URL'] localhost:6379 Redis存储区也可以在coverband.r
闪烁哈希层 该门户是完全用Node.js编写的高性能Stratum服务器。 该软件的一个实例可以启动... 如果您使用系统软件包管理器可能附带的旧版本的Node.js或Redis,那么您将遇到问题。 按照链接的说明获取最新的稳定版本。
性能上和红黑树,AVL树不相上下,但跳表的原理非常简单,目前Redis和LevelDB中都有用到。 跳表是一种可以替代平衡树的数据结构。跳表追求的是概率性平衡,而不是严格平衡。因此,跟平衡二叉树相比,跳表的插入和删除...
10.3.3 查找缓存 196 10.3.4 缓存未命中 198 10.3.5 缓存命中 198 10.3.6 缓存内容陈旧需重新验证 202 10.3.7 缓存内容无效需重新执行请求 205 10.3.8 缓存响应 206 10.3.9 缓存头总结 207 10.4 Nginx HTTP缓存设置 ...
二分查找和各种变种的二分查找 各类排序算法以及复杂度分析(快排、归并、堆) 各类算法题(手写) 理解并可以分析时间和空间复杂度。 动态规划(笔试回回有。。)、贪心。 红黑树、AVL树、Hash树、Tire树、B树、B+...
⽤户标签系统数据库设计 ⽬前主流的博客系统、CMS都会有⼀个TAG标签系统,不仅可以让... 2)其他的⽅案,当然是发挥Nosql的线性能⼒来设计存储的Key了,尤其是使⽤redis的时候,使⽤的Key的结构可以完美的提⾼查询效率
使用Redis作为缓存,提高系统性能。采用分页插件实现分页功能,优化数据加载速度。总之,SpringBoot学生读书笔记共享平台为用户提供了一个便捷、高效的读书笔记共享环境,有助于学生之间的知识交流和学习进步。