Redis Simplified
Jun 05, 2020
What can I do with it?
Redis is many things but essentially its an in-memory key-value store.
Conventionally defined as:
An in-memory, fast non-relational database with key:value mapping. It has persistent storage, replication to scale read performance & client side sharding.
All this means:
- You can store key-value pairs to disk
- You can replicate redis nodes so that you can read faster
- You can also shard redis nodes.
- You can do master/slave replication as well.
Saving Key value pairs
Let’s set a key value pair.
$ redis-cli
redis 127.0.0.1:6379> set hello world
OK
Le magic. You’ve now set hello:world.
Run GET hello
to get world back.
[ You don’t need to use the CLI. There are redis ‘clients’ for most common languages like Python,Ruby and JS]
PS: Commonly in redis, we use the format foo:bar:variable
for keys. Remember since strings are hashed, you can keep a big string and you wont lose huge perfomance, but try to balance readability and performance.
Core Data Structures in Redis
There are 5 main data structures in redis:
1. Strings
This looks like your classical key:value pairs. You can do things like SET
,GET
, & DEL
.
2. Lists
This is where things get interesting. You can store Linked-Lists in redis and do common things like push, pop, access by index, & retrieve the whole list..
3. Sets
Super obvious, but you can store a unique set of strings, check for membership, & view all elements.
4. Hashes
Hashes store a mapping of key:value pairs, similar to a dictionary in python. You can access individual elements of the hash, or set the value.
5. Sorted Set (ZSET)
Redis offers a unique data-structure. Its a ‘sorted’ dictionary in which you can store key:value pairs [values are called scores] except the values must be floating point numbers. You can access the ZSet via the key but also by the sorted order of the values (scores).
Remeber the dictionary is sorted by the values not the key.
What can I do with it?
1. Cache [but a persistant one]
Save session caches or use it for a Full-Page Cache. The sky’s the limit.
2. Publish-Subscribe
Redis has an inbuilt Publisher-Subscriber feature where you need to only subscribe to a channel to get the messages.
3. Somekind of Counter
Go wild, use it as a counter for page views or likes or a leaderboard or any kind of statistic. Its easy enough to make a key for each webpage you have and increment/decrement views via the inbuilt commands (INCR and DECR).
4. Machine Learning
You need a large in-memory data system, which is also fast? Well, redis does this. Its inbuilt RedisML may be what you are looking for, else build your own solution and use redis as a way to store your data.
5. Store Objects in a key:val fashion
Need to save your user data? Keep them in a Hash! Its simple enough. Make a key like, user:19283
and store name, email-id, visits.
6. User Network
Need to build a facebook or LinkedIn kind of network? Build it using sets. Using a key like friends:user:19283
add user 19283’s friends via their unique ID. You can easily express Friend-of-Friend functionality via a Breadth first search.
7. Queues
Implement any kind of queue via lists in Redis. Maybe you want to build a Job Queue, or a processing queue, doesnt matter since
Share