Redis is hot in the tech community right now. It’s come a long way from being a small personal project from Antirez, to being an industry standard for in memory data storage. With that comes a set of best practices that most people can agree upon for using Redis properly. Below we’ll explore 10 quick tips on using Redis correctly.

1. STOP USING KEYS *

Okay, so maybe shouting at you isn’t a great way to start this article. But it’s quite possibly the most important point. Too often do I look at an redis instance, pull up a quick commandstats, and see a glaring KEYS staring right back at me. In all fairness, coming from a programmatical standpoint it would make sense to have a psuedocode that does something like:

  1. for key in 'keys *':
  2. doAllTheThings()

But when you have, say, 13 million keys, things are going to slowdown. Since KEYS is O(n) where n is the number of keys returned, your complexity is bound by the dbsize. Also, during this whole operation, nothing else can be run against your instance.

As a substitute, check out SCAN which allows you to well… scan through your database in increments instead. This operates on an interator so you can stop and go as you see fit.

2. Find Out What’s Slowing Down Redis

Since Redis doesn’t have the most verbose of logs, it’s often hard to trackdown what exactly is going on inside your instance. Luckily Redis provides you with the commandstat utility to show you this:

  1. 127.0.0.1:6379> INFO commandstats
  2. # Commandstats
  3. cmdstat_get:calls=78,usec=608,usec_per_call=7.79
  4. cmdstat_setex:calls=5,usec=71,usec_per_call=14.20
  5. cmdstat_keys:calls=2,usec=42,usec_per_call=21.00
  6. cmdstat_info:calls=10,usec=1931,usec_per_call=193.10

This gives you a breakdown of all the commands, how many times they’ve been run, the number of microseconds it took to execute (total and avg per call)

To reset this simply run CONFIG RESETSTAT, and you’ve got a brand new slate.

3. Use Redis-Benchmark as a Baseline, Not the Gospel Truth

Salvatore, the creator of Redis put it geniously: “To test Redis doing GET/SET is like testing a Ferrari checking how good it is at cleaning the mirror when it rains.” A lot of times people come to me wondering why their Redis-Benchmark results are less than optimal. But we have to take into account many different factors, such as:

  • What client-side limitations could we have run into?
  • Was there a difference in versioning?
  • Are the tests being performed relevant to what the application will be performing?

Redis-Benchmark provides an awesome baseline to make sure your redis-server isn’t behaving abnormally, but it should never be taken as a true “load test”. Load tests need to be reflective of how your application behaves, and from an environment as close to production as possible.

4. Hashes Are Your Best Friend

Invite hashes over for dinner. Wine and dine hashes. You’ll be amazed at what happiness they can bring if you just give them the chance. I’ve seen one too many key structures like this before:

  1. foo:first_name
  2. foo:last_name
  3. foo:address

In the above example, foo would be maybe a username for a user, and each one of those is a separate key. This adds room for errors, and adds unnecessary keys to the fold. Instead, consider a hash. Suddenly you’ve only got one key:

  1. 127.0.0.1:6379> HSET foo first_name "Joe"
  2. (integer) 1
  3. 127.0.0.1:6379> HSET foo last_name "Engel"
  4. (integer) 1
  5. 127.0.0.1:6379> HSET foo address "1 Fanatical Pl"
  6. (integer) 1
  7. 127.0.0.1:6379> HGETALL foo
  8. 1) "first_name"
  9. 2) "Joe"
  10. 3) "last_name"
  11. 4) "Engel"
  12. 5) "address"
  13. 6) "1 Fanatical Pl"
  14. 127.0.0.1:6379> HGET foo first_name
  15. "Joe"

5. Set That TTL!

Whenever possible, take advantage of expiring keys. A perfect example is storing something like temporary authentication keys. When you retrieve the auth key—let’s use OAUTH as an example—you often are given an expiration time. When you set the key, set it with the same expiration, and Redis will clean up for you! No more need for KEYS * to iterate through all those keys, eh?

6. Choosing the Proper Eviction Policy

While we’re on the topic of cleaning up keys, let’s touch on eviction. When your Redis instance fills up, Redis will attempt to evict keys. Depending on your use case, I highly recommend volatile-lru—assuming you have expiring keys. If you’re running something like a cache and don’t have an expiry set, you could consider allkeys-lru. I’d recommend checking out the available options here.

7. If Your Data is Important, Try/Except

If it’s absolutely critical for data to make it to your Redis instance, I heavily recommend putting in a try/except. Since almost all Redis clients are configured to “fire-and-forget,” there should always be consideration for when a key doesn’t actually make it to the database. The complexity added to your redis call is next to nothing in this case, and you can ensure your important data makes it to where it should be.

8. Don’t Flood One Instance

Whenever possible, split up the workload amongst multiple Redis instances. As of version 3.0.0, Redis Cluster is now available. Redis Cluster allows you to break apart keys amongst sets of given masters/slaves based on key ranges. A full breakdown of the magic behind Cluster can be found here. And if you’re looking for a tutorial, then look no further. If clustering is not an option, consider namespacing and distributing your keys among multiple instances. An amazing write-up on partitioning your data can be found on the redis.io website here.

