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/latest/develop/data-types/strings/
- Redis Strings Explained - Redis
Type: Lists
Usage: Implement stacks and queues, build queue management for background worker systems.
Redis lists are linked lists of string values.
1 | rpush mylist A ~ rpush adds a new element into a list on the left (at the head) |
Type: Sorted Set
Usage: Leaderboards, Rate limiters
A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically.
1 | zadd hackers 1940 "Alan Kay" ~ adds "Alan Kay" to the collection with score 1940 |
Type: Set
Usage: Track unique items, Represent relations
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: Represent basic objects and to store groupings of counters
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/latest/develop/data-types/hashes/
- https://redis.io/commands/?group=hash
- https://redis.io/commands/hincrby/
Type: Stream
Usage: Event sourcing, Sensor monitoring, Notifications
Models a log data structure in a more abstract way.
Type: Geospatial
Usage: Redis geospatial indexes let you store coordinates and search for them, finding nearby points within a given radius or bounding box.
- https://redis.io/docs/latest/develop/data-types/geospatial/
- https://redis.com/redis-best-practices/indexing-patterns/geospatial/
- https://redis.io/commands/geopos/
Type: Bitmaps
Usage: Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type which is treated like a bit vector. Since strings are binary safe blobs and their maximum length is 512 MB, they are suitable to set up to 2^32 different bits.