Redis Types

WIP

Redis can be used as a database, cache store, message broker, streaming engine and probably more! I was interested in the types so had a hoon, hopefully what I learn here I can apply to Redis Administrator.

Playground - Docker

I did not want to install Redis locally and containers are cool so brought up a bride so the containers were on the same network, redis server and redis cli

1
2
3
4
5
6
docker network create --driver bridge redis-bridge-network

docker run --name red-srv -d -p 6379:6379 --network redis-bridge-network redis redis-server --appendonly yes

# -it mean interactive, --rm means clean up when it exits
docker run -it --network redis-bridge-network --rm redis redis-cli -h red-srv

Keys

The suggested redis keys are object-type:id, for Redis Administrator (which is the other way around) I used 1:2:seed_string. 1 and 2 could be something sensible to the business (like a user and department id) and seed_string could be the human readable explanation of that data for example the department name.

Searching on a partial key could then be something like 1:* which would match anything starting with 1:. In general redis will create a key/value if it doesnt exist should you try write to it.

Types

Love a good type, I thrive on structure!

Types: String

Usage: Any kind of data as they are binary safe.

  • Example store could be: integers, binary, csv, serialized json, xml, images, video, documents, sound
  • Practice usage: caching responses that dont change often and you would then include the ttl. Could be api reponse, session storage, html pages
    • Can also be used for a counter as it has built in increment/decrement support (incr, incrby)

Implemented as key value pair, this is the fundamental redis data type. If the key exists it is replaced regardless of its type.

1
2
3
4
5
6
7
8
set mykey somevalue
set mykey somevalue EX 5 ~ sets key/value with the ttl of 5 seconds
get mykey
exists mykey
del mykey
type mykey
expire mykey 5 ~ 5 seconds
ttl mykey

CLI examples with a complex key and JSON serialized value. Although this is fine, a type of Hash may be better suited, see futher below.

1
2
3
4
5
> set 1:2:seed_string {"Id":6338,"SomeGuid":"cfb9aace-08e7-4b7a-9e3a-8775d366240a","DateTime":"2022-02-19T04:00:57.072271+00:00"}
OK

> get 1:2:seed_string
{"Id":6338,"SomeGuid":"cfb9aace-08e7-4b7a-9e3a-8775d366240a","DateTime":"2022-02-19T04:00:57.072271+00:00"}

Example data from Redis Administrator‘s seed function could look as follows where VALUE is a json serialized object.

1
2
3
KEY               VALUE
1:2:seed_string {"Id":6338,"SomeGuid":"cfb9aace-08e7-4b7a-9e3a-8775d366240a","DateTime":"2022-02-19T04:00:57.072271+00:00"}
2:3:seed_string {"Id":2004,"SomeGuid":"e7e80043-bf6c-47bf-87b6-e7e801994d83","DateTime":"2022-02-19T04:00:57.2217754+00:00"}

String Type : Redis Administrator

Type: Lists

Usage: ?.

Implemented as linked list.

1
2
3
4
5
6
7
8
rpush mylist A                     ~ rpush adds a new element into a list on the left (at the head)
rpush mylist B
rpush mylist 1 2 3 4 5 "foo bar"
lpush mylist first ~ lpush adds a new element into a list on the right (at the tail)
lrange mylist 0 -1 ~ lrange extracts ranges of elements
~ takes two indexes, the first and the last element of the range to return.
rpop mylist ~ retrieving the element from the list, and eliminating it from the list, at the same time.
~ pop elements from left and right

Type: Sorted Set

Usage: ?.

A collection of unique strings, sorted by score

1
2
3
4
5
6
7
8
zadd hackers 1940 "Alan Kay"              ~ adds "Alan Kay" to the collection with score 1940
zadd hackers 1957 "Sophie Wilson"
zadd hackers 1953 "Richard Stallman"
zadd hackers 1 "carl" 2 "paton" ~ adding a collection by score/value
zrange hackers 0 -1 ~ extracts ranges of elements
zrevrange hackers 0 -1 ~ extracts ranges of elements ordering the score desc
zrange hackers 0 -1 withscores ~ extract with scores
zrangebyscore hackers -inf 1950 ~ return by range with a score between negative infinity and 1950

Type: Set

Usage: ?.

Redis Sets are an unordered collection of strings. In Redis, you can add, remove, and test for the existence of members in O(1) time complexity.

1
2
3
4
5
sadd myset 1 2 3             ~ adds new elements to a set
sadd myset 42
smembers myset ~ returns all the members of the set value stored at key.
spop myset ~ removes and returns one or more random members from the set value store at key.
~ not sure about the random, when I tested it kept removing at the end

Type: Hash

Usage: ?.

Collection of key value pairs. These are maps between string fields and string values used to represent objects.

1
2
3
4
hmset user:1000 username antirez birthyear 1977 verified 1     ~ sets multiple fields of the hash
hget user:1000 username ~ gets a single field
hmget user:1000 username birthyear no-such-field ~ gets an array of values
hincrby user:1000 birthyear 10 ~ increments the number stored at field in the hash stored at key by increment.

Type: Stream

Usage: ?.

Models a log data structure in a more abstract way.

Type: Geospatial

Usage: ?.

Type: Bitmaps

Usage: ?.

Type: Hyperloglogs

Usage: ?.

References