NoSQL Taiwan #6 Redis Data Structure

  • 11,453 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
11,453
On Slideshare
0
From Embeds
0
Number of Embeds
10

Actions

Shares
Downloads
0
Comments
0
Likes
47

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. NoSQL Taiwan #6 Redis Data Structure
  • 2. About Me• Felix Lin• hellolucky• Rails developer• http://twitter.com/hellolucky123• http://blog.hellolucky.info
  • 3. This talk will tell you ......• How to build a simple social website by using SQL• Whats the performance issue about SQL• How to use the data structures of Redis• How to solve the performance issue of SQL by using Redis• Why I love Redis so much
  • 4. This talk won’t tell you ......• How fast Redis is• How to Use Lua script in Redis• How to calculate memory usage• How to administrate Redis• The best practice of Redis
  • 5. Database
  • 6. User
  • 7. User UserFollowRelation
  • 8. User UserFollowRelation
  • 9. FollowUser UserFollowRelation
  • 10. FollowUser UserFollowRelation Follower
  • 11. FollowUser UserFollowRelation Follower Feed
  • 12. FollowUser UserFollowRelation Follower Feed
  • 13. About User
  • 14. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 15. About Relation
  • 16. About Relation# Set Following UserINSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 2)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 3)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 4)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 5)
  • 17. About Relation# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 18. About Relation UserFollowRelation Records > 1000000# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 19. About Relation UserFollowRelation Records > 1000000# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1 Slow!
  • 20. About Relation# Find Following User CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 21. About Relation UserFollowRelation Records ># Find Following User Count 1000000SELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 22. About Relation UserFollowRelation Records ># Find Following User Count 1000000SELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1 Slow!
  • 23. About Relation# Find User1s Following UsersSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find User12 Following UsersSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 2# The Same Following UsersUser1s Following Users & User2s Following Users
  • 24. About Relation# Find User1s Following Users Slow + SlowSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find User12 Following UsersSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 2# The Same Following UsersUser1s Following Users & User2s Following Users
  • 25. About Relation# Find User1s Following Users Slow + SlowSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find User12 Following UsersSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 2# The Same Following UsersUser1s Following Users & User2s Following Users = Very Slow!
  • 26. About Feed
  • 27. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 28. About FeedBEGIN# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1COMMIT
  • 29. About FeedBEGIN Transaction# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1COMMIT
  • 30. About FeedBEGIN Transaction# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1COMMIT Slow!
  • 31. About Feed# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Following Users FeedsSELECT `feeds`.* FROM `feeds` WHERE user_id in (2,3,4,5)
  • 32. About Feed A Little Data# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Following Users FeedsSELECT `feeds`.* FROM `feeds` WHERE user_id in (2,3,4,5)
  • 33. About Feed A Little Data# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Following Users FeedsSELECT `feeds`.* FROM `feeds` WHERE user_id in (2,3,4,5) Very Slow!
  • 34. About Others
  • 35. About Others# Find Top 10 Of The Talkiest UserSELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10# Find Top 10 Of The Newest FeedsSELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 36. Redis?
  • 37. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 38. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 39. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find User StringsSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 Strings are the most basic kind of Redis value. Redis Strings are binary# Update UserUPDATE `users` SET `name` = string can contain any kind of data, for safe, this means that a Redis hellolucky123 WHERE `users`.`id` = 1# DeleteaUser image or a serialized Ruby object. instance JPEG - http://redis.io/topics/data-typesDELETE FROM `users` WHERE `users`.`id` = 1
  • 40. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hellolucky set user:1:name set user:1:feeds_count 0UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 set user:1:created_at 1350906890# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 41. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hellolucky set user:1:name set user:1:feeds_count 0UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 set user:1:created_at 1350906890# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 42. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find User KeySELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hellolucky set user:1:name set user:1:feeds_count 0UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 set user:1:created_at 1350906890# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 43. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find User ValueSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hellolucky set user:1:name set user:1:feeds_count 0UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 set user:1:created_at 1350906890# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 44. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User Hash is betterUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 45. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 Hashes# Update User Redis Hashes are maps between string fields and string values, so theyUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 are the perfect data type to represent objects.# Delete User - http://redis.io/topics/data-typesDELETE FROM `users` WHERE `users`.`id` = 1
  • 46. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 47. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 48. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* KeyFROM `users` WHERE `users`.`id` = 1# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 49. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find User FieldsSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 50. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 Values# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 51. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 52. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERESets Sorted `users`.`id` = 1# Update User Sorted Sets are, similarly to Redis Sets, non repeating RedisUPDATE `users` SET `name`difference is thatWHERE `users`.`id`a=Sorted collections of Strings. The = hellolucky123 every member of 1 Set is associated with score, that is used in order to take the sorted# Delete UserDELETE FROM from the smallest to the greatest score. While members set ordered, `users` WHERE `users`.`id` = 1 are unique, scores may be repeated. - http://redis.io/topics/data-types
  • 53. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 54. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User feeds_count hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 55. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User user_id hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 56. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update User For top 10 of the talkiest user hmset user:1 name hellolucky feeds_count 0 created_at 1350906890UPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1 zadd global:user_ids 0 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 57. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 58. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete Userhmget user:1 name feeds_count created_atDELETE FROM `users` WHERE `users`.`id` = 1
  • 59. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete User HGETALL is betterDELETE FROM `users` WHERE `users`.`id` = 1
  • 60. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete Userhgetall user:1DELETE FROM `users` WHERE `users`.`id` = 1
  • 61. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 62. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUEShset user:1 name hellolucky123(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 63. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)# Find UserSELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 64. About User# Create UserINSERT INTO `users` (`name`, `feeds_count`, `created_at`) VALUES(hellolucky, 0, 1350906890)delFind User# user:1SELECT `users`.* FROM `users` WHERE `users`.`id` = 1# Update UserUPDATE `users` SET `name` = hellolucky123 WHERE `users`.`id` = 1# Delete UserDELETE FROM `users` WHERE `users`.`id` = 1
  • 65. About Relation# Set Following UserINSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 2)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 3)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 4)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 5)
  • 66. About Relation# Set Following UserINSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 2)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 3) SetsINSERT are the most basic kind of Redis value. Redis Strings are binaryStrings INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 4) this means that a Redis string can contain any kind of data, forsafe,instance a JPEG image or a serialized Ruby object.INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 5) - http://redis.io/topics/data-types
  • 67. About Relation# Set Following UserINSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 2)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 3)INSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUESsadd user:1:follow_ids 2(1, 4) user:2:follower_ids 1saddINSERT INTO `user_follow_relations` (`user_id`, `follow_id`) VALUES(1, 5)
  • 68. About Relation# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 69. About Relation# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 70. About Relation# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHEREsmembers user:1:follow_ids`user_follow_relations`.`follow_id` = 1
  • 71. About Relation# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 72. About Relation # Find Following User Idssmembers user:1:follower_idsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 73. About Relation# Find Following User CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 74. About Relation# Find Following User CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 75. About Relation# Find Following User CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =scard user:1:follow_ids`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 76. About Relation# Find Following User CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Follower CountSELECT COUNT(*) FROM `users` INNER JOIN`user_follow_relations` ON `users`.`id` =`user_follow_relations`.`user_id` WHERE`user_follow_relations`.`follow_id` = 1
  • 77. About Relation # Find Following User Countscard user:1:follower_ids `users` INNER JOIN SELECT COUNT(*) FROM `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1 # Find Follower Count SELECT COUNT(*) FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`user_id` WHERE `user_follow_relations`.`follow_id` = 1
  • 78. About Relation# Find User1s Following UsersSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find User12 Following UsersSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 2# The Same Following UsersUser1s Following Users & User2s Following Users
  • 79. About Relation # Find User1s Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 1sinter user:1:follow_ids user:2:follow_ids # Find User12 Following Users SELECT `users`.id FROM `users` INNER JOIN `user_follow_relations` ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE `user_follow_relations`.`user_id` = 2 # The Same Following Users User1s Following Users & User2s Following Users
  • 80. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 81. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 82. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 83. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 84. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890) Store feed# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 85. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 86. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update Counter Push feed id toUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + followershmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 87. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update Counter I’m lazy to list, itUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) + should more.....hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 88. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1rpush global:feed_ids 1
  • 89. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update Counter ListsUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE Lists are simply lists of strings, sorted by insertion order. It is Redis `users`.`id` = 1possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list. - http://redis.io/topics/data-types
  • 90. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +hmset feed:1 user_id=11content description created_at 13509068901 WHERE `users`.`id`sadd user:2:follow:feed_ids 1 For top 10 of the newest feedsrpush global:feed_ids 1
  • 91. About Feed# Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 92. About Feedhincrby user:1 feeds_count 1zincrby global:user_ids 1 1 # Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 93. About Feed Update counterhincrby user:1 feeds_count 1zincrby global:user_ids 1 1 # Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 94. About Feedhincrby user:1 feeds_count 1zincrby global:user_ids 1 1 # Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 95. About Feed For top 10 of the talkiest userhincrby user:1 feeds_count 1zincrby global:user_ids 1 1 # Create FeedINSERT INTO `feeds` (`user_id` , `content`, `created_at`) VALUES (1,description, 1350906890)# Update CounterUPDATE `users` SET `feeds_count` = COALESCE(`feeds_count`, 0) +1 WHERE `users`.`id` = 1
  • 96. About Feed# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Following Users FeedsSELECT `feeds`.* FROM `feeds` WHERE user_id in (2,3,4,5)
  • 97. About Feed# Find Following User IdsSELECT `users`.id FROM `users` INNER JOIN `user_follow_relations`ON `users`.`id` = `user_follow_relations`.`follow_id` WHERE`user_follow_relations`.`user_id` = 1# Find Following Users FeedsSELECT `feeds`.* FROM `feeds` WHERE user_id in (2,3,4,5)smembers user:1:follow:feed_ids
  • 98. About Others# Find Top 10 Of The Talkiest UserSELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10# Find Top 10 Of The Newest FeedsSELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 99. About Others# Find Top 10 Of The Talkiest UserSELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10# Find Top 10 Of The Newest FeedsSELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 100. About Others# Find Top 10 Of The Talkiest UserSELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10# Find Top 10 Of The Newest FeedsSELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10zrevrange global:user_ids 0 9
  • 101. About Others# Find Top 10 Of The Talkiest UserSELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10# Find Top 10 Of The Newest FeedsSELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 102. About Otherslrange global:feed_ids 0 9# Find Top 10 Of The Talkiest UserSELECT `users`.* FROM `users` ORDER BY feeds_count LIMIT 10# Find Top 10 Of The Newest FeedsSELECT `feeds`.* FROM `feeds` ORDER BY created_at desc LIMIT 10
  • 103. Conclusion
  • 104. MySQL + Redis = Lego
  • 105. QA