Redis – Client Connection

Redis – Client Connection ”; Previous Next Redis accepts clients’ connections on the configured listening TCP port and on the Unix socket, if enabled. When a new client connection is accepted, the following operations are performed − The client socket is put in non-blocking state since Redis uses multiplexing and non-blocking I/O. The TCP_NODELAY option is set in order to ensure that we don”t have delays in our connection. A readable file event is created so that Redis is able to collect the client queries as soon as new data is available to be read on the socket. Maximum Number of Clients In Redis config (redis.conf), there is a property called maxclients, which describes the maximum number of clients that can connect to Redis. Following is the basic syntax of command. config get maxclients 1) “maxclients” 2) “10000” By default, this property is set to 10000 (depending upon the maximum number of file descriptors limit of OS), although you can change this property. Example In the following example, we have set the maximum number of clients to 100000, while starting the server. redis-server –maxclients 100000 Client Commands Sr.No Command Description 1 CLIENT LIST Returns the list of clients connected to Redis server 2 CLIENT SETNAME Assigns a name to the current connection 3 CLIENT GETNAME Returns the name of the current connection as set by CLIENT SETNAME 4 CLIENT PAUSE This is a connections control command able to suspend all the Redis clients for the specified amount of time (in milliseconds) 5 CLIENT KILL This command closes a given client connection. Print Page Previous Next Advertisements ”;

Redis – Sets

Redis – Sets ”; Previous Next Redis Sets are an unordered collection of unique strings. Unique means sets does not allow repetition of data in a key. In Redis set add, remove, and test for the existence of members in O(1) (constant time regardless of the number of elements contained inside the Set). The maximum length of a list is 232 – 1 elements (4294967295, more than 4 billion of elements per set). Example redis 127.0.0.1:6379> SADD tutorials redis (integer) 1 redis 127.0.0.1:6379> SADD tutorials mongodb (integer) 1 redis 127.0.0.1:6379> SADD tutorials mysql (integer) 1 redis 127.0.0.1:6379> SADD tutorials mysql (integer) 0 redis 127.0.0.1:6379> SMEMBERS tutorials 1) “mysql” 2) “mongodb” 3) “redis” In the above example, three values are inserted in Redis set named ‘tutorials’ by the command SADD. Redis Sets Commands Following table lists some basic commands related to sets. Sr.No Command & Description 1 SADD key member1 [member2] Adds one or more members to a set 2 SCARD key Gets the number of members in a set 3 SDIFF key1 [key2] Subtracts multiple sets 4 SDIFFSTORE destination key1 [key2] Subtracts multiple sets and stores the resulting set in a key 5 SINTER key1 [key2] Intersects multiple sets 6 SINTERSTORE destination key1 [key2] Intersects multiple sets and stores the resulting set in a key 7 SISMEMBER key member Determines if a given value is a member of a set 8 SMEMBERS key Gets all the members in a set 9 SMOVE source destination member Moves a member from one set to another 10 SPOP key Removes and returns a random member from a set 11 SRANDMEMBER key [count] Gets one or multiple random members from a set 12 SREM key member1 [member2] Removes one or more members from a set 13 SUNION key1 [key2] Adds multiple sets 14 SUNIONSTORE destination key1 [key2] Adds multiple sets and stores the resulting set in a key 15 SSCAN key cursor [MATCH pattern] [COUNT count] Incrementally iterates set elements Print Page Previous Next Advertisements ”;

Redis – Hashes

