How safe is it to store sessions with Redis?

Trent picture Trent · Apr 23, 2012 · Viewed 61.9k times · Source

I'm currently using MySql to store my sessions. It works great, but it is a bit slow.

I've been asked to use Redis, but I'm wondering if it is a good idea because I've heard that Redis delays write operations. I'm a bit afraid because sessions need to be real-time.

Has anyone experienced such problems?

Answer

Sripathi Krishnan picture Sripathi Krishnan · Apr 23, 2012

Redis is perfect for storing sessions. All operations are performed in memory, and so reads and writes will be fast.

The second aspect is persistence of session state. Redis gives you a lot of flexibility in how you want to persist session state to your hard-disk. You can go through http://redis.io/topics/persistence to learn more, but at a high level, here are your options -

  1. If you cannot afford losing any sessions, set appendfsync always in your configuration file. With this, Redis guarantees that any write operations are saved to the disk. The disadvantage is that write operations will be slower.
  2. If you are okay with losing about 1s worth of data, use appendfsync everysec. This will give great performance with reasonable data guarantees