3

If I am using both redis and memcached, does it make sense to still use the built-in session API?

I understand that ruby on rails can easily use memcached using its built-in api's, but I want to use both memcached and redis.

If I have to manually access redis, when do I connect to it, per request?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

5

You can still use the Rails session API with other stores, including Redis and Memcached. The store is just that--the place where the session data is stored. The Rails session API still manages the sessions for you.

Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
3

You don't mention which contexts you're using Redis and Memcached in (or why you're using both, or how), but I'm guessing you're thinking about session data and caching (based on this earlier question: Rails and caching, is it easy to switch between memcache and redis?)

In either case, there's no real value to not use the APIs that Rails provides, unless you have a particularly distinct use case.

If you absolutely must use both, just tell Rails which storage engine you wish to use for which function.

If you're doing something else, perhaps a bit more information would help people give more useful answers.

Community
  • 1
  • 1
dnch
  • 9,565
  • 2
  • 38
  • 41
-2

In Ruby, you can store your data in any datastore. Not sure if you can use the built-in session API to store to Redis AND memcached for the same session.

If you manually access Redis, just connect to it as few times as possible and use persistent connections, if appropriate, to improve performance.

For PHP and other server-side scripting languages, you might not want to use the built-in session management. Many server-side scripting languages (i.e. PHP) store session information in a temporary directory on the hard disk. Only the session ID is stored in a cookie. An advantage of Redis and memcached is they can avoid disk access and store all session/user information in memory. So, rather than using the built-in session API, just write your own little session API using cookies and interacting with Redis or memcached.

BMiner
  • 16,669
  • 12
  • 53
  • 53
  • In Rails, you get to choose where the session data is stored. By default, it stores it in a cookie, but using a MySQL store is a common solution. Unfortunately this has the potential to be [pretty expensive](http://www.engineyard.com/blog/2011/3-common-rails-mysql-mistakes/), so you can opt for an in-memory store of some sort. No file based storage is used. – Michelle Tilley Mar 02 '11 at 03:40
  • 3
    The session APIs that are baked into Rails abstract the storage mechanism so that the problems you describe are avoided completely. Ruby on Rails behaves completely differently to PHP. – dnch Mar 02 '11 at 03:57