DynamoDBStore is an express-session store that uses DynamoDB as the backing store.

Remarks

DynamoDB is an excellent choice for session stores because it is a fully managed service that is highly available, durable, and can scale automatically (to nearly unlimited levels) to meet demand.

DynamoDB reads will typically return in 1-3 ms if capacity is set correctly and the caller is located in the same region as the Table.

Example of Pricing

Disclaimer: perform your own pricing calculation, monitor your costs while launching, and setup cost alerts to avoid unexpected charges.

Saved AWS Pricing Calculation

Assumptions:

  • Using Provisioned Capacity with auto-scaling
  • Using Eventually Consistent Reads
  • 2 KB average session size
  • 100k RPM (requests per minute) average load
  • 1 million new sessions per month (~0.4 new sessions / second)
  • 8 million existing sessions
  • 2 million session updates / expirations per month (~0.8 updates / second)

Pricing:

  • Storage
    • 2 KB * 8 million = 16 GB of storage
    • 16 GB * $0.25 / GB / month = $4 / month for storage
  • Reads
    • 100k RPM / 60 seconds = ~1,700 RPS (requests per second)
    • 1 RCU (read capacity unit) per item * 0.5 (eventually consistent reads) = 0.5 RCU per read
    • 1,700 RPS * 0.5 RCU per read = 850 RCUs
    • 850 RCUs / read * 720 hours / month * $0.00013 / RCU / hour = ~$80 / month for reads
  • Writes
    • 0.4 new sessions / second + 0.8 updates / second = 1.2 WPS (writes per second)
    • 1.2 WPS * 2 WCU (write capacity unit) per item = 2.4 WCUs
    • Allocate more WCUs to handle bursts
    • 100 WCUs * 720 hours / month * $0.00065 / WCU / hour = ~$50 / month for writes
  • Total
    • $4 / month for storage
    • $80 / month for reads
    • $50 / month for writes
    • $134 / month total

Hierarchy

  • Store
    • DynamoDBStore

Constructors

  • Create a DynamoDB Table-based express-session store.

    Parameters

    Returns DynamoDBStore

    Remarks

    ⛔️ NOT SUGGESTED ⛔️: createTableOptions is not recommended for production use.

    Note: This does not await creation of a table if createTableOptions is passed (which should only be used in quick and dirty tests). Use DynamoDBStore.create() instead to await creation of the table in testing scenarios.

Properties

_createTableOptions?: Partial<CreateTableCommandInput>
_ddbDocClient: DynamoDBDocument
_dynamoDBClient: DynamoDBClient
_hashKey: string
_prefix: string
_tableName: string
_touchAfter: number
_useStronglyConsistentReads: boolean
captureRejectionSymbol: typeof captureRejectionSymbol
captureRejections: boolean

Sets or gets the default captureRejection value for all emitters.

defaultMaxListeners: number
errorMonitor: typeof errorMonitor

This symbol shall be used to install a listener for only monitoring 'error' events. Listeners installed using this symbol are called before the regular 'error' listeners are called.

Installing a listener using this symbol does not change the behavior once an 'error' event is emitted, therefore the process will still crash if no regular 'error' listener is installed.

Accessors

  • get hashKey(): string
  • Hash key name of the existing DynamoDB table or name of the hash key to create if the table does not exist and the createTableOptions do not provide a hash key name.

    Returns string

  • get prefix(): string
  • Prefix to add to the sid in the hashKey written to the DynamoDB table.

    Returns string

  • get tableName(): string
  • Name of the DynamoDB table to use (and optionally create)

    Returns string

  • get touchAfter(): number
  • Only update the session TTL on touch events if touchAfter seconds has passed since the last time the session TTL was updated.

    Set to 0 to always update the session TTL. - This is not suggested.

    Returns number

    Remarks

    Writes on DynamoDB cost 5x as much as reads for sessions < 1 KB.

    Writes on DynamoDB cost 20x as much as reads for sessions >= 3 KB and < 4 KB

    • Reading a 3.5 KB session takes 1 RCUs
    • Writing that same 3.5 KB session takes 4 WCUs

    Calculating Write Capacity Units - from AWS Docs

    Managing settings on DynamoDB provisioned capacity tables

    UpdateItem — Modifies a single item in the table. DynamoDB considers the size of the item as it appears before and after the update. The provisioned throughput consumed reflects the larger of these item sizes. Even if you update just a subset of the item's attributes, UpdateItem will still consume the full amount of provisioned throughput (the larger of the "before" and "after" item sizes).

  • get useStronglyConsistentReads(): boolean
  • Use Strongly Consistent Reads for session reads

    Returns boolean

    Remarks

    Strongly Consistent Reads should rarely be needed for a session store unless the values in the session are updated frequently and they must absolutely be the most recent version (which is very unliley as the most recent write could fail, in which case the session would not be the most recent version...).

    Reasons not to use Strongly Consistent Reads:

    • They cost 2x more than Eventually Consistent Reads
    • They can return a 500 if there is a network error or outage
    • They can have higher latency than Eventually Consistent Reads
  • set useStronglyConsistentReads(value): void
  • Parameters

    • value: boolean

    Returns void

