- Redis (REmote DIctionary Server) is a popular in-memory data platform used as a cache that can be deployed on-premises, across clouds, and hybrid environments.
Use Case | Data Type | Remarks |
---|---|---|
Caching | All | One of the advantage of cache is to remember the result of an expensive operation, to speed up the reads. |
Session Storage | All | In order to address scalability and to provide a shared data storage for sessions that can be accessible from any individual web server, you can abstract the HTTP sessions from the web servers themselves. |
Distributed Locks | Strings | We can use a Redis string to acquire locks among distributed services. - Example - Twitter Hit Counter |
Rate Limiting | All | We can apply a rate limiter for certain user IPs. |
Rank/Leaderboard | SortedSet | We can use SortedSet to sort the articles. - Example - Best sellers, ranking etc. |
GeoSpatial | GeoSpatial Indexes | Redis geospatial indexes let you store coordinates and search for them. - This data structure is useful for finding nearby points within a given radius or bounding box. |
Message Broker - Queue with PubSub model | Lists | We can use List for a message queue (with PubSub modal). |
Counter | All | We can count how many likes or how many reads for articles. |
Calculate user retention | Bitmap | We can use Bitmap to represent the user login daily and calculate user retention. |
Shopping cart | Hash | We can use Redis Hash to represent key-value pairs in a shopping cart. |
Global ID generator | Int | We can use Redis Int for global ID. |
Supported features | Remarks |
---|---|
Speed - 100K queries per second | Since Redis uses in-memory for storage, it is very fast. Generally, Redis is 5 times faster than database. - Mostly O(1) behavior, to get data from redis. |
Redis Value - Ideal Size | The ideal size of redis value is less than 100 KB. |
Transaction (atomicity) | Using Redis Transaction lock, we can achieve atomicity on the Redis operations (i.e. set/increase the key, add/remove elements from set, increase counter etc.). |
Multiple data types | Multiple data types are supported in Redis. |
Rich Client-Side library | Go, Java, PHP, C/C++ etc. |
Single-Leader Replication | This helps in both High-Availability and read-scalability of redis. |
Sharding (using Redis Cluster) | This helps in write scalability of redis. |
Data Persistence | It's generally suggested to turn off persistence on the master node, so that you get consistent low latency response time. - No forking to disk will be done. - No wasted I/O. |
LRU Eviction Policy | LRU is the default eviction policy in redis. |
Lua Scripting | Using Redis EVAL command, server-side Lua scripts can be invoked. |
Redis Pipelining | Redis pipelining is a technique for improving performance by issuing multiple commands at once without waiting for the response to each individual command. Read more |
Redis vs Memcache |
Data type | Remarks |
---|---|
⭐ Strings | Strings are Redis most basic data type. |
JSON - RedisStack | JSON can be stored as values in Redis. - Another option is to stringify JSON and then store it as string datatype in Redis. |
⭐ Sorted sets | Every member of a Sorted Set is associated with a score, that is used to keep the Sorted Set in order, from the smallest to the greatest score. - While members are unique, scores may be repeated. - Sample - Create a Redis Leaderboard with Golang |
Hashes | Redis Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (e.g. A User with a number of fields like name, surname, age, and so forth). |
Sets | Redis Sets are an unordered collection of Strings. - It is possible to add, remove, and test for existence of members in O(1) (constant time regardless of the number of elements contained inside the Set). |
Lists | Redis Lists are simply lists of strings, sorted by insertion order. - The main features of Redis Lists from the point of view of time complexity are the support for constant time (O(1)) insertion and deletion of elements near the head and tail, even with many millions of inserted items. - Accessing elements is very fast near the extremes of the list but is slow if you try accessing the middle of a very big list, as it is an O(N) operation. |
GeoSpatial Indexes | Redis geospatial indexes let you store coordinates and search for them. This data structure is useful for finding nearby points within a given radius or bounding box. |
BitMaps | Redis bitfields let you set, increment, and get integer values of arbitrary bit length. |
Streams | Redis stream is a data structure that acts like an append-only log but also implements several operations to overcome some of the limits of a typical append-only log. |
TimeSeries - RedisStack | Redis TimeSeries ingest and query time series data with Redis (with start time and end-time). |
- Redis - Glossary Terms
- Introduction To Redis
- Redis Interview Questions & Answers
- Top Redis Use Cases by Core Data Structure Types
- What are Redis master-slave and Redis clusters and the difference between them?
- Why Migrate a Dynomite Database to a Redis Enterprise Active-Active Database?
- AWS ElastiCache vs RDS ReadReplica
- Design Cache
- ByteByteGo - System Design: Why is single-threaded Redis so fast?
- Achieve over 500 million requests per second per cluster with Amazon ElastiCache for Redis 7.1