Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> ฐานข้อมูล

10 Quick Tips ใช้ Redis อย่างมีประสิทธิภาพ

Redis Hot ใน Tech Community มาจาก Small Personal Project Antirez -> Industry Standard In-memory Data Storage Best Practices ที่ส่วนใหญ่ Agree สำหรับ Using Redis Properly 10 Quick Tips ด้านล่าง

1. หยุดใช้ KEYS *

อาจดูรุนแรงแต่ Most Important Point บ่อยครั้ง Look Redis Instance Commandstats -> KEYS Glaring Back Fairness: Programmatically Pseudocode:

for key in 'keys *': doAllTheThings()

แต 13 Million Keys -> Slowdown KEYS = O(n) n = Keys Returned Complexity Bound By Dbsize ระหว่าง Operation Nothing Else Can Run Against Instance

Substitute: SCAN Scan Database In Increments Iterator Stop/Go As Fit

2. หาว่าอะไรทำให้ Redis ช้า

Redis Logs Not Verbose Hard Track Down Inside Instance Luckily Redis Provides Commandstat Utility:

127.0.0.1:6379> INFO commandstats # Commandstats cmdstat_get:calls=78,usec=608,usec_per_call=7.79 cmdstat_setex:calls=5,usec=71,usec_per_call=14.20 cmdstat_keys:calls=2,usec=42,usec_per_call=21.00 cmdstat_info:calls=10,usec=1931,usec_per_call=193.10

Breakdown Commands, Calls, Microseconds Total/Avg Per Call Reset: CONFIG RESETSTAT Fresh Slate

3. Redis-Benchmark As Baseline, Not Gospel Truth

Salvatore (Creator) Genius: "Test Redis GET/SET Like Test Ferrari How Good At Cleaning Mirror When Rains" People Wonder Redis-Benchmark Results Less Optimal Factors:

  • Client-side Limitations?
  • Versioning Differences?
  • Tests Relevant To App Behavior?

Redis-Benchmark Awesome Baseline Ensure Redis-server Not Behaving Abnormally But Never True "Load Test" Load Tests Reflect App Behavior Environment Close To Production

4. Hashes Are Your Best Friend

Invite Hashes Dinner Wine And Dine Amazed Happiness Bring If Give Chance Seen Too Many Key Structures Like:

foo:first_name foo:last_name foo:address

foo = Username Each Separate Key Adds Errors Unnecessary Keys Instead Hash -> Single Key:

127.0.0.1:6379> HSET foo first_name "Joe" (integer) 1 127.0.0.1:6379> HSET foo last_name "Engel" (integer) 1 127.0.0.1:6379> HSET foo address "1 Fanatical Pl" (integer) 1 127.0.0.1:6379> HGETALL foo 1) "first_name" 2) "Joe" 3) "last_name" 4) "Engel" 5) "address" 6) "1 Fanatical Pl" 127.0.0.1:6379> HGET foo first_name "Joe"

5. Set That TTL!

Whenever Possible Expire Keys Perfect Example: Temporary Auth Keys Retrieve Auth Key (OAUTH Example) Given Expiration Time Set Key Same Expiration Redis Cleans Up For You! No More KEYS * Iterate Keys

6. Choosing Proper Eviction Policy

Cleaning Up Keys Topic Eviction Redis Instance Fills Up -> Attempt Evict Keys Use Case: Recommend Volatile-lru (Assuming Expiring Keys) Cache No Expiry -> Allkeys-lru Check Options Here

7. If Data Important, Try/Except

Absolutely Critical Data Make It To Redis -> Heavy Recommend Try/Except Redis Clients Fire-and-forget Consider Key Doesn't Make It To DB Complexity Next To Nothing Ensure Important Data Makes It

8. Don't Flood One Instance

Split Workload Multiple Redis Instances Version 3.0.0 Redis Cluster Available Break Apart Keys Among Masters/Slaves Based On Key Ranges Cluster Breakdown Here Tutorial Here If Clustering Not Option -> Namespacing Distribute Keys Multiple Instances Amazing Write-up Partitioning Data redis.io

9. More Cores = More Better, Right?! Wrong.

Redis Single-threaded Process Consumes At Most 2 Cores (Persistence Enabled) Unless Multiple Instances Same Host (Dev Testing Only!) Shouldn't Need >2 Cores Redis Instance

10. HA All The Things!

Redis Sentinel Well-tested Many Users Production (ObjectRocket Included!) Rely Heavily Redis -> Need HA Solution Stay Online Don't Want Manage Yourself -> ObjectRocket HA Platform 24x7 Support Give It A Shot