Session Management Commands

Session management commands for setting up and managing user sessions in your Azu application.

Overview

The Azu CLI provides commands to set up and manage session storage backends. Sessions are essential for maintaining user state across HTTP requests, implementing authentication, and storing temporary user data.

Supported Backends

Azu supports three session storage backends:

Backend
Description
Use Case

Redis

Fast, in-memory key-value store

Production, high-traffic applications

Database

Persistent storage in your application database

Audit requirements, queryable sessions

Memory

In-process memory storage

Development, testing only

Commands

azu session:setup

Configure and install session management for your application.

Synopsis

azu session:setup [options]

Description

Sets up session management by generating configuration files, initializers, and (optionally) database migrations. The command integrates with your existing application and adds the necessary dependencies.

Options

Option
Short
Description
Default

--backend <type>

-b

Session backend: redis, memory, or database

redis

--force

-f

Overwrite existing configuration files

false

Examples

Generated Files

The setup command generates the following files:

All Backends

Database Backend Only

Setup Steps

After running the command, complete these steps:

  1. Install dependencies:

  2. Run migrations (database backend only):

  3. Set environment variables:

  4. Require the initializer in your application:

Backend-Specific Configuration

Redis Backend

Pros:

  • Fast performance

  • Automatic expiration

  • Scales horizontally

  • No database overhead

Cons:

  • Requires Redis server

  • Data is not persistent across Redis restarts (unless configured)

Configuration:

Database Backend

Pros:

  • Persistent storage

  • Queryable sessions

  • No additional infrastructure

  • Good for audit trails

Cons:

  • Slower than Redis

  • Increases database load

  • Requires migrations

Configuration:

Memory Backend

Pros:

  • No external dependencies

  • Fast for development

  • Simple setup

Cons:

  • Not production-safe

  • Sessions lost on restart

  • Not scalable

  • Single process only

Configuration:


azu session:clear

Clear all sessions from the configured backend.

Synopsis

Description

Removes all active sessions from storage, effectively logging out all users. Use this command for maintenance, security incidents, or when changing session structure.

Options

Option
Short
Description

--force

-f

Skip confirmation prompt

--backend <type>

-b

Override detected backend

Examples

Confirmation Prompt

Unless --force is specified, you'll be prompted:

Backend Detection

The command automatically detects the session backend by:

  1. Checking src/initializers/session.cr for store type

  2. Reading SESSION_BACKEND environment variable

  3. Defaulting to Redis

Clearing Behavior by Backend

Redis

Removes all keys matching the session prefix:

Output:

Database

Executes a DELETE query on the sessions table:

Output:

Memory

Cannot be cleared remotely:

Output:


Common Workflows

Initial Setup

Switching Backends

Security Incident Response

Maintenance

Best Practices

1. Use Strong Session Secrets

Generate cryptographically secure secrets:

Never commit secrets to version control:

2. Choose the Right Backend

For Production:

  • High traffic: Redis

  • Compliance/audit: Database

  • Hybrid: Redis with database backup

For Development:

  • Memory backend for simplicity

3. Set Appropriate TTL

Balance security and user experience:

4. Monitor Session Storage

5. Implement Session Cleanup

For database backend, clean expired sessions:

Schedule with cron:

Configuration Examples

Redis with Custom Options

Database with Cleanup

Environment-Specific Configuration

Troubleshooting

Sessions Not Persisting

Check configuration:

Verify backend connectivity:

Check cookie settings:

Sessions Expiring Too Quickly

Adjust TTL:

Check Redis eviction policy:

Redis Connection Errors

Verify URL format:

Test connection:

Database Migration Issues

Ensure migration ran:

Verify table exists:

Re-run migration:

Security Considerations

1. Session Hijacking Prevention

2. Session Fixation Protection

Regenerate session ID after authentication:

4. Session Secret Rotation

Environment Variables

Variable
Description
Required

SESSION_SECRET

Encryption key for session data

Yes

SESSION_BACKEND

Backend type (redis, database, memory)

No

REDIS_URL

Redis connection URL

Yes (Redis backend)

DATABASE_URL

Database connection URL

Yes (Database backend)

See Also

Last updated