Redis has been in the database game for quite a while now. The popularity has been increasing due to various factors that are discussed below. But before starting we should understand what is Redis actually.
So as per the docs state,
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.
Now to be honest, the definition is difficult to understand. But I am quite sure that by the end of this article you would comprehend it properly.
Let’s start by understanding what are NoSQL Databases in general. NoSQL databases are used to store data in any other format than rows and columns. These are widely used to store data that is non relational in nature.
For example, consider two tables Person and Mobile_Phones, the two tables tend to have a relation (A person can have one or many mobile phones). So in scenarios where we need to relation among data SQL Databases are preferred.
Consider another scenario where the data to be stored has entries for two schemas such as Furniture and Mobile_Phones. It is quite evident that there is no such relation in above data. So storing this type of data in NoSQL database may increase overall performance.
Examples for relational databases consist of MySQL,PostgreSQL. On the other hand, MongoDB,Redis,Neo4j etc are examples for NoSQL databases.
Redis has different data structures to store data. Let’s explore them one by one.
Strings
This is the base type of all the types. Every single value can be stored as strings.
SET name “fido”Key-Value Pair
The most easy and used data structure for Redis is storing string in key-value pair.
SET name “fido”GET name// fidoLists
Lists are actually lists of strings stored in a particular order. Lists are also good when we want just to create a collection of N items where usually we access just the top or bottom items, or when N is small. Because random access is slow and takes O(N) time to complete.
Lists have several commands such as RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP. These commands help to perform specific operations lists.
LRANGE gives a subset of the list.LLEN returns the length of linked listLPOP and RPOP removes (pops) elements from start and end respectively.Sets
Among differences between sets and lists is that sets store unique elements unlike lists.
Sets are an unordered data collection. Sets perform fast iteration than lists across elements.
Sets support complex operations like intersections, unions, and so forth, so this is a good data structure for using Redis in a “computational” manner, when you have data and you want to perform transformations on that data to obtain some output.
In sets adding, removing and lookup for an element takes O(1) constant time.
Small sets are encoded in a very efficient way.
Among many commands for Sets we will discuss SADD, SREM, SISMEMBER, SMEMBERS.
Sorted Sets
Sorted sets are the only other data structures, besides lists, to maintain ordered elements.
Sorted sets are like more powerful lists where inserting, removing, or getting ranges from the the middle of the list is always fast. But they use more memory, and are O(log(N)) data structures.
ZADD command is used to add all the specified members with the specified scores to the sorted set stored at key. ZRANGE displays all elements within the set -1 indicates the last index of set
Hashes:
Hashes are used to store collective information about something. Let’s discuss an example to get better understanding.
They act as maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.
In Redis, every hash can store up to more than 4 billion field-value pairs.
Apart from it’s many use cases, there are some popular use cases for Redis such as
- Caching Layer Database
- Pub/Sub
- Get top analysis for something
- Message Queues
Let’s discuss each of them briefly,
Caching Layer Database:
Redis being an in-memory store can be used as cache for storing user session or other relevant details. Redis is highly performant to read data, which is one of the reasons of using it as a caching layer database on top of the main database.
Pub/Sub:
Redis Pub/Sub implements the messaging system where the publishers sends the messages while the subscribers receive them. The link by which the messages are transferred is called channel. A subscriber can subscribe to multiple publishers bases on scenario.
PUBLISH chat “Hi there”
SUBSCRIBE chatGet top analysis for something:
We can get top users, top trends or anything on edges. By edges, that means anything that lies on top or bottom of our list.
For example suppose we have some users and they have some scores. So we can easily store this in a sorted set and query as:
ZRANGE user_scores 0 10 WITHSCORESThe above query would return top 10 users with their scores respectively. In scenarios like this Redis is the go to database.
Message Queues:
Redis has the same concept of queues as in general programming. Message queues can be implemented with push and pop if there are multiple processes running.
There are many libraries that provide ease and enhanced functionalities to work with Redis queues.
This was a brief introduction to the world of Redis there is a lot to explore. I would highly recommend to give a shot to this.
No comments:
Post a Comment