Skip to content

Express + Redis Sessions

Node.js/Express application using Redis for session storage and caching.

Overview

Tech Stack Node.js / Express + Redis
Services 1 (web, port 3000)
Cache Redis (managed)
Pattern Cache integration, session storage

The appmecca.yaml

containers:
  - name: express-redis-sessions
    dockerfile: Dockerfile
    context: .

deploy:
  - name: web
    container: express-redis-sessions
    port: 3000
    health_check:
      path: /health

redis:
  enabled: true

Key Concepts

Redis Sessions with connect-redis

The app uses express-session with connect-redis for server-side session storage. Sessions are stored in Redis with a configurable prefix, making them fast to access and easy to share across multiple instances.

Cache Key Prefixing (CACHE_PREFIX)

All Redis keys are namespaced with the CACHE_PREFIX environment variable, which AppMecca injects for you. This is essential because every app in the environment shares one managed cache -- each app and each branch gets a different prefix, so keys never collide. Read the value from the environment rather than hardcoding it.

API Endpoints

Method Path Description
GET /health Health check (verifies Redis connectivity)
GET /api/counter Increment visit counter (session-based)
POST /api/cache Store a cache entry
GET /api/cache/:key Retrieve a cached value
DELETE /api/cache/:key Delete a cached value
GET /api/stats Redis statistics

Step-by-Step Deployment

1. Register the app

mecca register git@gitlab.com:appmecca-sample-apps/express-redis-sessions.git \
  -e dev

2. Build

mecca build trigger \
  -e dev \
  -g git@gitlab.com:appmecca-sample-apps/express-redis-sessions.git

3. Deploy

mecca app deploy -e dev express-redis-sessions

4. Verify

curl https://express-redis-sessions-web.dev.appmecca.net/health
curl https://express-redis-sessions-web.dev.appmecca.net/api/counter

The service name (web) is always part of the hostname, even for a single-service app. Run mecca app show express-redis-sessions -e dev to see the exact URL for your group.

What's Next