Reputation: 143
I need to receive data from IoT sensors and store the latest data information in Valkey.
At this time, there is a logic that checks whether the key already exists, and if it does not, it logs the event in another database. However, we occasionally observe cases where the log is recorded even though the key already exists.
A peculiar observation is that we are currently testing with only two sensors, and both sensors' logs appear at the same time when this issue occurs. Additionally, at that moment, a log stating "DB saved on disk" appears in Valkey.
Is there any way to resolve this issue? In the settings, both snapshot and AOF are already disabled.
I'm using docker compose and below is the configuration file of valkey.
notify-keyspace-events Ex
io-threads 4
################################## PERSISTENCE ###############################
# Disable RDB snapshot saving
save ""
################################## MEMORY ###################################
# Max memory usage (optional, adjust as needed)
maxmemory 512mb
maxmemory-policy allkeys-lru
# Disable AOF
appendonly no
Upvotes: 0
Views: 40
Reputation: 21
In order to verify if a key already exist in Valkey you can use the set if not exist command.
You can use the following example code in node.js, client library valkey-glide. This is only an example, any client library in any language will do the work.
const { GlideClient } = require('@valkey/valkey-glide');
const client = await GlideClient.createClient({
addresses: [{
host: config.host,
port: config.port
}],
useTLS: config.useTls
});
await client.set('custom:key', 'custom:value', {
conditionalSet: "onlyIfDoesNotExist",
returnOldValue: true});
For more info see https://valkey.io/commands/set/
Alternative solution is to use Lua script for more complex atomic operation. Depends on your use case.
Upvotes: 1