Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Cơ Sở Dữ Liệu

10 Quick Tips sử dụng Redis Effectively

Redis hot in tech community right now. Come a long way từ small personal project từ Antirez, đến industry standard cho in-memory data storage. Với đó comes set of best practices most people agree upon for using Redis properly. Dưới đây explore 10 quick tips using Redis correctly.

1. STOP USING KEYS *

Okay, shouting at you maybe not great way start article. But quite possibly most important point. Too often look at redis instance, pull up quick commandstats, see glaring KEYS staring back. In all fairness, coming from programmatical standpoint would make sense have pseudocode does something like:

for key in 'keys *': doAllTheThings()

But khi bạn có, say, 13 million keys, things slowdown. Vì KEYS là O(n) where n is number of keys returned, complexity bound by dbsize. Also, during whole operation, nothing else can be run against your instance.

As substitute, check out SCAN cho phép scan through database in increments instead. Operates on iterator so you can stop and go as you see fit.

2. Find Out What's Slowing Down Redis

Vì Redis không have most verbose logs, often hard trackdown what exactly going on inside instance. Luckily Redis provides commandstat utility show you this:

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

Gives breakdown all commands, how many times run, microseconds execute (total và avg per call).

Reset this: run CONFIG RESETSTAT, brand new slate.

3. Use Redis-Benchmark as Baseline, Not Gospel Truth

Salvatore, creator of Redis put it geniously: "To test Redis doing GET/SET is like testing a Ferrari checking how good it is at cleaning the mirror when it rains." Lot of times people come to me wondering why Redis-Benchmark results less than optimal. But take into account many factors, như:

  • What client-side limitations could we have run into?
  • Was there a difference in versioning?
  • Are tests being performed relevant to what application will be performing?

Redis-Benchmark provides awesome baseline make sure redis-server isn't behaving abnormally, but should never be taken as true "load test". Load tests need reflective of how your application behaves, và from environment as close to production as possible.

4. Hashes Are Your Best Friend

Invite hashes over for dinner. Wine and dine hashes. You'll be amazed at what happiness they can bring if you just give them the chance. I've seen one too many key structures like this before:

foo:first_name foo:last_name foo:address

Example: foo maybe username for user, each one separate key. Adds room for errors, và adds unnecessary keys to fold. Instead, consider a hash. Suddenly you've only got one 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, take advantage of expiring keys. Perfect example: storing temporary authentication keys. Khi retrieve auth key - dùng OAUTH as example - often given expiration time. Khi set key, set it with same expiration, và Redis clean up for you! No more need KEYS * iterate through all those keys, eh?

6. Choosing Proper Eviction Policy

While on topic cleaning up keys, touch on eviction. Khi Redis instance fills up, Redis attempt evict keys. Depending on use case, highly recommend volatile-lru - assuming you have expiring keys. Nếu running something like cache và don't have expiry set, could consider allkeys-lru. Recommend checking available options here.

7. If Your Data is Important, Try/Except

Nếu absolutely critical data make it to Redis instance, heavily recommend putting in try/except. Since almost all Redis clients configured "fire-and-forget", should always consider when key doesn't actually make it to database. Complexity added to redis call next to nothing in this case, và you can ensure important data makes it where it should be.

8. Don't Flood One Instance

Whenever possible, split up workload amongst multiple Redis instances. As of version 3.0.0, Redis Cluster now available. Redis Cluster allows break apart keys amongst sets of given masters/slaves based on key ranges. Full breakdown magic behind Cluster here. Looking for tutorial, look no further. Nếu clustering not option, consider namespacing và distributing keys among multiple instances. Amazing write-up on partitioning data on redis.io website here.

9. More Cores = More Better, Right?!

Wrong. Redis là single threaded process và will, at most, consume two cores nếu persistence enabled. Unless plan running multiple instances on same host - hopefully only for dev testing case! - shouldn't need more than two cores for a Redis instance.

10. HA All the Things!

Redis Sentinel now very well tested, many users have it running in production (ObjectRocket included!). Nếu relying heavily on Redis for application, then need consider HA (high availability) solution keep you online. Of course, nếu don't want manage all that yourself, ObjectRocket offers HA platform with 24x7 support for your consumption, give it a shot.