Redis in-memory storage
Recollection
Include binary data and image files in your IoT projects with Redis, an open source, in-memory data structure store.
For many Internet of Things (IoT) projects, a message-queuing system like MQTT (message-queuing telemetry transport) is all you need to connect sensors, devices, and graphic interfaces. However, if you have requirements for high throughput or you are storing special data types (e.g., binary data or image files), you should take a look at Redis.
Redis (Remote directory server) [1] is an open source, in-memory data structure store that can be used as a database, cache, and message broker. It supports a wide range of data structures, such as strings, hashes, lists, sets, bitmaps, HyperLogLogs, and geospatial indexes. Redis servers can be loaded locally, or they are available as web-hosted solutions. Redis libraries are available for a wide variety of programming languages.
In this article, I show you how to set up a Redis system with two examples. The first is a flame scanner that connects an Arduino module to a Node-RED web dashboard. The second example is a Raspberry Pi weather station, in which a webcam image is stored in a Redis server and a PHP web page shows the data.
Getting Started Locally
To install Redis on Ubuntu systems, go to the terminal and enter:
$sudo apt-get update $sudo apt-get install redis-server
Once Redis is installed, it can be started by:
$redis-server
The redis-cli
command-line tool can be used for monitoring and testing; for example, to assign and read a value:
$redis-cli 127.0.0.1:6379> set mytag1 12.3 OK 127.0.0.1:6379> get mytag1 "12.3"
The default Redis security is set to local access only. To open the server to remote connections, edit the /etc/redis/redis.conf
file and either comment out all the bind
statements (e.g., #bind 127.0.0.1
) or add bind
statements for all your valid IP addresses. After editing the config file, you need to restart the Redis server by entering the
sudo service redis-server restart
command.
Redis Performance
The Redis in-memory data store boasts some incredible throughput numbers. I wanted to test this out, so I loaded Redis on an old Acer Aspire One laptop with a 1.6GHz N270 Intel Atom processor and 4GB of RAM. A Redis tool called redis-benchmark
allows me to do some upfront performance analysis configured for different messages, client counts, and test durations.
For my IoT project, I wanted to check whether my low-end Ubuntu laptop could handle 10 clients. My maximum throughput for GET and SET messages can be estimated by the redis-benchmark
command shown in Listing 1.
Listing 1
redis-benchmark
Although I have many performance factors to consider, for my IoT system with 10 Arduino/Pi modules, I probably won't generate more than 50 SETs/sec. According to the test, my old Ubuntu laptop appears to be able to do 5,000+ SETs/sec, so I shouldn't have any problems using it for my IoT system.
Flame Scanner Example
My goal for the flame scanner example is to monitor my fireplace remotely with an ESP8266-compatible Arduino module and a Redis server to store the data (Figure 1). A lot of IoT dashboard options are available, but I like to use Node-RED because it is 100% standalone.
For my hardware setup, I mounted a low-cost infrared (IR) flame sensor [2] ($3) on some Meccano pieces, and I aimed it at the center of the fireplace. A DHT11 temperature/humidity sensor [3] ($4) was also connected to the Arduino module to record the room temperature (Figure 2). I did my testing with a Wemos D1 module and a proto shield, but any of the ESP8266-based Arduino modules could be used.
Redis supports a number of storage methods, with the most common methods being SET/GET and PUBLISH/SUBSCRIBE. For this example, I used the PUBLISH/SUBSCRIBE method to make it similar to a messaging system like MQTT.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.