Redis – Hashes ”; Previous Next Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects. In Redis, every hash can store up to more than 4 billion field-value pairs. Example redis 127.0.0.1:6379> HMSET tutorialspoint name “redis tutorial” description “redis basic commands for caching” likes 20 visitors 23000 OK redis 127.0.0.1:6379> HGETALL tutorialspoint 1) “name” 2) “redis tutorial” 3) “description” 4) “redis basic commands for caching” 5) “likes” 6) “20” 7) “visitors” 8) “23000” In the above example, we have set Redis tutorials detail (name, description, likes, visitors) in hash named ‘tutorialspoint’. Redis Hash Commands Following table lists some basic commands related to hash. Sr.No Command & Description 1 HDEL key field2 [field2] Deletes one or more hash fields. 2 HEXISTS key field Determines whether a hash field exists or not. 3 HGET key field Gets the value of a hash field stored at the specified key. 4 HGETALL key Gets all the fields and values stored in a hash at the specified key 5 HINCRBY key field increment Increments the integer value of a hash field by the given number 6 HINCRBYFLOAT key field increment Increments the float value of a hash field by the given amount 7 HKEYS key Gets all the fields in a hash 8 HLEN key Gets the number of fields in a hash 9 HMGET key field1 [field2] Gets the values of all the given hash fields 10 HMSET key field1 value1 [field2 value2 ] Sets multiple hash fields to multiple values 11 HSET key field value Sets the string value of a hash field 12 HSETNX key field value Sets the value of a hash field, only if the field does not exist 13 HVALS key Gets all the values in a hash 14 HSCAN key cursor [MATCH pattern] [COUNT count] Incrementally iterates hash fields and associated values Print Page Previous Next Advertisements ”;

Redis – Publish Subscribe

Redis – Publish Subscribe ”; Previous Next Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels. Example Following example explains how publish subscriber concept works. In the following example, one client subscribes a channel named ‘redisChat’. redis 127.0.0.1:6379> SUBSCRIBE redisChat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “redisChat” 3) (integer) 1 Now, two clients are publishing the messages on the same channel named ‘redisChat’ and the above subscribed client is receiving messages. redis 127.0.0.1:6379> PUBLISH redisChat “Redis is a great caching technique” (integer) 1 redis 127.0.0.1:6379> PUBLISH redisChat “Learn redis by tutorials point” (integer) 1 1) “message” 2) “redisChat” 3) “Redis is a great caching technique” 1) “message” 2) “redisChat” 3) “Learn redis by tutorials point” Redis PubSub Commands Following table lists some basic commands related to Redis Pub/Sub. Sr.No Command & Description 1 PSUBSCRIBE pattern [pattern …] Subscribes to channels matching the given patterns. 2 PUBSUB subcommand [argument [argument …]] Tells the state of Pub/Sub system. For example, which clients are active on the server. 3 PUBLISH channel message Posts a message to a channel. 4 PUNSUBSCRIBE [pattern [pattern …]] Stops listening for messages posted to channels matching the given patterns. 5 SUBSCRIBE channel [channel …] Listens for messages published to the given channels. 6 UNSUBSCRIBE [channel [channel …]] Stops listening for messages posted to the given channels. Print Page Previous Next Advertisements ”;

Redis – Transactions

Redis – Transactions ”; Previous Next Redis transactions allow the execution of a group of commands in a single step. Following are the two properties of Transactions. All commands in a transaction are sequentially executed as a single isolated operation. It is not possible that a request issued by another client is served in the middle of the execution of a Redis transaction. Redis transaction is also atomic. Atomic means either all of the commands or none are processed. Sample Redis transaction is initiated by command MULTI and then you need to pass a list of commands that should be executed in the transaction, after which the entire transaction is executed by EXEC command. redis 127.0.0.1:6379> MULTI OK List of commands here redis 127.0.0.1:6379> EXEC Example Following example explains how Redis transaction can be initiated and executed. redis 127.0.0.1:6379> MULTI OK redis 127.0.0.1:6379> SET tutorial redis QUEUED redis 127.0.0.1:6379> GET tutorial QUEUED redis 127.0.0.1:6379> INCR visitors QUEUED redis 127.0.0.1:6379> EXEC 1) OK 2) “redis” 3) (integer) 1 Redis Transaction Commands Following table shows some basic commands related to Redis transactions. Sr.No Command & Description 1 DISCARD Discards all commands issued after MULTI 2 EXEC Executes all commands issued after MULTI 3 MULTI Marks the start of a transaction block 4 UNWATCH Forgets about all watched keys 5 WATCH key [key …] Watches the given keys to determine the execution of the MULTI/EXEC block Print Page Previous Next Advertisements ”;

Redis – Sorted Sets

