Deploy Memcached with Terraform

Bahriya provides managed Memcached instances that run alongside your containers. Use them for session caching, query result caching, or any key-value workload that benefits from in-memory speed.

Updated 23 Jun 20262 min read

Bahriya provides managed Memcached instances that run alongside your containers. Use them for session caching, query result caching, or any key-value workload that benefits from in-memory speed.

Required fields

FieldTypeExampleDescription
handlestring"session-cache"Permanent identifier. Cannot be reused after deletion.
namestring"Session Cache"Display name.
memorymbnumber512Cache size in megabytes. An integer, not a string.
activeregionslist["helsinki-1"]One or more regions to deploy to.
projectstringbahriya_project.x.idThe project UUID (not handle).

Minimal example

resource "bahriya_project" "app" {
  handle  = "my-app"
  name    = "My Application"
  regions = ["falkenstein-1"]
}
 
resource "bahriya_memcached" "cache" {
  handle        = "app-cache"
  name          = "Application Cache"
  memorymb      = 512
  activeregions = ["falkenstein-1"]
  project       = bahriya_project.app.id
}

Multi-region cache

When deployed to multiple regions, each region gets its own independent cache. Caches are not synchronised across regions.

resource "bahriya_memcached" "cache" {
  handle        = "session-cache"
  name          = "Session Cache"
  memorymb      = 1024
  activeregions = ["falkenstein-1", "helsinki-1", "virginia-1"]
  project       = bahriya_project.app.id
}

Connecting from a container

Memcached instances are reachable from containers in the same project over the internal network on port 11211:

<memcached-handle>.<org-handle>-<project-handle>:11211

Pass this as an environment variable to your container:

resource "bahriya_container" "api" {
  # ... other required fields ...
 
  newenvvar {
    key   = "MEMCACHED_HOST"
    value = "app-cache.myorg-my-app:11211"
  }
}

Available memory sizes

The platform supports: 64 MB, 128 MB, 256 MB, 512 MB, 1024 MB (1 GB), 2048 MB (2 GB), 4096 MB (4 GB), 8192 MB (8 GB).

Outputs

AttributeDescription
idMemcached instance UUID.
statusCurrent status (running, provisioning, terminated, etc.).
output "cache_status" {
  value = bahriya_memcached.cache.status
}

Notes

  • memorymb is an integer (e.g. 512), not a string. This differs from container fields like mincpu which are strings.
  • Handles cannot be reused after deletion (soft-delete).
  • Each region runs an independent cache — there is no cross-region replication.