Deep Dive - Redis Flashcards

1
Q

What is Redis?

A

A scalable key-value store.
Often used for these purposes:
1. Cache - check Redis before hitting the datastore.
2. Distributed Lock - atomic increment
3. Leaderboards
4. Rate Limiting
5. Proximity Search

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you implement a Rate Limiter in Redis

A

Implementation of this in Redis is simple. When a request comes in, we increment (INCR) the key for our rate limiter and check the response. If the response is greater than N, we wait. If it’s less than N, we can proceed. We call EXPIRE on our key so that after time period W, the value is reset.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you use Redis for Proximity Search?

A

Redis natively supports geospatial indexes with commands like GEOADD and GEORADIUS. The basic commands are simple:

GEOADD key longitude latitude member # Adds “member” to the index at key “key”
GEORADIUS key longitude latitude radius # Searches the index at key “key” at specified position and radius
The search command, in this instance, runs in O(N+log(M)) time where N is the number of elements in the radius and M is the number of members in our index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can we deal with “Hot Keys”, where one key is accessed significantly more than other keys? This might happen for a popular shopping item on an e-commerce website

A

Three possible solutions with different tradeoffs

1) We can add an in-memory cache in our clients so they aren’t making so many requests to Redis for the same data.
2) We can store the same data in multiple keys and randomize the requests so they are spread across the cluster.
3) We can add read replica instances and dynamically scale these with load.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some basic Redis commands (command line):

A

SET foo 1
GET foo # Returns 1
INCR foo # Returns 2

XADD mystream * name Sara surname OConnor # Adds an item to a stream

ZADD tiger_tweets 500 “SomeId1” # Add the Tiger woods tweet

ZADD tiger_tweets 1 “SomeId2”
# Add some tweet about zoo tigers

ZREMRANGEBYRANK tiger_tweets 0 -5
# Remove all but the top 5 tweets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a sorted set in Redis

A

A Sorted Set is like a Heap
It can be queried in Log(N) time, if there are N items

This could be useful if we want to find the top tweets (Posts) for a given keyword like “tiger”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly