/

Getting Started with Redis: A Beginner's Guide

Getting Started with Redis: A Beginner’s Guide

Redis is a powerful in-memory data storage system that provides fast, scalable, and reliable data storage. In this guide, we will walk you through the first steps of using Redis and introduce you to some of its basic functionalities.

Using Redis-cli

Once you have Redis installed and running, the simplest way to interact with it is through the redis-cli command-line interface. This tool comes bundled with Redis and allows you to send commands to Redis without the need for any additional setup.

To connect to a remote Redis server, you can use the following command:

1
redis-cli -h <host> -p <port> -a <password>

Storing and Retrieving Data

Once you are connected to the Redis CLI, you can start storing data into Redis.

To store a value, you can use the SET command followed by the key-value pair:

1
SET name "Flavio"

To retrieve a value, you can use the GET command followed by the key:

1
GET name

Checking Key Existence

You can check whether a key exists in Redis using the EXISTS command:

1
EXISTS name

This command will return either 1 (exists) or 0 (does not exist) depending on the key’s presence.

Setting Key If Not Exists

If you want to set a key only if it does not already exist, you can use the SETNX command:

1
SETNX name "Roger"

Deleting a Key

To delete a key from Redis, you can use the DEL command:

1
DEL name

Listing Keys

You can list all the keys stored in Redis using the KEYS * command. This will return all the keys in the Redis database.

To filter the keys based on a pattern, you can modify the command. For example, the command KEYS n* will return the keys starting with the letter “n”.

Expiring Keys

Redis allows you to set expiration for a key using the SETEX command:

1
SETEX <key> <seconds> <value>

You can check the time remaining for a key to be cleared using the TTL command:

1
TTL <key>

Incrementing and Decrementing Values

Redis provides commands for incrementing and decrementing numeric values. You can use the INCR command to increment a value and the DECR command to decrement a value.

For example, to increment a key by 1, you can use the command:

1
INCR <key>

You can also specify a specific value to increment by using the INCRBY command:

1
INCRBY <key> <amount>

Similarly, the DECR and DECRBY commands can be used to decrement a value.

Working with Complex Data Structures

Redis supports more complex data structures beyond simple key-value pairs. Some of these data structures include lists, sets, sorted sets, and hashes.

In the upcoming lessons, we will explore these data structures and learn how to work with them in Redis.

tags: [“Redis”, “Redis-cli”, “Redis commands”, “SET command”, “GET command”, “EXISTS command”, “SETNX command”, “DEL command”, “KEYS command”, “SETEX command”, “TTL command”, “INCR command”, “DECR command”, “INCRBY command”, “DECRBY command”, “complex data structures”]