SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Redis 使用案例探索
  Remote Dictionary Server




Bruce Wang
Agenda


  •   基本数据结构和功能
  •   Use Case
  •   Redis 使用注意事项
基本数据结构和功能


  • String
  • Keys

  • List

  • Set

  • Sorted Set

  • Hash
String

    1. Blob 可以放在 String 类型的 Cache 中
    2. String 可以做 Append 操作
    3. String Key 中可以存 Integer 类型的值
Keys

 1. Key 可以设置超时时间
 2. Key 可以做正则匹配
List
List

  1. 左右插入链表
  2. 截取链表
  3. 读取链表
Set
Set
  1. Set 元素惟一性
  2. Set 集合运算
Sorted Set
Sorted Set


 1. 按排名按 Score 增序倒序获取元
    素
 2. 更改元素 Score
Hash
Hash

 1. 设置 Hash K-V
 2. 获取 Hash K-V
USE CASE


     • 排重
     • 计数器

     • List 应用

     • Set 应用

     • Sorted Set 应用

     • Hash 应用

     • Other
Use Case 排重


 1. SETNX 命令
排重 - 秒杀系统



 每个 Item 的值只会有一次写入的机会



 Setnx $itemId $winnerId
Use Case 计数器

1. INCR, INCRBY, DECR, DECRBY, HINCRBY 命令
计数器应用


1.   ID 生成器
2.   Web 分析
3.   防 DDOS 攻击
4.   API 访问率控制
计数器 - ID 生成器


 1. 逻辑中心化 & 部署上分布式
 2. 可根据不同的 DC 做不同的步长
 3. 处理某个类型的 Seq 必定落在固定的 Instance 上, 由 Redis
    Server 的单进程机制保证惟一性



  Incrby Seq:$ObjectType $step
计数器 - Web 分析

 1. 可多样化统计用户行为

  Incr view:$pageId

  Incr view:$pageId: $timeFrame

  Incr logon:$userId

  Incr logon:$userId:$timeFrame
计数器 - Anti-DDOS , API 访问率控制


 1. 可根据不同需求设置统计时间片和最大 hit 的阀值


   $Count = Incr hitCount:$user:$API_id
   if(count==0)
       expire hitCount:$user:$API_id $timeFrame
   Else If (hitCount$user > 阀值 )
      拒绝服务
   Else
      响应服务
List-LOGGER 收集


 使用 List
 • App 客户端: Lpush logger:$appid:serverId $log
 • Logger Server: Worker 轮询 Ltrim logger:$appid 0
   99 write to NFS, HDFS….


 Logger 其他方案—使用 Blob
 • App 客户端 : append logger:$appId:$serverId $log
 • Logger Server: Worker 轮询 ogger:$appId:$serverId
   $log
Set 应用


 1. 社区好友
 2. 随机推荐
Set- 社区好友


 Sadd   friend:$userId1   $userA
 Sadd   friend:$userId1   $userB
 Sadd   friend:$userId2   $userC
 Sadd   friend:$userId2   $userA

 Sadd onlineUser $userA


 共同好友 SInter friend:$userId1 $friend:$userId1
 在线好友 Sinter friend:$userId1 onlineUser
Set- 随机推荐


 • 随机推荐好友
   Srandmember recommandFriendsSet:$userId

 • 随机推荐热图
   Srandmember recommandPhotosSet:$hotPhotos
Use Case - Sorted Set 使用


 1. 排行榜
 2. GEO 相关搜索
 3. 最近浏览页面
Sorted Set – 排行榜
Sorted Set - GEO 搜索


 附近搜索
 1. 经纬度  64 位 Hash
     Hash 值的特点: 越近的地点 Hash 值前面相同的位越大
     海底捞 (120.2E ,30.3N) 14326455945304181035
     外婆家 (121.3E, 31.1N) 14326502174498709381
 2. Zadd resturant:set $hash $resturant
 3. 获取推荐餐馆
   zrangebyScore resturant:set ($userLocationHash - $offset)
    ($userLocationHash + $offset)
