Is it possible on Nodejs to organize kind of proxy for a Redis connection which would simply rewrite incoming request keys.
The basic idea is to separate/isolate a Data Space for a set of clients on a single Redis instance with kind of perfixing their keys (16 instances is not a solution here)
At the very high level it seems to be as simple as
const shardPrefix = 'some-prefix/'
const server = net.createServer((client) => {
const redisClient = redis.createClient({...})
clientSocket.on('data', (data) => {
const [command, key, ...args] = data.toString().split(' ')
const rewrittenKey = shardPrefix + key
// Forward command and rewritten key to Redis server
redisClient
.sendCommand(command, [rewrittenKey, ...args])
.then((reply) => {
clientSocket.write(reply.toString())
});
});
clientSocket.on('end', () => {
redisClient.quit()
})
})
server.listen(proxyPort)
Still to be sure this have any chance to work
-
Is it correct to expect that all Redis commands have the same format as
command key ...rest
-
would it really work on a such a simple socket streaming or does Redis have any proper protocol for this?
-
On a second thought it seems that even some responses must be patched as well (e.g.
KEYS *
. Are there any more) -
Is there maybe a ready solution for this
-
Or maybe this is achievable simply on a native Redis implementation level it self or its nodejs client
Thanks