Concepts
Thiết lập ban đầu (Initial Setup)
Peformance & Benchmark
Troubleshooting
Các câu hỏi thường gặp (FAQs)
Glossary
Mục này mô tả cách client kết nối tới cụm Redis Replication của FPT Database Engine.
FPT Database Engine cung cấp hai loại endpoint cho cụm Redis Replication:
Primary endpoint luôn trỏ tới Redis Primary node hiện tại và endpoint này sẽ được giữ nguyên ngay cả khi hệ thống xảy ra failover.
Application có thể sử dụng endpoint này để thực hiện các thao tác Redis thông thường như: GET, SET, DEL, EXPIRE, Pub/Sub.
Trường hợp sử dụng khuyến nghị:
Redis Sentinel là thành phần chịu trách nhiệm giám sát các Redis nodes, phát hiện lỗi, thực hiện tự động failover và cung cấp cơ chế service discovery cho Redis clients. Sentinel giúp application tự động kết nối tới Redis Primary hiện tại mà không cần cập nhật lại cấu hình khi xảy ra failover.
Advanced endpoint là endpoint Redis Sentinel được sử dụng cho:
Application sử dụng Sentinel-aware Redis client libraries nên kết nối tới endpoint này thay vì dùng Primary endpoint. Khi failover xảy ra, Sentinel sẽ cập nhật Primary node mới và các Sentinel-aware client libraries sẽ reconnect theo topology mới.
3.1. redis-cli
redis-cli không phải Sentinel-aware client library. redis-cli chỉ kết nối tới một host/port tại một thời điểm và không có cơ chế multi-Sentinel auto discovery giống application libraries. Vì vậy, với redis-cli hãy sử dụng Primary endpoint.
redis-cli -h {vip} -p 6379 -a {redis_password}
|
3.2. Python (redis-py)
pip install redis
from redis.sentinel import Sentinel
sentinel = Sentinel(
[
("{ip_node1}", 26379),
("{ip_node2}", 26379),
("{ip_node3}", 26379),
],
socket_timeout=2,
)
redis_master = sentinel.master_for(
service_name="mymaster",
password="{redis_password}",
decode_responses=True,
)
redis_master.set("test", "hello")
print(redis_master.get("test"))
|
3.3. Node.js (ioredis)
npm install ioredis
const Redis = require("ioredis");
const redis = new Redis({
sentinels: [
{ host: "ip_node1", port: 26379 },
{ host: "ip_node2", port: 26379 },
{ host: "ip_node3", port: 26379 },
],
name: "mymaster",
password: "redis_password",
});
async function test() {
await redis.set("test", "hello");
const value = await redis.get("test");
console.log(value);
}
test();
|
3.3. Spring Boot
spring:
data:
redis:
password: redis_password
sentinel:
master: mymaster
nodes:
- ip_node1:26379
- ip_node2:26379
- ip_node3:26379
|
3.4. Java Spring Boot
Với Java Spring Boot, khách hàng nên sử dụng Spring Data Redis với Lettuce hoặc Jedis client. Cấu hình khuyến nghị là khai báo Sentinel master-name và danh sách Sentinel nodes trong application.yml/application.properties.
# application.properties
spring.data.redis.password=redis_password
spring.data.redis.sentinel.master=mymaster
spring.data.redis.sentinel.nodes=ip_node1:26379,ip_node2:26379,ip_node3:26379
|
Nếu ứng dụng đang dùng phiên bản Spring Boot cũ, key cấu hình có thể là spring.redis.* thay vì spring.data.redis.*. Khách hàng cần kiểm tra theo version Spring Boot đang sử dụng.
| Trường hợp | Endpoint khuyến nghị |
|---|---|
| Kết nối application thông thường | Primary endpoint |
| Kết nối application sử dụng Sentinel-aware client libraries | Advanced endpoint |
| Sử dụng redis-cli | Primary endpoint |