Redis From Scratch

Redis – Home

Redis means “Remote Dictionary Server”. It is a memory-based (in-memory) NoSQL database. It is known for its fast data processing and is used in many modern applications for functions such as caching, queuing systems, session management.

Redis – Overview

Redis uses the key-value structure. It is very fast because it stores data on RAM. Use different data types (string, list, set, hash, sorted set, etc.) supports. It includes advanced features such as distributed system support (replication, clustering), persistence (persistence), pub/sub mechanism.

Redis – Environment

Redis must be installed on a server to run Redis. It is usually run in a Linux environment. The command line tool is managed with redis-cli.

Commands:

redis-server # Starts the Redis server
redis-cli # Connects to the Redis client

Redis – Configuration

The configuration file is redis.it is made with conf. For example:

port 6379: The port that the Redis server is listening to

requirepass: Password protection

appendonly yes: Enable AOF (Append Only File)

Command:
redis-server /etc/redis/redis.conf

Redis – Data types

Redis supports many types of data:

String

Hash

List

The Set

Sorted Set

HyperLogLog

Bitmaps

Streams

Redis – Commands

Redis commands cover data reading/writing, management, connection and monitoring operations.

Example commands:
SET key value
GET the key
DEL key
EXISTS key

Redis – Keys

In Redis, each data has a key. Key management is important.

Commands:
KEYS * # Lists all keys
EXISTS mykey # Is there a key?
EXPIRE mykey 60# Define the duration of the key
TTL mykey # Time remaining time

Redis – Strings

It is the simplest and most common data type. It can store all kinds of data (number, text).

Commands:

SET name “Ibrahim”
GET name
INCR counter
APPEND name “star”

Redis – Hashes

It stores JSON-like field-value pairs.

Commands:

HSET user:100 name “Ali” age “25”
HGET user:100 name
HGETALL users:100

Redis – Lists

A sequential, double-ended list. It can be used as a queue or stack.

Commands:
LPUSH mylist “a”
RPUSH mylist “b”
LP mylist
ORANGE mylist 0 -1

Redis – Sets

A set of unique items. It’s out of order.

Commands:

SADD tags “redis”
SADD tags “nosql”
SMEMBERS tags
SISMEMBER tags “redis”

Redis – Sorted Sets

Sequential data sets based on score. For example: the leaderboard.

Commands:

ZADD scores 100 “Ali”
ZADD scores 200 “Parents”
ZRANGE scores 0 -1 WITHSCORES

Redis – HyperLogLog

It is used for approximate unique counting.

Commands:

PFADD visitors “user1”
PFADD visitors “user2”
PFCOUNT visitors

Redis – Publish Subscription

Real-time messaging. Data is broadcast over a channel, subscribers receive it.

Commands:

SUBSCRIBE to news
PUBLIC news “There is news!”

Redis – Connections

Management of clients connected to Redis.

Commands:
CLIENT LIST
CLIENT KILL ip:port

Redis – Server

Status of the Redis server, statistics.

Commands:

INFO
MONITOR
GET CONFIG *

INFO IMPORTANT AREAS

  1. Server

General information of the Redis server:

  • redis_version: Redis version
  • os: Operating system (Darwin= macOS)
  • config_file: The path of the Redis configuration file
  1. Clients
  • connected_clients: The number of clients currently connected
  • blocked_clients: Blocked (WAIT command, etc.) number of clients
  • pubsub_clients: The number of clients using the Pub/Sub property
  1. Memory
  • used_memory_human: Total memory used
  • used_memory_dataset: Memory used only for the data set
  • maxmemory: Memory limit (unlimited if it is 0)
  • mem_fragmentation_ratio: Memory fragmentation rate (inefficient use above 1.0)
  1. Persistence (Permanent Memory)
  • rdb_last_bgsave_status: Is the RDB registration successful?
  • aof_enabled: Is AOF active? (0→ passive, 1→ active)
  1. Stats
  • total_commands_processed: The total number of commands processed
  • keyspace_hits, keyspace_misses: Cache hit/miss numbers
  • expired_keys: The number of expired keys
  1. Replication
  • role: Is this Redis sample master or replica?
  • connected_slaves: Connected replication
  1. Keyspace
  • db0:keys=19: There are 19 keys
  • expires=0: None of them will be deleted automatically
  • avg_ttl=0: Average life span (ms), if 0, there is no TTL
  1. Modules
  • vectorset: A special module installed in Redis for vector operations

Redis – Backup

RDB and AOF mechanisms are used for data persistence. Manual or automatic backup.

Commands:

SAVE # Synchronous backup
GET THE CONFIG DIR
BGSAVE # Backup in the background

Redis – Security (Security)

Purpose: Access control, authorization, external closure.

  • Setting a password: requirepass Welcome1 (redis.conf or runtime)
  • Connecting with a password:

CONFIG set requirepass “tutorialspoint”
arrow

CONFIG get requirepass
1) “requirepass”
2) “tutorialspoint”

AUTH “tutorialspoint”
To remove the password, CONFIGURE SET requirepass “”

ACL (Access Control List):

ACL SETUSER abraham on >sifre123~*+@all
ACL LIST
ACL DELUSER ibrahim

Redis – Benchmarks

redis-benchmark
redis-benchmark -q -n 10000 -c 50 -P 16

Redis – Partitioning

Redis normally runs on a single server. But if the data is too much, if the RAM is not enough, it will not be enough.
Partitioning means splitting data into multiple Redis servers. So:

The data is stored on different machines
The load is balanced
More RAM is used
erformance increases

Redis Cluster: The Real Application of Fragmentation

There is a slot-based shredding method in Redis. This is called a Redis Cluster.

Basic Features:

  • There are a total of 16384 slots.
  • Each key is assigned to a slot: hash_slot = CRC16(key) mod 16384
  • These slots are divided into nodes.
  • Each node is responsible for certain slots.

Installation Example (3 master nodes)

redis-cli –create a cluster 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 — cluster-replicas 0

This command does this:

  • creates a cluster consisting of 3 nodes
  • automatically divides the 16384 slot into 3 nodes
  • Eg:
    • 0–5460 → 7000
    • 5461–10922 → 7001
    • 10923–16383 → 7002

Installation Example (3 master + 3 replica)

redis-cli –create a cluster \
127.0.0.1:7000 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 \
–cluster-replicas 1

This command does this:

  • installs 3 master, 3 replica nodes
  • Each master has 1 replica
  • Slots are distributed automatically again

What Does 16384 Slots Mean?

The Redis Cluster places the keys in numerical zones called slots.
The number of slots is fixed: 16384 (from 0 to 16383).

Each key is assigned to a slot by the → hash algorithm:
So:

  • Each key falls into a single slot
  • But there can be thousands of keys in one slot

Let’s say you have 100 million keys.
Redis distributes this 100M key to 16384 slots.
So each slot can accommodate an average of ~6100 keys.

  • Held steady because:
  • It’s getting divided enough
  • Ideal in terms of performance
  • Slot management is easy

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir