Redis : Remote Dictionary Server
In-Memory 데이터베이스이다.
- Redis : 메모리에 데이터 저장 (속도가 더 빠르다 🔥)
- 기존 관계형 데이터베이스 (ex. MySQL, Oracle) : 디스크에 데이터 저장
NoSQL 이다.
- Redis : 다양한 자료구조 를 지원한다.
싱글쓰레드이다.
- atomic하다. : 모든 쓰레드가 데이터에 접근할 때, 순서가 잘 지켜진다.
Redis & Node
Redis 는 캐시로 많이 사용될 뿐만 아니라, 노드클러스터링을 사용할 때, 인스턴스 간에 상태정보를 공유하는 식으로 활용할 수도 있다.
brew install redis
redis-server
redis-cli ping
// 정상 구동된 경우 PONG 리턴됨
npm i redis
npm i express
const express = require('express')
const redis = require('redis')
// 1. app 생성 및 설정
const app = express()
const port = 3000
// 2. redis client 생성 & listen 에러
const client = redis.createClient(6379, '127.0.0.1')
client.on('error', (err) => {
console.log('Error ' + err)
})
// 3. redis 에 number : 10 저장
client.set('number', 10)
// 4. GET '/' 요청이 들어오는 경우, console에 number 의 value (=10) 로깅
app.get('/', (req, res) => {
client.get('number', (err, value) => {
if (err) throw err
console.log(value)
})
res.send('Hello World')
})
// 5. app listen
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
❤