Redis Database – Part 03 – Datatypes

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

  • Set value, Set value if key does not exist, Set multiple values, Set multiple values if none of the keys exist
  • Get value, Get multiple values, Set value, return old value

PSETEX, SETEX

  • Set with expiry (MS), Set with expiry (SECS)

INCR, INCRBY, INCRBYFLOAT

DECR, DECRBY

  • Increment integer, Add to integer, Add to float
  • Decrement integer, Subtract from integer

 

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

  • Set field value, Set field value if field does not exist, Set multiple field values
  • Get field value, Get multiple field values

HLEN, HKEYS, HVALS, HGETALL

  • Get Number of fields, Get all field keys, Get all field values, Get all fields and values

HEXISTS, HDEL

  • Check field exists, delete field

HINCRBY, HINCRBYFLOAT

  • Increment field integer value, Increment field float value

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

  • Add Value at beginning, Add value at the end
  • Only push if key already exists

LLEN, LRANGE

  • Get number of values, Get values from Start to Stop

LINDEX, LSET, LINSERT

  • Get a value by index, Set a value by index, Add a value before or after another

LREM, LTRIM

  • Delete element from list, Trim list by range

LPOP, RPOP

  • Delete and Get the first element, Delete and Get the last element

 

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

  • Add one or more members, Move a member from one set to another, Remove one or more members, Remove and return one or multiple random members

SCARD, SMEMBERS, SISMEMBER, SRANDMEMBER

  • Get number of members, Get all members, Test if member exists, Get one or more random members

SUNION, SUNIONSTORE

  • Get all keys from all sets, no duplicates, same operation but store results in a new key

SINTER, SINTERSTORE

  • Get keys that exist in all sets only, same operation but store results in a new key

SDIFF, SDIFFSTORE

  • Return keys from the first set that are not in the subsequent sets, same operation but store results in a new key

 

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

  • Add one or more members or update score, Increment the score of a member, Remove one or more members

ZCARD, ZCOUNT, ZSCORE, ZRANK

  • Get number of members, Count members within sort key (score) range, Get the score associated with the given member, Determine the index of a member

ZRANGE, ZRANGEBYLEX, ZRANGEBYSCORE, ZLEXCOUNT

  • Get members sorted by sort key (score), Return a range of members by lexicographical range, Return a range of members by score, Count the number of members between a given lexicographical range

ZREM, ZREMRANGEBYLEX, ZREMRANGEBYRANK, ZREMRANGEBYSCORE

  • Remove one or more members, Remove all members between the given lexicographical range, Remove all members within the given indexes, Remove all members within the given scores

ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANK, ZREVRANGEBYLEX

  • Return a range of members by index with scores ordered from high to low, Return a range of members by score, with scores ordered from high to low, Determine the index of a member with scores ordered from high to low, Return a range of members by lexicographical range, ordered from higher to lower strings

ZINTERSTORE, ZUNIONSTORE

  • Get keys that exist in all sets only and store the resulting sorted set in a new key, Add multiple sorted sets and store the resulting sorted set in a new key

 

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

  • Append one or more elements, count number of elements

PFMERGE

  • Merge elements from multiple keys

 

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

  • Append one or more elements, count number of elements, remove a member

GEOHASH, GEOPOS

  • Returns a hash of the sort key (Score), return Latitude and Longitude of a member

GEODIST

  • Return distance between two members

GEORADIUS, GEORADIUSBYMEMBER

  • Return members near a specific point

 

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

  • Sets or clears bit a string offset, Gets bit value at string offset

BITPOS, BITCOUNT

  • Find first bit set or cleared in a string, count set bits in string

BITOP, BITFIELD

  • Perform bitwise operations, Perform bit field integer operations on strings

Commands that Effect all Datatypes

See the command reference for more information.

DEL, EXISTS, KEYS, RANDOMKEY, RENAME, RENAMENX

  • Delete a key, Test for a keys existence, find all keys matching a pattern, Get a random key, Rename a key, Rename a key if a key of the same name does not exist

EXPIRE, EXPIREAT, PEXPIRE, PERSIST, TTL, PTTL,

  • Set TTL of a key in secs, Set TTL using a UNIX timestamp, Set TTL in milliseconds, Remove expiration from key, Get TTL of a key in seconds, Get TTL of a key in milliseconds

TYPE, OBJECT, DUMP, RESTORE

  • Get the datatype of a key, Inspect internals of a key, Dump a serialized version of the key, Create a key using the value from DUMP

 

In the next blog I will begin creating and querying data in Redis.