Search code examples
redisreadonlyjedis

Is there a read only connection in Redis?


I searched google for this but didn't get a clue.

Suppose a user has full read/write access to Redis database. I am wondering, is there any way to connect to database in read-only mode?


Solution

  • Updated after Redis 7

    REDIS ACL. can be used to create a read-only users on your database.

    Here's how to do it

    First create a user with read only rights

    ACL SETUSER readonlyuser on >password ~* +@read
    

    Then connect to the database specifying the connection string to ensure that you don't connect to the default user with all rights.

    redis-cli -u redis://readonlyuser:password@localhost:6379
    

    Now read commands like these would work

    GET testkey
    KEYS *
    

    But write commands won't.

    SET testkey "somevalue"
    

    Error that I got was

    localhost:6379> SET testkey "somevalue"
    (error) NOPERM User readonlyuser has no permissions to run the 'set' command
    

    Old answer

    Redis, until and including v5, does not have the notion of user privileges as it does not support the notion of users. There is one user as far as the database is concerned, and that user is omnipotent.

    That said, since v2.6 replicas are configured by default to reject writes, effectively making them a read-only interface (the replica-read-only configuration directive).

    Note: it is expected that in its next major version, Redis will provide user access lists.