Shirya matsala da sarrafa kurakurai a cikin NodeJS aikace-aikacen lokacin haɗawa da shi Redis muhimmin tsari ne don tabbatar da daidaito da ingancin aikace-aikacen.
A ƙasa akwai wasu cikakkun bayanai da misalai kan yadda ake yin matsala da sarrafa kuskure lokacin aiki tare da Redis aikace NodeJS -aikacen.
Duba Redis log
Redis yana ba da rajistan ayyukan rikodin muhimman abubuwan da suka faru, gargaɗi, da kurakurai. Waɗannan rajistan ayyukan na iya zama da amfani don magance matsala tare da Redis. Don kunna shiga Redis, kuna buƙatar canza redis.conf
fayil ɗin sanyi kuma saita matakin shiga da ya dace.
Ga misali na kunna shiga cikin fayil:
# In redis.conf
logfile /var/log/redis/redis.log
loglevel verbose
Tabbatar cewa log kundin fayil ɗin yana wanzu kuma ana iya rubuta shi ta hanyar Redis tsari.
Amfani Redis Monitor
Redis Monitor ginannen umarni ne wanda ke ba ku damar saka idanu kan Redis umarnin da aka aiwatar akan sabar. Yana da taimako don fahimtar ainihin umarnin da ake aika zuwa Redis.
Ga misalin amfani Redis Monitor da ɗakin karatu na "ioredis" a cikin NodeJS aikace-aikace:
const Redis = require('ioredis');
const redis = new Redis();
redis.monitor((err, monitor) => {
console.log('Started monitoring Redis commands');
monitor.on('monitor',(time, args, source, database) => {
console.log('Command:', args);
});
});
Wannan lambar tana saita Redis na'ura mai saka idanu wanda ke buga kowane Redis umarni da uwar garken ya karɓa a ainihin-lokaci.
Kula da kurakurai marasa daidaituwa
Lokacin aiki tare da Redis aikace NodeJS -aikacen, yawancin Redis ayyuka ba su daidaita, ma'ana suna amfani da su callback
ko Promises
.
Gudanar da kurakurai da kyau yana da mahimmanci don guje wa faɗuwar aikace-aikacen. Ga misali na magance kurakurai tare da callback
:
const Redis = require('ioredis');
const redis = new Redis();
redis.get('key',(err, result) => {
if(err) {
console.error('Error:', err);
return;
}
console.log('Result:', result);
});
Kuma amfani async/await
da Promises
:
const Redis = require('ioredis');
const redis = new Redis();
async function getValue() {
try {
const result = await redis.get('key');
console.log('Result:', result);
} catch(err) {
console.error('Error:', err);
}
}
getValue();
Sarrafa Redis haɗi
Don sarrafa Redis haɗin kai, ana ba da shawarar yin amfani da tafkin haɗin gwiwa wanda Redis ɗakin karatu na abokin ciniki ya samar. Misali, tare da "ioredis":
const Redis = require('ioredis');
const redis = new Redis({
// connection options here
});
Abokin ciniki zai sarrafa haɗin kai ta atomatik kuma ya sake amfani da su da kyau.
Karɓar lokuta lokacin da Redis babu
Don gudanar da shari'o'in lokacin da Redis babu ko amsa a hankali, yi la'akari da saita lokacin da ya dace da kuma sarrafa kurakuran haɗi cikin alheri.
const Redis = require('ioredis');
const redis = new Redis({
retryStrategy:(times) => {
return Math.min(times * 50, 2000); // Retry with exponential backoff up to 2 seconds
},
});
Amfani Redis Sentinel
Redis Sentinel yana ba da dama mai yawa da saka idanu ga Redis gungu. Yana sarrafa gazawar ta atomatik lokacin da babu babban kumburi.
Ga misalan daidaitawa:
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout my
Tsarin da ke sama yana saita Redis Sentinel mai kula da Redis maigida tare da ƙasa-bayan-dakiku na 5000ms, gazawar-lokaci na 10000ms, da daidaitawa guda 1.
Ta bin waɗannan matakai da misalan, za ku iya yadda ya kamata warware matsala da sarrafa kurakurai yayin aiki tare da Redis aikace NodeJS -aikacen, tabbatar da aminci da aikin aikace-aikacenku.