How to Install Redis and Connect to Your App

If you’re building a website or app, you may want it to be fast and responsive.

One way to help with that is by using Redis.

In this post, I’ll show you how to install Redis and connect it to your app. I’ll use simple steps, plain language, and share a bit of my own experience too.

Let’s begin.


What Is Redis?

Before we install anything, let’s understand what Redis is.

Redis stands for Remote Dictionary Server. It’s a type of database.

But it’s different from regular databases like MySQL or PostgreSQL.

Here’s how:

  • Redis stores data in memory instead of on a hard drive.
  • That makes it very fast.
  • It’s great for caching, real-time data, and short-term storage.

I like to think of Redis as a super-fast notebook for my app. Instead of going to the bookshelf (a regular database), Redis is like having the data on your desk.


Why Would You Use Redis?

There are a few good reasons:

  • Speed. Since it uses RAM, it’s faster than disk-based databases.
  • Simplicity. The commands are easy to learn.
  • Support. Many programming languages and tools work with Redis.

Some things you can use Redis for:

  • Storing sessions in web apps (like login sessions)
  • Caching slow database queries
  • Building a queue system
  • Tracking real-time data like chat messages or game scores

I personally use Redis to cache WordPress pages and speed up background tasks in a Flask app.

Have you ever wondered why your app slows down when lots of people visit it? Redis can help.


How to Install Redis on Ubuntu

Let’s install Redis.

I’m using Ubuntu in this example. If you use CentOS or another Linux distro, let me know and I can write a version for that too.

Step 1: Update your system

Open your terminal and run:

sudo apt update

Step 2: Install Redis

Now install Redis with this command:

sudo apt install redis-server -y

Step 3: Start and enable the Redis service

Make sure Redis starts on boot:

sudo systemctl enable redis
sudo systemctl start redis

Step 4: Test if Redis is working

Run the Redis command line tool:

redis-cli

Then type:

ping

If you see this:

PONG

That means Redis is running fine.

Type exit to leave the Redis CLI.


Configuring Redis (Optional but Helpful)

The default Redis settings are okay for many small apps. But if you want to secure or tweak it, here are some things I recommend.

1. Change the bind IP (for security)

By default, Redis only listens on 127.0.0.1 (localhost). That’s good.

If you do want to connect from another server, you’ll need to edit the config file:

sudo nano /etc/redis/redis.conf

Find the line:

bind 127.0.0.1

Add your server’s IP or 0.0.0.0 to allow all (not recommended unless you secure it).

Also make sure this line is set:

protected-mode yes

Then restart Redis:

sudo systemctl restart redis

2. Set a password

To set a password, edit the same file:

sudo nano /etc/redis/redis.conf

Find the line with:

# requirepass foobared

Uncomment it and change foobared to your password.

Example:

requirepass mysecurepass123

Restart Redis again.


Connecting Redis to Your App

You’ve got Redis installed. Now, let’s use it in your app.

Redis works with many languages like Python, PHP, Node.js, Ruby, and Go.

I’ll give two short examples: one in Python and one in PHP.


Python Example

You’ll need the redis package.

Install it like this:

pip install redis

Here’s a basic script:

import redis

r = redis.Redis(host='localhost', port=6379, password='mysecurepass123')

r.set('message', 'Hello Redis')
print(r.get('message'))

You should see:

b'Hello Redis'

That means it worked.


PHP Example

For PHP, install the Redis extension:

sudo apt install php-redis -y

Basic usage in a PHP script:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('mysecurepass123');

$redis->set("message", "Hello from PHP");
echo $redis->get("message");
?>

You should see the message when you run the script.


Some Use Cases for Redis

Here are common ways I use Redis:

  • Web app sessions – keeps users logged in without using the database every time.
  • Background jobs – queues tasks like sending emails or processing images.
  • Rate limiting – stops users from doing something too many times.

Two Handy Lists

Tools Redis Works Well With

  • Laravel, Symfony (PHP frameworks)
  • Flask, Django (Python frameworks)
  • Express.js (Node.js)
  • WordPress (with object cache plugin)

What to Watch Out For

  • Redis stores data in memory. If the server runs out of RAM, Redis may crash or get killed.
  • Don’t expose Redis to the internet unless you secure it.
  • Always set a password if Redis is accessible from outside.

Final Thoughts

Redis is fast, simple, and useful. It helped me speed up apps without making things complicated.

It’s not meant to store everything forever, but it’s perfect for quick, temporary storage.

Leave a Reply