Subscribed unsubscribe Subscribe Subscribe

Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

Using Redis for Real Time Application

I'm curious about how scale out WebSocket-based implementation of, for example, chat-like service. Can it be easily scaled out to multiple app servers? I think Server-Sent Events and Redis can easily solve the problem.

  • The biggest difference between WebSocket and SSE is bi-directional or not.
  • Posting messages can be done via usual XHR.
  • So, the problem just sits on how server can sent message chunks to clients in real time.

Using Redis (Pub/Sub and replication), can we get more robust architecture?

Pub/Sub with replication by Redis

Redis settings

  • Redis installed via homebrew
$ redis-server -v
Redis server v=2.6.4 sha=00000000:0 malloc=libc bits=64

Slave setting

Add the settings below into config file for slave (name the file slave.conf copied from redis.conf):

port 6380
slaveof 127.0.0.1 6379

Launch Redis servers

master:

$ redis-server # master

slave:

$ redis-server slave.conf # slave

Launch clients

publisher:

$ redis-cli -p 6379
redis 127.0.0.1:6379>

subscribers:

$ redis-cli -p 6379
redis 127.0.0.1:6379> SUBSCRIBE 'test'
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "test"
3) (integer) 1
$ redis-cli -p 6380
redis 127.0.0.1:6379> SUBSCRIBE 'test'
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "test"
3) (integer) 1

Publish something:

From the publisher console:

redis 127.0.0.1:6379> PUBLISH 'test' booooo
(integer) 1

Then both of clients get message from publisher!

The client connecting to the master:

redis 127.0.0.1:6379> SUBSCRIBE 'test'

...

1) "message"
2) "test"
3) "booooo"

The client connecting to the slave:

redis 127.0.0.1:6380> SUBSCRIBE 'test'

...

1) "message"
2) "test"
3) "booooo"

App server

Use anything you like to send message to all the clients connecting to server via SSE.

Here's my PoC implementation using Rails4. It now seems not to work well due to the change of Rails4, though.

http://blog.kentarok.org/entry/2012/11/22/005844

Pros.

  • Easy to scale out bottle necks.
  • SSE is more understandable than WebSockets

Cons.

  • We need one more middleware (Redis)
  • Any other?

Conclusion

  • Replication setting of Redis is easy.
  • Pub/Sub in Redis works fine also in replication.
  • In the sense of ease of scaling out, SSE + Pub/Sub with Redis can be better for chat-like service than WebSocket with single event-besed server.

What do you think?