Sorted Set – 最近浏览页面



 •   页面浏览:
         Zadd recentPageView:$User $timestamp page1
         Zadd recentPageView:$User $timestamp page2
 •   获取最近浏览 10 个页面 :
     zrevrange recentPageView:$User 0 10 withscores
Hash– 购物车
购物车添加商品:
    Hincrby $cartId $productId $number
获取商品 :
    Hgetall $cartId
Use Case - Other


 1. Session Storage
 2. 公平调度器
其他– Session 存储




    登录: Set $SessionKey $anyValue
   ••登录: Set $SessionKey $anyValue
    每次访问: Expire $sessionKey
   ••每次访问: Expire $sessionKey
   $timeFrame
    $timeFrame
其他– 公平调度器




1. Worker
   •Worker: ZADD worker:nodes $load $hostname
   •Worker: BLPOP $hostname
2. Client
   •Work client: Zrange worker:nodes 0 2
   •Work Client: LPUSH $hostname $task
其他– 优先级 Queue


1. Producer:
    Lpush $queueName anyValue
    Zadd task:$queueName $priority $task

    Zincrby task:$queueName $task $incrNum


2. Subscriber:
    Blpop $queueName
    Task = Zrevrange task:$queueName 0 1
    Sadd $subscriberName $Task

    Execute task
    Srem t $subscriberName $Task
Redis 使用注意事项


 •Use Hash VS encoded object

 •Key 可能分布在不同的 Redis Instance 中

 •不要使用 Redis 做它不擅长的事情

 •Expire 与内存最大值

 •Expire when rename key
We Provide
     The Best !

Weitere ähnliche Inhalte

Was ist angesagt? (15)

Glider
GliderGlider
Glider
 
OpenResty/Lua Practical Experience
OpenResty/Lua Practical ExperienceOpenResty/Lua Practical Experience
OpenResty/Lua Practical Experience
 
scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎scrapy+sphinx搭建搜索引擎
scrapy+sphinx搭建搜索引擎
 
Java soap
Java soapJava soap
Java soap
 
Google developer tool 簡介
Google developer tool 簡介Google developer tool 簡介
Google developer tool 簡介
 
揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing揭秘家用路由器Ch10 sharing
揭秘家用路由器Ch10 sharing
 
12. 網路通訊方案
12. 網路通訊方案12. 網路通訊方案
12. 網路通訊方案
 
Node分享 展烨
Node分享 展烨Node分享 展烨
Node分享 展烨
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kao
 
AJAX Basic
AJAX BasicAJAX Basic
AJAX Basic
 
網頁設計 - 資料庫存取
網頁設計 - 資料庫存取網頁設計 - 資料庫存取
網頁設計 - 資料庫存取
 
Web scraping入門1
Web scraping入門1Web scraping入門1
Web scraping入門1
 
Mvc
MvcMvc
Mvc
 
Zoo keeper
Zoo keeperZoo keeper
Zoo keeper
 
Node getting-started
Node getting-startedNode getting-started
Node getting-started
 

Andere mochten auch

Riak seattle-meetup-august
Riak seattle-meetup-augustRiak seattle-meetup-august
Riak seattle-meetup-august
pharkmillups
 
Riak Use Cases : Dissecting The Solutions To Hard Problems
Riak Use Cases : Dissecting The Solutions To Hard ProblemsRiak Use Cases : Dissecting The Solutions To Hard Problems
Riak Use Cases : Dissecting The Solutions To Hard Problems
Andy Gross
 

Andere mochten auch (20)

Redis and its many use cases
Redis and its many use casesRedis and its many use cases
Redis and its many use cases
 
Introduction to some top Redis use cases
Introduction to some top Redis use casesIntroduction to some top Redis use cases
Introduction to some top Redis use cases
 
See who is using MemSQL
See who is using MemSQLSee who is using MemSQL
See who is using MemSQL
 
Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)
 