Methods

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Returns all sessions in the store

    Parameters

    • callback: ((err, obj?) => void)
        • (err, obj?): void
        • Parameters

          • err: any
          • Optional obj: null | SessionData[] | {
                [sid: string]: SessionData;
            }

          Returns void

    Returns void

  • Delete all sessions from the store.

    Parameters

    • Optional callback: ((err?) => void)
        • (err?): void
        • Parameters

          • Optional err: any

          Returns void

    Returns void

  • Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • session: SessionData

    Returns void

  • Create the table if it does not exist Enable TTL field on the table if configured

    Returns Promise<void>

    Remarks

    ⛔️ NOT SUGGESTED ⛔️: This is not recommended for production use.

    For production the table should be created with IaaC (infrastructure as code) such as AWS CDK, SAM, CloudFormation, Terraform, etc.

  • Destroy the session in DynamoDB

    Parameters

    • sid: string

      Session ID

    • Optional callback: ((err?) => void)

      Callback to return an error if the session was not destroyed

      Param

      Error

      Returns

      void

        • (err?): void
        • Callback to return an error if the session was not destroyed

          Parameters

          • Optional err: unknown

            Error

          Returns void

          void

    Returns void

  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

  • Returns (string | symbol)[]

  • Parameters

    • sid: string

      Session ID

    • callback: ((err, session?) => void)

      Callback to return the session data

      Param

      Error

      Param

      Session data

      Returns

      void

        • (err, session?): void
        • Callback to return the session data

          Parameters

          • err: unknown

            Error

          • Optional session: null | SessionData

            Session data

          Returns void

          void

    Returns void

  • Returns number

  • Returns the amount of sessions in the store.

    Parameters

    • callback: ((err, length) => void)
        • (err, length): void
        • Parameters

          • err: any
          • length: number

          Returns void

    Returns void

  • Parameters

    • event: string | symbol

    Returns number

  • Parameters

    • event: string | symbol

    Returns Function[]

  • Parameters

    • sid: string
    • callback: ((err, session?) => any)
        • (err, session?): any
        • Parameters

          • err: any
          • Optional session: SessionData

          Returns any

    Returns void

  • Parameters

    • sess: SessionData

    Returns number

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Parameters

    • event: string | symbol

    Returns Function[]

  • Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • callback: ((err?) => any)
        • (err?): any
        • Parameters

          • Optional err: any

          Returns any

    Returns void

  • Parameters

    • Optional event: string | symbol

    Returns DynamoDBStore

  • Parameters

    • event: string | symbol
    • listener: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns DynamoDBStore

  • Parameters

    • n: number

    Returns DynamoDBStore

  • Reset the TTL on the DynamoDB record to 100% of the original TTL

    Parameters

    • sid: string

      Session ID

    • session: SessionData & {
          lastModified?: string;
      }

      Session data

    • Optional callback: ((err?) => void)

      Callback to return an error if the session TTL was not updated

        • (err?): void
        • Callback to return an error if the session TTL was not updated

          Parameters

          • Optional err: unknown

          Returns void

    Returns void

    Remarks

    This is called by the session middleware on every single get request.

  • Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number

    Deprecated

    since v4.0.0

  • Parameters

    • emitter: EventEmitter
    • event: string

    Returns AsyncIterableIterator<any>

  • Parameters

    • emitter: NodeEventTarget
    • event: string | symbol

    Returns Promise<any[]>

  • Parameters

    • emitter: DOMEventTarget
    • event: string

    Returns Promise<any[]>

Generated using TypeDoc