Have you ever heard about an in-memory data structure store that can be used as a database, cache and message broker? Yes, you’ve figure it out, we’re talking about Redis.
Most of today’s systems are a lot of code capable of storage and information processing, delivering information in a useful format for its users. Almost all of this information travels from computer to computer, generating high data traffic and requiring guaranteed delivery and storage.
That’s why several technologies are being created to increase processing capacity and data storage. One of those technologies is Redis, which has been used to avoid great data congestion and to store in-memory data.
Let’s check how to configure a Redis database executing basic commands in a docker container, how to insert and recover values using a c# StackExchange.Redis library and how to use a shared volume between our computer and the container to verify the files created by Redis.
According to DB-Engines, Redis is the most used key-value database nowadays. This open source tool calls attention not just for enabling in-memory data storage, but also because it allows two different types of data persistence: RDF and AOF.
While RDF supports point-in-time snapshot, guarantees high performance, and easies backup and disaster recovery, AOF avoids data loss and organizes the logs in an easy to understand manner.
Both options can be activated simultaneously, and they can also be deactivated if you just want to in-memory data storage. According to the team that maintains the tool, the joint use of both technologies is highly recommended, as well as backing up daily for a different server. A balance is needed between performance, disk I/O and data durability.
Learn more about tech trends and Blockchain
When using Redis with Windows, we can use a Linux based container. After installing the docker on the PC, we just have to change it to Linux containers. We also need to run the following commands to download the Redis image and create a new instance on port 6379.
docker pull redis docker run -d -p 6379:6379 --name redis-container -v c:/data:/data redis --appendonly yes
Switch to Linux containers
To guarantee that the container was created and is running, we need to execute the command docker ps and the answer we get should look like the following:
Notice that by default, the RDF mode is active and the AOF isn’t. To activate the AOF, upon starting the container, the command –appendonly yes should be run.
To ease the visualization of the files created by the docker, the container’s folder “data” must reference to the computer’s folder “C:\data”, through the command -v c:/data:/data.
When activating the AOF mode, a .aof file will be created with the container creation. When the container is rebooted, the data from the file AOF is written to the memory again.
If the file is too big it is possible that Redis takes more time than expected to restart.
Having created the Redis container and having run it, it is time to execute simple commands to ensure the tool is working. First, we have to change the bash context to the container:
docker exec -it redis-container bash
Then we have to run the command redis-cli to connect to Redis. By default, redis-cli connects to the server at 127.0.0.1 port 6379.
The next commands are run to create a new value and then recover it:
set mykey 123 get mykey
Check the complete redis-cli list of commands
Now, to make sure data is being persisted we can go to the folder D:\data and open the *.aof file. There, it is possible to check all the inserted keys up until this point.
You can find and download clients in different languages. For each language Redis recommends one or more specific clients.
Here you will find how to insert a new base value and then recover it, using StackExchange.Redis:
class Program { static void Main(string[] args) { var redis = ConnectionMultiplexer.Connect("localhost:6379"); var db = redis.GetDatabase(); db.StringSet("anotherkey", "running docker into a container!"); Console.WriteLine(db.StringGet("anotherkey")); Console.ReadLine(); } }
The following piece of code creates a channel called messages, where every 3 seconds a thousand messages are sent:
private static ISubscriber sub; static void Main(string[] args) { var redis = ConnectionMultiplexer.Connect("localhost:6379"); sub = redis.GetSubscriber(); var timer1 = new Timer(3000); timer1.Elapsed += (sender, e) => OnTimedEvent(sender, e, "sender 1"); timer1.Enabled = true; Console.ReadLine(); } private static void OnTimedEvent(object source, ElapsedEventArgs e, string message) { Task.Run(() => { for(var i = 0; i <= 999; i++) sub.Publish("messages", message); }); }
Running in a different console application, the following code shows a message from the row when it is received.
static void Main(string[] args) { var redis = ConnectionMultiplexer.Connect("localhost:6379"); var sub = redis.GetSubscriber(); var i = 0; sub.Subscribe("messages", (channel, message) => { Console.WriteLine($"{message} + {i++}"); }); Console.WriteLine("Application running!"); Console.ReadLine(); }
Finally, to follow all Redis executions in real time, you just have to open again the console into the container. Then execute this command:
monitor
Eduardo is a Software Engineer at PoaTek. He holds a BSc in Information Systems. Most of the time he works with Microsoft Technologies and web applications. He believes that there are no bad technologies, only bad developers.