Skip to content

Connect your apps

Connect clients over TLS with the EZGH CA bundle, set timeouts, honor the endpoint's TTL, and build a rate limiter on Valkey.

Updated View as Markdown
ezgh caches get cch_4k2m9x0a7qpe

The console shows the same on the cache’s Connectivity tab: the endpoint, its ports, and the default user’s Secret.

  • Endpoint: the cache’s DNS name. It stays the same through failovers, maintenance, upgrades and blue/green switchovers.
  • Port: 6379 for Valkey. 11211 (TLS) and 11212 (no TLS) for Memcached.
  • User and password: in each user’s Secret. On Valkey, its uri field is a ready-made rediss:// URL.

Connect from an address in the cache’s allowed sources, inside its VPC.

TLS

Valkey takes only TLS connections. The cache’s certificate is for its endpoint and chains to the EZGH CA bundle. Verify the server’s certificate and its name against the bundle:

curl -fsSO https://get.ezghcloud.com/caches/ca-bundle.pem
valkey-cli --tls --cacert ca-bundle.pem -h cache-hylq6kelvuwvn2ks4yqhp4jaqu.us-west-1.ezghcompute.com \
  -p 6379 --user default --askpass

redis-cli takes the same options.

import redis

r = redis.Redis(
    host="cache-hylq6kelvuwvn2ks4yqhp4jaqu.us-west-1.ezghcompute.com",
    port=6379,
    username="default",
    password=password,
    ssl=True,
    ssl_ca_certs="ca-bundle.pem",
    socket_connect_timeout=2,
    socket_timeout=2,
)
import { readFileSync } from "node:fs";
import Redis from "ioredis";

const redis = new Redis({
  host: "cache-hylq6kelvuwvn2ks4yqhp4jaqu.us-west-1.ezghcompute.com",
  port: 6379,
  username: "default",
  password,
  tls: { ca: readFileSync("ca-bundle.pem") },
  connectTimeout: 2000,
  commandTimeout: 2000,
});
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(bundle)
rdb := redis.NewClient(&redis.Options{
	Addr:         "cache-hylq6kelvuwvn2ks4yqhp4jaqu.us-west-1.ezghcompute.com:6379",
	Username:     "default",
	Password:     password,
	TLSConfig:    &tls.Config{RootCAs: pool, ServerName: "cache-hylq6kelvuwvn2ks4yqhp4jaqu.us-west-1.ezghcompute.com"},
	DialTimeout:  2 * time.Second,
	ReadTimeout:  2 * time.Second,
	WriteTimeout: 2 * time.Second,
})

Memcached clients connect with a username and password over the binary protocol (SASL): with TLS on port 11211, or without TLS on port 11212 while the cache’s plaintextPort is on. Turn it off when every client uses TLS:

ezgh caches update cch_4k2m9x0a7qpe --plaintext-port=false

Timeouts

After a failover or maintenance, the endpoint points somewhere new. An open connection doesn’t follow it: it ends only when it fails or times out, however short the DNS TTL is. Set a connect timeout and a read timeout of a few seconds in every client, and let the client reconnect to the endpoint when a connection fails. Pooled connections should be tested or replaced when they fail.

DNS

The endpoint is a DNS name for a private address in your VPC. Its record has a 5-second TTL. Set clients to cache it for no longer:

  • Java: a JVM caches names for 30 seconds by default. Set networkaddress.cache.ttl=5, in the JDK’s conf/security/java.security or with java.security.Security.setProperty("networkaddress.cache.ttl", "5") before the first connection.
  • Your own caches: a resolver cache on the instance, or a connection string with an address in place of the endpoint, has the same effect. Use the endpoint, and honor its TTL.

Instances resolve the endpoint through public DNS, so an app’s instance needs a path to the internet to look it up, for example through a Network Gateway or a public IPv4 address. The connection itself stays inside the VPC.

Rate limiting

A fixed-window limiter counts requests per key with INCR and starts the window with EXPIRE:

INCR ratelimit:api:203.0.113.7
EXPIRE ratelimit:api:203.0.113.7 60 NX

Allow the request while the count is at or below the limit. To do both in one round trip and atomically, run them as a script:

local n = redis.call("INCR", KEYS[1])
if n == 1 then redis.call("EXPIRE", KEYS[1], ARGV[1]) end
return n
limit = r.register_script(SCRIPT)
allowed = limit(keys=[f"ratelimit:api:{client_ip}"], args=[60]) <= 100

Users with the readWrite preset can run scripts (EVAL, EVALSHA, FUNCTION) and use pub/sub. Decide what your app does when the cache can’t be reached, such as allowing the request, and keep the client’s timeouts short so a slow answer doesn’t hold requests up.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close