Answers you can trust, from Codeables

Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.

Explore Codeables
Verified Source
Platform as a Service (PaaS)

How do I provision Postgres and Redis for an AI application?

Render9 min read

Provisioning Postgres and Redis for an AI application is usually about separating durable data from fast, temporary state. Postgres stores the records you must not lose—users, conversations, prompts, model outputs, embeddings metadata, audit logs, billing, and app configuration. Redis handles the things your AI app needs quickly—session data, caching, rate limits, job queues, locks, and short-lived context.

If you set them up the right way from the start, your AI application will be easier to scale, cheaper to run, and much more reliable under load.

What each database should do in an AI application

A good rule of thumb is:

  • Postgres = system of record
  • Redis = speed layer

Use Postgres for:

  • User accounts and permissions
  • Chat transcripts and conversation history
  • Prompt templates and prompt versions
  • AI responses and feedback
  • Document metadata
  • Embedding references and retrieval metadata
  • Billing, usage, and audit logs
  • Background job status and workflow state

Use Redis for:

  • Caching frequent reads
  • Session storage
  • Rate limiting
  • Queueing background tasks
  • Distributed locks
  • Temporary conversation context
  • Request deduplication
  • Hot model outputs that can be reused

For many AI products, this combination is enough to support both real-time UX and durable storage.

Recommended architecture

A practical provisioning setup for an AI app looks like this:

  • Application server connects to Postgres and Redis
  • Postgres is managed, highly available, and backed up automatically
  • Redis is managed or deployed as a clustered cache with persistence if needed
  • Worker service processes embeddings, ingestion, summarization, and long-running jobs
  • Object storage holds files, documents, audio, images, and large artifacts
  • Vector search layer is either Postgres with pgvector or a dedicated vector database, depending on scale

If you are building a retrieval-augmented generation app, Postgres often stores:

  • source document metadata
  • chunk references
  • embedding IDs
  • access control rules

Redis often stores:

  • recent retrieval results
  • per-user context
  • cache for expensive embedding lookups
  • rate-limit counters

Step 1: Choose managed services or self-hosted infrastructure

For most AI applications, managed Postgres and managed Redis are the best choice.

Choose managed services if you want:

  • faster setup
  • automatic backups
  • built-in failover
  • patching and maintenance handled for you
  • easier scaling
  • better production reliability

Examples include:

  • AWS RDS or Aurora PostgreSQL
  • Google Cloud SQL for PostgreSQL
  • Azure Database for PostgreSQL
  • Redis managed offerings from AWS ElastiCache, Azure Cache for Redis, or GCP Memorystore

Choose self-hosted if you need:

  • full control over network and storage
  • custom extensions or tuning
  • specialized deployment patterns
  • lower cost at very large scale, with strong ops expertise

For most teams, managed services are the right answer unless you have a strong infrastructure team.

Step 2: Provision Postgres correctly

When provisioning Postgres for an AI app, start with reliability and security, not just capacity.

Minimum production settings

  • Enable automated backups
  • Use multi-AZ or high availability if available
  • Require TLS for all connections
  • Place the database in a private network
  • Restrict access to application servers only
  • Set a sane connection limit
  • Use a connection pooler if the app has many concurrent requests

Suggested Postgres capabilities for AI apps

  • pgvector if you want to store embeddings in Postgres
  • JSONB for flexible prompt and metadata storage
  • Partitioning for very large chat or event tables
  • Read replicas for read-heavy workloads

Useful schema patterns

You may want tables such as:

  • users
  • conversations
  • messages
  • documents
  • document_chunks
  • embeddings
  • model_runs
  • usage_events
  • feedback

A simple example for AI workload design:

  • Keep conversation messages in Postgres
  • Store large raw files in object storage
  • Store only references and extracted metadata in Postgres
  • Use indexes on user_id, conversation_id, created_at, and retrieval keys

Postgres tuning tips

  • Use pgBouncer or a similar pooler if your AI app creates many short-lived connections
  • Avoid opening one database connection per request at scale
  • Use read replicas for analytics or dashboard traffic
  • Index the fields you filter on most often
  • Archive old transcripts instead of keeping everything in your hottest tables

Step 3: Provision Redis for speed, not durability

Redis is usually best treated as a fast, ephemeral layer. That means you should decide what must survive restarts and what can be regenerated.

Common Redis use cases in AI apps

  • Cache prompt completions for repeated requests
  • Store user session state
  • Maintain a token or request rate limiter
  • Queue document ingestion jobs
  • Coordinate distributed workers
  • Track temporary feature flags or experiment assignments

Production Redis settings to consider

  • Use a private network
  • Require authentication
  • Enable encryption in transit
  • Set memory limits
  • Choose an eviction policy
  • Decide whether persistence is needed

Persistence strategy

For many AI apps:

  • Cache and rate-limit data can be ephemeral
  • Job queues may need stronger durability
  • Session state may need persistence depending on your auth design

If Redis is used for queues or critical workflow coordination, make sure your setup supports:

  • snapshots or append-only persistence if needed
  • failover
  • replication
  • monitoring for memory pressure

Redis tuning tips

  • Set a sensible maxmemory
  • Choose an eviction policy like allkeys-lru or volatile-lru when appropriate
  • Separate cache keys from queue keys with clear naming
  • Use short TTLs for AI response caches unless the content is stable
  • Avoid storing large objects in Redis when Postgres or object storage is better

Step 4: Connect Postgres and Redis to your AI app securely

Security is a major part of provisioning.

