Amfani Redis Clustering da in NodeJS

Redis Clustering hanya ce mai rarrabawa kuma mai daidaitawa don sarrafa bayanai a cikin Redis, sanannen ma'ajin bayanan ƙwaƙwalwar ajiya. Tari yana ba da damar Redis nodes da yawa don yin aiki tare azaman tsarin haɗin kai, samar da samuwa mafi girma, haƙuri da kuskure, da ingantaccen aiki don sarrafa manyan bayanan bayanai.

A cikin Redis Clustering, an raba bayanai a cikin nodes da yawa, kuma kowane kumburi yana riƙe da wani yanki na bayanai kawai. Wannan rarrabuwa yana ba da damar sikeli a kwance, inda za'a iya ƙara sabbin nodes zuwa gungu don ɗaukar buƙatun girma bayanai. Bugu da ƙari, Redis Clustering yana ba da ginanniyar kwafi, yana tabbatar da sake dawowar bayanai da kuma gazawa idan aka sami gazawar kumburi.

Mabuɗin abubuwan sun Redis Clustering haɗa da:

  1. Babban Samun: Redis Clustering yana tabbatar da cewa ko da wasu nodes sun gaza, tsarin gabaɗaya ya ci gaba da aiki, godiya ga kwafin bayanai da hanyoyin gazawar atomatik.

  2. Ƙimar Daidaitawa: Yayin da girman bayanai ke ƙaruwa, ana iya ƙara sabbin nodes zuwa gungu, rarraba nauyin bayanai da haɓaka aiki.

  3. Sharding Data: An raba bayanai zuwa shards, kuma kowane ɓangarorin an sanya shi zuwa wani ƙayyadadden kumburi, yana ba da damar rarraba bayanai masu inganci da kuma dawo da su.

  4. Gudanar da Tari: Redis Clustering yana amfani da haɗin Redis Sentinel da Manajan Cluster don saka idanu lafiyar kumburi da yin ayyukan gazawa.

  5. Daidaitawa: Redis yana ba da daidaito na ƙarshe, inda ake yada canje-canje ga bayanai a cikin tari a hankali.

 

Don amfani Redis Clustering a cikin NodeJS, bi waɗannan matakan:

Shigar Redis

Da farko, kuna buƙatar shigarwa Redis akan sabar ku. Kuna iya sauke shi daga gidan yanar gizon hukuma ko amfani da mai sarrafa fakiti kamar apt ko brew.

Cấu hình Redis cho clustering

Sanya Redis don Tari: Buɗe Redis fayil ɗin sanyi(redis.conf) kuma yi canje-canje masu zuwa:

# Enable clustering mode  
cluster-enabled yes  
cluster-config-file nodes.conf  
cluster-node-timeout 5000  

Saita cluster-enabled don yes kunna yanayin tari. cluster-config-file Yana ƙayyade sunan fayil ɗin inda za'a adana jihar tari. cluster-node-timeout yana bayyana lokacin ƙarewa a cikin millise seconds don kuɗaɗen tari.

Fara Redis Misalai

Fara Redis lokuta da yawa akan tashoshin jiragen ruwa daban-daban, waɗanda zasu samar da Redis tari. Kowane misali yakamata yayi amfani da fayil ɗin daidaitawa iri ɗaya.

Redis Cluster in NodeJS

n NodeJS aikace-aikacen ku, yi amfani da Redis ɗakin karatu na abokin ciniki wanda ke goyan bayan Redis tari, kamar "ioredis". Abokin ciniki zai sarrafa yanayin tari da buƙatun hanya ta atomatik zuwa kuɗaɗen da suka dace.

Misalin haɗawa da Redis Cluster "ioredis" a cikin NodeJS:

const Redis = require('ioredis');  
  
const redis = new Redis.Cluster([  
  { host: '127.0.0.1', port: 7000 },  
  { host: '127.0.0.1', port: 7001 },  
  { host: '127.0.0.1', port: 7002 },  
  // Add more Redis nodes if needed  
]);  

Maye gurbin adireshin IP da tashoshin jiragen ruwa tare da adiresoshin Redis nodes ɗin ku.

Gwaji Redis Clustering

Tare da saita gungu da kuma NodeJS haɗa aikace-aikacen zuwa gare ta, zaku iya fara amfani da Redis umarni kamar yadda kuka saba. Abokin Redis ciniki zai sarrafa rarraba bayanai ta atomatik da gazawar a tsakanin maƙallan tari.

 

Ta bin waɗannan matakan, zaku iya amfani Redis Clustering da aikace-aikacen ku NodeJS, ba shi damar yin sikeli a kwance da sarrafa bayanai masu yawa cikin sauƙi.