It is like one giant JSON object that has key value pairs
So what does key-value signify basically when you work with relational databases there you have to define a proper schema, you have to define if a particular key is primary, and one has to define that a particular column will not accept a null value but that is not the case with Redis its a completely schema less database it stores information in the form of key-value where the key is String and the value could be anything number, blob, video, etc.
It is a In memory database that makes it completely dependent on main memory which brings out the following advantages
- It makes data retrieval fast
- latency is in sub-milli-seconds
- The most important one is we can cache data
The only one disadvantage is that data loss can occur in case of server failure.
Redis is a single-threaded system one might thought that single threaded database might not be as much effective as multi-threaded database but that’s not true a single-threaded system still scales because one can spin up as many instances as one wants .
Optional durability
there are two ways to persist data in Redis and both happen in the background asynchronously
-
Journaling
So what does journaling do suppose if you write a key-value pair obviously it will write it to memory but also at the same time time it will write it to the disk memory.
-
Snapshotting
the other way it persist data is by taking snapshot i.e copying of the data at one point of time and storing it in disk. Suppose if system failure occurs in between transaction of data then it will cause data loss, only that data will be stored in disk which was snapshotted earlier if snapshotting was enabled .
Let’s move to the coding part I will be using Docker to show the commands so that one can quickly get started with Redis database.
- Firstly will spin up Redis instance on docker
sudo docker run --name redisdatabase -p 6379:6379 redis
- Now to get into the redis-cli run the following command
sudo docker exec -it redisdatabase redis-cli
- Now comes the most hardest part how to set the key-value pair let’s take a look
set name "devyank"
here name refer to the key and devyank refers to the value
- So we have set the key-value pair now question arises how to get the value that is too very easy we just have to write the following command to get the value
get name
- We can also set an expiration time for the key-value pair
set hobby "running" EX 10
In above example the expiration time for the key-value pair is 10 seconds , if you will run get command after 10 seconds you will get the following result i.e nil
- There is also a command to check whether a key exist or not in a database
exists key
So if a key exists it returns 1 integer value and if key does not exist it returns 0 integer value.
- There is also a command to retrieve all the keys from the database
keys *
Pub-Sub model
- So in Redis you can subscribe to a channel even if it does not exist and later on if any video will be added to the channel you will get notified .Let’s take a look at command line how does this happen
subscribe devyankvideos
So in above example I have subscribed to a channel “devyankvideos” which didn’t existed earlier
- Now I am going to publish a video in this channel and will see what result I will get
publish devyankvideos "Redis tutorial"
Now you can observe that we received a message that a new video is added.
Top comments (0)