9. More Cores = More Better, Right?!

Wrong. Redis is a single threaded process and will, at most, consume two cores if you have persistence enabled. Unless you plan on running multiple instances on the same host—hopefully only for dev testing in that case!—you shouldn’t need more than two cores for a Redis instance.

10. HA All the Things!

Redis Sentinel is now very well tested, and many users have it running in production (ObjectRocket included!). If you’re relying heavily on Redis for your application, then you need to consider an HA (high availability) solution to keep you online. Of course, if you don’t want to manage all of that yourself, ObjectRocket offers our HA platform with 24x7 support for your consumption, give it a shot.

reference:http://objectrocket.com/blog/how-to

10 quick tips for Redis的更多相关文章

  1. 翻译--Blazing fast node.js: 10 performance tips from LinkedIn Mobile

    1.避免使用同步代码: // Good: write files asynchronously fs.writeFile('message.txt', 'Hello Node', function ( ...

  2. Redis系列之(一):10分钟玩转Redis(转)

    1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...

  3. 10分钟快速入门Redis

    Redis安装 来源:https://github.com/jaywcjlove/handbook 官方编译安装 $ wget http://download.redis.io/releases/re ...

  4. Redis系列之(一):10分钟玩转Redis

    1. Redis介绍 Redis是一个开源的使用ANSI C语言编写.基于内存的Key-Value数据库. 它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集 ...

  5. python 基础 10.0 nosql 简介--redis 连接池及管道

    一. NOSQL 数据库简介 NoSQL 泛指非关系型的数据库.非关系型数据库与关系型数据库的差别 非关系型数据库的优势: 1.性能NOSQL 是基于键值对的,可以想象成表中的主键和值的对应关系,而且 ...

  6. 10分钟彻底理解Redis的持久化机制:RDB和AOF

    作者:张君鸿 juejin.im/post/5d09a9ff51882577eb133aa9 什么是Redis持久化? Redis作为一个键值对内存数据库(NoSQL),数据都存储在内存当中,在处理客 ...

  7. 10分钟彻底理解Redis持久化和主从复制

    在这篇文章,我们一起了解 Redis 使用中非常重要的两个机制:Reids 持久化和主从复制. 什么是 Redis 持久化? Redis 作为一个键值对内存数据库(NoSQL),数据都存储在内存当中, ...

  8. 10 分钟彻底理解 Redis 的持久化和主从复制

    在这篇文章,我们继续有关Redis方面知识的学习,一起了解一下其中一个非常重要的内容:Redis的持久化机制. 什么是Redis持久化? Redis作为一个键值对内存数据库(NoSQL),数据都存储在 ...

  9. Ubuntu 15.10 下Scala 操作Redis Cluster

    1 前言 Redis Standalone,Redis Cluster的安装在前面介绍过,地址:http://www.cnblogs.com/liuchangchun/p/5063477.html,这 ...

随机推荐

  1. BZOJ 3052 树上带修莫队

    思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...

  2. Spark基本运行流程

    不多说,直接上干货! Spark基本运行流程 Application program的组成 Job : 包含多个Task 组成的并行计算,跟Spark action对应. Stage : Job 的调 ...

  3. Core Java(三)

    三.运算符&流程控制 运算符---http://blog.csdn.net/typa01_kk/article/details/45000535 在一个程序执行的过程中,各条语句的执行顺序对程 ...

  4. 五年磨一剑:Java 开源博客 Solo 1.0.0 发布了!

    从 Solo 第一个版本发布至今,已经过去 5 年了.今天我们非常自豪地宣布,Solo 1.0.0 正式发布,感谢一直以来关注 B3log 开源的朋友! 目前 B3log 开源有三款产品: GitHu ...

  5. Unity 烘焙的2种方式

    游戏场景通常有许多光源,使用实时渲染会非常消耗性能,解决办法是烘焙,烘焙有2种方式. 1, 在3dmax等模型制作软件中对场景进行烘焙.将烘焙好的模型以及贴图导入到unity3d. 相对复杂但效果好 ...

  6. 12 个最佳 GNOME(GTK)主题

    作者: Phillip Prado 译者: LCTT 郑 | 2019-04-14 09:45   评论: 1 收藏: 2 让我们来看一些漂亮的 GTK 主题,你不仅可以用在 Ubuntu 上,也可以 ...

  7. activity工作流学习地址

    https://wenku.baidu.com/view/8572153150e2524de4187e5d.html

  8. 百度图标echarts.js的使用

    echarts官网:http://echarts.baidu.com/api.html#echarts echarts是百度公司开的一种开源制作图片工具,是一个专门制作图表的开源工具,功能非常强大,地 ...

  9. 判断浏览器是PC设备还是移动设备

    var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { ...

  10. HDU-2955 Robberies 浮点数01背包 自变量和因变量位置互换

    题目链接:https://cn.vjudge.net/problem/HDU-2955 题意 突然想找几个银行抢钱. 给出各银行的钱数和被抓的概率,以及能容忍的最大被抓概率. 问他最多能抢到多少钱? ...