Redis data types
Reids is not a plain-value store, actually it is a data structures server, supporting different kinds of values.
Redis keys
- Any binary sequences can be used as a key, from a string to the content of a JPEG file.
Rules about keys
- Very long keys are not a good idea. (in order to antenuate the key-comparison cost)
– Hashing a very long key first (SHA1) is a better idea. - Very short keys are often not a good idea, too short to be readable.
– “u1000flw” vs “user:1000:followers” - The maximum allowed key size is 512MB.
Redis Strings
Operations on Redis Strings
- set, get, getset
- Parsing the Redis String Values as an integer/float
- incr, decr, incrby, decrby, incrbyfloat
- Altering and querying key space
- commands useful in order to interact with space of keys, not defined on particular types
- exists, del, type
- Setting or retrieving the value of multiple keys in a single command
- mset, mget
- Bitmaps
- constant-time single bit operations: setbit, getbit
- operations on groups of bits: bitop, bitcount, bitpos
Redis Lists
- Redis lists are implemented via Linked Lists.
Common use cases for lists
- Remember the latest updates posted by users into a social network
- Communication process, using a consumer-producer patern
Operations on Redis Lists
- Push and Pop
- rpush, lpush, rpop, lpop
- Retrieving a range of elements at one time
- lrange
- Capped lists
- ltrim
- Blocking operations on lists
- Why need these operations?
– Non-blocking operations may lead to useles commands or unnecessary waiting time. - blpop, brpop
– BLPOP and BRPOP are able to block waiting for elements from multiple lists.
- Why need these operations?
Redis Hashes
- Hashes are handy to represent objects, with the keys representing the objects’ attributes.
- Setting or retrieving the value (one or multiple)
- hmset, hmget, hset, hget
- Performing opertions on individual fields
- hincrby, hincrbyfloat
- hexists, hkeys, hvals, hgetall
Redis Sets (unordered)
- Setting or retrieving
- sadd, srem
- Operations on multiple sets
- sdiff, sinter, sunion, sdiffstore, sinterstore, sunionstore
Redis Sored Sets
- Setting or retrieving
- zadd, zrange, zrevrange
- using withscores to return scores in zrange order
- Operating on ranges
- zrangebyscore, zremrangebyscore, zrank, zrevrank,
– zrangebyscore hackers -inf 1950
- zrangebyscore, zremrangebyscore, zrank, zrevrank,
- Lexicographical scores
- zrangebylex, zrevrangebylex, zremrangebylex, zlexcount
Redis HyperLogLogs
A HyperLogLog is a probabilistic data structure used in order to count unique things (technically this is referred to estimating the cardinality of a set).
- pfadd, pfcount