8000 GitHub - ChetanSonigra/Redis: Redis fundamentals and working with redis through python
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ChetanSonigra/Redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis

Redis fundamentals and working with redis through python

Change default port of redis:

Modify /etc/redis/redis.conf file

Redis is case sensitive for keys,values.

Check $home/.rediscli_history for redis commands history.

Important redis commands:

  1. redis-cli:   CLI utility to run redis commands
  2. keys *:   To get all key-values
  3. SET key value:   To store key value
  4. GET key:   To get value of a key
  5. redis-benchmark -q -n 1000:   to benchmark requests
  6. dbsize:   gives number of records
  7. flushall:   removes all records
  8. SET course_name:1:chetan "Python, 9999, 4.7" ex 10:   key= course_name:1:chetan value="Python, 9999, 4.7" expires in 10 seconds
  9. DEL key_name:   Deletes a key
  10. incr key_name:   increments a key value by one
  11. mset key value [key value ...]:  to set multiple key,values at once
  12. mget key1 key2 ...:   to get multiple values for key at once
  13. exists key1 key2 ...: checks if key exists or not
  14. ttl key:   gives time to live for a key until expires
  15. expire key seconds:   to set expire for existing key
  16. persist key:   to remove expire
  17. lpush list_name value [value ....]:   creates a list of values if not exists else adds values at top
  18. rpush list_name value [value ...]:   creates a list of values if not exists else adds values at bottom
  19. lrange list_name 0 -1:   gives all values of list from 0 to -1 both included.
  20. llen list_name:  gives lenght of list_name.
  21. lpop list_name [count]:   pops "count" item from top of a list.
  22. rpop list_name [count]:   pops "count" item from bottm of a list.
  23. ltrim list_name start stop:   keeps only values form start to stop in a list
  24. hset key field1 value1 [field2 value2 ...]:   creates key if not exist else adds in existing key pairs of key-value.
  25. hget key field:   gives value of a field in a key/hash
  26. hmget key field1 field2 ...:  gives values of fields in a key/hash
  27. hmset key field1 value1 [field2 value2 ...]:  similar to hset, deprecated.
  28. sadd users_ip ip1 ip2 ...:  adds values into set
  29. smembers users_ip:  shows all members of set
  30. scard users_ip:  length of a set
  31. sismember users_ip ip1:  checks if member is in set
  32. sdiff set1 set2:  members of set1-set2
  33. spop setname [count]:  pops one or more random members from a set.
  34. srem setname memeber:  removes a member from a set.
  35. watch key:  watches a key during multi transaction
  36. multi:  to start multiple transaction
  37. exec:  to run all commands in a transaction
  38. discard:  to discard a transaction
  39. sentinel master master_name:   gives master node details.
  40. slaveof ip_address port:   makes a redis slave slave of node having given ip and port.
  41. save:   saves current data in dump.rdb in redis-server.

Data Persistence:

  1. RDB:  Performs point in time snapshot of data at specified interval of time.
       save 60 1000 30 500(if every 60 seconds 1000 keys, or 30 seconds 500 keys)
       dir path/to/dir/to_save/redis_dump.rdb
       add above 2 line in redis.conf
       Advantages:
       - Compact, single file of redis data
       - Perfect for backups
       - Performance is good
       - Allows faster restarts
       Disadvantages:
       - Chances of data loss
       - Fork process, may impact write operations for few milliseconds.

  2. AOF:  Append Only File - Keeps log of every write operations in redis.
       Define fsycn policies, always, everysec
       appendonly yes
       Add above line in redis.conf
       Advantages:
       - High Durability
       - Allows different fsync policies
       - Append only logs, no chances of corruption
       - Corrupted files can be fixed by redis-check-aof tool
       - AOF files can be parsed, lines can be removed.
       Disadvantages:
       - Size is bigger than RDB
       - Slower than RDB

  3. Hybrid:  (RDB+AOF) - Both can work together.:

Replication:

  • Add replicaof master_ip master_port in slave's redis.conf file.
  • change port, logfilename, dump.rdb file name for slaves in redis.conf file.
  • Run redis server for master and slaves.
  • slaves can only be used to read data.

High Availability:

  • Install sentinel on minimum 3 servers.
  • Quorum (No. of sentinel which needs agree on master down and elect a new master)
  • down-after milliseconds
  • Redis port = 6379, Sentinel port = 26379
  • redis.conf, sentinel.conf
  1. Sentinel Deployment 01:

    • Never do this deployment
    • 2 sentinels, 1 master 1 slave
    • quorum =2
    • Only works if redis is down but sentinels are working on both servers.
    • if complete box is down,redis will become unavailable.
  2. Sentinel Deployment 02:

    • 3 sentinels, 1 master, 2 slaves
    • Quorum = 2
    • Good approach if we have min 3 servers.
    • issues:
    •   1. If nodes are seperated by network partition
    •   Mitigation - min-replica-to-write 1 (stops writes if can't write to replica), min-replicas-max-lag 10
    •   2. If two replicas are down, master stops accepting writes.
  3. Sentinel Deployment 03:

    • 3 sentinels(on application server), 1 master 1 slave
    • Quorum 2
    • Good approach if we have only 2 redis servers.
    • Issue:
    •   Network disconnect issue between redis server and application server.
  4. Sentinel Deployment 04:

    • 4 sentinels (2 on application server, 2 on redis server), 1 master, 1 redis
    • Quorum = 3
    • Good approach if we have min 3 servers.

Redis Cluster:

  • minimum 3 masters and 3 slaves needed.
  • Multi master architecture
  • Data is partitioned into 16k buckets.
  • Each bucket are assigned a master.
  • Pros:
  •   Scalable
  •   Can scale upto 1000 nodes cluster
  •   Performance is good
  •   Allows multiple masters
  • Cons:
  •   Need atleast 6 servers to setup a cluster
  •   Upgrading or downgrading to other topology is difficult.

Steps:

  1. create n1_redis.conf file with details as below in each node:
      port 7001/7002/7003/7004/7005/7006
      cluster-enabled yes
      cluster-config-file nodes.conf
      cluster-node-timeout 5000
      appendonly yes

  2. Start redis-server on each node
      redis-server n1_redis.conf &

  3. Create a cluster:
      redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1

  4. Connect to server:
      redis-cli -c -p 7001

  5. Check cluster health:
      redis-cli --cluster check 127.0.0.1:7001
      redis-cli -p 7001 cluster nodes

About

Redis fundamentals and working with redis through python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0