Laravel 5: Redis – Setup and Stuff

Redis: setup and stuff


Redis is an open source, advanced key-value store.
It can contain strings, hashes, lists, sets, and sorted sets.
It can be used in Laravel in: queues, cache, broadcasting.
Redis can also be used as a fast, persistent database – great for horizontal projects (many servers), as it can easily replicate on these servers (nodes).

Redis for Windows

Most likely you will need it for local development, as I do.

I develop using XAMPP with PHP 7 on Windows 8.1, so I need Redis for my win-dev environment.
When I am done developing, I will push it onto Linux box, but it does not change the fact, that I need something for Windows now.

Here where Redis for Windows can be found.

NOTE:
For development do not use password, or anything you do using console is going to drive you bananas, as it will scream for that password.

 


Redis for Linux

To get Redis for Linux:

  1. sudo apt-get install redis-tools
  2. sudo apt-get install redis-server
  3. go inside of installation dir and you can use redis-cli command

You can also see here.

 


Using Redis CLI

On Windows, Redis is located int:

So, to access it on console, you have to cd to Redis directory first:

… and then use:

It works same on Linux, just you have to cd to different directory.

Available Redis commands are here.

 


Using redis with Laravel

You have two choices:

  • Predis and
  • PhpRedis

Some links: PhpRedis  (see also here) and Predis.
Both scripts provide the same access wrapper to Redis server.

So, you need to have Redis installed first!

PhpRedis is written directly in C, while Predis is written in PHP, so in some cases Phpredis is faster.

How much faster?
Well Aleksey Korzun took his time to run some benchmarks and here are the results.

 


Redis version

To check Redis version, use this command from within Redis directory in console:

 


To check, if Redis is up, you may use ping

Redis is going to answer with: pong.

 


Setting password

Password can be set in redis.conf file.
In Windows, this file is called: redis.windows.conf.
Password is set around line#443.
Just uncomment this line and add password to it:

Note:
In Windows, any changes made in redis.windows.conf will take effect only after computer is restarted.
So, if you come across same kind of a problem, you need to apply password using console, like this:
Make your password very complex, to avoid being hacked.
Something like: rTY6UNMeyAkjL23YTX*qXG8X1p1u#RDd4v85rQNW@82FM4q should do the trick.
Important:
If out of the blue, you get an error: “failed: NOAUTH Authentication required” and you are on Windows (not tested on Linux/Unix), it may mean that you attempted to set password (requirepass) in file redis.windows.conf (and failed). In such case, after next computer restart, you can get this unexpected error.
Actually, that is why you failed to apply your password in first place, because computer restart is required to get this going.
Solution:
Just comment that out and restart Windows again, or pass your new password, whenever you interact with Redis in your app.
Frankly, when developing, it is good to develop with password in place, as later, it can be tedious to put it in all the right places. Unless you use .env based Redis constant password. Just I am not sure, if you can use it in *.js files, like node scripts. Haven’t tested that yet. Drop me a line, if you did.

 


Protect redis by loopback
Loopback and password
If you protect Redis by loopback, you are taking it effectively from direct access from web.
So, in such case, there is no need for using password.
As long as connecting client is secure and connection is secure (SSL) – in case it is not localhost – Redis is protected.

Protect redis by loopback interface – uncomment (if commented) in redis.conf:

If your redis is not on same server (so 127.0.0.1 cannot be used), explicitly state allowed client:

You can write as many interfaces (IPs) as you need, by listing them with space between IPs, as shown above.

If you are explicitly binding localhost, you may do this including IPV6 notation:

This part:

is IPV 6 equivalent of:

… in IPV4 format.

 


Order of commands in CLI (console) are important!

This will NOT work:

This WILL work:

As you can see, password has to go first and then rest of commands.

If server has no password set, anything passed via ‘-a’ is being disregarded.

 


How to start Redis server.

On Windows, when you install downloaded MSI file (installer), it will start Windows equivalent of daemon process.
Deamon process in Linux is a background, long running process.

But, if for any reason, you stop such process, e.g. by using console command:

… Windows ‘daemon’ is ‘exorcised’ 😉 and thus dead.

To get Redis again, you can:

use console

From within redis folder run command:

note:
Windows do not run daemons, so console will be disabled by process and process will die, when console is closed.

setup Windows task

Start it as Windows task – see here, how it is done.
Example is about running pseudo-chron job based on Windows native tasking system, but this task can be setup in the same way.

One difference:
in ‘New Action’ tab, do not add ‘New Arguments’.