转 What is Redis and what do I use it for?

原文: http://stackoverflow.com/questions/7888880/what-is-redis-and-what-do-i-use-it-for

Redis = Remote Dictionary Service

TL;DR: If you can map a use case to Redis and discover you aren‘t at risk of running out of RAM by using Redis there is a good chance you should probably use Redis.

It‘s a "NoSQL" key-value data store. More precisely, it is a data structure server.

Not like MongoDB (which is a disk-based document store), though MongoDB could be used for similar key/value use cases.

The closest analog is probably to think of Redis as Memcached, but with built-in persistence (snapshotting or journaling to disk) and more datatypes.

Those two additions may seem pretty minor, but they are what make Redis pretty incredible. Persistence to disk means you can use Redis as a real database instead of just a volatile cache. The data won‘t disappear when you restart, like with memcached.

The additional data types are probably even more important. Key
values can be simple strings, like you‘ll find in memcached, but they
can also be more complex types like Hashes, Lists (ordered collection,
makes a great queue), Sets (unordered collection of non-repeating
values), or Sorted Sets (ordered/ranked collection of non-repeating
values).

This is only the tip of the Redis iceberg, as there are other
powerful features like built-in pub/sub, transactions (with optimistic
locking), and Lua scripting.

The entire data set, like memcached, is stored in-memory so it is
extremely fast (like memcached)... often even faster than memcached.
Redis had virtual memory, where rarely used values would be swapped out
to disk, so only the keys had to fit into memory, but this has been
deprecated. Going forward the use cases for Redis are those where its
possible (and desirable) for the entire data set to fit into memory.

Redis is a fantastic choice if you want a highly scalable data store
shared by multiple processes, multiple applications, or multiple
servers. As just an inter-process communication mechanism it is tough to
beat. The fact that you can communicate cross-platform, cross-server,
or cross-application just as easily makes it a pretty great choice for
many many use cases. Its speed also makes it great as a caching layer.

Update 4/1/2015: Redis 3.0 (stable) was released today. This version of Redis brings cluster support, which makes it much easier to scale Redis.

@acidzombie24 Its possible you could use Redis in-place-of MySQL but it really depends on the use case. If your data set could grow to 20GB or you need to use some business analytic tools, you need to make heavy use of joins, etc. then it might not make sense. It‘s really hard to make a blanket statement except to say that there are certainly cases where Redis would be appropriate in place of MySQL.

@KO. Just like Redis offers features which memcached doesn‘t, RDBMS offers many many features which Redis does not. Redis should be FAR faster than any RDBMS, but it also can‘t do as much. If you can map your RDBMS use case to Redis then it might be worth taking a look, but if you can‘t don‘t force it. Best tool for the job and all that. A No-SQL store that has a better chance of replacing your RDBMS is MongoDB, but even that needs to be evaluated carefully and you should go with the best fit, which may be an RDBMS

A very thought-provoking thread here. Being an old dog, it was quite a thunder-clap when I realized the reason most data is now persisted to DBs is simply because a whole generation of programmers have grown up with cheap, ubiquitous DBs and they don‘t KNOW any other way of persisting data. No clue what qsort() & bsearch() are capable of for example. RE: joins with Redix, if people knew how simple it is to do joins in memory they‘d be shocked. The data value you join on simply becomes a token that is replaced by the data the FK indexes. It‘s ~ string replacement problem

DBs are wonderful, and very flexible, but horribly expensive in terms of resources, and very slow. Slow, not because the internals aren‘t fast, but because they have a large fixed cost which is realized as a lot of latency. As an example, my employer has an ENUM server with 45 ms latency - good by industry standards. I am writing a CPS throttle with .067 microsecond latency. Not even in the same ballpark. Likewise, bsearch() against a pointer array, the result of a ptr qsort(), and fetching data from SSDs, is thousands of times faster than any DB - even in-memory ones like ours.

With respect to Redix only holding indexes in memory, with SSDs, I hope this option is still available. The latest generation of SSD focused RAIDs by Adaptec (ASR-8885 RAID) and LSI perform at 12Gbps - spectacular for 256 -2k byte random I/Os a data structure server would be fetching. The reason so many alternative to SQL, like NoSQL, are showing up, is because SQL is the problem. Too much parsing, too much data conversion, metadata driving the very internals of your database, and too much conversion again on the backend. With data structures you have the answer before the SQL gets to the NIC


109 down vote

What it can be use for? Few examples from http://highscalability.com/blog/2011/7/6/11-common-web-use-cases-solved-in-redis.html:

  1. Show latest items listings in your home page. This is a live in-memory cache and is very fast. LPUSH is used to insert a content ID at the head of the list stored at a key. LTRIM is used to limit the number of items in the list to 5000. If the user needs to page beyond this cache only then are they sent to the database.
  2. Deletion and filtering. If a cached article is deleted it can be removed from the cache using LREM.
  3. Leaderboards and related problems. A leader board is a set sorted by score. The ZADD commands implements this directly and the ZREVRANGE command can be used to get the top 100 users by score and ZRANK can be used to get a users rank. Very direct and easy.
  4. Order by user votes and time. This is a leaderboard like Reddit where the score is formula the changes over time. LPUSH + LTRIM are used to add an article to a list. A background task polls the list and recomputes the order of the list and ZADD is used to populate the list in the new order. This list can be retrieved very fast by even a heavily loaded site. This should be easier, the need for the polling code isn‘t elegant.
  5. Implement expires on items. To keep a sorted list by time then use unix time as the key. The difficult task of expiring items is implemented by indexing current_time+time_to_live. Another background worker is used to make queries using ZRANGE ... with SCORES and delete timed out entries.
  6. Counting stuff. Keeping stats of all kinds is common, say you want to know when to block an IP addresss. The INCRBY command makes it easy to atomically keep counters; GETSET to atomically clear the counter; the expire attribute can be used to tell when an key should be deleted.
  7. Unique N items in a given amount of time. This is the unique visitors problem and can be solved using SADD for each pageview. SADD won‘t add a member to a set if it already exists.
  8. Real time analysis of what is happening, for stats, anti spam, or whatever. Using Redis primitives it‘s much simpler to implement a spam filtering system or other real-time tracking system.
  9. Pub/Sub. Keeping a map of who is interested in updates to what data is a common task in systems. Redis has a pub/sub feature to make this easy using commands like SUBSCRIBE, UNSUBSCRIBE, and PUBLISH.
  10. Queues. Queues are everywhere in programming. In addition to the push and pop type commands, Redis has blocking queue commands so a program can wait on work being added to the queue by another program. You can also do interesting things implement a rotating queue of RSS feeds to update.
  11. Caching. Redis can be used in the same manner as memcache.

