What is Redis?
The name Redis is an acronym for REmote DIctionary Server.
Redis is a fast, open source, in-memory key-value data structure store. Redis comes with a set of versatile in-memory data structures which enable users to easily create a variety of custom applications. Leading use-cases for Redis include caching, session management, pub/sub, and leaderboards.
Due to its speed and ease-of-use, Redis is a popular choice for Web, Mobile, Gaming, Ad-Tech, and IoT applications that require best-in-class performance.
Redis is usually provided as support through a fully managed and optimized database services or the users are allowed to run self-managed Redis server.
Blazing Fast Performance
All Redis data resides in its server’s main memory, in contrast to most database management systems that store data on disks (either HDD or SSD).
By eliminating the need to access disks, in-memory databases such as Redis avoid seek time delays and can access data with simpler algorithms that use fewer CPU instructions. Typical operations require less than a millisecond to execute.
In-memory Data Structures
Redis allows users to store keys that map to various data types - String, Lists, Sets, Hashes, Sorted Sets and Bitmaps and HyperLogLogs.
Nearly any type of data can be stored in memory using Redis.
Versatility and Ease-of-use
Redis comes with a number of tools that make development and operations faster and easier, including:
- Pub/Sub - to publish messages to channels, which are delivered to subscribers – great for chat and messaging systems;
- TTL keys can have a determined Time To Live, after which they delete themselves – useful to avoid filling the database with unneeded data;
- Atomic counters the ensure that race conditions do not create inconsistent results;
- Lua - a powerful but lightweight scripting language.
Replication and Persistence
Redis employs a master-slave architecture and supports asynchronous replication where data can be replicated to multiple slave servers. This can provide both improved read performance (as requests can be split among the servers) and recovery when the primary server experiences an outage.
To provide durability, Redis supports both point-in-time Snapshots (copying the Redis data set to disk) and creating an Append Only File (AOF) to store each data change to disk as it is written. Either method allows rapid restoration of Redis data in the event of an outage.
Support for Development Language
Redis Use Cases
Redis placed in “front” of another database creates a highly performant in-memory cache to decrease access latency, increase throughput, and ease the load off a relational or NoSQL database.
Redis is highly suited for session management tasks. Simply use Redis as a fast key-value store with appropriate TTL on session keys to manage your session information. Session management is commonly required for online applications, including games, e-commerce websites and social media platforms.
Using the Redis Sorted Set data structure, elements are kept in a list, sorted by their scores. This makes it easy to create dynamic leaderboards to show who is winning a game, or posting the most-liked messages, or anything else where you want to show who’s in the lead.
Redis can measure and, when needed, throttle the rate of events. By using a Redis counter associated with a client's API key, you can count the number of access requests within a certain time period and take action if a limit is exceeded. Rate limiters are commonly used for limiting the number of posts on a forum, limiting resource utilization and containing the impact of spammers.
The Redis List data structure makes it easy to implement a lightweight, persistent queue. Lists offer atomic operations as well as blocking capabilities, making them suitable for a variety of applications that require a reliable message broker or a circular list.
Chat and Messaging
Redis supports the PUB/SUB standard with pattern matching. This allows Redis to support high performance chat rooms, real-time comment streams, and server intercommunication. You can also use PUB/SUB to trigger actions based on published events.
Important Redis Metrics
Since Redis is an in-memory database, RAM is perhaps its most limited resource. Once Redis exhausts the RAM it is configured to use for storing data, it will employ its configured eviction policy (more on that below) resulting in either out-of-memory errors or data eviction.
Every operation is sent to Redis within the context of a connection from the client application. The maximum number of concurrent connections to your Redis server is always limited—whether by your operating system, Redis’ configuration, or your service provider’s plan—and you should always ensure you have enough free resources to allow the connection of new application clients or an administrative session.
Cache Hit Ratio
Redis can be used as an intelligent cache by configuring it with the proper eviction policy. When employed that way, a cache is most effective when the application accesses contents that are actually stored in it. By tracking the Cache Hit Ratio, the percentage of successful reads out of all read operations, you can monitor its effectiveness.
If your cache’s hit ratio drops too low, that may mean that eviction has been triggered causing the removal of data from Redis and resulting in cache misses. To verify that, check your memory usage readings vis–à–vis evictions.
Since eviction is triggered when Redis’ memory is running low, perhaps you’re not expiring cache objects. While Redis supports expiry, it is up to the application to set each object’s time-to-live and this chore is too often neglected.
By going over the expired object’s readings, you can find whether expiry occurs or if data keeps piling up in your cache.
Many Data Types
Redis is a data structure server that can serve up many different data types. It also provides the commands you need to make the most of those data types.
Simple text or binary values that can be up to 512MB in size. Redis gives you more tools for leveraging this datatype by offering commands for bitwise operations, bit-level manipulation, floating point increment/decrement support, range queries, and multi-key operations.
Hashes are sort of like a key value store within a key value store. They map between string fields and string values. Field->value maps using a hash are slightly more space efficient than key->value maps using regular strings.
Hashes are useful as a namespace, or when you want to logically group many keys. With a hash you can grab all the members efficiently, expire all the members together, delete all the members together, etc. Great for any use case where you have several key/value pairs that need to group.
Redis lists are ordered collections of strings. They are optimized for inserting, reading, or removing values from the top or bottom (aka: left or right) of the list. Redis provides many commands for leveraging lists, including commands to push/pop items, push/pop between lists, truncate lists, perform range queries, etc. Lists make great durable, atomic, queues. These work great for job queues, logs, buffers, and many other use cases.
Sets are unordered collections of unique values. They are optimized to let you quickly check if a value is in the set, quickly add/remove values, and to measure overlap with other sets.
These are great for things like access control lists, unique visitor trackers, and many other things. Most programming languages have something similar (usually called a Set). This is like that, only distributed.
Sorted Sets are also collections of unique values. These ones, as the name implies, are ordered. They are ordered by a score, then lexicographically (think alphabetically). This can be useful for things like auto-complete features. This data type is optimized for quick lookups by score. Getting the highest, lowest, or any range of values in between is extremely fast.
Redis has several commands for storing, retrieving, and measuring geographic data. This includes radius queries and measuring distances between points. Technically geographic data in redis is stored within sorted sets, so this isn't a truly separate data type. It is more of an extension on top of sorted sets.
Bitmap and HyperLogLog
Like geo, these aren't completely separate data types. These are commands that allow you to treat string data as if it's either a bitmap or a hyperloglog.
Bitmaps are bit-level operators used for managing Strings data type.
HyperLogLog allows you to use a constant extremely small amount of space to count almost unlimited unique values with shocking accuracy. Using only ~16KB you could efficiently count the number of unique visitors to your site, even if that number is in the millions.
Redis is more than a cache. It is an in-memory data structure server. Below you will find a quick overview of things Redis can do beyond being a simple key/value cache like memcached.
Redis is better documented than memcached. While this can be subjective, it is proving to be true more and more over time.
redis.io is a fantastic easily navigated resource. It lets you try redis in the browser and even gives you live interactive examples with each command in the docs.
By default redis persists your data to disk using a mechanism called snapshotting. If you have enough RAM available it's able to write all of your data to disk with almost no performance degradation.
In snapshot mode there is a chance that a sudden crash could result in a small amount of lost data. If you absolutely need to make sure no data is ever lost, don't worry, redis has solution providing AOF (Append Only File) mode. In this persistence mode data can be synced to disk as it is written. This can reduce maximum write throughput to however fast your disk can write, but should still be quite fast.
There are many configuration options to fine tune persistence if you need, but the defaults are very sensible. These options make it easy to setup redis as a safe, redundant place to store data. It is a real database.
Transactions and Atomicity
Commands in redis are atomic, meaning you can be sure that as soon as you write a value to redis that value is visible to all clients connected to redis. There is no wait for that value to propagate.
While not quite the same as transactions in relational databases, redis also has transactions that use "optimistic locking" (WATCH/MULTI/EXEC).
Redis provides a feature called 'pipelining'. If you have many redis commands you want to execute you can use pipelining to send them to redis all-at-once instead of one-at-a-time. With pipelining, redis can buffer several commands and execute them all at once, responding to all commands in a single reply.
Redis has commands dedicated to pub/sub functionality, allowing redis to act as a high speed message broadcaster. This allows a single client to publish messages to many other clients connected to a channel.
Redis scripting is used to evaluate scripts using the Lua interpreter. It is built into Redis. The command used for scripting is EVAL command.
As mentioned above, redis includes built in support for clustering and is bundled with its own high availability tool called redis-sentinel.
A NoSQL (originally referring to "non SQL", "non-relational" or "not only SQL") database provides a mechanism for storage and retrieval of data which is modeled in means other than the tabular relations used in relational databases. NoSQL databases are increasingly used in big data and real-time web applications.
NoSQL systems are also sometimes called "Not only SQL" to emphasize that they may support SQL-like query languages.
Based on popularity rankings, Redis is one of the most popular NoSQL databases.
According to a recent survey, database performance is the No.1 challenge for developers.
Fortunately, making Redis a part of your stack can go a long way toward solving that problem. Still, ensuring that your Redis is top-performing is an ongoing task that requires methodical monitoring and using the proper tools to resolve alerts when they come.
By optimizing your own Redis server or using advanced hosted Redis services, you can continue delivering the best experience to your users.
Carl Zulauf - stackoverflow.com
Amazon ElastiCache - amazon.com
Itamar Haber - blog.newrelic.com
#server #cache #overview