Vận hành Redis production tốt đòi hỏi cấu hình security đúng (auth, TLS, ACL), chọn persistence phù hợp, tránh blocking operation, và monitor các metric quan trọng như evicted_keys và slowlog.
Best practices khi vận hành Redis trong production:
- Security: bật authentication (
requirepass), dùng ACL (ACL SETUSER ... nocommands +@write) thay vìrename-commandcho Redis 6.0+ để kiểm soát permission chi tiết hơn, bind Redis chỉ trên internal interface (không expose ra internet), dùng TLS cho Redis 6+. - Availability: dùng Sentinel hoặc Cluster, đặt
replica-priorityhợp lý, test failover định kỳ. - Performance: đặt
maxmemoryphù hợp với RAM (để lại 20-30% cho OS và fragmentation), chọnmaxmemory-policyđúng, monitorslowlog(slowlog-log-slower-than 10000microseconds). - Tránh blocking operation: không dùng
KEYS *trên production (dùngSCAN), không dùngSMEMBERStrên Set lớn (dùngSSCAN), cẩn thận vớiSORT,LRANGEtrên List dài. - Connection management: dùng connection pool (
max pool size = 10-50), settcp-keepalive 300. - Backup: cấu hình RDB snapshot, monitor
rdb_last_save_timevàrdb_changes_since_last_save. - Upgrade: Redis minor version backward compatible, major version cần test kỹ; dùng Redis managed service (ElastiCache, Redis Cloud) để giảm operational burden.
Running Redis in production well requires correct security configuration (auth, TLS, ACL), appropriate persistence settings, avoiding blocking operations, and monitoring key metrics like evicted_keys and slowlog.
Most important best practices for Redis in production:
- Security: enable authentication (
requirepass), use ACL (ACL SETUSER ... nocommands +@write) instead ofrename-commandfor Redis 6.0+ to control permissions more granularly, bind Redis only to internal interfaces (never expose to the internet), and use TLS for Redis 6+. - Availability: use Sentinel or Cluster, set
replica-priorityappropriately, and test failover regularly. - Performance: set
maxmemoryto leave 20-30% of RAM free for the OS and fragmentation, choose the correctmaxmemory-policy, and monitor the slow log (slowlog-log-slower-than 10000microseconds). - Avoid blocking operations: never use
KEYS *in production (useSCAN), never useSMEMBERSon large Sets (useSSCAN), and be cautious withSORTandLRANGEon long Lists. - Connection management: use a connection pool (max pool size 10-50) and set
tcp-keepalive 300. - Backups: configure RDB snapshots and monitor
rdb_last_save_timeandrdb_changes_since_last_save. - Upgrades: Redis minor versions are backward compatible; major versions require thorough testing. Consider using a managed Redis service (ElastiCache, Redis Cloud) to reduce operational overhead.