Redis – Sorted Sets ”; Previous Next Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a set. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. In Redis sorted set, add, remove, and test for the existence of members in O(1) (constant time regardless of the number of elements contained inside the set). Maximum length of a list is 232 – 1 elements (4294967295, more than 4 billion of elements per set). Example redis 127.0.0.1:6379> ZADD tutorials 1 redis (integer) 1 redis 127.0.0.1:6379> ZADD tutorials 2 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD tutorials 3 mysql (integer) 1 redis 127.0.0.1:6379> ZADD tutorials 3 mysql (integer) 0 redis 127.0.0.1:6379> ZADD tutorials 4 mysql (integer) 0 redis 127.0.0.1:6379> ZRANGE tutorials 0 10 WITHSCORES 1) “redis” 2) “1” 3) “mongodb” 4) “2” 5) “mysql” 6) “4” In the above example, three values are inserted with its score in Redis sorted set named ‘tutorials’ by the command ZADD. Redis Sorted Sets Commands Following table lists some basic commands related to sorted sets. Sr.No Command & Description 1 ZADD key score1 member1 [score2 member2] Adds one or more members to a sorted set, or updates its score, if it already exists 2 ZCARD key Gets the number of members in a sorted set 3 ZCOUNT key min max Counts the members in a sorted set with scores within the given values 4 ZINCRBY key increment member Increments the score of a member in a sorted set 5 ZINTERSTORE destination numkeys key [key …] Intersects multiple sorted sets and stores the resulting sorted set in a new key 6 ZLEXCOUNT key min max Counts the number of members in a sorted set between a given lexicographical range 7 ZRANGE key start stop [WITHSCORES] Returns a range of members in a sorted set, by index 8 ZRANGEBYLEX key min max [LIMIT offset count] Returns a range of members in a sorted set, by lexicographical range 9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] Returns a range of members in a sorted set, by score 10 ZRANK key member Determines the index of a member in a sorted set 11 ZREM key member [member …] Removes one or more members from a sorted set 12 ZREMRANGEBYLEX key min max Removes all members in a sorted set between the given lexicographical range 13 ZREMRANGEBYRANK key start stop Removes all members in a sorted set within the given indexes 14 ZREMRANGEBYSCORE key min max Removes all members in a sorted set within the given scores 15 ZREVRANGE key start stop [WITHSCORES] Returns a range of members in a sorted set, by index, with scores ordered from high to low 16 ZREVRANGEBYSCORE key max min [WITHSCORES] Returns a range of members in a sorted set, by score, with scores ordered from high to low 17 ZREVRANK key member Determines the index of a member in a sorted set, with scores ordered from high to low 18 ZSCORE key member Gets the score associated with the given member in a sorted set 19 ZUNIONSTORE destination numkeys key [key …] Adds multiple sorted sets and stores the resulting sorted set in a new key 20 ZSCAN key cursor [MATCH pattern] [COUNT count] Incrementally iterates sorted sets elements and associated scores Print Page Previous Next Advertisements ”;

Redis – HyperLogLog

Redis – HyperLogLog ”; Previous Next Redis HyperLogLog is an algorithm that uses randomization in order to provide an approximation of the number of unique elements in a set using just a constant, and small amount of memory. HyperLogLog provides a very good approximation of the cardinality of a set even using a very small amount of memory around 12 kbytes per key with a standard error of 0.81%. There is no limit to the number of items you can count, unless you approach 264 items. Example Following example explains how Redis HyperLogLog works. redis 127.0.0.1:6379> PFADD tutorials “redis” 1) (integer) 1 redis 127.0.0.1:6379> PFADD tutorials “mongodb” 1) (integer) 1 redis 127.0.0.1:6379> PFADD tutorials “mysql” 1) (integer) 1 redis 127.0.0.1:6379> PFCOUNT tutorials (integer) 3 Redis HyperLogLog Commands Following table lists some basic commands related to Redis HyperLogLog. Sr.No Command & Description 1 PFADD key element [element …] Adds the specified elements to the specified HyperLogLog. 2 PFCOUNT key [key …] Returns the approximated cardinality of the set(s) observed by the HyperLogLog at key(s). 3 PFMERGE destkey sourcekey [sourcekey …] Merges N different HyperLogLogs into a single one. Print Page Previous Next Advertisements ”;

