8 min read

Postgres keeps the record. A second store keeps the flood.

Postgres keeps the order and the invoice. ElastiCache serves a read the app fetches on every request. DynamoDB takes a simple write that happens constantly, such as a counter or an event.

SaaS production readinessAWSPostgreSQLDynamoDBElastiCacheArchitecture

I do not add ElastiCache and DynamoDB just because Postgres looks slow. Postgres is doing three jobs at once. It keeps the order and the invoice. It serves a read the app fetches on every request. It also takes simple writes that happen constantly, such as a counter or an event. I move one of those jobs only when I can point at which one is slow, and I know what the product does if the new store loses the value or saves it twice.

Name the job before you name the service

A startup reaches for Redis, ElastiCache Serverless, and DynamoDB in the same week it hears the word scale. That is three consistency problems and three new lines on the bill. The scale path already says when a second data store is allowed: a specific query or tenant forces it. This is how I choose the store.

The three jobs

  1. The record. The order, the invoice, the permission. It commits or it rolls back with the rows it depends on.
  2. The flood. Counters, usage meters, click and event ingest, idempotency keys. High rate, one key, no join.
  3. The repeated read. The session, the entitlement, the catalog row every request fetches again.

What I ask before I open the console

  1. Is the user waiting on this write in the same second?
  2. Does it have to commit in the same transaction as the record?
  3. What is the key, and who else writes that key?
  4. If the value disappears, or arrives twice, what does the product do?

If those four answers are still vague, the work stays in Postgres.

Keep the record in Postgres

The record stays where the constraints are. Foreign keys, a unique invoice, and a rollback that covers the whole sale are the reason you picked Postgres. Moving that row so a CPU graph looks calmer throws away the property you are paying for.

Make the write honest first

A slow write is often a shape problem. I fix the shape before I introduce a second store.

  1. Index the predicate the write and the lock actually use. A sequential scan on tenant_id looks like a capacity crisis.
  2. Shorten the transaction. Do not hold a row lock across an HTTP call.
  3. Put a queue in front of any write the user does not need in the same second. That seam is already on the scale path.
  4. Watch lock time and connection slots, not only CPU. Postgres can be out of connections while the CPU graph is quiet.

If the statement is fast once the shape is honest, stop. You do not have a second-store problem yet.

ElastiCache Serverless takes what you can rebuild

The repeated read is the first thing I will move. ElastiCache Serverless runs Valkey or Redis OSS without a node size you have to guess. It is a cache. I treat every value in it as disposable.

The key, the TTL, and the miss

  1. Put the tenant id in the key. A key of user:42 is how one tenant's session becomes another tenant's response. The same rule I use for tenant storage boundaries applies to a cache key.
  2. Set a TTL. A key with no expiry becomes a second database you cannot rebuild.
  3. Define the miss. On a miss, read Postgres, then fill the cache. If the cache comes back empty, Postgres has to serve that read alone.
  4. Jitter the TTL on a hot key so a thousand clients do not miss in the same second and stampede the database.

What this store costs when the hit rate is low

Serverless cache bills on ElastiCache Processing Units and data stored. You never picked an instance class, and you can still overpay. A low hit rate is the expensive shape: you pay the cache and you still pay Postgres for the query. I want the hit rate and the Postgres read rate side by side before I call it a win. That is the same consumption rule I use when a team is buying capacity it has not earned.

What I will not put there

The order. A job queue. A counter you cannot recompute. ElastiCache will evict, expire, or come back empty. If the product cannot survive that, the value does not belong in the cache. Write-behind from the cache back to Postgres is how a flush becomes data loss.

DynamoDB takes the flood with an honest key

The durable write that leaves Postgres is the one with a key you can say out loud. Usage meters, event ingest, idempotency keys, per-tenant counters. DynamoDB partition key design is the whole design. The key is the access pattern. A join, a report across tenants, or an ad hoc filter means the write is still a Postgres write.

What an honest key looks like

  1. High cardinality. tenant_id alone is a hot partition for your largest customer. tenant_id plus a time bucket, or an event id, spreads the flood.
  2. One item is one fact. A counter is an UpdateItem with an atomic add. An event is a put of that event. Neither item is a row you plan to join later.
  3. Idempotent puts. A condition expression that rejects a duplicate idempotency key makes a second delivery a no-op.
  4. On-demand capacity until the rate is stable. You are absorbing a spike you can already measure, not reserving a forecast.

One transaction owns the fact

Some floods never lived in Postgres. The click, the meter tick, the webhook receipt can go to DynamoDB first. The request path does not also insert them into the relational database.

Some facts have to exist in Postgres and also be readable at high rate. The application does not write them to both stores. It inserts the record and an outbox row in the same Postgres transaction. A worker reads the outbox and projects the item into DynamoDB. The projection may lag. It is not the only copy. When the worker retries, the condition expression makes the put safe to repeat.

That split is the decision. The record has one owner. The flood is a different fact, or a projection of one.

Open the seam when a number says so

A second store without a trigger is a smaller version of a million-user bill.

The sequence I will write down

  1. Now. The Postgres write is indexed, the transaction is short, and anything that can wait sits on a queue.
  2. Next. ElastiCache Serverless in front of the read every session repeats, with a tenant in the key, a TTL, and a miss path you have tested by flushing the cache.
  3. Later. DynamoDB for the one write whose key is honest and whose duplicate or delay you have already defined.
  4. Much later. A read replica, when the remaining pain is read volume on the record itself.

The trigger to spend

Each step needs a number from production. Write p99, row-lock time, cache hit rate, DynamoDB throttle count. When the number shows up, we open the next store. Until then the extra service stays off the invoice. I put that sequence on the same written scale path as the rest of the seams.

Frequently asked questions

Should I add Redis so Postgres can handle more writes?

Add ElastiCache Serverless for a value you can rebuild or expire. A durable write does not get safer by landing in Redis first. If the write must survive a flush, it belongs in Postgres or in DynamoDB, and only one of those is the owner.

Is ElastiCache Serverless the same as a Redis cluster I size myself?

You skip the node size. You still own the key, the TTL, and the miss path. The bill follows processing units and storage. A cache that rarely hits is an extra bill in front of the same Postgres query.

When should DynamoDB absorb writes from Postgres?

When the write is a different fact: a counter, a meter, an event, an idempotency key. The key has to be the access pattern. A relational row that changed tables is still a relational row, now without foreign keys.

Can the application write the order to Postgres and DynamoDB together?

Two writes from the request can succeed in one store and fail in the other. Postgres commits the record and an outbox row. A worker projects DynamoDB. The projection can lag. It cannot be the only place the order exists.

What do I measure to prove the cache helped?

Cache hit rate, and the Postgres read rate on that query, before and after. A faster p99 can hide a miss path that still hits the database on every cold key.

What do I need to make the call?

The slow statement, the key you would use, and whether the user is waiting on the result. Those three facts tell you if the write stays, if it belongs in a cache, or if DynamoDB should take the flood. That is the core of an architecture review: which store owns the fact, and which stores stay off the diagram for now.

Comments

    Leave a comment