`

Redis: REmote DIctionary Server

阅读更多

Redis is a key-value store NoSQL database。
性能高的原因在于大部分数据都被放在内存中,所以你可以将它看成是个内存数据库。


Problems:
key一定是String类型,怎么能够按照一定顺序,输出database中所有的keys?(keys * 不能保证有序) list all keys in some order?


Redis Documentation:
http://redis.io/documentation

Redis, from the Ground Up:
http://blog.mjrusso.com/2010/10/17/redis-from-the-ground-up.html#heading_toc_j_5
A whirlwind tour of the next bigthing in NoSQL data storage:
http://www.scribd.com/doc/33531219/Redis-Presentation



Install & Configuration & redis.conf:
安装redis server的正确方式参考 - Redis Quick Start:
http://redis.io/topics/quickstart
redis.conf 2.6版本说明:
https://raw.github.com/antirez/redis/2.6/redis.conf
其他参考资料:
Redis Administration - http://redis.io/topics/admin


Redis CLI:
# 连接至远程 redis server
$ redis-cli -h hostOfRemoteServer -p portOfRemoteServer




Redis Data Types:
http://redis.io/topics/data-types
A fifteen minute introduction to Redis data types:
http://redis.io/topics/data-types-intro
Redis是个key-value store,所有的redis里数据都是以key-value的形式存的,key是数据的唯一标示;key只能也只会是(Redis里的)String类型,即使你存的是int或其他,内部也是转成Strings存的,参见:
http://stackoverflow.com/questions/7973228/storing-integers-in-a-redis-ordered-set
但是有一点必须明确:redis中的String类型,其实是以字节数组的形式存的(Redis是C写的,使用C中的byte[]作为其String类型的存储形式),这保证了,redis的String类型是二进制安全(binary-safe)的,String本身是可以包含任意类型的数据的(因为任意的数据,在计算机世界里,都是可以转化为binary sequence的),比如一个图片,或者一个序列化之后的java对象(当然也可以是一个普通的字符串,如java中的String类型对象)。
在这里我们说的Redis Data Types,指的是key-value store中value的数据类型。Redis中key-value之value支持以下5种内置的数据类型:
String:如上所说,redis里的String类型是可以包含任意类型的数据的。
List:元素为String类型,并且有序的列表。redis的List数据类型是基于链表(Linked List)的,因为是基于链表的,所以其优点是插入和删除快,但缺点是定位和查找慢。
Set:元素为String类型,不可以重复,并且没有顺序的集合。
Sorted Set(zset):元素为String类型,不可以重复,但有顺序的集合。
Hash:value(redis key-value store之value)为Hash类型,其实就是value本身又是key-value pairs集合,内层键值对儿的数量最多可达2^32-1个。使用Hash类型,你可以实现类似下面的两层的key-value键值对儿映射关系:key:{subkey1:subvalue1;subkey2:subvalue2;...},其中任意的key、value都是String类型的;内层的二级key也不可以重复。看的出来,其实redis的Hash类型,对应的就是java里的Map类型。通过Hash类型,你可以实现简单的两层的对象模型,如:
#为了存储userId为1000的用户的信息,我们使用Hash类型,以user:1000为key,将该用户的基本信息存入作为value的“多个二级内层key-value pairs”里:
HMSET user:1000 username antirez password P1pp0 age 34
可以看出,String类型是redis数据结构的核心,其他的list、set、zset、hash,其内部的元素(hash是内部key和内部value),都还(一定)是String类型的。
“key-value store structure”之value如果是list、set、zset、hash数据类型:
key在其声明使用之初被创建(如果尚未存在);
反之,如果因为删除等操作(list:lrem/lpop/rpop、set:srem、zset:zrem、hash:hdel等)致使这4种数据结构的元素数为0(hash是内部键值对儿数目为0),则对应的key也将自动从database中删除。


Redis数据模型设计:
通过对redis数据类型的分析,可以看的出来,Redis的所谓数据类型其实比较简单,远远不能应付复杂的业务数据模型场景。复杂业务数据模型,使用redis的话,可以在其“key-value store structure”的key上做文章,使key有业务含义,如下面的这些例子:
A case study: Design and implementation of a simple Twitter clone using only the Redis key-value store as database and PHP:
http://redis.io/topics/twitter-clone
引用
INCR global:nextUserId => 1000
SET uid:1000:username antirez
SET uid:1000:password p1pp0
We use the global:nextUserId key in order to always get an unique ID for every new user. Then we use this unique ID to populate all the other keys holding our user data. This is a Design Pattern with key-values stores! Keep it in mind.
http://blog.codingnow.com/2011/11/dev_note_2.html
http://doc.open-open.com/view/952fa31a0aa346ee9ccbe1d0b90632b6
Complex data structures Redis:
http://stackoverflow.com/questions/8810036/complex-data-structures-redis
Data modeling practices in Redis?
http://stackoverflow.com/questions/4908197/data-modeling-practices-in-redis
引用
Fundamentally Redis is a data structure server. How you structure your data depends almost entirely on what it is and how you need to access it。
Designing for Redis in my experience is more along the lines of "how simple is my structure?". Can you model your data via hash? If so, use the hash commands in Redis. Do you need to combine sets and key-values or hashes? Then do it the same in redis. Essentially, assume you were not using a database. If you did it entirely within your programming language and in memory, how would you model your data? Odds are that is how you'd do it in Redis - or close enough to figure the rest out.



Redis Server & instances & Databases:
一个运行中的Redis Server,就是一个Redis instance;每个Redis instance都和一个特定的host:port绑定;一个Redis instance中,可以有多个database;database的数目可以在redis.conf文件中指定,默认16个,使用0-based-index作为其唯一标示(默认的16个为0,1,...15);所有新的客户端连接,总是默认使用index为0的database(New connections always use DB 0)。
redis instance是单线程的,所以无法充分利用多核cpu的优势;如果你想充分利用多核的优势,你应该考虑启动多个redis instance。
引用
http://redis.io/topics/benchmarks
Redis is a single-threaded server. It is not designed to benefit from multiple CPU cores. People are supposed to launch several Redis instances to scale out on several cores if needed. It is not really fair to compare one single Redis instance to a multi-threaded data store.
Working with Multiple Databases:
http://rediscookbook.org/multiple_databases.html
database相关命令:SELECT MOVE(注意move是把当前db的指定key移动到指定的db!而不是拷贝!当前db中不再有该key!) FLUSHDB FLUSHALL。
使用什么命令可以显示当前db的index?
没有提供这样的命令!
move只可以将当前db的单个key移至指定的另外一个db,怎么实现拷贝,而不是移动那?更进一步,怎么将一个database中所有的keys,或符合一定条件的keys,一次性全部拷贝入另一个database中?


Lock & Transaction & multi/exec/watch/discard:
http://redis.io/topics/transactions
Redis instance 的 single-threaded的特性,保证了每个基本命令都已经是原子性(atomic)的;假使多个client对一个Redis instance的同一个key做读取/处理,则因为redis instance是单线程的原因,当多个clienit出现了race conditions时,Redis instance是根据先到先处理的原则,其他排队等待(可设置为直接丢弃)。
但是,很多场景下,我们还是需要transaction,来保证让多个命令的组合被作为一个原子操作来执行。redis提供两种简单的锁机制:
1 multi & exec,这是一个悲观锁,被锁住的key将会无法再被其他client读取或写入;multi命令表征事务的开始,exec表示作为一个原子操作来执行multi和exec之间的命令组合;另外需要注意,与rdbms中事务不同的是,redis的锁机制下,如果事务中的某条命令执行失败,事务是不会回滚,只是将失败的命令丢弃掉,事务中的其他命令还是会被执行。
2 使用watch实现“Check and Set”(CAS) transactions(乐观锁):如果被watch的key在exec之前被修改了(可能是被其他的licents),则当前client的multi/exec间的事务被aborted。
引用
http://blog.mjrusso.com/2010/10/17/redis-from-the-ground-up.html#heading_toc_j_4
Redis transactions can be used to make a series of commands atomic. (Every Redis command is already atomic; transactions, however, allow us to combine a number of commands into a single atomic unit.)
MULTI delineates the start of a transaction block; EXEC is responsible for actually running the transaction (i.e., executing all commands between the MULTI and the EXEC, with no other commands executed in-between).
There is an important caveat: because the commands are queued and then executed sequentially (all at once, as if they were a single unit), it is not possible to read a value while a transaction is being executed and to then subsequently use this value at any point during the course of the same transaction.
To address this restriction, Redis supports “Check and Set” (CAS) transactions. CAS is available via the WATCH command.



serialization & deserialization(java为例):



Commands:
http://redis.io/commands#
引用
set
get
setnx:SET-if-not-exists
del
INCR: atomically increment a number stored at a given key
EXPIRE: be sure a key-value pairs only exist for a certain length of time
格式:expire key secondNum 如:
TTL:time to live,test how long a key will exist。The -1 for the TTL of a particular key means that it will never expire. Note that if you SET a key, its TTL will reset.

使用list(A list is a series of ordered values)作为key的相关命令:
RPUSH puts the new value at the end of the list.
LPUSH puts the new value at the start of the list.
LLEN returns the current length of the list.
LRANGE gives a subset of the list. It takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. A value of -1 for the second parameter means to retrieve all elements in the list.
LPOP removes the first element from the list and returns it.
RPOP removes the last element from the list and returns it.

使用set(each element in a set may only appear once)作为key的相关命令:
SADD adds the given value to the set.
SREM removes the given value from the set.
SISMEMBER tests if the given value is in the set.
SMEMBERS eturns a list of all the members of this set.
SUNION combines two or more sets and returns the list of all elements.
sorted set相关:
ZADD
ZRANGE




Redis容错及灾难恢复:
一 Redis Persistence:
引用
http://redis.io/topics/persistence
redis提供两种持久化机制:
RDB:这是默认的持久化策略,默认的rdb文件名为dump.rdb。.rdb文件格式参考:
https://github.com/sripathikrishnan/redis-rdb-tools/wiki/Redis-RDB-Dump-File-Format
引用
Redis *.rdb file is a binary representation of the in-memory store. This binary file is sufficient to completely restore Redis’ state
AOF: Append Only File。默认是关闭的,你可以通过将redis.conf中的“appendonly no”改为“appendonly yes”(或命令行下执行:config set appendonly yes)来开启该持久化机制;开启后aof文件的默认名字为appendonly.aof
二 Redis master-slave Replication:
http://redis.io/topics/replication


srcs:
Redis几个认识误区:
http://timyang.net/data/redis-misunderstanding/
redis + java + nohm(node.js) 的一个例子:
http://www.ibm.com/developerworks/java/library/j-javadev2-22/index.html

分享到:
评论

相关推荐

    redis.zip,Redis(Remote Dictionary Server ),即远程字典服务

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。其中有一个启动bat,启动后的端口号为6379

    Redis API文档 全称:Remote Dictionary Server 远程字典服务

    Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由...

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API

    03尚硅谷_Redis1

    3.1简介Redis:Remote Dictionary Server(远程字典服务器)官网:JavaEE 课程系列更多 Java –大数据 –前端 –pyth

    Redis客户端及服务端的安装教程详解

    Redis:Remote Dictionary Server 远程字典服务器 基于内存管理(数据存在内存),实现了5种数据结构(分别应对各种具体需求),单线程模型的应用程序(单进程单线程),对外提供插入–查询–固化–集群功能。 正是...

    Redis-x64-5.0.9.7z

    以下是对redis的介绍:Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起...

    redis学习课件.zip

    Redis(Remote Dictionary Server)是一个开源的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。以下是一些学习Redis的基本步骤和要点: 了解Redis的基本概念和特性: Redis是一个基于内存的key...

    Redis设计与实现 (数据库技术丛书)_redis_redis设计_remote_

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

    Redis数据类型视频

    Redis(REmote DIctionary Server)是一个key-value存储系统,是当下互联网公司最常用的NoSQL数据库之一,是进入互联网行业的Java开发工程师必备技术。 在本课程中,你将了解Redis是什么、能干什么、如何用,了解...

    Java面试题比较全的知识点总结.docx

    答:Remote Dictionary Server(Redis)是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key- Value数据库,并提供多种语言的API。它通常被称为数据结构服务器 2. Redis有哪些数据结构? ...

    2020最新版Redis架构全套视频教程课件

    Redis(REmote DIctionary Server)是一个Key Value存储系统,是非常著名的NoSQL数据库之一。Redis常常作为系统的缓存Cache使用。在互联网行业应用十分广泛,是进入互联网行业Java攻城狮必备技能,在本课程中,您能...

    Redis-7.0.8-安装服务自动启动

    1.Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。 安装服务启动 @echo off cd /d %~dp0...

    尚硅谷Redis入门视频

    Redis(REmote DIctionary Server)是一个key-value存储系统,是当下互联网公司最常用的NoSQL数据库之一,是进入互联网行业的Java开发工程师必备技术。 在本课程中,你将了解Redis是什么、能干什么、如何用,了解...

    Redis数据库最详细安装步骤.doc

    Redis(Remote Dictionary Server)是一个开源的使用C语言编,基于内存亦可持久化的key-value数据库,并且提供多种语言的API。支持数据的持久化,可以将内存中的数据保存在磁盘中,重启时候可以再次加载进行使用。不...

    Redis源码解读.7z

    Redis源码解读,讲解Redis内部实现机制。...Redis(全称:Remote Dictionary Server 远程字典服务)是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API

    redis学习笔记(详细总结)

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它支持存储的 value 类型相对更多,包括 ...

    Redis中文入门手册

    Redis是REmote DIctionary Server的缩写,在Redis在官方网站的的副标题是A persistent key-value database with built-in net interface written in ANSI-C for Posix systems,这个定义偏向key value store。...

    redis阿里大厂面试专题

    Redis 的全称是:Remote Dictionary.Server,本质上是一个 Key-Value 类型的内存数据库,很像 memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据 flush 到硬盘 上进行保存。...

    Redis—整体知识

    REmote DIctionary Server(Redis)是一个由 Salvatore Sanfilippo 写的开源的、高性能的、使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存亦可持久化的日志型、key-value 存储系统,并提供多种语言的 API...

    3.1.4_redis简介及其搭建1

    3.1.4Redis简介REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存

Global site tag (gtag.js) - Google Analytics