An Introduction to Redis for the Junior Developer

Not sure that ASCII art really works
  1. Downloading Redis
  2. Trying it out on the command line
  3. Using it in your backend code
  4. Why it is useful

Downloading Redis

You can skip the tarball on the website, and download it with a package manager:

Trying it on the Command Line

We will run Redis on the command line to give you a bit of background on the tool. Open the command line with redis-cli.

127.0.0.1:6379> SET name Elliott
OK
127.0.0.1:6379> GET name
"Elliott"
127.0.0.1:6379> GET dne
(nil)
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> EXISTS dne
(integer) 0
127.0.0.1:6379> DEL name
(integer) 1
127.0.0.1:6379> DEL dne
(integer) 0
  • You can set expiration times on keys
  • You can use the Keys command with patterns to filter
  • You can increment/decrement numbers

Using on the Backend

If you have not used a database before (or have only used sqlite), you may need to familiarize yourself with the idea of running your database and program separately. Like PostgreSQL or MongoDB, Redis is run separately. Before you install a library for your programming language, make sure redis-server is running.

const redis = require('redis');
const redisClient = redis.createClient();
client.set("key", "value", console.log);
client.get("key", console.log);
const redis = require('redis');
const { promisify } = require("util");

const redisClient = redis.createClient()
const redisGet = promisify(redisClient.get).bind(redisClient);
const redisSetEx = promisify(redisClient.setex).bind(redisClient);

redisGet.then(console.log).catch(console.error);
require "redis"
redis = Redis.new

redis.set("mykey", "hello world")
# => "OK"

redis.get("mykey")
# => "hello world"
services:
backend:
...

redis:
image: 'redis:6'
ports:
- '6379'
const redisClient = redis.createClient('redis://redis');

Advantages of Redis

Like any database, it persists even while your application is down or rebooting.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store