How do I change between redis database? - redis

I am new with redis and I didn't figured out how to create and change to another redis database.
How do I do this?

By default there are 16 databases (indexed from 0 to 15) and you can navigate between them using select command. Number of databases can be changed in redis config file with databases setting.
By default, it selects the database 0. To select a specified one, use
redis-cli -n 2 (selects db 2)

Note: this is not a direct answer to the OP's question. However, this text is too long for a comment, and I thought I'd share it anyway, to clarify things to the OP. Hope I don't break too many SO rules by doing this...
Some extra info on multiple databases:
Please note that using multiple databases in one redis instance is discouraged.
It is a nice feature for playing around and getting to know redis.
In more serious setups, if you have multiple ports at your disposal, it's preferred and more performant to use separate instances. At our company, we run about 50 instances on the development/staging server, and about 5 on the production server.
The reason is, that redis transactions are only atomic within one db number anyway. Most (if not all) clients nicely seperate that for you in the connect() phase. And if you have to connect separately, it's just as easy to connect to a different port.
The core of redis is also single threaded. That's one of the things that makes redis so quick and simple. Everything is sequential. If you use multiple instances instead of just one, you gain the benefit of multi-processing (on multi-core machines).

redis-cli //connect server firstly
redis-cli info //show all existing database - at the bottom
//exit
redis-cli -n 1 //1 is the name of database

Related

Redis replication at key level

We are using redis. We have two set of data. One set of data(Assume it is using the prefix redis:local: eg: redis:local:key1) is used by the main application and no need of replication.
Another set of data (Prefix redis:replicate: eg: redis:replicate:key2) is used by the main application and should be replicated to slave redis instances.
I have two questions.
Is it possible to configure redis to replicate only keys with prefix redis:replicate:?
If that is not possible, Is it possible to configure redis to replicate only one database? We will store the first set of data in database-0 and the second set of data in database-1. So we have to replicate only database-1.
Currenly, we are running two instances of redis to solve the issue.
Redis only supports replication of whole instances. Limiting replication to a key prefix or database is not possible.
Running two instances of Redis is simplest and reliable option.
Another way would be to write a custom replication program which is difficult and failure prone in comparison.
There is also another question concerning replication of only one database: Replicate a single Redis database from an instance that has multiple databases

Consideration before creating a single Redis instance

