Redis stands for “remote dictionary server.”
It’s an in-memory NoSQL database server — there aren’t tables, columns, or rows.
In other words, Redis is a simple key/value database.
To execute the Redis client from the command line: redis-cli
To execute the Redis server from the command line: redis-server
apt install redis // for ubuntu
brew install redis // for macos
redis-cli commands:
ECHO “<message>”
PING // returns PONG
SET key “value” // sets value of key
GET key // returns value of key
SETNX key “value” // set if the key does not exist; returns 0 if it didn’t set; returns 1 if it did set
GETRANGE key start-value end-value, e.g. getrange name 0 0 // returns first character of a string
SETRANGE name 5 “us” // updates from position 5
MGET key1 key2 key3 // returns value for each key; if one doesn’t exist, “nil” is returned
MSET key1 “value1” key2 “value2” // and on and on — as many key/value pairs as you want
DEL key1 // the number that’s returned is the number of keys deleted
KEYS * // lists all keys in the Redis database
FLUSHALL // deletes all keys
Redis convention for keys: blog:post-id:comment-id
EXPIRE key <seconds>, e.g. expire name 60
TTL key // determine how many seconds remain for the expiring key
SETEX key <seconds> “value” // set a key with a value and an expiration in a single statement
To install Redis for node.js: npm install ioredis
Simple use case on node.js:
const Redis = require(‘ioredis’);
const redis = new Redis({
host: ‘127.0.0.1’,
port: 6379
});
(async () => {
const value = await redis.set(‘key1’, ‘value1’);
redis.quit();
})();
NODE PROCESS <—> TCP CONNECTION <—> REDIS SERVER