We'll be using Redis Stack for our flavor of Redis. Redis Stack includes the OSS Redis that you know and love plus several modules that extend it's capabilities. More on modules later.
We'll also be using RedisInsight which is a graphical client for Redis. We'll use it to issue commands and browse our database.
This section will walk you through running a Docker image with Docker Desktop. If you haven't used Docker Desktop, check out installation and usage instructions here.
Installing with Docker is pretty easy. Just run the following command:
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
And that's it. You now have Redis Stack installed and running. And you now have RedisInsight running too. To use it, just point your browser at http://localhost:8001 and browse your database.
Let's confirm that the installation of Redis Stack is working by entering our first command from RedisInsight.
-
Select workbench on the left naviagtion bar to access the command-line console.
-
Enter
PING
into the console and click the play button. -
Observe the
PONG
in response.
I'm gonna be giving you a lot of Redis commands. Don't expect screenshots all the time, as they're kind of a pain for me make and hard for you to copy and paste from. So, instead, future command-line examples will often be presented in the following format:
127.0.0.1:6379> PING
PONG
This is the format used by redis-cli
, the command-line client that ships with OSS Redis.
We're gonna build a simple Express API. All the Express stuff is already there so don't worry about that. We'll just focus on endpoints that do things.
If you haven't cloned this repo yet, do so. You're gonna need it:
git clone [email protected]:justincastilla/beyond-the-cache.git
Note the folders:
data
: Contains sample Bigfoot Tracker data we can load into Redis and a shell script to load it.docs
: Contains all the instructions for the workshop. You are reading them right now.slides
: Contains the slides I'll be presenting during the workshop.solution
: This is the folder that contains the completed API that you'll be creating. If you get stuck and need to see the answer (i.e. cheat) this is where you can do it.src
: This is the folder you will be working from. Change into here to run the application.
The rest of the instructions will assume that you are in the src
folder. So, do this:
cd src
This is the Beyond the Cache with Redis + Node.js workshop. We've installed Redis. Now you need Node.js. I'm gonna assume that you are able to download and install Node.js yourself. You might even have it installed already.
However, I took advantage of the top-level await feature in newer versions of JavaScript. So, you'll need to use a version of Node.js that supports that. Specifically, that would be 14.8 or later. I used version 16.16, which is listed in the .nvmrc
file.
Speaking of the .nvmrc
file, I like to use nvm
. It's a tidy way to manage various Node.js versions on my machine. And, if you use nvm
you can just enter:
nvm install `cat .nvmrc`
That installs the version of Node.js I used. And then tell nvm
to use it:
nvm use
But you don't have to do that. Install Node.js however you want as long as it's version 14.8 or later. Don't let me tell you how to live your life.
The API is configured using dotenv so you need a .env
file that contains that configuration. In the root of the folder, there's a sample.env
file. Copy that file to .env
and make some changes:
cp sample.env .env
Open this file. If you are running Redis Stack via Docker, the default setting should be fine. If you installed it some other way or had to use a different port or something, update the REDIS_HOST
, REDIS_PORT
, and REDIS_PASSWORD
values to match the values you are using.
By default, the file has the API listening port port 8080. If this won't work for you, feel free to change it by updating the SERVER_PORT
,
You also need to install all the Node.js packages the application uses. Packages like Node Redis. You know, the things this workshop is showing you how to use.
You probably know what happens next, but just in case:
npm install
You have Redis and Node.js. You have the code. Everything is configured and installed. You should be able to run the application. So let's do that:
npm start
You should see:
> [email protected] start
> nodemon --inspect ./server.js
[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node --inspect ./server.js`
Debugger listening on ws://127.0.0.1:9229/1a6c17e9-2a1c-4bcc-907f-6bf991373448
For help, see: https://nodejs.org/en/docs/inspector
👣 Bigfoot Tracker API ready at http://localhost:8080. 👣
It's up and running. Give it a quick test pointing your browser at http://localhost:8080. You should see a simple JSON response of:
{
"hello": "world"
}
Now that's it's running, let's take a look at some of the code we've just run and learn the basics of Node Redis.