I currently have some different project that works on different redis instance ( consider the sample where I've 3 different asp.net application that are on different server each one with its redis server).
We've been asked to virtualize and to remove useless instances so I was wondering what happens if I have only one redis server and all the 3 asp.net points to the same redis instance.
For the application key I think there's no problem, I can prefix my own key with the application name , for example "fi-agents", "ga-agents", and so on... but I was wondering for the auth session what happens?
as far as I've read the Prefix is used as internal and it can't be used by final user to separate... it's just enought to use different Db?
Thanks
Generally and unless there are truely compelling reasons, you don't want to mix different applications and their data in the same database. Yes, it does lower ops costs initially but it can quickly deteriorate to scaling and performance nightmare. This, I believe, is true for any database.
Specifically with Redis, technically yes - you could use a key prefix or the shared/numbered database approach. I'm not sure what you meant by "auth" sessions but you can probably apply the same approach to them. But you really shouldn't... since Redis is a single-threaded process you can end up where one of the apps is blocking the other two. Since Redis by itself is so lightweight, just spin up dedicated servers - one per app - even in the same VM if you must. You can read more background information on why you don't want to opt for the shared approach here: https://redislabs.com/blog/benchmark-shared-vs-dedicated-redis-instances

Redis active-active replication

I am using redis version 2.8.3. I want to build a redis cluster. But in this cluster there should be multiple master. This means I need multiple nodes that has write access and applying ability to all other nodes.
I could build a cluster with a master and multiple slaves. I just configured slaves redis.conf files and added that ;
slaveof myMasterIp myMasterPort
Thats all. Than I try to write something into db via master. It is replicated to all slaves and I really like it.
But when I try to write via a slave, it told me that slaves have no right to write. After that I just set read-only status of slave in redis.conf file to false. Hence, I could write something into db.
But I realize that, it is not replicated to my master replication so it is not replicated to all other slave neigther.
This means I could'not build an active-active cluster.
I tried to find something whether redis has active-active cluster capability. But I could not find exact answer about it.
Is it available to build active-active cluster with redis?
If it is, How can I do it ?
Thank you!
Redis v2.8.3 does not support multi-master setups. The real question, however, is why do you want to set one up? Put differently, what challenge/problem are you trying to solve?
It looks like the challenge you're trying to solve is how to reduce the network load (more on that below) by eliminating over-the-net reads. Since Redis isn't multi-master (yet), the only way to do it is by setting up each app server with a master and a slave (to the other master) - i.e. grand total of 4 Redis instances (and twice the RAM).
The simple scenario is when each app updates only a mutually-exclusive subset of the database's keys. In that scenario this kind of setup may actually be beneficial (at least in the short term). If, however, both apps can touch all keys or if even just one key is "shared" for writes between the apps, then you'll need to bake locking/conflict resolution/etc... logic into your apps to consolidate local master and slave differences (and that may be a bit of an overkill). In either case, however, you'll end up with too many (i.e. more than 1) Redises, which means more admin effort at the very least.
Also note that by colocating app and database on the same server you're setting yourself for near-certain scalability failure. What will happen when you need more compute resources for your apps or Redis? How will you add yet another app server to the mix?
Which brings me back to the actual problem you are trying to solve - network load. Why exactly is that an issue? Are your apps so throughput-heavy or is the network so thin that you are willing to go to such lengths? Or maybe latency is the issue that you want to resolve? Be the case as it may be, I recommended that you consider a time-proven design instead, namely separating Redis from the apps and putting it on its own resources. True, network will hit you in the face and you'll have to work around/with it (which is what everybody else does). On the other hand, you'll have more flexibility and control over your much simpler setup and that, in my book, is a huge gain.
Redis Enterprise has had this feature for quite a while, but if you are looking for an open source solution KeyDB is a fork with Active Active support (called Active Replica).
Setting it up is just a little more work than standard replication:
Both servers must have "active-replica yes" in their respective configuration files
On server B execute the command "replicaof [A address] [A port]"
Server B will drop its database and load server A's dataset
On server A execute the command "replicaof [B address] [B port]"
Server A will drop its database and load server B's dataset (including the data it just transferred in the prior step)
Both servers will now propagate writes to each other. You can test this by writing to a key on Server A and ensuring it is visible on B and vice versa.
https://github.com/JohnSully/KeyDB/wiki/KeyDB-(Redis-Fork):-Active-Replica-Support

Multiple Redis Instances

Most folks seem to recommend running separate Redis instances on different ports (6379 and 6380). Why is this more commonly recommended over creating a second database? I'm not completely through the documentation yet, but most examples don't really mention 'selection of a Redis database' when connecting. An example from the Ruby client, nrk/predis's README:
$redis = new Predis\Client(array(
'scheme' => 'tcp',
'host' => '10.0.0.1',
'port' => 6379,
));
We currently run Hubot in our office with Campfire, and I'm working on a second one for GTalk since you can only have a single adapter in use for each Hubot instance. So I'm considering creating a second database or instance of Redis so that data between the two hubots is isolated. But before I got much further, I wanted to understand why you would use separate instances instead of just creating a second database.
Two main reasons:
using multiple databases is considered generally bad and to be deprecated some day, and they have some performance penalties, though pretty minor.
the main reason is that redis is single threaded, if you need two different data sources, another redis instance will improve performance since it will utilize another CPU you probably have, whereas one instance will always utilize just one.
Also different redis instances can have distinct persistence settings. For example one instance can use only memory and other can use files as storage
Redis Persistence
Then there are other advantages as having separate auth passwords, LRU strategies, etc - which can only be done at the instance level.

Is it possible to monitor only one database?

Currently, my understanding is that the 'monitor' command outputs all commands received by the server, no matter which database number they are sent to.
This is a problem for me as I use one db for holding 'normal' data and one db for holding session data, and the output from the session db makes it near impossible to read the output from the other db.
Is there a way to limit the output to only one database ?
What about this?
redis-cli monitor |grep '(db 1)'
That way you would just get the output of DB 1
Databases in redis are not at all like databases in SQL. They are essentially just a predefined key prefix with no configuration of their own.
If you only want to see changes to the real data, you will need to set it up as a separate instance so that session data goes to a different process.
There isn't much overhead in doing this (in most scenarios it will actually improve performance) and there are other good reasons for using multiple instances. For example you probably want your real data written to disk in real time and backed up, but session data is worthless after a server restart so doesn't need to be saved to disk at all. With a shared instance you would have to save and back up everything, which isn't particularly good for performance with session data changing much more than permanent data.
If only this bug was resolved, the following would work:
redis-cli -n 1 monitor
Where 1 is database number.