redis-connection
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 : Endpoint kết nối Redis tiêu chuẩn cho application thông qua port 6379 , định dạng: {vip}:6379. Ví dụ: 172.26.47.3:6379. Người dùng có thể lấy được endpoint này bằng cách truy cập màn hình Overview của cụm cơ sở dữ liệu trên Console Portal.
- Advanced endpoint : Endpoint Redis Sentinel dùng cho service discovery và failover. Application kết nối tới các Sentinel nodes qua port 26379 , định dạng: {ip_node1}:26379,{ip_node2}:26379,{ip_node3}:26379. Ví dụ: 192.168.47.10:26379,192.168.47.20:26379,192.168.47.30:26379.
1. Primary endpoint
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ị:
- Application thông thường.
- Redis client đơn giản.
- Hệ thống không cần cấu hình Sentinel-aware.
2. Advanced endpoint
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:
- Service discovery.
- Tự động xử lý failover.
- Tự động phát hiện Primary node hiện tại.
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.
Trường hợp sử dụng khuyến nghị:
- Hệ thống yêu cầu High Availability.
- Ứng dụng cần tự động reconnect khi failover.
- Redis client nâng cao có hỗ trợ Sentinel.
- Môi trường production yêu cầu topology awareness
3. Hướng dẫn kết nối với từng application client libraries
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.
4. Nên sử dụng endpoint nào?
| 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 |