Games, financial services, social media, IoT, and many other areas actively use Redis today due to its high performance and ease of use. Redis is often chosen for its ability to handle millions of requests per second with sub-millisecond latency. However, without a thorough understanding of its data structures, commands, and how it works, we may not be able to fully leverage its performance capabilities.
We face these challenges all the time:When using Redis, most applications involve more complex logic than simply performing GET/SET operations. The most common challenges we face implementing the logic, involve supporting atomic operations and reducing network round trips.
Atomic operations:An atomic operation is one that is completed in a single, indivisible step. This is crucial when performing operations on multiple keys that must be executed atomically to ensure data consistency.
\ Let's imaging, we want to increment the total number of devices and save the device state only if the device is not already registered and the threshold of registered devices has not been reached.
PSEUDOCODE: “Check for IoT in the database” “Get the current number of registered IoT “ IF ( “IoT does not exist” AND “Current number of registered IoT” < 100) THEN “Save IoT state in a database” “INCR the number of registered IoT” RETURN 1 END RETURN 0\ Obviously, such logic requires locks to prevent race conditions, avoid idempotency issues and ensure data consistency.
Network Round Trips:Network round trips refer to the complete cycle of sending a request to a server and receiving a response. Fewer round trips result in less waiting time for responses, which leads to faster application performance.
\ Let’s imaging, we want to retrieve multiple values, make a computation, and then store the result as another value.
PSEUDOCODE: “Get user salary” “Get user bonus” “Get user discount” “Computation(User salary + User bonus – User discount)” “Save computation to DB” RETURN ComputationThis logic would normally require 4 network round trips, causing latency.
\ ==In both two cases Lua scripting can be a valuable tool.== Lua scripting in Redis is a powerful feature that should be used when we need to perform complex operations requiring atomicity, efficiency, and reduced latency.
Using Lua with Redis in Node.jsRedis supports Lua scripting via the EVAL and EVALSHA commands. The application should first load the script with SCRIPT LOAD command and then call EVALSHA once more to run the cached script by its SHA1 sum. Most of Redis' clients already provide utility APIs for doing that automatically. In this article I will use ioredis for Node.js as a Redis client.
\ Let's take the example described in the Atomic Operations sub-section above and solve it entirely using a LUA script.
\
\ \ \ \ \ \ \ \ \
\
\
Run node server.js to register a hardcoded IoT device (MAC: 8934WRTS4512)
Let’s check the result in redis-cli:
\ \ \ \ \ \ \ \
What we achieved:When using Redis, we face many challenges related to achieving optimized performance and solving problems such as idempotency or race conditions. But the most common ones are ensuring atomicity and reducing network roundtrips. This article provides an example of using LUA to solve these two problems.
All Rights Reserved. Copyright , Central Coast Communications, Inc.