Yet another one: A very simple, but useful tool for programmers. A build-counter server. Eg. your application version may be 1.2.7 (build #473). It‘s very easy to add a new build number for your project, and your build-script can easily request a new (unique) number.

Oh, and it‘s awfully easy to use this for making two scripts that run on two different platforms communicate with eachother. For instance one of those Cortex-A based TV-boxes can send data back and forth to my desktop computer - so I‘m using it like a FIFO.

时间: 2024-11-08 21:41:31

转 What is Redis and what do I use it for?的相关文章

Redis+keepalived实现双机热备

一. 简介 安装使用centos 5.6(64) Master 192.168.2.235 Slave 192.168.2.236 Vip 192.168.2.200 编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby: 当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从

Python操作数据库(mysql redis)

一.python操作mysql数据库: 数据库信息:(例如211.149.218.16   szz  123456) 操作mysql用pymysql模块 #操作其他数据库,就安装相应的模块 import  pymysql ip='211.149.218.16' port=3306 passwd='123456' user='root' db='szz' conn=pymysql.connect(host=ip,user=user,port=port,passwd=passwd,db=db,cha

Redis在Linux中安装使用

一.安装$ wget http://download.redis.io/releases/redis-x.x.x.tar.gz $ tar xzf redis-x.x.x.tar.gz $ cd redis-x.x.x $ make sudo cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-check-rdb redis-trib.rb redis-sentinel redis-server /usr/loca

redis 学习 四 队列

<?php /** * redis实战 * * 利用列表list实现简单队列 * * @example php cache.php */ header('content-type:text/html;chaeset=utf-8'); $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); // 进队列 $userId = mt_rand(000000, 999999); $redis->rpush('QUEUE_NAME',j

Redis实战(三)Redis主从复制

从架构 1.主从架构图 2.通过命令 mkdir redisCluster创建redis集群文件夹 3.通过命令mkdir 6380   mkdir 6381   mkdir 6382在redisCluster文件夹下创建三个文件夹 4.通过以下命令将redis.conf分别拷贝到6380.6381. 6382文件夹下 cp /usr/local/redis/redis-3.0.2/redis.conf  ./6380 cp /usr/local/redis/redis-3.0.2/redis.

Redis Cluster集群部署搭建

在Oracle的路上走了许多年,换换感觉,尝试一下新的知识,也是一个不错的感觉.Redis,一个超轻量化的内存数据库,只做一小块数据库功能实现,却非常优秀的一个产品.今天,就分享一下安装Redis集群的过程. 搭建redis集群,建议至少需要准备3台服务器,共搭建6个节点,3个master,3个slave,并且要求3个master节点不能全部跑到同一台服务器上,保证节点安全,3台服务器的配置相同,使用redistest账号搭建,对应的端口是7000/7001/7002端口 我的集群分配如下,每个

redis的单机安装与配置以及生产环境启动方案

简单介绍一下redis的单机安装与配置,方便自己记录安装步骤的同时方便他人获取知识. 首先,从官网下载最新版的(稳定版)的redis安装包.官网地址如下:https://redis.io/download 下载源码包后,redis需要编译安装.需要安装gcc和tcl,gcc用于编译tcl用于测试. 使用命令安装gcc,yum install gcc,一路选择yes,gcc就可以安装成功. 接下来安装tcl,首先获取tcl源码包(见百度云盘)或者使用命令:wget http://downloads

Redis学习之Sort Set详解

本文和大家分享的主要是Redis中Sort Set相关内容,一起来看看吧,希望对大家学习redis有所帮助. 游戏服务器需要做一个排行榜实时更新,如果沿用传统的方法,一般是通过后端的定时任务去跑数据来生成排行榜数据,这种方法一方面无法满足产品对功能实时性的要求,另一方面也一定程度上消耗服务器端有限的资源.如果从每次数据库读取数据并进行排名(使用Mysql的sort关键字进行排序),在关卡数据量的级数大时是一种效率低的方法.在查阅大量资料后,发现了Redis中的有序集合(Sort Set). Re

redis 集群

redis 集群 redis集群是redis提供分布式数据库方案, 集群通过分片(Sharding)来进行数据共享,并提供复制和故障转移功能. 节点 redis集群通常由多个节点(node)组成,在开始每个node 都是相互独立的. 要组建成真正可工作的集群,我们必须将各个独立的节点连接起来,构成一个包含多个节点的集群. 命令 cluster meet <ip> <port> 向一个node 发送命令 cluster meet,让节点与ip/port所指定的节点 进行握手(hand

常见redis.conf解析

参数说明redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程daemonize no2. 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定pidfile /var/run/redis.pid3. 指定Redis监听端口,默认端口为6379,作者在自己的一篇博文中解释了为什么选用6379作为默认端口,因为6379在手机按键上MERZ对应的号码