Riak seattle-meetup-august
Riak seattle-meetup-augustRiak seattle-meetup-august
Riak seattle-meetup-august
 
Riak add presentation
Riak add presentationRiak add presentation
Riak add presentation
 
Basho and Riak at GOTO Stockholm: "Don't Use My Database."
Basho and Riak at GOTO Stockholm:  "Don't Use My Database."Basho and Riak at GOTO Stockholm:  "Don't Use My Database."
Basho and Riak at GOTO Stockholm: "Don't Use My Database."
 
Scaling with Riak at Showyou
Scaling with Riak at ShowyouScaling with Riak at Showyou
Scaling with Riak at Showyou
 
Redis : Play buzz uses Redis
Redis : Play buzz uses RedisRedis : Play buzz uses Redis
Redis : Play buzz uses Redis
 
Modelling Data as Graphs (Neo4j)
Modelling Data as Graphs (Neo4j)Modelling Data as Graphs (Neo4j)
Modelling Data as Graphs (Neo4j)
 
Riak Use Cases : Dissecting The Solutions To Hard Problems
Riak Use Cases : Dissecting The Solutions To Hard ProblemsRiak Use Cases : Dissecting The Solutions To Hard Problems
Riak Use Cases : Dissecting The Solutions To Hard Problems
 
An intro to Neo4j and some use cases (JFokus 2011)
An intro to Neo4j and some use cases (JFokus 2011)An intro to Neo4j and some use cases (JFokus 2011)
An intro to Neo4j and some use cases (JFokus 2011)
 
Recommendations with Neo4j (FOSDEM 2015)
Recommendations with Neo4j (FOSDEM 2015)Recommendations with Neo4j (FOSDEM 2015)
Recommendations with Neo4j (FOSDEM 2015)
 
Advanced Neo4j Use Cases with the GraphAware Framework
Advanced Neo4j Use Cases with the GraphAware FrameworkAdvanced Neo4j Use Cases with the GraphAware Framework
Advanced Neo4j Use Cases with the GraphAware Framework
 
Modelling Data in Neo4j (plus a few tips)
Modelling Data in Neo4j (plus a few tips)Modelling Data in Neo4j (plus a few tips)
Modelling Data in Neo4j (plus a few tips)
 
Best Buy Web 2.0
Best Buy Web 2.0Best Buy Web 2.0
Best Buy Web 2.0
 
Introduction to Redis Data Structures: Sorted Sets
Introduction to Redis Data Structures: Sorted SetsIntroduction to Redis Data Structures: Sorted Sets
Introduction to Redis Data Structures: Sorted Sets
 
The BestBuy.com Cloud Architecture
The BestBuy.com Cloud ArchitectureThe BestBuy.com Cloud Architecture
The BestBuy.com Cloud Architecture
 
Redis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HARedis as a Main Database, Scaling and HA
Redis as a Main Database, Scaling and HA
 
Using Redis at Facebook
Using Redis at FacebookUsing Redis at Facebook
Using Redis at Facebook
 

Ähnlich wie Redis use cases

Another Introduce to Redis
Another Introduce to RedisAnother Introduce to Redis
Another Introduce to Redis
jiaqing zheng
 
KISSY for starter
KISSY for starterKISSY for starter
KISSY for starter
yiming he
 
Android進階UI控制元件
Android進階UI控制元件Android進階UI控制元件
Android進階UI控制元件
艾鍗科技
 
InnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCCInnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCC
frogd
 

Ähnlich wie Redis use cases (20)

Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and Design
 
纵览Loadrunner核心功能
纵览Loadrunner核心功能纵览Loadrunner核心功能
纵览Loadrunner核心功能
 
Nosql三步曲
Nosql三步曲Nosql三步曲
Nosql三步曲
 
Another Introduce to Redis
Another Introduce to RedisAnother Introduce to Redis
Another Introduce to Redis
 
