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
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.