Learning notes related to Redis data store
Redis
is lightweight, persistent key value data storage
with flexible mechanism for organizing your data in terms of data structures
Redis is simple and performant
with Docker
$ docker pull redis
$ docker run redis
to enter container
$ docker exec -it <container_id> sh
-
Caching
As value access in redis is super fast it's popular alternative for varnish, memcached.
-
Real time data
Redis often used for services that works with real time data. It has flexible mechanism for setting TTL for data. Due to exposing common data types you can achieve good perfomance when read-write your data.
Redis not a simply key-value storage like Memcached it is better called data structures service.
Redis has set of common builtin data types each has own set of accepted commands.
Most common one are String
, Set
, Hash
, SortedSet
Common commands can be found here
-
Mostly you store string key-value pairs. For storing complex json like structures you just use
json.loads
for stringifing it and store as pure string -
If you need more control over qeuring you can store objects like
Hash
structure. But be aware that Redis do not allow you to store nestedHashes
. Probably you need recap you data relation model for such cases. -
For storing uniq uniq values you can use
Set
structure. Set has all common operations like union joins intersect , etc. -
Common way for storing separate records of data but related to same domain like Users or Twits is by prefixing each record with
users:
. So sample user object can be stored as a keyusers:{some_user_id}
. For quering such data you can usekeys
command with pattern, but it's not a recommended way. Better way will be to store user as a hashes, but if you need strctly this structure you can create separateSet
that will hold user related keys. So then querining operation will beO(x)
wherex
isSet
length instead ofO(n)
wheren
is global number of keys. -
There are lots of other recipes how to use Redis, but the beauty of Redis is that you can come with a way of how to use it it that nobody thought about before
- Little Redis book
- Official Redis documentation
aioredis
for async Python client