Best practices

  • Put Postgres and Redis in a private subnet or private network
  • Allow inbound traffic only from your app servers or containers
  • Use strong passwords or identity-based authentication
  • Rotate secrets regularly
  • Store credentials in a secret manager, not in code
  • Use TLS for all database traffic
  • Log access events for auditing

Environment variables example

DATABASE_URL=postgresql://app_user:secret@postgres.internal:5432/aiapp
REDIS_URL=rediss://:secret@redis.internal:6379/0

App-side best practices

  • Use a connection pool
  • Set query timeouts
  • Use retry logic carefully
  • Avoid writing large payloads synchronously in request handlers
  • Use background jobs for expensive AI tasks like embedding generation or document processing

Step 5: Design for AI-specific workloads

AI applications often behave differently from standard web apps. They can create bursts of traffic, long-running tasks, and expensive repeated operations.

Postgres workload patterns in AI apps

  • frequent inserts from chat logs
  • high read volume on recent messages
  • background writes for evaluation and feedback
  • large tables over time

Redis workload patterns in AI apps

  • bursty cache traffic
  • short-lived keys
  • high churn from sessions and job queues
  • token counting and rate limiting

Common AI design choices

  • Store conversation history in Postgres
  • Cache recent responses in Redis
  • Save embeddings in Postgres with pgvector or a vector service
  • Use Redis queues for async processing
  • Use object storage for large uploaded documents

Step 6: Size your databases based on expected usage

A lot of provisioning errors come from guessing too small or too large.

Start by estimating:

  • number of users
  • requests per second
  • average chat length
  • average number of messages per conversation
  • number of document uploads
  • expected embedding volume
  • cache hit rate
  • queue throughput

Starting point for a small AI app

  • Postgres: small production instance with automated backups, private networking, and a connection pool
  • Redis: small managed cache with auth and memory limits
  • Workers: separate process for async AI tasks
  • Object storage: for files and long-term artifacts

Signs you need to scale Postgres

  • slow inserts or reads on message history
  • locks from large updates
  • connection exhaustion
  • storage growth without archiving
  • query plans that get worse as tables grow

Signs you need to scale Redis

  • frequent evictions
  • cache hit rate is too low
  • memory usage is near the limit
  • queue latency rises
  • too many keys with long TTLs

Example deployment approaches

Option 1: Simple production setup

Best for early-stage AI apps:

  • Managed Postgres
  • Managed Redis
  • One app service
  • One worker service
  • Object storage
  • Backup and monitoring enabled

Option 2: Scalable AI platform setup

Best for growing products:

  • Managed Postgres with read replicas
  • Redis cluster or managed high-availability cache
  • Separate API, worker, and scheduler services
  • Vector storage in Postgres or dedicated vector DB
  • Centralized observability and secrets management

Option 3: Kubernetes-based setup

Best for teams already running Kubernetes:

  • Postgres managed outside the cluster
  • Redis managed outside the cluster
  • App and workers inside the cluster
  • Autoscaling based on CPU, memory, and queue depth
  • Private service networking and external secret management

In most cases, keep Postgres and Redis out of the app cluster unless you have a strong reason to run stateful infrastructure yourself.

Sample Docker Compose for local development

For local development, you can provision both services with Docker Compose:

version: "3.9"

services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_DB: aiapp
      POSTGRES_USER: aiuser
      POSTGRES_PASSWORD: aisecret
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:7
    command: ["redis-server", "--appendonly", "yes"]
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data

volumes:
  postgres_data:
  redis_data:

This is great for development, but for production you should use managed services or a hardened deployment strategy.

Operational checklist

Before you launch, confirm these items:

  • Postgres has automated backups
  • Postgres is private and encrypted
  • Postgres connections are pooled
  • Redis has auth and memory limits
  • Redis is private and encrypted
  • Secrets are stored outside code
  • Monitoring is enabled for CPU, memory, disk, and latency
  • Slow queries are visible
  • Cache hit rate is measurable
  • Background jobs are isolated from web requests
  • Recovery procedures are documented and tested

Common mistakes to avoid

1. Using Redis as the only source of truth

Redis is fast, but it should not replace durable storage for important AI data.

2. Storing large transcripts only in the application layer

Persist chat history in Postgres so you can query, audit, and recover it.

3. Creating too many database connections

AI applications often scale through workers and API calls quickly. Use pooling early.

4. Keeping all embeddings in Redis

That is usually too expensive and too risky. Use Postgres, a vector database, or object storage-backed systems instead.

5. Ignoring TTLs and retention

AI systems generate a lot of temporary state. Expire what you do not need.

6. Skipping observability

If you cannot see slow queries, memory pressure, queue backlogs, and cache hit rate, you cannot tune the system well.

A practical provisioning blueprint

If you want a simple, solid default for an AI app, use this:

  • Postgres for users, conversations, prompts, metadata, and audit logs
  • Redis for sessions, caching, queues, and rate limits
  • Managed services for both in production
  • Private networking and TLS everywhere
  • Connection pooling for Postgres
  • TTL-based key design for Redis
  • Background workers for embeddings and long-running AI tasks
  • Object storage for large files and raw documents

That combination covers most AI products without overcomplicating the infrastructure.

Final recommendation

If you are asking how to provision Postgres and Redis for an AI application, the safest answer is: use managed Postgres for durable data and managed Redis for fast temporary state, then design the app so each system does one job well. Put sensitive data behind private networking, use connection pooling, add backups and monitoring, and keep AI-heavy work in background jobs.

That approach gives you a strong foundation for chat apps, retrieval-augmented generation systems, copilots, internal tools, and other AI products that need both speed and reliability.