Redis (which stands for REmote DIctionary Server) is an open source, in-memory NoSql database that uses key/value pairs to store data. Although Redis can be used as a stand alone database, it is often combined with other databases to improve performance. Redis excels in applications where live, instant data is needed such as leader boards, messaging, gaming, etc.
If I already have a database, why would I use Redis?
Redis holds most of its data in-memory (RAM), as opposed to on disk, which enables it to have extremely low-latency when reading and writing. Most of the time, read and writes in Redis are constant time 0(1). It reduces response time down to sub-milliseconds (around 1,000 to 10,000 times faster than accessing disk storage)! It is great in situations where a lot of caching is needed. It can make a huge difference in speed, especially when needing data from external data sources. In traditional systems, slow response times can cause performance bottlenecks, which is never good for the end user. Redis is here to save the day and provide feedback in lightning fast times. If a client is requesting the same data over and over again, Redis can cache that data and provide the data much, much faster.
Another great benefit about Redis is that it is easy to integrate and work with. There are so many ways to interact and use Redis for it to fit your specific needs. There are over a 100 open source clients available in the Redis library and I am sure that number will only grow! Redis has many useful systems and packages you can utilize, such as Redis Sentinel or Redis Cluster to ensure your data is well protected and functioning well. Redis Enterprise provides extra services, such as clusters for load balancing, etc so you or your team can focus on other aspects of development.
Although Redis's bread and butter is in memory storage, it can also persist data to disk using RDB snapshots so that it can protect the data stored to is, especially if it is used as a stand-alone database.
Redis can store almost every type of data, which makes it suitable for most situations. Redis also pairs great with AI and LLM.
Redis supports these datatypes:
String
Hash
List
Set
Sorted set
Vector set
Stream
Bitmap
Bitfield
Geospatial
JSON
Probabilistic data types
Time series
As mentioned above, one great quality of Redis is that it is easy to work with. The queries are simple to use and avoids using complex SQL query planning and parsing.
Let's take a look at some of the simple commands. *Note, generally all caps are used for the key words but it is not necessary. Your commands will still work with lower cased letters but it is industry standard to use capitalized letters.
To set a key value pair we can use the command Set.
example:
SET name Joy
# returns OK
If we want to get specific data We can use the command Get.
GET name
# returns "Joy"
*note all returned data will be returned as a string, similar to JSON.So integers will be returned as a string. In Javascript, for objects you will need to use JSON.parse() and JSON.stringify(). Keep that in mind when saving and querying data.
We can include an expiration time if we will only need the data for a short period of time. Let's say I only need the name to be valid for 1 minute. I can write:
SET name Joy Ex 60
You can use the command PERSIST to remove an expiration, making it permanent.
To delete a key value, use the command DEL
DEL name
If you want to check to see if a key exists, you can use EXISTS. If it doesn't it will return 0, if it does exist it will return 1. Below, since we deleted name we will get 0
EXISTS name
#returns 0
You can push items into a key value pair similar to the push method in javascript. You can either push to the left using LPUSH or to the right RPUSH. Subsequently you can pop items off using LPOP and RPOP.
These are just a few of the many commands, to give you a basic idea of how easy the commands are to use. It makes it easier for developers to hit the ground running with these simple to use commands.
Redis has a lot to offer. This article only touched base on just a couple of the basic points but hope you enjoyed it and learned a little bit about Redis. It has so much more to offer so I recommend checking out further resources (such as Redis docs etc) to see all of the many things Redis has to offer.
Top comments (0)