Last week while migrating neetoGit's production deployment to neetoDeploy, we faced a challenge. Redis 7 addon only had TLS url in Heroku. We were not able to get the dump in a straightforward manner using redis-cli, since we didn't have access to the certificates for making a TLS connection. We were able to connect to the addon, so we decided to write a script to manually copy all the keys and values over to the Redis addon in neetoDeploy.
While writing the program we realized that we'd need a switch-case that checked what data type each key was. We had to search for the get and set methods for each data type manually while writing the program. Later we used the redis-rb gem and was able to get the dump with OpenSSL::SSL::VERIFY_NONE, but we had still faced the issue of having to look up what the command was for different data types like zset and hash.
I thought this could be a good opportunity to write a blog post summarizing all the data types and the commands associated with them. The Redis documentation has a page about the different data types, but all the commands are not available at a single place.
Redis key-value database
When we say that Redis is a key-value database, there is more to it than what meets the eye. Redis keys can store values of several different data types. Redis has a set of commands for each of these different data types to do operations with them. In this post, we’ll go over the different data types, what they are and how we can work with them.
What are the different data types?
Redis has more than a couple of data types, which can be used to store different data based on your needs. These include the following:
- String - The basic data type we are all familiar with.
- List - An array of strings.
- Hash - A collection of key-value pairs, similar to a Ruby Hash.
- Set - A collection of unique strings.
- Sorted Set - A collection of unique strings maintaining by each string’s score.
- HyperLogLog - Probabilistic estimates of cardinality of large sets.
- Stream - An append only log.
- Geospatial Index - Data structure for storing geographic coordinates.
Checking data type of your keys
You can use the TYPE command to check what data type your key is. Once you know what type your key is, you can use the commands associated with your key's data type to interact with it.
1127.0.0.1:6379> TYPE schedule 2zset
Cheatsheet for Redis commands based on data type
Each Redis data type has its own set of commands for doing operations with the key and its value. Here's a quick overview of the basic data types you would encounter and some of the basic commands to set, retrieve and delete data from them.
Read more about the different data types in Redis and all the different commands that are available in their official documentation.