redis-sessions

Redis Sessions

This is a Node.js module to keep sessions in a Redis datastore and add some useful methods.

The main purpose of this module is to generalize sessions across application server platforms. We use nginx reverse proxy to route parts of a website to a Node.js server and other parts could be Python, Ruby, .net, PHP, Coldfusion or Java servers. You can then use rest-sessions to access the same sessions on all app servers via a simple REST interface.

Installation

Basics

create: A session is created by supplying the app and an id (usually the unique id of the user). A token will be returned.

get: A session is queried with the app and token. This will refresh the ttl (timeout) of a session.

set: Additional data (key/value) can be stored in the session.

kill: A session can be killed with the app and token.

killall: All sessions of an app can be killed.

Additional methods

activity: Get the amount of active sessions of an app within the last n seconds.

soid: Get all sessions of a single id.

killsoid: Kill all sessions that belong to a single id. E.g. log out user123 on all devices.

soapp: Get an array of all sessions of an app which were active within the last n seconds.

Automatic cleanup of old sessions.

Performance

With Redis running on the same machine as the test script (run via npm test) on a 2017 iMac:

Creates 1000 sessions in around 95ms.

Gets those 1000 sessions and validates them in around 80ms.

Removes those 1000 sessions in 8ms.

Cache (optional setting)

Note: If you want to use the cachetime option you must not supply the client option.

Modern apps might also use a lot of requests while a user is active. This results in a lot of Redis requests to look up sessions. What's faster than an in-memory cache in Redis? An in-memory cache right in your app!
When you enable caching you can speed up session lookups by a lot. Consider the following before you enable it:

How does the cache work

The reply to the get() method will be cached for the time specified in the cachetime option.

Every set() or kill* method will flush the cache.

The idle and r keys that will be returned will not change for cached sessions.

What would be a good value for the cachetime option?

If your sessions last for 24h and the average user-session is 20m. You might as well set the cachetime to around 30m.
Consider the size of your session object that has to be kept in memory. Setting the cachetime lower is ok. Because after all it just takes a quick Redis request to fill your cache again.

As long as the ttl isn't reached this token can be used to get the session object for this user.

Remember that a user (user1001 in this case) might have other sessions.

If you want to limit the number of sessions a user might have you can use the soid (sessions of id) method to find other sessions of this user or the killsoid (Kill sessions of id) method to kill his other sessions first.

Set/Update/Delete

Set/Update/Delete parameters by supplying app, token and some data d.
The d object contains a simple key/value list where values
can be string, number, boolean or null.
To remove keys set them to null, keys that are not supplied will not be touched.

More Node.js and Redis projects?

If you run a Redis server and currently use Amazon SQS or a similar message queue you might as well use this fast little replacement. Using a shared Redis server multiple Node.js processes can send / receive messages.

Lightweight: Just Redis. Every client can send and receive messages via a shared Redis server.

Guaranteed delivery of a message to exactly one recipient within a messages visibility timeout.

No security: Like memcached. Only for internal use in trusted environments.

A Node.js helper library to make tagging of items in any legacy database (SQL or NoSQL) easy and fast. Redis is used to store tag-item associations and to allow fast queries and paging over large sets of tagged items.

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.