tracking online user count under memcached

Skylos skylos at
Fri Jun 17 14:50:41 PDT 2005

On 6/17/05, Xuefer <xuefer at> wrote:
> On 6/18/05, Skylos <skylos at> wrote:
> > On 6/17/05, Xuefer <xuefer at> wrote:
> > > On 6/17/05, Skylos <skylos at> wrote:
> > > > with incr/decr calls based on login/logout events on a key named for
> > > > the particular site?  The calls are atomic, after all.
> > >
> > > this cannot handle session expiring
> >
> > If a session expires by some action on the web server, then you can
> > use that action to generate an event to the memcached.  (like
> > Session_OnEnd in ASP)
> ASP do Session_OnEnd when he know the session is expiring. in another
> word, he knows all session data, at least the expires time, and scan
> it periodly.
> but memcached don't to it

Correctamundo.  Memcached will drop expired data on the floor, but it
won't tell you about it.

> > In a system I designed and programmed, my cache is backed by a
> > database out of necessity.  The session expiration time is entered in
> > the database, so there's always a way for me to tell what sessions are
> > active by doing a simple query.  I can then retrieve the appropriate
> > keys from memcached based on that list.
> >
> > Sounds like you really want a relational database, so you can return
> > sets of related records, rather than a hash indexed caching engine.
> i'm currently store session in db but i'd like to use memcached.

Okay, DO store it in memcached it will speed things up.  You should
back-store it into the database too and use that for your group

You can use things like expiring keys to trigger a backstore update. 
Like, user 'SKY' has keys:data

sky-STORED: 1 (expires in 60 seconds)

So say your code gets cookie 332233, if memcached doesn't have data,
it yanks from DB..  Otherwise, it yanks data from memcached for
relevant needs.  at some point during the request it checks exist of
sky-STORED - if it doesn't exist, it dumps any changes/updates as
relevant into the database, and recreates sky-STORED with another 60
second expiration.

You cut DB hits mostly, and you still have minute-resolution on
session activity.  Performance from memcached, reliability from db.


More information about the memcached mailing list