Copyright © 2016 M/Gateway Developments Ltd
EWD 3 Training Course
Part 35
QEWD Session Locking
Rob Tweed
Director, M/Gateway Developments Ltd
Twitter: @rtweed
Copyright © 2016 M/Gateway Developments Ltd
QEWD Sessions
• QEWD uses the ewd-session module to provide
its Session functionality
• QEWD Session data is physically stored in your
embedded Global Storage database:
– Redis, Cache or GT.M
– Uses the DocumentStore / DocumentNode
abstraction
• All Worker processes have access to the
Session document in your Global storage
– So it doesn't matter which physical worker process
handles each requst for the same Session
Copyright © 2016 M/Gateway Developments Ltd
QEWD Sessions
• There's nothing to stop a browser / client
from sending 2 simultaneous requests to
QEWD
– If this happens, the one that arrives first at the
QEWD back-end will be dispatched to the first
available worker process
– The second request will probably be next in
the queue, and if there's another available
worker process, the second request will be
dispatched to it
Copyright © 2016 M/Gateway Developments Ltd
QEWD Sessions
• So it's possible to have two Worker
processes handling requests from the
same client and for the same QEWD
Session
• If the handler functions for these requests
modify the QEWD Session contents,
there's a risk of data corruption if they both
access / change the same Session data at
the same time
Copyright © 2016 M/Gateway Developments Ltd
QEWD Session Locking
• To prevent this happening, you can
optionally enable Session Locking.
• In this mode, the first Worker locks the
Session document for the QEWD Session
it's handling
• The second Worker tries to do the same,
but can't set the lock, so it waits
– Remember QEWD uses synchronous APIs,
so the second worker process blocks
Copyright © 2016 M/Gateway Developments Ltd
QEWD Session Locking
• When the first Worker finishes processing (by
invoking the finished() function), it releases the
Session lock
• The second Worker can now set the lock and
commence processing
– It releases the lock again when it finishes
• Note that other workers processing requests for
other Sessions won't be affected
– They will be locking/unlocking different Sessions
Copyright © 2016 M/Gateway Developments Ltd
QEWD Session Locking
• Session locking therefore forces processing of
simultaneously sent requests from the same
browser / client to be serialised
– ie requests for a specific QEWD Session will only be
handled one at a time
• Note that QEWD will process them in the order it
received them
– If you need them to be processed in a specific order, you'll need
to control the sequence within the browser / client yourself
Copyright © 2016 M/Gateway Developments Ltd
Enabling Session Locking
• Simple change to your QEWD startup file
• Add the lockSession property to the config
object
• Set its value to true
Copyright © 2016 M/Gateway Developments Ltd
Edit your QEWD Startup File
var config = {
managementPassword: 'keepThisSecret!',
serverName: 'My QEWD Server',
port: 8080,
poolSize: 2,
database: {
type: 'cache',
params: {
path: 'c:InterSystemsCache2015-2mgr’
},
lockSession: true
}
};
var qewd = require('qewd').master;
qewd.start(config);
Copyright © 2016 M/Gateway Developments Ltd
Restart QEWD
• Session locking is now enabled

EWD 3 Training Course Part 35: QEWD Session Locking

  • 1.
    Copyright © 2016M/Gateway Developments Ltd EWD 3 Training Course Part 35 QEWD Session Locking Rob Tweed Director, M/Gateway Developments Ltd Twitter: @rtweed
  • 2.
    Copyright © 2016M/Gateway Developments Ltd QEWD Sessions • QEWD uses the ewd-session module to provide its Session functionality • QEWD Session data is physically stored in your embedded Global Storage database: – Redis, Cache or GT.M – Uses the DocumentStore / DocumentNode abstraction • All Worker processes have access to the Session document in your Global storage – So it doesn't matter which physical worker process handles each requst for the same Session
  • 3.
    Copyright © 2016M/Gateway Developments Ltd QEWD Sessions • There's nothing to stop a browser / client from sending 2 simultaneous requests to QEWD – If this happens, the one that arrives first at the QEWD back-end will be dispatched to the first available worker process – The second request will probably be next in the queue, and if there's another available worker process, the second request will be dispatched to it
  • 4.
    Copyright © 2016M/Gateway Developments Ltd QEWD Sessions • So it's possible to have two Worker processes handling requests from the same client and for the same QEWD Session • If the handler functions for these requests modify the QEWD Session contents, there's a risk of data corruption if they both access / change the same Session data at the same time
  • 5.
    Copyright © 2016M/Gateway Developments Ltd QEWD Session Locking • To prevent this happening, you can optionally enable Session Locking. • In this mode, the first Worker locks the Session document for the QEWD Session it's handling • The second Worker tries to do the same, but can't set the lock, so it waits – Remember QEWD uses synchronous APIs, so the second worker process blocks
  • 6.
    Copyright © 2016M/Gateway Developments Ltd QEWD Session Locking • When the first Worker finishes processing (by invoking the finished() function), it releases the Session lock • The second Worker can now set the lock and commence processing – It releases the lock again when it finishes • Note that other workers processing requests for other Sessions won't be affected – They will be locking/unlocking different Sessions
  • 7.
    Copyright © 2016M/Gateway Developments Ltd QEWD Session Locking • Session locking therefore forces processing of simultaneously sent requests from the same browser / client to be serialised – ie requests for a specific QEWD Session will only be handled one at a time • Note that QEWD will process them in the order it received them – If you need them to be processed in a specific order, you'll need to control the sequence within the browser / client yourself
  • 8.
    Copyright © 2016M/Gateway Developments Ltd Enabling Session Locking • Simple change to your QEWD startup file • Add the lockSession property to the config object • Set its value to true
  • 9.
    Copyright © 2016M/Gateway Developments Ltd Edit your QEWD Startup File var config = { managementPassword: 'keepThisSecret!', serverName: 'My QEWD Server', port: 8080, poolSize: 2, database: { type: 'cache', params: { path: 'c:InterSystemsCache2015-2mgr’ }, lockSession: true } }; var qewd = require('qewd').master; qewd.start(config);
  • 10.
    Copyright © 2016M/Gateway Developments Ltd Restart QEWD • Session locking is now enabled