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
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.