Large-Scale Cluster Mangement & Kubernetes Under The Hood
Large-Scale Cluster Mangement & Kubernetes Under The HoodLarge-Scale Cluster Mangement & Kubernetes Under The Hood
Large-Scale Cluster Mangement & Kubernetes Under The Hood
 
RxJS 6 新手入門
RxJS 6 新手入門RxJS 6 新手入門
RxJS 6 新手入門
 
MySQL InnoDB 源码实现分析(一)
MySQL InnoDB 源码实现分析(一)MySQL InnoDB 源码实现分析(一)
MySQL InnoDB 源码实现分析(一)
 
CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
進擊的前端工程師:今天就用 JSON Server 自己打造 API 吧!
 
KISSY for starter
KISSY for starterKISSY for starter
KISSY for starter
 
合久必分,分久必合
合久必分,分久必合合久必分,分久必合
合久必分,分久必合
 
Android進階UI控制元件
Android進階UI控制元件Android進階UI控制元件
Android進階UI控制元件
 
Fiddler使用经验分享
Fiddler使用经验分享Fiddler使用经验分享
Fiddler使用经验分享
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
Win dbg入门
Win dbg入门Win dbg入门
Win dbg入门
 
Windbg入门
Windbg入门Windbg入门
Windbg入门
 
InnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCCInnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCC
 
CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
Web请求异步处理和海量数据即时分析在淘宝开放平台的实践
Web请求异步处理和海量数据即时分析在淘宝开放平台的实践Web请求异步处理和海量数据即时分析在淘宝开放平台的实践
Web请求异步处理和海量数据即时分析在淘宝开放平台的实践
 
線上埋碼資料收集實作
線上埋碼資料收集實作線上埋碼資料收集實作
線上埋碼資料收集實作
 

