Code Monkey home page Code Monkey logo

fast_sessions's Introduction

= Fast Sessions =

`FastSessions` is a sessions class for `ActiveRecord` sessions store created to work fast 
(really fast). It uses some techniques which are not so widely known in developers' community 
and only when they cause huge problems, performance consultants are trying to help with them.


==The Problem==

Original `ActiveRecord` sessions store is slow. It is fine for some low traffic blogs, but 
it is too slow to use it on some big/large/huge sites. First of all, it is slow because 
`ActiveRecord` is slow. It is powerful ORM framework, but it is overkill for such simple 
task as a sessions management.

That is why people created `SqlSession` store. It works with mysql directly with database 
APIs and works much faster than original AR session store. But it is still slow because:

  * it creates/updates session on each hit - even dumb bots crawling your sites create 
  thousands of thousands of useless records in your sessions table, 99% of hits do not 
  require any session updates!

  * it uses 32-char string as a key for sessions records - all databases work with string 
  keys MUCH slower that with integers keys, so it would be much better to use integers, 
  but we have so long session ids and all session stores use these session ids as a key.

  * it uses auto_increment primary key, which causes table-level locks in InnoDB for all 
  MySQL versions prior to 5.1.21. These table-level locks with unnecessary inserts cause 
  really weird problems for large sites.


==The Solution==

`FastSessions` plugin was born as a hack created for [http://www.scribd.com Scribd.com] 
(large RoR-based web project), which was suffering from `InnoDB` auto-increment table-level 
locks on sessions table. 

So, first of all, we removed `id` field from the table. Next step was to make lookups 
faster and we've used a following technique: instead of using (session_id) as a lookup 
key, we started using (CRC32(session_id), session_id) - two-columns key which really 
helps MySQL to find sessions faster because almost all lookups use crc32 field only to 
find needed record.

And last, but most powerful change we've tried to make was to not create database records 
for empty sessions and to not save sessions data back to database if this data has not 
been changed during current request processing.

All of these changes were implemented and you can use them automatically after a simple 
plugin installation.


==Controversial Decisions==

Many plugin users would never think about one problem we've introduced when removed that 
auto-increment primary key, so I'd like to describe it here. The problem is following.

`InnoDB` groups all data in tables by primary key. This means that when we create 
auto-increment primary key and insert records to a table, our sessions records are 
grouped together and saved sequentially on the disk. But if we'll make pretty random 
value (like crc32 of a random session id) a primary key, then every session record will 
be inserted in its own place and it will generate some random I/O which is not so good 
for I/O bound servers.

So, we decided to let the user choose what primary key to use in his deployment of our 
plugin, so if you're going to use this module with MySQL 5.1.22+, then you'dlike to set 

  ActiveRecord::SessionStore::FastSessions.use_auto_increment = true

because it will provide you with consecutive data inserts in InnoDB. Another cases when 
you'd like to use it is when your MySQL server is I/O bound now and you do not want to 
add random I/O because of randomized primary key.


==Working With Old AR Sessions Table==

If you do not like to lose old sessions created with default AR sessions plugin, you could set 

  ActiveRecord::SessionStore::FastSessions.fallback_to_old_table = true

and then all session reads will fall back to old sessions table if some session_id was not 
found in default fast sessions table. Old sessions table name could be set using 

  ActiveRecord::SessionStore::FastSessions.old_table_name

variable.


==Installation==

This plugin installation is pretty simple and described in a few steps below:

1) Install this plugin in your `vendor/plugins` directory. For example:

  ./script/plugin install git://github.com/mudge/fast_sessions.git

2) Enable `ActiveRecord` session store in your `config/initializers/session_store.rb` file:

  ActionController::Base.session_store = :active_record_store

3) Create migration for your new sessions table:

  ./script/generate fast_session_migration AddFastSessions

4) Open your newly created migration and change `table_name` and `use_auto_increment` 
parameters of the plugin (if you want to).

5) Run your migration:

  rake db:migrate

6) Start your application and try to perform some actions which would definitely 
save some data to your session. Then check your `fast_sessions` table (if you did 
not renamed it) for records.


==Downloading==

The most recent version of this plugin can be found at: 

  http://github.com/mudge/fast_sessions

or get the original version from the project site at:

  http://code.google.com/p/rails-fast-sessions/

or in SVN repository:

  http://rails-fast-sessions.googlecode.com/svn/trunk/


==Author==

This plugin has been created by Alexey Kovyrin. Development is sponsored 
by [http://www.scribd.com Scribd.com].

This Rails 2.3 compatible version was created by Paul Mucur, http://mucur.name with contributions by Erik Andrejko http://railsillustrated.com

fast_sessions's People

Contributors

mudge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

eandrejko yolk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.