Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis databases on a dev machine with multiple projects

Tags:

redis

How do you manage multiple projects on your development and/or testing machine, when some of those projects use Redis databases?

There are 2 major problems:

  1. Redis doesn't have named databases (only numbers 0-16)
  2. Tests are likely to execute FLUSHDB on each run

Right now, I think we have three options:

  1. Assign different databases for each project, each dev and test environment
  2. Prefix keys with a project name using something like redis-namespace
  3. Nuke and seed the databases anytime you switch between projects

The first one is problematic if multiple projects assign "0" for the main use and "1" for the test and such. Even if Project B decided to change to "2" and "3", another member in the project might have a conflict in another projects for him. In other words, that approach is not SCM friendly.

For the second one, it's a bad idea simply because it adds needless overhead on runtime performance and memory efficiency. And no matter what you do, another project might be already using the same key coincidentally when you joined the project.

The third option is rather a product of compromise, but sometimes I want to keep my local data untouched while I deploy small patches for another projects.

I know this could be a feature request for Redis, but I need a solution now.

Any ideas, practices?

like image 824
kenn Avatar asked Feb 28 '11 22:02

kenn


People also ask

Can Redis have multiple databases?

Redis comes with support for multiple databases, which is very similar to the concept in SQL databases. In SQL databases, such as MySQL, PostgreSQL, and Oracle, you can define a name for your databases. However, Redis databases are represented by numbers.

How many databases can be created in Redis?

Managing Databases. Out of the box, a Redis instance supports 16 logical databases. These databases are effectively siloed off from one another, and when you run a command in one database it doesn't affect any of the data stored in other databases in your Redis instance.

How do I run multiple instances of Redis?

Open command line as administrator. Change directory to C:\Program Files\Redis. Run the below command line to create a new service instance. Customize the service name, display name, and conf file path to match your configuration (in bold below), where XXXX is the port number for the new instance.

Should Redis be used as a database?

Redis is a database for a range of data sizes, from a few megabytes to hundreds of terabytes. With Redis Enterprise, you can use Redis as both an in-memory cache and a primary database in a single system, thus eliminating the complexity and latency of two separate systems.


1 Answers

If the projects are independent and so do not need to share data, it is much better to use multiple redis instances - each project configuration has a port number rather than a database name/id. Create an appropriately named config file and startup script for each one so that you can get whichever instance you need running with a single click.

Make sure you update the save settings in each config file as well as setting the ports - Multiple instances using the same dump.rdb file will work, but lead to some rather confusing bugs.

I also use separate instances for development and testing so that the test instance never writes anything to disk and can be flushed at the start of each test.

like image 164
Tom Clarkson Avatar answered Sep 18 '22 19:09

Tom Clarkson