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 | docker network create --driver bridge redis-bridge-network |
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 | set mykey somevalue |
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 | > set 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 | KEY VALUE |
- https://redis.io/commands/set
- https://redis.io/docs/manual/data-types/data-types-tutorial/#strings
- Redis Strings Explained - Redis
Type: Lists
Usage: ?.
Implemented as linked list.
1 | rpush mylist A ~ rpush adds a new element into a list on the left (at the head) |
Type: Sorted Set
Usage: ?.
A collection of unique strings, sorted by score
1 | zadd hackers 1940 "Alan Kay" ~ adds "Alan Kay" to the collection with score 1940 |
- Redis Sorted Sets Explained - Redis
- https://redis.io/docs/manual/data-types/data-types-tutorial/#sorted-sets
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 | sadd myset 1 2 3 ~ adds new elements to a set |
Type: Hash
Usage: ?.
Collection of key value pairs. These are maps between string fields and string values used to represent objects.
1 | hmset user:1000 username antirez birthyear 1977 verified 1 ~ sets multiple fields of the hash |
- https://redis.io/docs/manual/data-types/data-types-tutorial/#hashes
- https://redis.io/commands/?group=hash
- https://redis.io/commands/hincrby/
Type: Stream
Usage: ?.
Models a log data structure in a more abstract way.
Type: Geospatial
Usage: ?.
- https://redis.com/redis-best-practices/indexing-patterns/geospatial/
- https://redis.io/commands/geopos/
Type: Bitmaps
Usage: ?.
Type: Hyperloglogs
Usage: ?.