Redis
BootcampBackend2020-12-17
Redis
Redis is an open-source (BSD-licensed), in-memory data structure store, used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperlogs, and geospatial indexes with radius queries.
- Super-fast in-memory data structure server
- Pub/Sub
- Rich client libraries
- Zero install and easy to operate
- Connect multiple node.js-servers to 1 redis channel
$ brew info redis
$ brew install redis$ redis-cli ping
$ redis-cli
$ redis-cli incr mycounter4. Commands
- add test unit in test/tdd/
- add a tab socket-redis and a component src/socket-redis/PubSubApp.js
- add pub/sub clients
$ redis-cli
> config set stop-writes-on-bgsave-error no- set
smoothieto initialize redissmoothie, to test persistant store.
$ redis-cli
127.0.0.1:6379> hset smoothie field1 'red'
(integer) 1
127.0.0.1:6379> hset smoothie field2 'blue'
(integer) 1
127.0.0.1:6379> hgetall smoothie
1) "field1"
2) "red"
3) "field2"
4) "blue"-
after the set, in
server.js, this is available:pub.hgetall('smoothie', callback); - set get loginCounts 10; get, incr, decr, del
- redis-cli> get loginCounts
- redis-cli> INFO
5. Redis-backed Pub/Sub
- 2 clients required: 1 for pub, 1 for sub (or multiple subs)
- use 1 channel, add metadata to your message
- browser: socket.emit
- server: socket.on redis.publish redis.subscribe socket.emit
- browser: socket.on
Q&A
- In order to send chat messages to users across servers we will update our server to use Redis as a PubSub service (along with session store). Redis natively supports pub-sub operations.
- Subscribe channel
- Unsubscribe channel
- Publish channel message
- redis.createClient()
