In this article I will try to explain a weird error that I had to deal with, working with Redis.The common message of the error is “(error) MOVED 12933 127.0.0.1:30003”. Now let’ssee when this could happened.
In order to follow me you maybe will need to have Redis, I will also assume that you areusing some UNIX like distro, but you could find out the equivalents command on Windows by yourself.You are a big boy. In total we only need:
Downloading
The current stable version of Redis is6.2.6, you could download itthrough this link or making use of curl, like this:
curl -sO https://download.redis.io/releases/redis-6.2.6.tar.gz
Extracting folder
tar -xf redis-6.2.6.tar.gz
Now you should have a folder calledredis-6.2.6, let’s position inside of it withcd
command:
cd redis-6.2.6
Compiling
Now we are good to go.
make
In case you want to run the tests inside the redis code, you could runmake test
. There are other parameters that you can pass in thebuild, that could be interesting to trying out, but not now.
In case you are in hurry, let me just summarize it to you. You will find this error when you try to use a redis client on a redis cluster,without specifying on this redis client that you are actually connecting to a redis cluster and not a normal redis node. Of course, if you justread this sentence could be the case that it makes no sense to you, right? Let’s go deeper then.
Let’s try to reproduce this error without any particular library in any language first, just making use ofredis-cli.
Let’s run a Redis cluster locally, it will be easy. If you already compiled the source code of redis in the previous setup, you willnotice that there’s a folder calledutils
. Inside this folder there’s another one calledcreate-cluster
, so I’m going to make useof the script inside this folder to create a cluster, is just the easiest way.
In order to run this scrip you need to be position in the same folder as the script, so let’s go into there:
cd utils/create-cluster/
Run the script:
./create-cluster start./create-cluster create
Yes, like that. Wait what? Is already created? Yep, is code written in C so… A little confusing what they mean bystart
andcreate
, usually the orderis firstcreate
and thenstart
, but naming is hard.
In thecreate
command it will ask youCan I set the above configuration? (type ‘yes’ to accept), so typeyes, you are following atutorial that’s the point.
Now we have our cluster locally running.
How to stop and clean this?
./create-cluster stop./create-cluster clean
Let’s connect to the cluster with redis-cli.In the previous step you must notice a line like this one in the logs printed on the./create-cluster create
command:
>>> Performing Cluster Check (using node 127.0.0.1:30001)
In my case the port that I will be trying to connect is30001, but in your case could be any other.
./src/redis-cli -c -p 30001
VERY IMPORTANT HERE
The-c
option is for
-c Enable cluster mode (follow -ASK and -MOVED re-direction).
So right now we are connecting using the cluster mode, so we won’t have any problem, the problem will come laterif we try to make operation with a connection that doesn’t have thisCluster mode enabled. I’ll comment more on that later.
Make aPING
command just to check if everything looks fine,
127.0.0.1:30001> PING
You should getPONG
as an answer.
Now let’s back to business ), I will set several
127.0.0.1:30001> SET name Lola-> Redirected to slot [5798] located at 127.0.0.1:30002OK127.0.0.1:30002> SET name1 Lola-> Redirected to slot [12933] located at 127.0.0.1:30003OK127.0.0.1:30003> SET name2 Manolo-> Redirected to slot [742] located at 127.0.0.1:30001OK127.0.0.1:30001> SET name3 JuanOK127.0.0.1:30001> SET name4 Juan-> Redirected to slot [8736] located at 127.0.0.1:30002OK127.0.0.1:30002
Take a look at these logs, yes I’m connected to port30001 but myname1
, on commandSET name1 Lola
was located in a slot in node30003. I will explain more in advance, for now let’s justreproduce the error.
Connecting without cluster mode enabled
Let’s try to connect to one of this node without enabling cluster mode, so open another terminal and run a this command:
./src/redis-cli -p 30001
IMPORTANT AGAIN
Here the difference is that the option-c
was not supplied.
Let’s makePING
, can we? Yes, we can!
127.0.0.1:30001> PINGPONG
Now comes the cool surprise, at the moment you must be thinking like,“Well I just made PING without problem, so I’m sure that I could retrieve those
Let’s try to retrieve them withGET command,
127.0.0.1:30001> GET name1
ERROR
127.0.0.1:30001> GET name1(error) MOVED 12933 127.0.0.1:30003127.0.0.1:30001>
Enjoy debugging this on your application, is not fun at all really. You start thinking, like, why? I ran the magical command calledPING
andeverything is fine.
Just to clarify, this isNOT an error of the server, this isYOUR FAULT, for setting up bad your redis client.
In general if you are using a redis cluster in your server, you should enabled the cluster mode on your client. There’s a lot of alternativesfor this, depending on the Programming Language that you are using. Here is a list from the redis website:
Related toGolang, the librarygo-redis, have also support for this.
Until next error guys ).