Files
floci/docs/services/elasticache.md
T
Allen Sanborn 684239b0ef fix(elasticache): create single-node redis clusters and close provisioning races (#3634)
* feat(elasticache): support single-node redis clusters in CreateCacheCluster

CreateCacheCluster refused every engine but memcached, telling callers to use
CreateReplicationGroup instead. Real AWS accepts Engine=redis and Engine=valkey
there as long as the cluster has one node, and that is exactly the call
terraform's aws_elasticache_cluster with engine = "redis" emits, so every such
example failed against Floci.

A single-node redis or valkey cluster is now provisioned the way a
cluster-mode-disabled replication group is: the same Valkey container, fronted by
the same auth proxy on a port from the same range, so the endpoint a describe
reports actually answers RESP. It is stored apart from replication groups, since
one created this way has none and must not appear in DescribeReplicationGroups.

NumCacheNodes greater than 1 is refused with the message a live account gives.
Following AWS, the cluster carries no ConfigurationEndpoint (that is memcached's
node discovery endpoint) and reports its node under CacheNodes when the request
sets ShowCacheNodeInfo, which is where terraform-provider-aws reads the port.

The memcached path is unchanged. ElastiCacheMemcachedIntegrationTest asserted the
old refusal with Engine=redis; it now asserts it with an engine ElastiCache
really does not have.

* fix(elasticache): address review findings

Cross-type id collision closed both directions through one provisioningIds
claim set (the claimed thing is one namespace: container name + proxy key);
wire code corrected to CacheClusterAlreadyExists; create request grown to
round-trip the optional members Terraform reads back, guarded by a
compat-terraform bats config asserting plan -detailed-exitcode after apply;
ListTagsForResource cluster arm brought to parity with the replicationgroup
arm without regressing memcached/member fall-through; standalone clusters
join getResources; create/delete responses no longer leak CacheNodes.

* fix(elasticache): correct review-round test assertion, bats port pin, docs note

The cross-type guard test asserted never() on a tryStart its own setup
legitimately performs — times(1), matching the sibling test. The single-node
bats config pinned port 6379, which the parallel bats runner's sibling
replication-group config fills first from the range base (the allocator
refuses rather than substitutes) — pinned to 6395. AtRestEncryptionEnabled
noted in docs as output-only on real AWS's CreateCacheCluster.

* docs(elasticache): the output-only note belongs to the single-node prose, not the CreateReplicationGroup row

AtRestEncryptionEnabled is a legitimate input member of
CreateReplicationGroupMessage; it is absent only from CreateCacheClusterMessage.

* fix(elasticache): reserve persisted ports on restore, cross-store id uniqueness, validate subnet group

Three findings from the PR review, all on the single-node cache cluster path.

Ports of records that survive a restart are now reserved before Floci serves
anything. Standalone cache clusters and cluster-mode-disabled replication groups
are not re-provisioned, but they still advertise the port they were given, and
nothing put those ports back into the in-use set: a create after a restart was
handed a port a live record still published, and the two then contended over one
reservation, so the first delete freed it and the next create took it again. A
delete now releases only a port this process actually reserved for that record,
since a restored record whose port was already taken advertises one it does not
own.

The id check spans all three stores that answer DescribeCacheClusters. A
standalone redis cluster, a Memcached cluster and a replication group can no
longer share an id in either direction. Both services read the same backends:
StorageFactory keys them by file path and hands the second caller the first
caller's instance, so no new wiring is needed for the two services to agree on
one namespace.

CacheSubnetGroupName must now name a subnet group that exists, instead of being
stored as a reference a describe reports and a caller cannot look up.
CreateReplicationGroup still does not make that check; it is called out in the
service and in the docs rather than changed here.

* style(elasticache): drop em dash in transient-field comment

* fix(elasticache): close the id and port gaps the delta review found

CreateReplicationGroup's duplicate-id check read only the group and redis
cluster stores, so a group could take the id of a live memcached cluster
while CreateCacheCluster refused the same collision the other way round.
Check the third store too, which is what makes the documented "an id is
taken across all three at once" actually hold.

Two port-reservation fixes alongside it. The claim on a new cache
cluster's port was recorded after the synchronized block that stores the
record, so a delete landing in the gap found nothing to release and
stranded the reservation; it now happens under the same monitor as the
put. And deleteReplicationGroup freed the group's port unconditionally,
while the cluster path has guarded that since the restore work went in: a
record restored from disk whose port was already taken advertises a port
it does not own, and freeing it hands a live record's port to the next
create. The holder set now covers both kinds, keyed by id, which is safe
because an id is unique across the stores.

* docs(elasticache): say which engines the subnet-group check covers

CreateCacheCluster with Engine=memcached routes to a create that takes
only the cluster id, so CacheSubnetGroupName is dropped on that path: not
checked, not echoed. The page claimed the check unqualified. Also name
CreateReplicationGroup in the id-uniqueness paragraph, now that it makes
the same check.

* test(elasticache): cover the ordinary group delete that frees its port

The two port tests around it both step past the create path: one deletes a
group written straight into the store, the other one restored after a
restart. So the claim recorded when a group is created in process had
nothing behind it, and dropping it would leave the suite green while every
DeleteReplicationGroup leaked its proxy port for the life of the process.

* fix(elasticache): claim the id on the memcached create path too

CreateCacheCluster for memcached checked the three stores for a duplicate id
and went straight on to start its container, without joining the in-flight
claim set the redis cluster and replication group paths take. No create path
persists its record until after container startup, so a memcached create and
a redis create for one id could both read three empty stores and both write,
leaving the id in two stores with DescribeCacheClusters reporting it twice
and the two containers and proxy registrations colliding under one name.

Move that set out of ElastiCacheService into an ElastiCacheProvisioningIds
bean both services inject, so the claim covers every engine, and take it in
the memcached path before the store checks with the same try/finally release.
A collision with an in-flight create is refused with the fault the store-hit
path already produces, CacheClusterAlreadyExistsFault.

* fix(elasticache): complete the CacheNode shape and correct the memcached fault code

DescribeCacheClusters returned a CacheNode carrying only an id, a status and an
endpoint. terraform-provider-aws dereferences CacheNodeCreateTime and
ParameterGroupStatus while adopting the resource without a nil check, so the
compat-terraform job created floci-tf-redis-single successfully and then died on
the very next refresh with "Unexpected nil pointer" naming both fields. The
endpoint was never the problem; the node shape was incomplete.

Both are stable metadata AWS returns on every available node, and both were
already known here: a single-node cluster's node was created with the cluster,
and its parameter group carries the same in-sync status already reported for the
cluster. CustomerAvailabilityZone is emitted from the cluster's AZ for the same
reason -- a node whose AZ disagrees with its cluster is a permanent plan diff.
Memcached returns early well above this block, so its wire output is unchanged.

Separately, the id claim added on the memcached create path threw
CacheClusterAlreadyExistsFault. The wire code is CacheClusterAlreadyExists -- the
Fault suffix is the botocore shape name, not the code an SDK matches on -- which
this branch had already established on the redis side and then contradicted.
Both memcached throw sites now use the wire code, including the pre-existing one,
and the five test assertions that pinned the wrong spelling are corrected.

* test(elasticache): stop the single-node fixture pinning the shared suite's port

The compat-terraform job runs every stack against one Floci, and this branch's
new elasticache-single-node-tf pinned port 6395 -- the same port main.tf already
pins for the replication group the `terraform` suite creates. ElastiCache
refuses a pinned port that is already reserved, by design, so the two stacks can
only both succeed if the first is fully destroyed before the second applies.

That is what the last run showed: elasticache_single_node failed its apply, so
its destroy never ran, 6395 stayed held, and the `terraform` suite then failed
with "Port 6395 is already in use by another replication group" -- a second
failure that reads like an allocator bug and is really a fixture collision.
Fixing the node shape removes the trigger but not the collision, which would
remain a teardown-ordering dependency between two independent suites.

6396 is free and inside the 6379-6399 proxy range.

* test(elasticache): name the extracted XmlPath instead of inferring it with var

Review follow-up: this was the only `var` in the file, and the type it hides
(io.restassured.path.xml.XmlPath) is the one a reader needs in order to follow
the getString/getInt calls immediately below it.

No behaviour change.

* style(test): import ArrayList and LinkedHashMap instead of naming them inline

AGENTS.md asks for imports rather than inline fully-qualified names.
ElastiCacheQueryHandlerTest had three: new java.util.ArrayList<>(...) and
two new java.util.LinkedHashMap<>(...). LinkedHashMap was already imported;
ArrayList is added.
2026-09-22 22:26:15 -04:00

13 KiB
Raw Blame History

ElastiCache

Protocol: Query (XML) for management API + Redis RESP protocol for data plane Management Endpoint: POST http://localhost:4566/ Data Endpoint: localhost:<proxy-port> (TCP)

Floci manages real Valkey/Redis Docker containers and proxies TCP connections to them. This means any Redis client works — including IAM authentication.

Supported Management Actions

Action Description
ValidateIamAuthToken Validate an IAM auth token (data-plane auth)
CreateReplicationGroup Start a new Redis/Valkey cluster; AtRestEncryptionEnabled, KmsKeyId (resolved to the key ARN), SnapshotRetentionLimit, SnapshotWindow and Tags are kept and returned, with the group ARN
DescribeReplicationGroups List clusters and their connection info
ModifyReplicationGroup Modify SnapshotRetentionLimit and SnapshotWindow, and the associated user groups
DeleteReplicationGroup Stop and remove a cluster
CreateUser Create an ElastiCache IAM user
DescribeUsers List ElastiCache users
ModifyUser Update user access strings
DeleteUser Remove an ElastiCache user
CreateCacheCluster Start a Memcached cluster, or a single-node Redis/Valkey one (NumCacheNodes must be 1)
DescribeCacheClusters List cache clusters: Memcached, single-node Redis/Valkey, and replication group members
DeleteCacheCluster Stop and remove a cache cluster
CreateCacheSubnetGroup Create a cache subnet group
DescribeCacheSubnetGroups List cache subnet groups
ModifyCacheSubnetGroup Replace a group's description or subnets
DeleteCacheSubnetGroup Delete a cache subnet group
CreateCacheParameterGroup Create a cache parameter group
DescribeCacheParameterGroups List parameter groups, including the AWS defaults
ModifyCacheParameterGroup Set parameters on a group
DescribeCacheParameters List the parameters set on a group
DeleteCacheParameterGroup Delete a cache parameter group
ListTagsForResource Tags on a parameter group ARN

Single-node Redis/Valkey clusters

CreateCacheCluster serves three engines. Engine=memcached starts a Memcached container and reports its node-discovery ConfigurationEndpoint. Engine=redis or Engine=valkey starts a single-node cluster with no replication group: the same Valkey container and auth proxy a cluster-mode-disabled replication group gets, on a port from the same proxy range, which defaults to 6379. AWS allows only one node in that shape, so NumCacheNodes greater than 1 is refused with InvalidParameterValue, as it is on a live account.

This is the call aws_elasticache_cluster with engine = "redis" emits, which is why it is not interchangeable with CreateReplicationGroup: a single-node cluster created this way never appears in DescribeReplicationGroups. Following AWS, it carries no ConfigurationEndpoint either, and reports its node's address under CacheNodes when the request sets ShowCacheNodeInfo. These clusters are re-provisioned after a Floci restart, the same as replication groups and Memcached clusters.

An id is taken across all three at once: a standalone cache cluster, a Memcached cluster and a replication group cannot share one. Two of them would have DescribeCacheClusters report the same id twice, and for a cache cluster against a replication group it is worse, since Floci names both their containers valkey-<id> and keys both their proxies by it. Whichever create arrives second is refused, whether it is CreateCacheCluster or CreateReplicationGroup, rather than allowed to remove the first's container.

CacheSubnetGroupName must name a subnet group that exists, as on AWS, on the Engine=redis and Engine=valkey paths. Engine=memcached drops the parameter: it is neither checked nor echoed back. (CreateReplicationGroup does not check it either, so a replication group can still be created against a name nothing resolves.)

Each record re-reserves its proxy port as it is restored, before Floci serves anything, so a create is never handed a port a surviving cluster or replication group still advertises.

SnapshotRetentionLimit, SnapshotWindow, PreferredMaintenanceWindow, PreferredAvailabilityZone, SecurityGroupIds, NetworkType, IpDiscovery and AtRestEncryptionEnabled (output-only on real AWS's CreateCacheCluster; accepted here leniently and echoed) are kept and echoed by DescribeCacheClusters, with the same defaults and the same validation the replication group applies, since every one is an optional aws_elasticache_cluster argument that would otherwise read back unset and leave a permanent plan diff. NotificationConfiguration and LogDeliveryConfigurations are not modelled: a request may send them, and the describe will not report them back.

Cluster Mode

CreateReplicationGroup provisions a real sharded Valkey cluster when the request asks for one: NumNodeGroups greater than 1, a default.*.cluster.on parameter group, a custom parameter group with cluster-enabled set to yes, or ClusterMode=enabled.

Floci starts one --cluster-enabled container per node — NumNodeGroups × (1 + ReplicasPerNodeGroup) in total — forms the cluster (config epochs, MEET, slot assignment, replica attachment), and fronts each node with its own auth-proxy port from the proxy port range. Nodes announce Floci's configured hostname as their preferred endpoint (cluster-announce-hostname with cluster-preferred-endpoint-type hostname, plus cluster-announce-client-ipv4/ cluster-announce-port), so CLUSTER SLOTS, CLUSTER SHARDS and MOVED/ASK redirects hand clients the same name the ConfigurationEndpoint reports, while the cluster bus keeps using the container network. Any cluster-aware Redis/Valkey client works against the reported ConfigurationEndpoint.

Because clients must resolve the announced name to follow redirects, a FLOCI_HOSTNAME that only resolves inside Floci's Docker network (such as the Compose service name floci) breaks cluster-aware clients connecting from outside it. Set FLOCI_SERVICES_ELASTICACHE_CLUSTER_ANNOUNCE_HOSTNAME to a universally resolvable name in that case — the shipped docker-compose.yml uses localhost.floci.io, which public DNS resolves to 127.0.0.1 on the host (reaching the published proxy ports) while the Compose network alias and Floci's embedded DNS resolve it to the Floci container from inside Docker. Cluster-mode groups then announce that name and report it as their ConfigurationEndpoint.

With persistent, hybrid or wal storage, every replication group, standalone cache cluster and Memcached cluster is re-provisioned from its persisted record on startup: containers are restarted, cluster-mode groups are re-formed, and proxy ports are re-reserved. Caches restart empty, as on any Floci restart: only the topology is persisted, never the keyspace.

Ports are re-reserved and records marked creating before Floci reports ready; the container restarts and cluster formation run in the background so a slow Docker daemon cannot delay readiness, and each record flips to available once its data plane is back. A replication group whose data plane cannot be brought back is reported with status create-failed instead of available, and its member clusters answer DescribeCacheClusters with restore-failed (CacheClusterStatus has no create-failed value). A standalone cache cluster or Memcached cluster that cannot be brought back reports restore-failed directly.

Reporting a failed record as failed matters more than it looks. A record that is left unreconciled keeps its available status with nothing behind the endpoint, so the control plane answers healthy while every connection fails, and the proxy port it still advertises is free for the next create to take: the old endpoint then reaches an unrelated cache rather than failing cleanly.

A Memcached cluster's endpoint follows its new container. Outside Docker, Floci publishes the backend on a host port Docker picks per run, so a restored cluster's ConfigurationEndpoint port can differ from the one it had before the restart. Replication groups and standalone cache clusters keep their port: it is a proxy port Floci owns and re-reserves.

A delete that arrives while a record is still creating wins. The restore takes the same per-record monitor DeleteReplicationGroup and DeleteCacheCluster take, and skips its write-back when the record is gone, so a group or cluster deleted in the first seconds after boot stays deleted rather than coming back available. Any container the abandoned restore had already started is stopped.

DescribeReplicationGroups reports the topology honestly: ClusterEnabled, one NodeGroup per shard with its Slots, NodeGroupMembers, and MemberClusters. Each member also answers DescribeCacheClusters (as on AWS), which is what terraform-provider-aws reads node type, engine version and port from.

Cluster mode requires a Valkey 8.1+ image (the default valkey/valkey:8 qualifies) for cluster-announce-client-ipv4 support. Each node consumes one port from the proxy range, so size FLOCI_SERVICES_ELASTICACHE_PROXY_BASE_PORT/_MAX_PORT to the number of nodes you need.

aws elasticache create-replication-group \
  --replication-group-id my-sharded-cache \
  --replication-group-description "Sharded dev cache" \
  --engine valkey \
  --cache-parameter-group-name default.valkey8.cluster.on \
  --num-node-groups 2 \
  --replicas-per-node-group 1 \
  --endpoint-url $AWS_ENDPOINT_URL

aws elasticache describe-replication-groups \
  --replication-group-id my-sharded-cache \
  --query 'ReplicationGroups[0].ConfigurationEndpoint' \
  --endpoint-url $AWS_ENDPOINT_URL

redis-cli -c -h localhost -p <configuration-endpoint-port> set mykey "hello"

Cache Subnet Groups

A subnet group's VPC and each subnet's availability zone are read from the subnets themselves, as AWS reads them, so the subnets have to exist in the emulator's EC2 first. Subnets that are unknown, or that span more than one VPC, are refused the way AWS refuses them.

Cache Parameter Groups

The default.* groups AWS publishes are listed for every family it supports, and cannot be modified or deleted — AWS refuses those by the identifier rule, since a name it accepts cannot contain a dot.

floci does not carry AWS's per-family catalogue of parameter names, which runs to dozens per family. It therefore stores whatever parameters a caller sets and reports them with source user, rather than rejecting names a partial catalogue happens to be missing, which would refuse configurations AWS accepts. DescribeCacheParameters returns those parameters; a request for system or engine-default parameters returns none, and listings are unpaged.

A replication group that names a parameter group is refused with CacheParameterGroupNotFound when no such group exists, and a parameter group still referenced by a replication group cannot be deleted: DeleteCacheParameterGroup answers InvalidCacheParameterGroupState until that replication group is gone. The reference counts from the moment CreateReplicationGroup accepts the name, not from when the group is stored, so a delete that lands while that create is still provisioning its container is refused too.

Configuration

Variable Default Description
FLOCI_SERVICES_ELASTICACHE_ENABLED true Enable or disable the service
FLOCI_SERVICES_ELASTICACHE_PROXY_BASE_PORT 6379 First host port in the ElastiCache proxy range
FLOCI_SERVICES_ELASTICACHE_PROXY_MAX_PORT 6399 Last host port in the ElastiCache proxy range
FLOCI_SERVICES_ELASTICACHE_DEFAULT_IMAGE valkey/valkey:8 Docker image for Redis/Valkey containers

Docker Compose

ElastiCache requires the Docker socket and port range exposure. For private registry authentication and other Docker settings see Docker Configuration.

services:
  floci:
    image: floci/floci:latest
    ports:
      - "4566:4566"
      - "6379-6399:6379-6399"   # ElastiCache proxy ports
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      FLOCI_SERVICES_DOCKER_NETWORK: my-project_default

Examples

export AWS_ENDPOINT_URL=http://localhost:4566

# Create a replication group (starts a Valkey container)
aws elasticache create-replication-group \
  --replication-group-id my-cache \
  --replication-group-description "Dev cache" \
  --endpoint-url $AWS_ENDPOINT_URL

# Get the connection port
PORT=$(aws elasticache describe-replication-groups \
  --replication-group-id my-cache \
  --query 'ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.Port' \
  --output text \
  --endpoint-url $AWS_ENDPOINT_URL)

# Connect with redis-cli
redis-cli -h localhost -p $PORT ping

# Use from your application
redis-cli -h localhost -p $PORT set mykey "hello"
redis-cli -h localhost -p $PORT get mykey

# Delete the cluster
aws elasticache delete-replication-group \
  --replication-group-id my-cache \
  --endpoint-url $AWS_ENDPOINT_URL

IAM Authentication

Floci supports ElastiCache IAM auth token validation. Create a user with access strings and validate tokens the same way real ElastiCache RBAC works.

# Create an ElastiCache user
aws elasticache create-user \
  --user-id alice \
  --user-name alice \
  --engine redis \
  --access-string "on ~* +@all" \
  --no-no-password-required \
  --endpoint-url $AWS_ENDPOINT_URL