Redis
1. Installing & running a Redis server
Run the following command:
docker run -d --name redis -p 6379:6379 redis
2. Connect to redis server
We now want to check that the installation worked.
We can check in 1 of 2 ways:
execinto the container, where you haveredis-clialready installed.- install
redis-clilocally on your machine and connect to the redis server from outside the container.
- A. Test connectivity from inside the container
Run the following command:
docker exec -it redis redis-cli
You should see the following terminal being opened:
127.0.0.1:6379>
- B. Test connectivity from localhost
There is no installation for redis-cli only.
To this day, all we can do is:
brew install redis
which bundles together both redis-server (used to run the redis server itself), and redis-cli. You just don't run the redis server using the redis-server tool, and only use redis-cli to connect to our dockerized redis server.
Immediately after installing redis, I saw that it ran the server automatically. But we want the docker one, so we'll need to stop the redis-server:
brew services stop redis
Now that the redis server on our localhost stopped, and the redis server on our docker runs, let's connect to it:
redis-cli
The above command is flagless, which assumes you're running a redis server on localhost, on the default port of 6379, with no authentication.
if you need a more detailed way, then:
redis-cli -h 127.0.0.1 -p 6379 -a pass123
The following also works:
redis-cli -u redis://username:password@host:port
redis-cli -u redis://host:port
Upon successful login you should see the following terminal being opened:
127.0.0.1:6379>
3. Redis Commands
- Command 1: ping
Syntax
ping
Description
Simple ping request.
Example response
PONG
- Command 2: FLUSHALL
Syntax
FLUSHALL
Description
Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.
By default, FLUSHALL will synchronously flush all the databases. Starting with Redis 6.2, setting the lazyfree-lazy-user-flush configuration directive to "yes" changes the default flush mode to asynchronous.
It is possible to use one of the following modifiers to dictate the flushing mode explicitly:
ASYNC: flushes the databases asynchronouslySYNC: flushes the databases synchronously
FLUSHALL SYNC
- An asynchronous
FLUSHALLcommand only deletes keys that were present at the time the command was invoked. Keys created during an asynchronous flush will be unaffected. - This command does not delete functions.
- Other than emptying all databases (similar to FLUSHDB), this command clears the RDB persistence file, aborts any snapshot that is in progress, and, if the save config is enabled, saves an empty RDB file.
- Command 3: SCAN
Syntax
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
Description
The SCAN command (and relatives SSCAN, HSCAN and ZSCAN) are used in order to incrementally iterate over a collection of elements.
SCANiterates the set of keys in the currently selected Redis database.SSCANiterates elements ofSetstypes.HSCANiterates fields ofHashtypes and their associated values.ZSCANiterates elements ofSorted Settypes and their associated scores.
Since these commands allow for incremental iteration, returning only a small number of elements per call, they can be used in production without the downside of commands like KEYS or SMEMBERS that may block the server for a long time (even several seconds) when called against big collections of keys or elements.
SCAN basic usage
An iteration starts when the cursor is set to 0:
> scan 0
1) "17"
2) 1) "key:12"
2) "key:8"
3) "key:4"
4) "key:14"
5) "key:16"
6) "key:17"
7) "key:15"
8) "key:10"
9) "key:3"
10) "key:7"
11) "key:1"
In NO. 2, we get back the results.
In NO. 1, we get back the cursor return value to use as input in order to get the next batch.
> scan 17
1) "0"
2) 1) "key:5"
2) "key:18"
3) "key:0"
4) "key:2"
5) "key:19"
6) "key:13"
7) "key:6"
8) "key:9"
9) "key:11"
Since in the second call the returned cursor is 0, the server signaled to the caller that the iteration finished, and the collection was completely explored.
SCAN with a COUNT option
SCAN also comes with a COUNT option, which can be used like so:
SCAN COUNT 20
This is used to specify a count to override the default COUNT which is set to 10 result per iteration. COUNT can be changed from one iteration to the next.
So continuing the example from before, if I now do:
SCAN 0 COUNT 3
And an example output is:
1) "4"
2) 1) "key11"
2) "key10"
3) "key9"
SCAN with a MATCH option
SCAN also has a MATCH option. This will iterate elements that match a specific pattern.
So in this example here:
SCAN 0 MATCH something
We're saying "match the word something".
We don't have to be specific, and can also use expressions like this:
SCAN 0 MATCH k*
Gets everything that starts with the letter k.
SCAN with other DATA TYPES
SCAN is also available with other data-types. The full list of SCANs available to use is:
SSCAN- Used with sets. Returns list of set members.HSCAN- Used with hashes. Returns array of elements with a field and value.ZSCAN- Used with sorted sets. Returns array of elements with associated score.
- Command 4: KEYS
Syntax
KEYS som*Pattern
KEYS will return all keys & values that match a specific pattern.
Note that it SHOULD BE AVOIDED IN PRODUCTION ENVIRONMENTS!!! Because it returns everything at once, and it can be very taxing on the system. It should only be used in development.
- Command 5: SET
Syntax
SET foo-key 42
Description
Set key to hold a string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
Upon successful set, returns:
"OK"
The SET command supports a set of options that modify its behavior:
EXseconds -- Set the specified expire time, in seconds (a positive integer). An error is returned when seconds is invalid.PXmilliseconds -- Set the specified expire time, in milliseconds (a positive integer).EXATtimestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds (a positive integer).PXATtimestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds (a positive integer).NX-- Only set the key if it does not already exist.XX-- Only set the key if it already exists.KEEPTTL-- Retain the time to live associated with the key.GET-- Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.
Complex example:
SET some-key "this value will expire in a minute" EX 60
Note: Since the SET command options can replace SETNX, SETEX, PSETEX, GETSET, it is possible that in future versions of Redis these commands will be deprecated and finally removed.
- Command 6: EXISTS
Syntax
EXISTS key [key ...]
Description
Returns 1 if a key (keys) exists. Returns 0 if does not exist.
Can be used on multiple keys at once.
When querying multiple keys, the return integer specifies the number of keys that exist from those specified as arguments.
- Command 7: GET
Syntax
GET key
Description
Get the value of key. If the key does not exist the special value nil is returned.
An error is returned if the value stored at key is not a string, because GET only handles string values.
- Command 7: GETEX
Syntax
GETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds |
PXAT unix-time-milliseconds | PERSIST]
Description
Get the value of key and optionally set its expiration. GETEX is similar to GET, but is a write command with additional options.
The GETEX command supports a set of options that modify its behavior:
EXseconds -- Set the specified expire time, in seconds.PXmilliseconds -- Set the specified expire time, in milliseconds.EXATtimestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds.PXATtimestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds.PERSIST-- Remove the time to live associated with the key.
Examples:
redis> SET mykey "Hello"
"OK"
redis> GETEX mykey
"Hello"
redis> TTL mykey
(integer) -1
redis> GETEX mykey EX 60
"Hello"
redis> TTL mykey
(integer) 60
redis>
- Command 8: TTL
Syntax
TTL key
Description
Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.
In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exists but has no associated expire.
Starting with Redis 2.8 the return value in case of error changed:
- The command returns
-2if the key does not exist. - The command returns
-1if the key exists but has no associated expire.
You also have the PTTL command that returns the same information only in milliseconds resolution (Only available in Redis 2.6 or greater).
- Command 9: DEL
Syntax
DEL key [key ...]
Description
Removes the specified keys. A key is ignored if it does not exist.
Examples:
DEL no-exist-key
(integer) 0
> SET key1 "Hello"
"OK"
> SET key2 "World"
"OK"
> DEL key1 key2 key3
(integer) 2
- Command 10: HSET
Syntax
HSET key field value [field value ...]
Description
Sets the specified fields to their respective values in the hash stored at key. This command overwrites the values of specified fields that already exist in the hash. If key doesn't exist, a new key holding a hash is created.
The command can set multiple field-value pairs at once. The return value is the number of fields that were added to the hash (not including updates to existing fields).
Benefit - instead of going roundtrip n times, you can in one trip set many key-value pairs.
Examples:
> HSET user:1000 username antirez email john@example.com age 30
(integer) 3
> HGET user:1000 username
"antirez"
> HGETALL user:1000
1) "username"
2) "antirez"
3) "email"
4) "john@example.com"
5) "age"
6) "30"
- Command 11: HGET
Syntax
HGET key field
Description
Returns the value associated with field in the hash stored at key. If the field doesn't exist in the hash, or if the key doesn't exist, the command returns nil.
Unlike HSET which can set multiple fields at once, HGET retrieves the value of only one specific field from a hash. If you need to get multiple fields, use HMGET for multiple specific fields or HGETALL to get all fields and their values.
Use Cases:
- Retrieve a specific piece of information from a hash (e.g., just the username from a user profile)
- Check if a particular field exists in a hash
- Access individual properties of complex data structures stored as Redis hashes
Examples:
> HSET user:1000 username antirez email john@example.com age 30
(integer) 3
> HGET user:1000 username
"antirez"
> HGET user:1000 email
"john@example.com"
> HGET user:1000 nonexistent
(nil)
> HGET nonexistent:key username
(nil)
- Command 12: HGETALL
Syntax
HGETALL key
Description
Returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.
If the key doesn't exist, the command returns an empty list. This command is useful when you need to retrieve all the data from a hash at once, but be careful with large hashes as it can be memory intensive.
Use Cases:
- Retrieve complete user profiles or configuration objects
- Debug or inspect the entire contents of a hash
- Export all data from a hash structure
- Check what fields exist in a hash
Performance Note:
For large hashes, consider using HSCAN instead of HGETALL to avoid blocking the Redis server.
Examples:
> HSET user:1000 username antirez email john@example.com age 30 country USA
(integer) 4
> HGETALL user:1000
1) "username"
2) "antirez"
3) "email"
4) "john@example.com"
5) "age"
6) "30"
7) "country"
8) "USA"
> HGETALL nonexistent:key
(empty array)
- Command 13: HSCAN
Syntax
HSCAN key cursor [MATCH pattern] [COUNT count]
Description
Incrementally iterates over the fields and values of a hash stored at key. This command is the hash-specific version of the SCAN command and is designed to handle large hashes without blocking the Redis server.
The HSCAN command returns:
- A new cursor value (to be used in the next call)
- An array of field-value pairs from the hash
Like SCAN, the iteration starts with cursor 0 and ends when the returned cursor is 0.
Options:
MATCH pattern- Only return fields that match the given pattern (supports glob-style patterns)COUNT count- Hint about how many elements to return per call (default is usually around 10)
Use Cases:
- Safely iterate through large hashes in production
- Search for specific fields in a hash using pattern matching
- Process hash data in chunks to avoid memory issues
- Inspect hash contents without risking server blocking
When to use HSCAN vs HGETALL:
- Use
HSCANfor large hashes (>1000 fields) or in production environments - Use
HGETALLfor small hashes or development/debugging
Examples:
> HSET user:1000 username antirez email john@example.com age 30 country USA city SF phone 555-1234
(integer) 6
> HSCAN user:1000 0
1) "0"
2) 1) "username"
2) "antirez"
3) "email"
4) "john@example.com"
5) "age"
6) "30"
7) "country"
8) "USA"
9) "city"
10) "SF"
11) "phone"
12) "555-1234"
> HSCAN user:1000 0 MATCH *e*
1) "0"
2) 1) "username"
2) "antirez"
3) "email"
4) "john@example.com"
5) "phone"
6) "555-1234"
> HSCAN user:1000 0 COUNT 2
1) "0"
2) 1) "username"
2) "antirez"
3) "email"
4) "john@example.com"