With every passing day, the popularity of redis is growing exponentially due to the immense community support. In this blog, we will learn about the Redis client and understand different ways to interact with redis. so let’s get started.
What is Redis
Redis is an open-source, in-memory,key-value type database. It is used as a database, streaming engine, cache, and message broker in many organizations.
If you are new to the redis world, then you can follow the below tutorials to get comfortable with redis.
What is Redis client?
To Interact with a redis server the user needs a redis client. There are many clients supported by redis. The most widely used redis clients are:-
- Python client
- Linux client
- Java client
- npm client
- etc.
The list of all supported clients in redis can be found here
Every client needs two mandatory parameters, a redis host and a port where the redis service is running. If the redis is running locally then the host should be 127.0.0.1 and the port must be 6379.
List of clients available in Redis
In this session, we will briefly discuss the various redis clients.
Redis client npm
Node-redis is the latest, high-performance Redis client for Node.js.The node.js client can be installed by typing
npm install redis
Below is the simple code snipped to connect to redis via npm
import { createClient } from 'redis'; const redis_client = createClient(); redis_client.on('error', (err) => console.log('Redis Client Error', err)); await redis_client.connect(); await redis_client.set('key', 'value'); const value = await redis_client.get('key');
The above redis client will connect to host=127.0.0.1 and port 6379. If you wish to connect to a different host and port then use the below code snipped
import { createClient } from 'redis'; const redis_client = createClient({url: 'redis://[[username][:password]@][host][:port][/db-number]'}); redis_client.on('error', (err) => console.log('Redis Client Error', err)); await redis_client.connect(); await redis_client.set('key', 'value'); const value = await redis_client.get('key');
More information about npm clients can be found here
Redis Lua client
redis-lua is a popular Lua client library for the Redis advanced key-value database. To use the redis-lua client library, the user needs the redis module and assigns it to a variable.
local redis = require 'redis'
you can use the below code snipped to send a ping command to redis
local redis = require 'redis' local client = redis.connect('127.0.0.1', 6379) local response = client:ping() -- true
More information about Lua redis client can be found here
Redis client JAVA
Jedis is a popular Java client for redis and is designed to keep performance and ease of use in mind. To get started with Jedis add the below config in your maven file.
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>4.2.0</version> </dependency>
and use the below code snipped to connect to redis
JedisPool pool = new JedisPool("localhost", 6379);
More information about Jedis client can be found here
Redis client python
redis-py is the most popular library to interact with redis using python. Use pip to install the redis-py library.
pip install redis
Once the python library is installed, refer to the below command to connect to redis
import redis r = redis.Redis(host='localhost', port=6379, db=0)
Now send some data to redis and verify the record
>>> r.set('foo1', 'bar1') True >>> r.get('foo1') b'bar1'
More information about the redis python client can be found here
Redis client Linux
Bash is the least widely used client library to interact with redis. You can use the Redis Client library to interact with redis.To use this library functions in your Bash script, you have to source the library first
source <path to redis.bash>
Then connect to redis using the below command
redis_connect [-h host] [-p port] [-a passwd] [-d database] [-t timeout in seconds]
Refer to this github page to get more information about the bash library
Redis client GUI
So far we have seen how to interact with redis using CLI, but redis also provides an awesome UI called RedisInsight.It is a visualization tool using which you can perform both CLI as well as GUI-based operations with your redis database.
RedisInsight provides the capability to design, develop as well as optimize the Redis application. It can be installed on macOS, Linux as well as on windows. Once you have installed the RedisInsight, it will ask you to pass the database details.
You need to pass the redis host, port, and database alias to connect to the redis instance. Once connected, you can write your redis queries and also perform a time series analysis.
You can follow this tutorial to get more detail about RedisInsight.
Conclusion
I hope you have liked this tutorial on the redis client. In this blog, we have understood what is redis client and what the different clients using which we can interact with redis. I also briefly explained various redis clients. Finally, we understood a way to interact with redis using GUI.
Please do let me know if you are facing any issues while following along.