In the last blog I introduced some Redis basics. In this blog I will cover the datatypes supported which is extremely important in order to create the correct data model.
Data Types
Redis has the following different datatypes that can be used for a keys value.
- String
- Hashes
- List
- Set
- Sorted Set
- HyperLogLogs
- Geospatial
- Bitmaps
Below I discuss what the different datatypes are and the commands available for each type. Pay close attention to the supported commands because these can help in determining the type to use for a specific query required. For example, a List does not have a SUNION or SINTER command but a Set does.
String
String is not the best term for this type because it is used to hold numeric types as well. Internally it is held as a byte array.
Anyhow, this is a simple scalar that can hold a single value or it can hold an XML or a JSON document for example. So it is not limited to a single value.
Here are some of the useful commands. See the command reference for more information.
SET, SETNX, MSET, MSETNX
GET, MGET, GETSET |
|
PSETEX, SETEX |
|
INCR, INCRBY, INCRBYFLOAT DECR, DECRBY |
|
Below we created a couple of keys and queried them back. The CLI takes care of the single and double quote issue by adding the escape character for us.
Note: the keys are case sensitive.
Hashes
I showed above that a string could hold a JSON document however; hashes are useful for representing objects. Hashes contain one or more fields.
Here are some of the useful commands. See the command reference for more information.
HSET, HSETNX, HMSET
HGET, HMGET |
|
HLEN, HKEYS, HVALS, HGETALL |
|
HEXISTS, HDEL |
|
HINCRBY, HINCRBYFLOAT |
|
List
A list is just a sequence of ordered elements. You can think of it as an array however; internally Redis stores them as a Linked List. They are designed this way so adding new elements to the end of a list is really fast. The downside is that indexing into the list can be slow. When this is required, Sorted Sets are a better option. See Sorted Sets further down. The values do not have to be unique.
Here are some of the useful commands. See the command reference for more information.
LPUSH, RPUSH LPUSHX, RPUSHX |
|
LLEN, LRANGE |
|
LINDEX, LSET, LINSERT |
|
LREM, LTRIM |
|
LPOP, RPOP |
|
Set
Sets are unordered collections of strings. They cannot have duplicate values and are good for expressing relations between objects. For example; we have a customer object that has a unique numerically key. What if we needed to retrieve the customers based on country. Redis does not have the concept of indexes, therefore have a country set with a list of the customer ids allows us to filter on country. Redis calls this tagging.
Here are some of the useful commands. See the command reference for more information.
SADD, SMOVE, SREM, SPOP |
|
SCARD, SMEMBERS, SISMEMBER, SRANDMEMBER |
|
SUNION, SUNIONSTORE |
|
SINTER, SINTERSTORE |
|
SDIFF, SDIFFSTORE |
|
Sorted Set
Sorted sets are a data type which is similar to a mix between a Set and a Hash. Sorted sets are implemented via a dual-ported data structure containing both a skip list and a hash table. Adding elements and retrieving sorted elements is extremely fast.
Here are some of the useful commands. See the command reference for more information.
ZADD, ZINCRBY |
|
ZCARD, ZCOUNT, ZSCORE, ZRANK |
|
ZRANGE, ZRANGEBYLEX, ZRANGEBYSCORE, ZLEXCOUNT |
|
ZREM, ZREMRANGEBYLEX, ZREMRANGEBYRANK, ZREMRANGEBYSCORE |
|
ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK, ZREVRANGEBYLEX |
|
ZINTERSTORE, ZUNIONSTORE |
|
HyperLogLogs
In order to understand HyperLogLogs it is probably easier with an example. On the face of it they look like a set however; they are very space efficient, extremely fast but they come at a cost. They are not one hundred percent accurate however; that is not an issue for some queries. Here is the example. Say you wanted to track the number of cars that crossed a traffic intersection. Does it matter if the value is exactly 50 million or 51 million? A rough estimate may suffice. By using HyperLogLogs we can save a tremendous amount of memory. Internally the data is stored as a string so the same string commands can be used on it.
Here are the commands. See the command reference for more information.
PFADD, PFCOUNT |
|
PFMERGE |
|
Geospatial
Geospatial data is about tracking an entities location in the world. Locations are mapped to Latitude and Longitude. Under the covers the data is stored in a Sorted Set so the same pros and cons apply. Also you can use the Sorted Set commands to carry out actions on the data.
Here are the commands. See the command reference for more information.
GEOADD, ZREM |
|
GEOHASH, GEOPOS |
|
GEODIST |
|
GEORADIUS, GEORADIUSBYMEMBER |
|
Bitmap
A Redis bitmap is just a string under the covers. They are really just a type that is used for tracking a huge amount of data at a bit level in order to reduce the memory footprint.
SETBIT, GETBIT |
|
BITPOS, BITCOUNT |
|
BITOP, BITFIELD |
|
Commands that Effect all Datatypes
See the command reference for more information.
DEL, EXISTS, KEYS, RANDOMKEY, RENAME, RENAMENX |
|
EXPIRE, EXPIREAT, PEXPIRE, PERSIST, TTL, PTTL, |
|
TYPE, OBJECT, DUMP, RESTORE |
|
In the next blog I will begin creating and querying data in Redis.