Redis – Lists

Redis – Lists ”; Previous Next Redis Lists are simply lists of strings, sorted by insertion order. You can add elements in Redis lists in the head or the tail of the list. Maximum length of a list is 232 – 1 elements (4294967295, more than 4 billion of elements per list). Example redis 127.0.0.1:6379> LPUSH tutorials redis (integer) 1 redis 127.0.0.1:6379> LPUSH tutorials mongodb (integer) 2 redis 127.0.0.1:6379> LPUSH tutorials mysql (integer) 3 redis 127.0.0.1:6379> LRANGE tutorials 0 10 1) “mysql” 2) “mongodb” 3) “redis” In the above example, three values are inserted in Redis list named ‘tutorials’ by the command LPUSH. Redis Lists Commands Following table lists some basic commands related to lists. Sr.No Command & Description 1 BLPOP key1 [key2 ] timeout Removes and gets the first element in a list, or blocks until one is available 2 BRPOP key1 [key2 ] timeout Removes and gets the last element in a list, or blocks until one is available 3 BRPOPLPUSH source destination timeout Pops a value from a list, pushes it to another list and returns it; or blocks until one is available 4 LINDEX key index Gets an element from a list by its index 5 LINSERT key BEFORE|AFTER pivot value Inserts an element before or after another element in a list 6 LLEN key Gets the length of a list 7 LPOP key Removes and gets the first element in a list 8 LPUSH key value1 [value2] Prepends one or multiple values to a list 9 LPUSHX key value Prepends a value to a list, only if the list exists 10 LRANGE key start stop Gets a range of elements from a list 11 LREM key count value Removes elements from a list 12 LSET key index value Sets the value of an element in a list by its index 13 LTRIM key start stop Trims a list to the specified range 14 RPOP key Removes and gets the last element in a list 15 RPOPLPUSH source destination Removes the last element in a list, appends it to another list and returns it 16 RPUSH key value1 [value2] Appends one or multiple values to a list 17 RPUSHX key value Appends a value to a list, only if the list exists Print Page Previous Next Advertisements ”;

Redis – Scripting

Redis – Scripting ”; Previous Next Redis scripting is used to evaluate scripts using the Lua interpreter. It is built into Redis starting from version 2.6.0. The command used for scripting is EVAL command. Syntax Following is the basic syntax of EVAL command. redis 127.0.0.1:6379> EVAL script numkeys key [key …] arg [arg …] Example Following example explains how Redis scripting works. redis 127.0.0.1:6379> EVAL “return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}” 2 key1 key2 first second 1) “key1” 2) “key2” 3) “first” 4) “second” Redis Scripting Commands Following table lists some basic commands related to Redis Scripting. Sr.No Command & Description 1 EVAL script numkeys key [key …] arg [arg …] Executes a Lua script. 2 EVALSHA sha1 numkeys key [key …] arg [arg …] Executes a Lua script. 3 SCRIPT EXISTS script [script …] Checks the existence of scripts in the script cache. 4 SCRIPT FLUSH Removes all the scripts from the script cache. 5 SCRIPT KILL Kills the script currently in execution. 6 SCRIPT LOAD script Loads the specified Lua script into the script cache. Print Page Previous Next Advertisements ”;

Redis – Connections

Redis – Connections ”; Previous Next Redis connection commands are basically used to manage client connections with Redis server. Example Following example explains how a client authenticates itself to Redis server and checks whether the server is running or not. redis 127.0.0.1:6379> AUTH “password” OK redis 127.0.0.1:6379> PING PONG Redis Connection Commands Following table lists some basic commands related to Redis connections. Sr.No Command & Description 1 AUTH password Authenticates to the server with the given password 2 ECHO message Prints the given string 3 PING Checks whether the server is running or not 4 QUIT Closes the current connection 5 SELECT index Changes the selected database for the current connection Print Page Previous Next Advertisements ”;