Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection to multiple Redis Enterprise DB from single application #343

Open
rpjtesting opened this issue Sep 8, 2023 · 5 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@rpjtesting
Copy link

rpjtesting commented Sep 8, 2023

I have two Redis Enterprise Cluster Databases with 2 different ports, and I need to connect to both databases from single redisom application. Can this be done ?

@bsbodden bsbodden self-assigned this Sep 13, 2023
@bsbodden bsbodden added the enhancement New feature or request label Sep 13, 2023
@bsbodden
Copy link
Contributor

Theoretically yes, but I haven't tested it. I'll try to put a test for this in the next few weeks and add it to the README

@jorgemerinoseera
Copy link

jorgemerinoseera commented Oct 2, 2023

I am interested in this kind of feature too because I am trying to make two connections: one for write and one for read. Can you share the status of it please?

I have tried to do it creating different JedisConnectionFactory and RedisTemplate but the application only injects the primary configuration in spite of I am setting the template for the @EnableRedisDocumentRepositories specifically:

@configuration
@EnableRedisDocumentRepositories(
basePackages = {
"com.seera.connectivity.cachemanager.domain.rules.model",
"com.seera.connectivity.cachemanager.domain.HotelSearch.model",
"com.seera.connectivity.cachemanager.repositories.write",
},
redisTemplateRef = "cacheManagerTemplate"
)

Thanks in advance.

@radhakrishna67
Copy link

@bsbodden , I have the same problem and situation. I have even created support ticket for the same.
Could you please help us with it?

@bsbodden
Copy link
Contributor

bsbodden commented Mar 6, 2024

After researching this it seems a bit more complex that it should be... Basically there would have to be a way to bind different Entities/Repository classes to specific connections. There doesn't seem to be anything built-in in Spring Data Redis to make this simple, so we'll work on our own way to do it, but it will probably take at least a month to get a proper implementation.

@nolindnaidoo
Copy link

nolindnaidoo commented Apr 16, 2024

@bsbodden
I too really desire this implementation, it would signifigantly decrease complexity and ops/infrastructure/cost for my use-case.

Reading and write to the same, and also writing to another at the same time. I tried getting creative before I found this GitHub issue.

import { createClient } from 'redis';

// Function to initialize Redis clients for different databases
function devClient(url: string, dbIndex: number) {
  const client = createClient({ url });

  // Select the appropriate database
  client.select(dbIndex, (err) => {
    if (err) {
      console.error(`Error selecting database ${dbIndex}:`, err);
    } else {
      console.log(`Connected to database ${dbIndex}`);
    }
  });

  // Log errors
  client.on('error', (err) => {
    console.error('Redis Client Error', err);
  });

  return client;
}

// Initialize Redis clients for production and development databases
const prodDB = devClient(
  'redis://',
  0 // Database index for production
);

const devDB = devClient(
  'redis://,
  1 // Database index for development
);

export { devDB, prodDB };
import { Repository, Schema } from 'redis-om';

import { devDB, prodDB } from '../redis-client';

const schema = new Schema(
  // intentional snake case exception to ease working with Redis
  'date_url',
  {
    dateURL: { type: 'string' },
  }
);

// Initialize Repository instances for production and development databases
const prod = new Repository(schema, prodDB);
const dev = new Repository(schema, devDB);

export { dev, prod };
// Save to the production database 
await prod.save(date.toLowerCase().trim(), redisData);
await prod.createIndex();

// Save to the development database 
await dev.save(date.toLowerCase().trim(), redisData);
await dev.createIndex();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants