:::: MENU ::::

Sunday, November 5, 2023

Agenda

  • What is Redis? Use cases and benefits of Redis?
  • Why Redis is suitable for microservices apps?
  • How Redis supports multiple data formats?
  • Data persistence and Recovery with redis?
  • How to optimize memory cost with Redis on Flash?
  • Scaling Redis & High Availability across multiple geographic locations?

What is Redis? Use cases and benefits of Redis?

It stands for Remote Dictionary Server. It stores data on computer’s memory (RAM). It is often used for caching data on top of other database (e.g. MySQL). But Redis is a fully fledged primary db. That can be used for persistence multiple data formats.

How Redis supports multiple data formats?

It allows developer to store different data structures e.g. graph, document, relational data etc.



Characteristics:
  • In memory data structure store
  • used as a NoSQL database
  • distributed cache
  • message broker

Supports various data structure

  • string
  • hashes
  • list
  • sets
  • sorted sets
  • bitmaps
  • hyperloglog
  • geo spatial indexes

Architecture

  • Redis client
  • Redis Server

Redis client sends and receives data from redis Server. It’s also possible master-slave configuration for distributed system.

Advantages

  • Exceptionally fast
  • persistence
  • rich support of data types
  • atomicity
  • multi-utility tool, caching, message queue etc.

Redis installation

on Linux: https://redis.io/docs/getting-started/installation/install-redis-on-linux/

To check if Redis is running on your local Linux machine after installing it via Snap, you can use the following steps:

  1. Status Command: You can check the status of the Redis service by running the following command in your terminal:
    sudo systemctl status snap.redis.server

    This command will show the status and information about the Redis service.

  1. Redis-CLI: You can also check if Redis is running by using the Redis command-line interface (redis-cli). Open a terminal and run the following command:
    redis-cli

    If Redis is running, this command will start the Redis CLI, and you'll see a prompt like 127.0.0.1:6379>. This indicates that Redis is up and running.

Redis Data types

String

  • 127.0.0.1:6379> keys *

    display all keys

  • > set [key] [value]

    it will set value on key. Redis sets key-value pair.

  • > get [key]

    It will display value of given [key]

  • > mset [key] [value] [key] [value]
  • > mget [key] [key]
  • > strlen [key]
  • > incr [key]

    it will increment value of given key.

  • > incrby [key] [value]
  • > decr [key]
  • > decrby [key] [value]
  • > incrbyfloat [key] [value]
  • > expire [key] [duration]

    expiring a key-value after a duration

  • > setex [key] [duration] [value]

list

  • > flushall

    it clears all key-value pairs

  • > lpush [list-name] [list-item]

    insert

  • > lrange [list-name] [start-index] [end-index]

    e.g. > lrange country 0 -1

  • > rpush [list-name] [list-item]
  • > llen [list-name]
  • > lpop [list-name]

    it removes last left-sided item

  • > rpop [list-name]

    it removes last right-sided item

  • > lset [list-name] [index] [item]

    it assigns [item] to [index] of [list-name]

  • > linsert [list-name] before|after [pivot-value] [item-value]
  • > lindex [list-name] [index]

    get value of index.Index is 0 based

  • > lpushx [list-name] [item]

    it will return 0 if [list-name] is not exist before.

  • > rpushx [list-name] [item]

set

  • > sadd [set-name] [set-item]
  • > sadd [set-name] [set-items]
  • > smembers [set-name]
  • > scard [set-name]

    return the length of set. card means cardinality

  • > sismember [set-name] [value]

    check is given [value] member of [set-name]

  • > smembers [set-name]
  • > sdiff [set1] [set2]
  • > sdiffstore [newset] [set1] [set2]
  • > sinter [set1] [set2] [set3]
  • > sinterstore [newset] [set1] [set2]
  • > sunion [set1] [set2] [set3]
  • > sunionstore [newset] [set1] [set2]

sorted set

  • > zadd [set-name] [score] [value]
  • > zadd [set-name] [score] [value] [score] [value]
  • > zrange [set-name] [start] [end]

    display all sorted elements

  • > zrange [set-name] [start] [end] withscores
  • > zcard [set]
  • > zcount users [min] [max]
  • > zrem [set] [value]

    it removes the set value

  • > zrevrange [set] [start] [end] withscores
  • > zscore [set] [value]
  • > zincrby [set] [scrore] [value]

hyperloglog

  • > pfadd [hll] [value]
  • > pfcount [hll]

Hash

keys are case-sensitive

  • > hset [hash-name] [key] [value]

  • > hkeys [hash]
  • > hvals [hash]
  • > hgetall [hash]
  • > hexists [hash] [key]

    return 0 if key isn’t exist in [hash], otherwise return 1

  • > hlen [hash]

    return the count of keys.

  • > hmset [hash] [key1] [value1] [key2] [value2]
  • > hmget [hash] [key1] [key2]
  • > hincrby [hash] [key] [incrementbyValue]

    e.g. hincrby myhash age 5

  • > hincrbyfloat [hash] [key] [incrementbyValue]
  • > hdel [hash] [key]
  • > hstrlen [hash] [key]

    return the length of value that is stored in [key] into [hash]

  • > hsetnx [hash] [key] [value]

    it saves key-value pair, if key isn’t exist before.

Redis transaction

  • > multi

    It helps to execute multiple commands in one transaction

    > multi
    OK
    > set name saiful
    Queued
    > get name
    Queued
    > set b 1
    Queued
    > exec
    OK
    (nil)
    OK
    OK
  • > discard

    it discard the transaction

    > multi
    OK
    > set name saiful
    Queued
    > get name
    Queued
    > set b 1
    Queued
    > discard
    OK
  • > watch

Redis pub-sub

  • > subscribe [channel-name]
    > subscribe news
    Reading messages ... (press Ctrl-C to quit)
    1) subscribe
    2) news
    3) (integer) 1
  • > subscribe [channel1] [channel2]
  • > psubscribe [channel-name-regex] [channel-name-regex]
    psubscribe news* h?llo o[ai]ll
  • > publish news [message]
    > publish news "new breaking news"
    (integer) 2

    the output 2 means there are two subscriber for news channel

  • > pubsub channels
  • > pubsub numsub [channel-name]
  • > pubsub numpat

Redis scripts

In Redis, a script refers to a Lua script that you can execute on the server. These scripts are used for complex operations that may involve multiple Redis commands. Redis supports Lua scripting as a way to perform atomic operations, batch operations, and more.

Here are some key points about Redis scripts:

  1. Lua Scripting: Redis uses the Lua scripting language for its scripting capabilities. Lua is a lightweight, embeddable scripting language known for its simplicity and efficiency.
  1. Atomic Execution: When you run a Lua script in Redis, it is executed atomically on the server. This means that the script's commands are executed as a single, indivisible operation. This is useful for ensuring that multiple commands are executed in a transaction-like manner.
  1. Batch Operations: Scripts are particularly useful when you need to perform multiple Redis operations in a single network round trip. Instead of sending multiple Redis commands over the network, you can send a single script that performs all the required operations.
  1. Evaluating Scripts: You can evaluate a Lua script using the EVAL or EVALSHA commands in Redis. EVAL allows you to provide the Lua script as a parameter, while EVALSHA takes a SHA1 hash of the script, which can be useful for caching commonly used scripts.
  1. Arguments: Redis scripts can accept arguments, which are passed to the script as part of the evaluation. This makes scripts dynamic and allows you to reuse them with different data.
  1. Return Values: Lua scripts can return values, and these return values can be used in your application. This allows you to perform computations and data manipulation on the server side.
  1. Error Handling: Redis scripts have error-handling capabilities. You can use the redis.error_reply and redis.status_reply functions in Lua to return specific errors or status messages to the client.
  1. Script Caching: Redis caches scripts internally based on their SHA1 hashes. This reduces the overhead of sending the same script multiple times.

Lua scripting in Redis is a powerful feature that can help you optimize and simplify complex operations while maintaining data integrity. It's commonly used for scenarios like implementing custom data processing logic, complex transactional operations, and more.

Example of commands

  • > eval “redis.call(’set’, KEYS[1],ARGV[1])” [numberofpair] [key] [value]

    e.g. eval “redis.call(’set’, KEYS[1], ARGV[1])” 1 name saiful

Connections and Security

  • > ping
  • > echo hello
  • > client list
  • > client setname [client-name]
  • > client kill id [client-id]
  • > config set requirepass [pass]
  • > auth [pass]

Redis benchmark

  • > redis-benchmark -n 1000 -d 1000000 -c 200

Measuring latency

If you are experiencing latency problems, you probably know how to measure it in the context of your application, or maybe your latency problem is very evident even macroscopically. However redis-cli can be used to measure the latency of a Redis server in milliseconds, just try:

redis-cli --latency -h `host` -p `port`

intrinsic latency comes for operating system, e.g. the environment where redis server is running. keep in mind that the intrinsic latency may change over time depending on the load of the system.

redis-cli --intrinsic-latency 100

Credits:

https://redis.io/docs/management/optimization/latency/#:~:text=Redis uses a mostly single,the requests are served sequentially.

https://redis.io/docs/management/optimization/latency/#measuring-latency

https://redis.com/blog/you-dont-need-transaction-rollbacks-in-redis/

0 comments:

Post a Comment