Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How safe is it to store sessions with Redis?

Tags:

redis

session

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?

like image 582
Trent Avatar asked Apr 23 '12 10:04

Trent


2 Answers

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
like image 106
Sripathi Krishnan Avatar answered Oct 22 '22 22:10

Sripathi Krishnan


This question is really about real-time sessions, and seems to have arisen partly due to a misunderstanding of the phrase 'delayed write operations' While the details were eventually teased out in the comments, I just wanted to make it super-duper clear...

You will have no problems implementing real-time sessions.

Redis is an in-memory key-value store with optional persistence to disk. 'Delayed write operations' refers to writes to disk, not the database in general, which exists in memory. If you SET a key/value pair, you can GET it immediately (i.e in real-time). The policy you select with regards to persistence (how much you delay the writes) will determine the upper-bound for how much data could be lost in a crash.

like image 17
Jordan Dodson Avatar answered Oct 22 '22 21:10

Jordan Dodson