Internet of Things equipment state updating method and system based on Redis cache
By using Redis caching and performing state difference determination in the IoT device status update system, the problems of real-time status reporting and database load optimization of IoT devices are solved, achieving efficient status updates and database load optimization.
Patent Information
- Application Number
- CN202511409798.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-29
- Publication Date
- 2026-01-13
AI Technical Summary
Existing technologies struggle to balance real-time performance with database load optimization when reporting the status of IoT devices, leading to frequent write operations and repetitive status reports, which in turn cause database load pressure and resource waste.
Redis is used to cache the storage device status, and a status difference determination mechanism is used to trigger database updates only when the status changes. Combined with exception handling and offline detection, the database load is optimized.
It significantly reduces the frequency of database write operations, ensures the real-time and reliable nature of state updates, reduces redundant writes, and optimizes database load.
Smart Images

Figure CN121334172A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of Internet of Things (IoT) technology, specifically to a method and system for updating the state of IoT devices based on Redis caching. Background Technology
[0002] With the popularization of IoT technology, a large number of IoT devices (such as smart charging piles, security cameras, and industrial sensors) have been widely used in production and daily life scenarios, and remote monitoring and management are achieved through IoT platforms. These devices typically need to report real-time status information (such as online status and fault codes) at fixed intervals (such as every second or every 10 seconds) so that the backend system can keep track of the device's operating status and trigger control logic.
[0003] In traditional solutions, device status information is often stored directly in relational databases (such as MySQL) to meet business needs such as status queries and statistical analysis. However, when tens of thousands of devices report their status frequently (e.g., once every 10 seconds, up to 86.4 million reports per day), the MySQL database faces significant challenges: on the one hand, frequent random write operations can cause disk I / O bottlenecks and lock contention, leading to increased write latency; on the other hand, a large number of repetitive statuses (such as continuous reports when no device malfunctions) will be repeatedly written to the database, resulting in wasted storage resources and performance degradation.
[0004] While existing technologies propose using caching (such as Redis) to temporarily store device status to reduce database writes, they often employ a "full caching + timed write" strategy (e.g., batch writing all device statuses from Redis to MySQL every 5 minutes), which cannot guarantee real-time status changes down to the second level. Alternatively, they may simply compare the timestamps reported by the devices without assessing the differences in the status content itself, potentially leading to redundant write operations due to devices periodically reporting invalid and repetitive statuses (such as unchanging "heartbeat packets"). Therefore, there is an urgent need for an IoT device status reporting method that balances real-time performance with database load optimization.
[0005] How to achieve IoT device status reporting while balancing real-time performance and database load optimization is a technical problem that needs to be solved. Summary of the Invention
[0006] The technical objective of this invention is to address the above shortcomings by providing a method and system for updating the status of IoT devices based on Redis caching, thereby solving the technical problem of how to achieve IoT device status reporting while balancing real-time performance and database load optimization.
[0007] In a first aspect, the present invention provides a method for updating the state of IoT devices based on Redis caching, applicable to a system including IoT devices, an IoT platform, a Redis caching cluster, and a MySQL database, comprising the following steps:
[0008] Device status reporting: IoT devices report status information to the IoT platform in real time according to a preset period. The status information includes device ID, status field set and reporting timestamp;
[0009] Redis cache read: After receiving the status information, the IoT platform reads the most recently stored valid status information of the corresponding IoT device from the Redis cache using the device ID as the key value, and performs a status difference judgment. If the status information of the corresponding IoT device does not exist in Redis, the database is updated and the cache is synchronized.
[0010] State difference determination: Compare the core field information in the currently reported state information with the core state field in the cached state information. If the comparison results are consistent, it is determined that the state of the IoT device has not changed, and the database update and cache synchronization are skipped. If the comparison results are inconsistent, it is determined that the state of the IoT device has changed, and the database update and cache synchronization are performed.
[0011] Database update and cache synchronization: Write the currently reported status information to the MySQL database and synchronously update the status information of IoT devices in the Redis cache;
[0012] Device offline detection: If the IoT platform does not receive status information reported by the IoT device within the scheduled time, the IoT device will be determined to be offline, and the status information of the IoT device in the Redis cache will be changed to offline.
[0013] Exception handling: When the Redis service is unavailable, it automatically switches to direct write MySQL mode and records exception logs. When the data writing to the Redis service is unavailable, the current state is temporarily stored in the local temporary cache and the write is retried through the exponential backoff mechanism.
[0014] Preferably, the core status field consists of key parameters that characterize the operating status of the equipment, including online status and fault codes.
[0015] As a preferred approach, when comparing the core field information in the currently reported status information with the core status fields in the cached status information, each field is checked and verified.
[0016] As a preferred option, when the device is being tested offline, the reporting cycle for the status information of the IoT device is adaptively adjusted according to the device type and application scenario.
[0017] Secondly, the present invention provides an IoT device status update system based on Redis caching, comprising an IoT device, an IoT platform, a Redis caching cluster, and a MySQL database;
[0018] IoT devices are used to report status information to the IoT platform in real time according to a preset period. The status information includes device ID, a set of status fields, and a reporting timestamp.
[0019] After receiving the status information, the IoT platform performs the following actions:
[0020] Redis cache read: Using the device ID as the key, read the most recently stored valid status information of the corresponding IoT device from the Redis cache as the cache status information, and perform status difference judgment. If the status information of the corresponding IoT device does not exist in Redis, perform database update and cache synchronization.
[0021] State difference determination: Compare the core field information in the currently reported state information with the core state field in the cached state information. If the comparison results are consistent, it is determined that the state of the IoT device has not changed, and the database update and cache synchronization are skipped. If the comparison results are inconsistent, it is determined that the state of the IoT device has changed, and the database update and cache synchronization are performed.
[0022] Database update and cache synchronization: Write the currently reported status information to the MySQL database and synchronously update the status information of IoT devices in the Redis cache;
[0023] Device offline detection: If no status information is reported by the IoT device within the scheduled time, the IoT device is determined to be offline, and the status information of the IoT device in the Redis cache is changed to offline;
[0024] Exception handling: When the Redis service is unavailable, it automatically switches to direct write MySQL mode and records exception logs. When the data writing to the Redis service is unavailable, the current state is temporarily stored in the local temporary cache and the write is retried through the exponential backoff mechanism.
[0025] Preferably, the core status field consists of key parameters that characterize the operating status of the equipment, including online status and fault codes.
[0026] As a preferred approach, when comparing the core field information in the currently reported status information with the core status fields in the cached status information, each field is checked and verified.
[0027] As a preferred option, when the device is being tested offline, the reporting cycle for the status information of the IoT device is adaptively adjusted according to the device type and application scenario.
[0028] The IoT device status update method and system based on Redis caching of the present invention has the following advantages: by comparing the current reported status of the device with the most recent valid status cached in Redis, the database update is triggered only when the status changes, thereby significantly reducing the frequency of MySQL write operations, alleviating the database load pressure, and ensuring the real-time performance of status updates. It utilizes the high-speed read and write characteristics of Redis to cache the most recent valid status of the device, filters invalid and duplicate reports by judging the differences in status content, and updates the database only when the status changes, realizing intelligent optimization of "update when it changes, save when it doesn't". Attached Figure Description
[0029] To more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0030] The invention will be further described below with reference to the accompanying drawings.
[0031] Figure 1 This is a flowchart of an IoT device status update method based on Redis caching, as shown in Example 1. Detailed Implementation
[0032] The present invention will be further described below with reference to the accompanying drawings and specific embodiments, so that those skilled in the art can better understand and implement the present invention. However, the embodiments are not intended to limit the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0033] This invention provides a method and system for updating the status of IoT devices based on Redis caching, which solves the technical problem of how to achieve IoT device status reporting while taking into account both real-time performance and database load optimization.
[0034] Example 1:
[0035] This invention discloses a method for updating the status of IoT devices based on Redis caching, applicable to a system including IoT devices, an IoT platform, a Redis cache cluster, and a MySQL database. The method includes six steps: device status reporting, Redis cache reading, status difference determination, database update and cache synchronization, device offline detection, and anomaly fault tolerance handling.
[0036] Step S100 Device Status Reporting: IoT devices report status information to the IoT platform in real time according to a preset period. The status information includes device ID, status field set, and reporting timestamp.
[0037] Step S200 Redis cache read: After receiving the status information, the IoT platform reads the most recently stored valid status information of the corresponding IoT device from the Redis cache using the device ID as the key value, and performs a status difference judgment. If the status information of the corresponding IoT device does not exist in Redis, the database is updated and the cache is synchronized.
[0038] Step S300: State difference determination: Compare the core field information in the currently reported state information with the core state field in the cached state information. If the comparison results are consistent, it is determined that the IoT device state has not changed, and the database update and cache synchronization are skipped. If the comparison results are inconsistent, it is determined that the IoT device state has changed, and the database update and cache synchronization are performed.
[0039] The core status fields are key parameters characterizing the device's operating status, including online status and fault codes. When comparing the core status field information in the currently reported status information with the core status fields in the cached status information, each field is checked and verified.
[0040] Step S400 Database Update and Cache Synchronization: Write the currently reported status information to the MySQL database and synchronously update the status information of IoT devices in the Redis cache.
[0041] Step S500 Device Offline Detection: If the IoT platform does not receive the status information reported by the IoT device within the predetermined time, the IoT device is determined to be offline, and the status information of the IoT device in the Redis cache is changed to offline.
[0042] When devices are detected offline, the reporting cycle for IoT devices to report status information is adaptively adjusted according to the device type and application scenario.
[0043] Step S600 Fault Tolerance: When the Redis service is unavailable, automatically switch to direct write MySQL mode and record the exception log. When the data writing to the Redis service is unavailable, temporarily store the current state in the local temporary cache and retry the writing through the exponential backoff mechanism.
[0044] Based on the method disclosed in this embodiment, and in conjunction with the accompanying drawings, the following example is given:
[0045] (1) Device status reporting: IoT devices (such as charging piles and cameras) report real-time status information (including device ID, status field set and reporting timestamp) to the IoT platform at preset intervals (such as every second / every 10 seconds);
[0046] (2) Redis cache read: After receiving the status information reported by the device, the IoT platform reads the most recently stored valid status information of the device (referred to as cache status) from the Redis cache using the device ID as the key; if there is no record of the device in the cache (such as the first report), proceed directly to step 4;
[0047] (3) State difference determination: Extract the core state fields of the current reported state (denoted as the current state) and the cached state. If the cache does not exist, proceed directly to step 4. If the cache exists, compare whether the core fields of the current state and the cached state are completely consistent. Verify each field. If they are consistent, it is determined that "the state has not changed" and the database update process is skipped. If they are inconsistent, it is determined that "the state has changed" and proceed to step 4.
[0048] (4) Database update and cache synchronization: Write the current status to the MySQL database to synchronously update the status information of the device in the Redis cache, and record the timestamp of this report;
[0049] (5) Detect whether the device is offline: If the IoT platform does not receive device data reports for a long time (e.g., 1 minute), the device will be judged as offline and the status information cached in Redis will be changed to offline.
[0050] (6) Exception handling: When the Redis service is unavailable, it automatically switches to the "direct write MySQL" mode and records the exception log; in the case of database write failure, the current state is retained in the local temporary cache and resubmitted through the retry mechanism to avoid state loss.
[0051] The method in this embodiment receives real-time records periodically reported by devices, utilizes Redis cache to store the most recent valid state of the devices, compares the core information differences between the current state and the cached state, and triggers a database update only when the state changes. Simultaneously, it switches to direct-write mode and supports a retry mechanism when the Redis cluster malfunctions. This invention significantly reduces the frequency of database read / write operations, balancing system real-time performance and system reliability, and is suitable for high-frequency status reporting scenarios involving massive numbers of IoT devices such as charging piles and cameras.
[0052] Example 2:
[0053] This invention discloses an IoT device status update system based on Redis caching, comprising an IoT device, an IoT platform, a Redis cache cluster, and a MySQL database.
[0054] IoT devices are used to report status information to the IoT platform in real time according to a preset period. The status information includes device ID, a set of status fields, and a reporting timestamp.
[0055] After receiving the status information, the IoT platform performs the following actions:
[0056] (1) Redis cache read: Using the device ID as the key, read the most recently stored valid status information of the corresponding IoT device from the Redis cache as the cache status information, and perform status difference judgment. If the status information of the corresponding IoT device does not exist in Redis, perform database update and cache synchronization.
[0057] (2) State difference determination: Compare the core field information in the current reported state information with the core state field in the cached state information. If the comparison results are consistent, it is determined that the state of the IoT device has not changed, and the database update and cache synchronization are skipped. If the comparison results are inconsistent, it is determined that the state of the IoT device has changed, and the database update and cache synchronization are performed.
[0058] (3) Database update and cache synchronization: Write the currently reported status information into the MySQL database and synchronously update the status information of IoT devices in the Redis cache;
[0059] (4) Device offline detection: If no status information is reported by the IoT device within the predetermined time, the IoT device will be determined to be offline, and the status information of the IoT device in the Redis cache will be changed to offline;
[0060] (5) Exception handling: When the Redis service is unavailable, it automatically switches to the direct write MySQL mode and records the exception log. When the data writing Redis service is unavailable, the current state is temporarily stored in the local temporary cache and the writing is retried through the exponential backoff mechanism.
[0061] The core status fields are key parameters characterizing the device's operating status, including online status and fault codes. When comparing the core status field information in the currently reported status information with the core status fields in the cached status information, each field is checked and verified.
[0062] When devices are under offline testing, the reporting cycle for IoT devices to report status information is adaptively adjusted according to the device type and application scenario.
[0063] The system in this embodiment can execute the method disclosed in Embodiment 1 to update the status of IoT devices.
[0064] The present invention has provided a detailed description of the IoT device status update method and system based on Redis caching. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. A method for updating the state of IoT devices based on Redis caching, characterized in that, For systems including IoT devices, IoT platforms, Redis caching clusters, and MySQL databases, the following steps are included: Device status reporting: IoT devices report status information to the IoT platform in real time according to a preset period. The status information includes device ID, status field set and reporting timestamp; Redis cache read: After receiving the status information, the IoT platform reads the most recently stored valid status information of the corresponding IoT device from the Redis cache using the device ID as the key value, and performs a status difference judgment. If the status information of the corresponding IoT device does not exist in Redis, the database is updated and the cache is synchronized. State difference determination: Compare the core field information in the currently reported state information with the core state field in the cached state information. If the comparison results are consistent, it is determined that the state of the IoT device has not changed, and the database update and cache synchronization are skipped. If the comparison results are inconsistent, it is determined that the state of the IoT device has changed, and the database update and cache synchronization are performed. Database update and cache synchronization: Write the currently reported status information to the MySQL database and synchronously update the status information of IoT devices in the Redis cache; Device offline detection: If the IoT platform does not receive status information reported by the IoT device within the scheduled time, the IoT device will be determined to be offline, and the status information of the IoT device in the Redis cache will be changed to offline. Exception handling: When the Redis service is unavailable, it automatically switches to direct write MySQL mode and records exception logs. When the data writing to the Redis service is unavailable, the current state is temporarily stored in the local temporary cache and the write is retried through the exponential backoff mechanism.
2. The IoT device state update method based on Redis caching according to claim 1, characterized in that, The core status fields are key parameters that characterize the operating status of the equipment, including online status and fault codes.
3. The IoT device state update method based on Redis caching according to claim 1, characterized in that, When comparing the core field information in the currently reported status information with the core status fields in the cached status information, each field is checked and verified.
4. The IoT device state update method based on Redis caching according to claim 1, characterized in that, When devices are under offline testing, the reporting cycle for IoT devices to report status information is adaptively adjusted according to the device type and application scenario.
5. An IoT device status update system based on Redis caching, characterized in that, This includes IoT devices, IoT platforms, Redis caching clusters, and MySQL databases; IoT devices are used to report status information to the IoT platform in real time according to a preset period. The status information includes device ID, a set of status fields, and a reporting timestamp. After receiving the status information, the IoT platform performs the following actions: Redis cache read: Using the device ID as the key, read the most recently stored valid status information of the corresponding IoT device from the Redis cache as the cache status information, and perform status difference judgment. If the status information of the corresponding IoT device does not exist in Redis, perform database update and cache synchronization. State difference determination: Compare the core field information in the currently reported state information with the core state field in the cached state information. If the comparison results are consistent, it is determined that the state of the IoT device has not changed, and the database update and cache synchronization are skipped. If the comparison results are inconsistent, it is determined that the state of the IoT device has changed, and the database update and cache synchronization are performed. Database update and cache synchronization: Write the currently reported status information to the MySQL database and synchronously update the status information of IoT devices in the Redis cache; Device offline detection: If no status information is reported by the IoT device within the scheduled time, the IoT device is determined to be offline, and the status information of the IoT device in the Redis cache is changed to offline; Exception handling: When the Redis service is unavailable, it automatically switches to direct write MySQL mode and records exception logs. When the data writing to the Redis service is unavailable, the current state is temporarily stored in the local temporary cache and the write is retried through the exponential backoff mechanism.
6. The IoT device status update system based on Redis caching according to claim 5, characterized in that, The core status fields are key parameters that characterize the operating status of the equipment, including online status and fault codes.
7. The IoT device status update system based on Redis caching according to claim 5, characterized in that, When comparing the core field information in the currently reported status information with the core status fields in the cached status information, each field is checked and verified.
8. The IoT device status update system based on Redis caching according to claim 5, characterized in that, When devices are under offline testing, the reporting cycle for IoT devices to report status information is adaptively adjusted according to the device type and application scenario.
