In the last blog I introduced Redis. In this blog I will cover some of the basics.
Command Line Interface
You can interact with the Redis server using the redis-cli command line interface. Here I am checking my Redis server is up and running. Note: I am using an installation by Jetware running on Ubuntu which is why my path is /jet. Yours maybe different.
All the server configuration settings are stored in /jet/etc/redis/redis.conf. We can get and set values using an editor or from the CLI using:
- config get
- config set
The configuration file has the following sections:
- Memory Management
- Lazy Freeing
- Append Only Mode
- LUA Scripting
- Redis Cluster
- Cluster Docker/NAT Support
- Slow Log
- Latency Monitor
- Event Notification
- Advanced Config
- Active Defragmentation
The server log file can be found using:
In certain situations you want to run Redis with some level of data persistence. This means if the server is shutdown or crashes, the data is stored on disk and is available the next time the server is started. The only time the data is generally not required is when Redis is being used as a cache. See the documentation here.
Redis has two modes of persistence and they can both be enabled simultaneously to get the best of both options.
RDB Snapshots (Default configuration for Redis)
- Point in time backup of the complete data in memory.
- Can be configured to take a backup when a certain number of keys are changed over time.
- Fast because they are run with a different thread and do not occur for every change of data.
- Cannot guarantee they have every data change at a specific instance of time.
- Configured in the Snapshotting section of the config file.
- By default the output file in dump.rdb.
- Disable RDB Snapshots by commenting out all SAVE lines in the config.
Append Only Mode
- Logs all command executed on the Redis server and played back at server start-up.
- Commits are written to the log and at most you may lose one transaction.
- There is a performance penalty for enabling this mode.
- Configured in the Append Only Mode section of the config file.
- By default the output file in appendonly.aof.
- Disable by setting appendonly no in the config.
From the cli we can get the folder location of the dumps using config get dir.
The Redis database is a memory resident database that can be persisted to disk as mentioned above. I find the Redis term “database” to be rather misleading. For example; we can display the maximum number of databases as follows:
Databases are named db0, db1…db15. The default is db0. We can see what databases are created as follows:
Notice we used the term keyspace. In this example we have one keyspace called db0 which has 5 keys defined. Keys need to be unique per database. If we create db1, both could have a key called “myKey”. We can switch to a different database by using select n, where n would be 0 to 15.
So why do I find it misleading? Backups and restores are typically done at the database level however; in Redis, all databases exist in the single file and so in affect, they are all saved and restored at the same time. I do come from a relational database background and my brain kind of sees it as a Schema where multiple schemas exist in a single database with isolation from each other. Anyway, that’s just me and I love all this NoSQL stuff and for me it’s about understanding the differences.
In the next blog I will discuss the different data types Redis uses for storing values.