Introduction
The article explains Redis, Redis stands for Remote Dictionary Server. The read and writes are very fast in Redis because it stores the data in the memory. In the article, we will cover
- What Redis is and basics
- Data Types in Redis and Sample Redis Commands
Although there are many commands that exist in Redis but will focus on the insertion and view aspect of elements in Redis.
What is Redis
Think of Redis as a big HashMap which stores Key-Value pairs. Redis doesn’t store the data on the disk. It stores the data in memory that’s why its performance is fast. It’s not good to store the structured data in memory as you can try to imagine it as a big JSON file sitting in memory with lots of key-value pairs in it.
As mentioned it’s a big HashMap think of Redis as a cache, and it should not be used as persistent storage.
The setup of Redis is straightforward, upon installation it gives us one folder and a bunch of .exe files inside it. First, we need to install the server using the command
redis-server.exe
Using the Redis CLI for accessing Redis,
redis-cli.exe
Data Types in Redis and Sample Redis Commands
Redis supports various types of data structures as values, the key in Redis is a binary-safe String with a max size of 512 MB. Redis supports the following datatypes
- String
- List
- Set
- Sorted Set
- Hash
String
The string is the most basic type of value that a cache can store. For Storing SET & for Retrieving GET, should be used.
Redis also allows setting “Time To live” (TTL) for every key
The value returned is -1 which indicates the key will never expire, but Redis allows to set expiration of any key
It returned 1, which means the key is marked for expiration. On querying the same key further after 5 seconds (nil) will be returned by Redis
List
Redis allows to store collection of Strings, the List implementation in Redis is of LinkedList type. There are a few commands in List that can be very handy while dealing with Lists
LPUSH/RPUSH
LPUSH inserts the elements at the head of the list
So, the in-memory representation is, employees -> [James -> Trent -> Mick -> David]
RPUSH inserts the elements at the tail of the List.
In-memory representation is, Managers -> [Sam -> David -> Mick]
LPOP/RPOP
LPOP command removes an element from the head of the List. LPOP employees will remove “James” first then “Trent” and so on. RPOP command removes an element from the tail of the list. RPOP managers will remove “Mick” first, then David, and so on.
LRANGE
This command is used to list elements, this command expects start and end index because the list contains hundreds of thousands of elements then without end index it will be non-performant.
There are a lot more commands in Redis for modification purposes, it’s difficult to cover all of them in this tutorial. I am coming up with a Cheat Sheet format for Redis.
Set
Set is a collection that doesn’t allow duplicate elements, List allow duplicates. Internally Redis Set in elements is backed by HashTable. Also, the set doesn’t store the elements in order.
SADD
This command allows adding elements to the set.
Upon adding a duplicate element Redis returns 0.
SMEMBERS
Allows us to see all the elements in Set
SISMEMBER is used to check if element exists 1 will be returned else 0.
Sorted Set
Sorted Set stores the elements in sorted order, Sorted Set in Redis are known as ZSets. Every element of a Sorted Set is associated with a score, that score is respected while maintaining the order. If the scores of multiple elements are the same, then it is ordered lexicographically.
ZADD command is used to add the elements in the set based on the score.
First the score then the value is added to the Set, in the example, score of John is 1 and Hari is 2.
ZRANGEBYSCORE
This command takes the start and end index and fetches the elements in the range of scores.
John has a score of 1 and Hari is 2, both are falling in the range that’s why both returned.
Hash
Hash is a key-value pair-based data structure that always has constant run time complexity.
HMSET
Is used to add elements to the Hash,
HGETALL is used to retrieve all the values in the hash based on the key.
HGETALL emps
HGET is used to retrieve the element based on the key in a hash.
Summary
The article covered the basics of Redis. In the next articles on Redis will cover Pub/Sub model on Redis and more advanced concepts.