Redis Client and Config Command Tutorial Part 2

Introduction

If you’re getting started with Redis, two important commands to know are the CLIENT and CONFIG commands. This second installment of our two-part series will pick up where we left off in our overview of Redis commands. In this article, we’ll provide a Redis CLIENT and CONFIG command tutorial, providing examples of how to use both of these commands.

Prerequisites

Be sure you’ve read the first part of the tutorial series before beginning this second installment.

Redis CONFIG command

In this section, we’ll continue our look at all the variations available for the CONFIG command.

Redis CONFIG GET *

This Redis CONFIG command uses a wild card to show the available options for a Redis configuration. If we use the command CONFIG GET *, our output will look like the following:

127.0.0.1:6379> CONFIG GET *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
7) "unixsocket"
8) ""
9) "logfile"
10) ""
11) "pidfile"
12) ""
13) "maxmemory"
14) "0"

This output shows the Redis configuration for the host system. For the purposes of this tutorial, we’re just showing you a partial result of the command as the full set of results contains up to 130 lines.

Redis CONFIG GET Example

Next, we’ll show you how to use different expressions in conjunction with the CONFIG GET command. Here’s a simple example:

127.0.0.1:6379> CONFIG GET *max-*-entries*
1) "hash-max-ziplist-entries"
2) "512"
3) "set-max-intset-entries"
4) "512"
5) "zset-max-ziplist-entries"
6) "128"

We can see that the Redis server will return results containing information that matches the specified expression.

Redis CONFIG SET Example

To understand this command, it’s best to start by using GET to retrieve a certain option as a point of reference:

127.0.0.1:6379> CONFIG GET lua-time-limit
1) "lua-time-limit"
2) "5000"

We can change this option’s default value using the following code:

27.0.0.1:6379> CONFIG SET lua-time-limit 6000
OK
127.0.0.1:6379> CONFIG GET lua-time-limit
1) "lua-time-limit"
2) "6000"
127.0.0.1:6379>

This example also proves that we can change the settings at runtime.

Redis INFO Command Example

The Redis INFO command will provide all available information about the server. The result set is categorized to make it more readable. In the following example, we’ll be showing you a partial result set since the full result set is quite large:

127.0.0.1:6379> INFO
# Server
redis_version:3.2.100
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:dd26f1f93c5130ee
redis_mode:standalone
os:Windows
arch_bits:64
multiplexing_api:WinSock_IOCP
process_id:1396
run_id:7c71fb5adb5c2d98141be3ed7d792e2a19f2eb58
tcp_port:6379
uptime_in_seconds:3274
uptime_in_days:0
hz:10
lru_clock:12747479
executable:C:\redis\redis-server.exe
config_file:

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

As you can see, the INFO command returns details for both the client and the server.

We can trim down our selection by using a specific option in the INFO command. In the following example, we’ll be using the CPU:

127.0.0.1:6379> INFO CPU
# CPU
used_cpu_sys:0.08
used_cpu_user:0.14
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
127.0.0.1:6379>

Redis COMMAND Command Example

In this section, we’ll show you an example of how to use Redis COMMAND:

127.0.0.1:6379> COMMAND
7) 1) "setrange"
2) (integer) 4
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1

This command returns another very lengthy result set, so we’ll be providing just a few lines of the results.

In Part 1 of this tutorial series, we mentioned that COMMAND will return six sub-results containing the following information:

  • Command name – setrange
  • Number of parameters – (integer) 4
  • Nested array reply of command flags – 1) write 2) denyoom
  • First key in argument list – (integer) 1
  • Last key in argument list – (integer) 1
  • The repeating keys – (integer) 1

NOTE: In this particular server we have 172 commands

We can also get information on a particular COMMAND using the following syntax: COMMAND INFO readonly

For this example, our output would look like the following:

127.0.0.1:6379> COMMAND INFO readonly
1) 1) "readonly"
2) (integer) 1
3) 1) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0

We can count how many commands are available on our server using the following command:

127.0.0.1:6379> COMMAND COUNT
(integer) 172

Redis CLIENT Command Example

In this section, we’ll look at some practical examples of how to use the Redis CLIENT command.

Redis CLIENT LIST example

Let’s start by using the CLIENT LIST command to retrieve a list of currently connected clients:

127.0.0.1:6379> CLIENT LIST
id=2 addr=127.0.0.1:52868 fd=9 name= age=4490 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=18446744073709523520 events=r cmd=client

This output tells us that we only have one client connected to the server. As more clients connect to our server, this number will grow.

Redis CLIENT KILL Command Example

In this section, we’ll look into the command used to close a client connection.

First, let’s make sure that we have multiple clients connected to the server:

127.0.0.1:6379> CLIENT LIST
id=2 addr=127.0.0.1:52868 fd=9 name= age=4983 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=18446744073709523520 events=r cmd=client
id=3 addr=127.0.0.1:53357 fd=10 name= age=19 idle=19 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=18446744073709537584 events=r cmd=command

The output of the CLIENT LIST command confirms that we have two clients connected to ports ‘52868’ and ‘53357’. Let’s say we want to close the connection of the client connected to port ‘53357’. We can use the following command to kill it:

27.0.0.1:6379> CLIENT KILL 127.0.0.1:53357
OK

Redis returns a response of OK, meaning that the client connected to port 53357 was terminated.

Conclusion

When you’re using Redis to store and manage data, it’s important to become familiar with some commonly-used commands. In this two-part article series, we showed how to use the CLIENT and CONFIG commands. With the instructions and examples provided in this Redis CLIENT and CONFIG command tutorial, you’ll be able to use any variation on these commands in your own Redis environment.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.