Redis: When to use what data structure ? [P1]



What is Redis

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain stringshasheslistssets and sorted sets.


Why i wrote this blog entry

 + Redis is a very popular nosql key - value store in the world
+ Not much developer can understand and use all Redis advandce data structure
+ Common terrible mistake when developer use wrong data structure.


Redis data structure

Key
+  Url: http://redis.io/commands#generic
+ Fearture: Set, get, add time to live, remove
+ When to use: Simple key-value storage, fast insert, fast lookup, set expire
* Store simple key - value
* Store binary object like content of an short film (up to 512MB)

String
+ Url: http://redis.io/commands#string
+ Fearture: Get length, Append, set bit, get bit, bitcount, bitwtop, bitpos, multi set, multi get, increment, decrement
+ When to use: Common string function, bitwise operation
  * Atomic increment, decrement operation 

Hash
+ Url: http://redis.io/commands#hash
+ Data structure: Map (Dictionary)
+ Fearture: Map set, get, get all key, get all key - value, remove
+ When to use: Multi key – value storage, fast insert, fast lookup by key, fast remove, set expire or delete one time for all key in same hash name.
  * Store millions of key - value objects in a small Redis instance
  * Dictionary

List
+ Url: http://redis.io/commands#list
+ Data structure: Linked list
+ Fearture: Put, get, get by range, pop, remove, sort, get length
+ When to use: 
  * Simple collection or queue (LPUSH, LPOP or BLPOP),
  * Queue with fixed length (LPUS, LTRIM, LPOP or BLPOP)
  * Remember the latest updates posted by users into a social network.
  * Communication between processes, using a consumer-producer pattern where the producer pushes items into a list, and a consumer (usually a worker) consumes those items executing actions.

Set

+ Url: http://redis.io/commands#set
+ Data structure: Hash set (I guess)
+ Fearture: Add, add multiple, get, multi get, get random, get all, pop, subtract multiple, intersect multiple, remove, move, sort, get length
+ When to use: Add, remove, check existed in O(1), subtract multiple, intersect multiple
  * A list of unique value
  * Check and get if collections have same members
  * Random get value from collection

Sorted Set
+ Url: http://redis.io/commands#sorted_set
+ Fearture: Add, add multiple, get, multi get, get random, get all, pop, subtract multiple, intersect multiple, remove, move, sort, get length
+ When to use: Add, remove, check existed in O(1), subtract multiple, intersect multiple
  * A list of unique and ordered value
  * Get top n or range of elements from your score board in a massive online game

In next part, we will discuss about Pub/sub, HyperLogLog and other Redis fearture (Transaction, Scripting
).

Comments