When talking about caching and shared caching most of us would think about memcached. Memcached is lightweight, easy to configure and has a very good performance. But what happens when the cached data is a bit more important? Let’s have a look at Redis!
Loosing cached sessions, because of an server-outage is not so bad, but could cause some annoyed customers when the need to re-login. When data becomes more and more important, most people think of persistence, backup, restore, high availability. This is the point where Redis jumps in and offers a lot of features to use both: a fast key-value in-memory DB on the one hand and data persistence, high availability, replication on the other hand.
In this article we will introduce to the world of Redis describing the key features. More detailed articles with code snippets will follow.
- Supported data types and abstractions
- LRU cache
- Master slave replication
- High availability (Sentinel)
- Even more …
Data structures supported by Redis
Redis itself is not a plain key-value store. It even handles and supports data structures with different kinds of values. Here is a short overview of the supported types and structures:
- Binary-safe strings
- Lists: collection of strings, sorted by time of insertion
- Sets: collection of unique, unsorted strings
- Sorted sets: strings are sorted via a floating number called score (it is possible to get a range out of the set: Give me the Top 10 or bottom 10)
- Hashes: maps composed of fields with values (Similar to python and ruby hashes)
- Bit arrays (bitmaps): Handle string values like arrays. Set and clear bits individual.
- HyperLogLogs: estimate the cardinality of a set
Visit the official Redis website for more information and examples.
When using Redis as a caching system, it can handle the dataset and purge old data automatically.
In general you can set the “maxmemory” directive to limit the amount of memory for the dataset.
You can configure different behaviors at Redis, when reaching the memory limit, this is called policy.
Master slave replication
The replication setup between a master and slave server is very simple to configure. The “slaveof” option defines a node as a slave.
Base facts about the replication are:
- asynchrounous replication is used
- master can have multiple slaves, slaves can have slaves itself
- replication is non-blocking at the master
- replication is non-blocking at the slave, the slave can handle queries from the old version of the dataset during initial sync
- can be used to scale for read-only queries or data redundancy
- best practice is to enable disk-persistence at the master
To get a bit security you can add a basic authentication via password, so that only allowed slaves can replicate.
There are different persistence options that save a dataset to your disk.
- logs every write operation
- minimal data loss in case Redis stops working
- log is human readable and commands can be removed (e.g. flushall)
- corrupted file is easy to rebuild
- point in time snapshot, perfect for backups
- very compact
- easy to transfer and restore
- Redis will fork a child process. It benefits from copy-on-write.
- only caching in memory/ram
AOF + RDB
- it is possible to enable both at the same time
- when both an RDB and AOF file exist, AOF will be used at startup
When your dataset is getting more and more important you probably think about high availability without any human interaction, should Redis fail. Redis Sentinel provides just that: monitoring, notifications and automatic failover. Plus it acts as a configuration provider for clients.
The big picture
- Monitoring: Sentinel checks if master and slaves work as expected
- Notification: inform via API when something is wrong with the Redis instances
- Automatic failover: if a master does not work as expected, Sentinel can start a failover process. A slave is promoted to master, other slaves are reconfigured automatically. Applications are informed about the new address to use.
- Configuration provider: Clients can use Sentinel for service discovery in order to get the current Redis master server.
Even more …
Redis supports a lot more features, here are some of them:
- Lua Scripting
- Keys with Limited TTL
- Redis Cluster
- Most programming languages can be used with Redis
This was just a short overview about the Redis universe. Stay tuned for more detailed blog articles in the coming weeks.
Looking for a job where you can work with cutting edge technology on a daily basis? We’re currently hiring Linux Systems Engineers in Karlsruhe, Pforzheim, Munich, Cologne and Hamburg!