skip to Main Content

My Confusion regarding Redis

If I install Redis on my server and my 4 different clients connect to that same redis server, so how will the data between them will be kept separate so as one client does not override the key-value pair that other client has saved.

Eg:-

client-1 set name="adam"

client-2 set name="henry"

So as Redis server is common between these clients the name key set by client-1 will be overwritten by client-2, So when
client-1 execute get name ==> henry (As it has been updated which is wrong, he was expecting it to be adam)

So how does Redis separates the multiple user instance running on same server ? Does it create separate databases internally or stores as per user or what ?

2

Answers


  1. Since you have key-value pairs and you use the very same key for multiple clients, you will need to differentiate your clients. A way to do so would be to prepend the identifier of your client to each key. So, instead of set name you could do something like set client1_name. You would do well to implement some functions in your application that would be called like setName and getName and it would prepend the client identifier to the name under the hood. So, you implement your helper functions once, ensuring that it correctly builds the keys both for getters and setters and never again worry about the client.

    Login or Signup to reply.
  2. Redis itself doesn’t separate your data. You’d have to separate those by yourself. There are many options to do that.

    1. Using Redis database: Redis supports multiple databases. Each application (in your case, client) can be set/allocated to use to use one specific database. This allocation has to be done in application end, not in Redis.

      The limitations of this approach are: i) Redis supports at most 16 databases (denoted from 0 to 15). ii) Redis cluster mode supports only one database.

      Note: SELECT command is used to select a specific database.

    2. Namespacing: Each application can be (for example) assigned an unique prefix. They’d prefix all their keys with that assigned prefix.

    3. Use separate Redis instance per application.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search