Redis use cases

  • 1. Redis 使用案例探索 Remote Dictionary Server Bruce Wang
  • 2. Agenda • 基本数据结构和功能 • Use Case • Redis 使用注意事项
  • 3. 基本数据结构和功能 • String • Keys • List • Set • Sorted Set • Hash
  • 4. String 1. Blob 可以放在 String 类型的 Cache 中 2. String 可以做 Append 操作 3. String Key 中可以存 Integer 类型的值
  • 5. Keys 1. Key 可以设置超时时间 2. Key 可以做正则匹配
  • 7. List 1. 左右插入链表 2. 截取链表 3. 读取链表
  • 8. Set
  • 9. Set 1. Set 元素惟一性 2. Set 集合运算
  • 11. Sorted Set 1. 按排名按 Score 增序倒序获取元 素 2. 更改元素 Score
  • 12. Hash
  • 13. Hash 1. 设置 Hash K-V 2. 获取 Hash K-V
  • 14. USE CASE • 排重 • 计数器 • List 应用 • Set 应用 • Sorted Set 应用 • Hash 应用 • Other
  • 15. Use Case 排重 1. SETNX 命令
  • 16. 排重 - 秒杀系统 每个 Item 的值只会有一次写入的机会 Setnx $itemId $winnerId
  • 17. Use Case 计数器 1. INCR, INCRBY, DECR, DECRBY, HINCRBY 命令
  • 18. 计数器应用 1. ID 生成器 2. Web 分析 3. 防 DDOS 攻击 4. API 访问率控制
  • 19. 计数器 - ID 生成器 1. 逻辑中心化 & 部署上分布式 2. 可根据不同的 DC 做不同的步长 3. 处理某个类型的 Seq 必定落在固定的 Instance 上, 由 Redis Server 的单进程机制保证惟一性 Incrby Seq:$ObjectType $step
  • 20. 计数器 - Web 分析 1. 可多样化统计用户行为 Incr view:$pageId Incr view:$pageId: $timeFrame Incr logon:$userId Incr logon:$userId:$timeFrame
  • 21. 计数器 - Anti-DDOS , API 访问率控制 1. 可根据不同需求设置统计时间片和最大 hit 的阀值 $Count = Incr hitCount:$user:$API_id if(count==0) expire hitCount:$user:$API_id $timeFrame Else If (hitCount$user > 阀值 ) 拒绝服务 Else 响应服务
  • 22. List-LOGGER 收集 使用 List • App 客户端: Lpush logger:$appid:serverId $log • Logger Server: Worker 轮询 Ltrim logger:$appid 0 99 write to NFS, HDFS…. Logger 其他方案—使用 Blob • App 客户端 : append logger:$appId:$serverId $log • Logger Server: Worker 轮询 ogger:$appId:$serverId $log
  • 23. Set 应用 1. 社区好友 2. 随机推荐
  • 24. Set- 社区好友 Sadd friend:$userId1 $userA Sadd friend:$userId1 $userB Sadd friend:$userId2 $userC Sadd friend:$userId2 $userA Sadd onlineUser $userA 共同好友 SInter friend:$userId1 $friend:$userId1 在线好友 Sinter friend:$userId1 onlineUser
  • 25. Set- 随机推荐 • 随机推荐好友 Srandmember recommandFriendsSet:$userId • 随机推荐热图 Srandmember recommandPhotosSet:$hotPhotos
  • 26. Use Case - Sorted Set 使用 1. 排行榜 2. GEO 相关搜索 3. 最近浏览页面
  • 27. Sorted Set – 排行榜
  • 28. Sorted Set - GEO 搜索 附近搜索 1. 经纬度  64 位 Hash Hash 值的特点: 越近的地点 Hash 值前面相同的位越大 海底捞 (120.2E ,30.3N) 14326455945304181035 外婆家 (121.3E, 31.1N) 14326502174498709381 2. Zadd resturant:set $hash $resturant 3. 获取推荐餐馆 zrangebyScore resturant:set ($userLocationHash - $offset) ($userLocationHash + $offset)
  • 29. Sorted Set – 最近浏览页面 • 页面浏览: Zadd recentPageView:$User $timestamp page1 Zadd recentPageView:$User $timestamp page2 • 获取最近浏览 10 个页面 : zrevrange recentPageView:$User 0 10 withscores
  • 30. Hash– 购物车 购物车添加商品: Hincrby $cartId $productId $number 获取商品 : Hgetall $cartId
  • 31. Use Case - Other 1. Session Storage 2. 公平调度器
  • 32. 其他– Session 存储 登录: Set $SessionKey $anyValue ••登录: Set $SessionKey $anyValue 每次访问: Expire $sessionKey ••每次访问: Expire $sessionKey $timeFrame $timeFrame
  • 33. 其他– 公平调度器 1. Worker •Worker: ZADD worker:nodes $load $hostname •Worker: BLPOP $hostname 2. Client •Work client: Zrange worker:nodes 0 2 •Work Client: LPUSH $hostname $task
  • 34. 其他– 优先级 Queue 1. Producer: Lpush $queueName anyValue Zadd task:$queueName $priority $task Zincrby task:$queueName $task $incrNum 2. Subscriber: Blpop $queueName Task = Zrevrange task:$queueName 0 1 Sadd $subscriberName $Task Execute task Srem t $subscriberName $Task
  • 35. Redis 使用注意事项 •Use Hash VS encoded object •Key 可能分布在不同的 Redis Instance 中 •不要使用 Redis 做它不擅长的事情 •Expire 与内存最大值 •Expire when rename key
  • 36. We Provide The Best !

Hinweis der Redaktion

  1. Morton , geoHash
  2. Morton , geoHash
  3. Morton , geoHash
  4. Hash 可以启用压缩, 较少占内存, Hash 按需取 Hash Key : field , 减少 IO 安全性 条件查询 Queue 功能 VS 消息服务器 在 2.4.x 里,如果设置了 maxmemory ,那么当内存占用没有达到极值时,设置 expire 的 key 到期后并不会被立即删除;所以,如果期望利用 expire 让 redis 清除过期数据,建议还是不要设置 maxmemory 。
  5. 谢谢观看!