InterviewKata
Software architecture · Level 3: Architecture styles · Module 15 of 46

Space-based architecture and caching

Almost every architecture eventually hits the same wall: the database. You can add web servers for a burst, but they all queue on one database. Space-based architecture removes the database from the request path entirely: each processing unit keeps the data it needs in memory, copies changes to its peers, and writes to the database later, in the background. It's how ticket sales, auctions and trading platforms survive extreme, unpredictable bursts. It also relies on caching, so this module covers the four caching topologies and the collisions that replicated caches cause.

See it builtThe Architecture styles track builds this style for one bike-share app, with code you can run: 13 Space-based.

1. The idea

A festival with many bars. Instead of every bartender walking to one central storeroom for each drink (the database), each bar keeps its own fridge stocked, and the bars phone each other when they open the last case of something. A van restocks the storeroom's records overnight. When the crowd doubles, you open more bars, each with a stocked fridge. The risk: two bars sell "the last bottle" at the same moment before their phone call connects.

The four caching topologies

TopologyHow it worksGood forWatch out for
Single in-memoryEach instance caches on its own; nothing is sharedOne instance, or data that never changesInstances disagree once data changes
DistributedA separate cache cluster (such as Redis) that every instance calls over the networkLarge data, frequent updates, many instancesA network hop per read; the cache cluster is a dependency
ReplicatedEvery instance holds a full copy in its own memory; changes are copied to the othersFast reads of small to medium data, few updatesMemory per instance; collisions on concurrent updates
Near-cache hybridA small local cache of the most-used items in front of a distributed cacheHot items read very oftenThe local layer can be briefly stale

How it rates

CharacteristicSpace-basedEvent-driven
Partitioningtechnicaltechnical
Quanta1 to many1 to many
Cost★★★★★★★★★★
Simplicity★★★★★★★★★★
Scalability★★★★★★★★★★
Elasticity★★★★★★★★★★
Fault tolerance★★★★★★★★★★
Evolvability★★★★★★★★★★
Deployability★★★★★★★★★★
Testability★★★★★★★★★★
Performance★★★★★★★★★★

Our qualitative ratings, 1 to 5 stars, more is better (for cost, more stars = cheaper). They compare styles in general; your context can move any of them.

2. How it works

A burst without a database bottleneck

A data collision in a replicated cache

Replication takes time. If two units update the same item within that window, one update can overwrite the other. Watch two units sell seats for the same concert.

3. Try it yourself

How often will replicated caches collide?

Collisions grow with the number of units, the update rate and the replication delay, and shrink with the number of items being updated. Adjust them and see the expected collisions per hour.

4
100
31,623
50 ms

Expected collisions per second ≈ N × (N − 1) × u² × delay ÷ items, for N units each making u updates a second spread evenly over the items. We checked the formula against a simulation; real updates are rarely even (popular items collide far more), so treat it as a lower bound.

4. Trade-offs

Elasticity vs consistency

Taking the database off the request path gives near-unlimited elasticity and speed. The database is now eventually consistent with memory, and concurrent updates in different units can collide.

Memory vs data size

Each unit must hold the data it needs in memory. That suits a bounded working set (this week's events, today's orders), not a decade of history.

Replicated vs distributed cache

Replicated caches read at memory speed but cost memory in every unit and collide on updates. Distributed caches hold one copy and avoid collisions, at the price of a network hop per read.

Power vs complexity

Space-based is one of the most complex styles to build and test: middleware, data pumps, readers and writers, and collision handling. Use it only where bursts truly demand it.

5. In real projects

Where it fits: concert and sports ticketing, online auctions, flash sales, betting and trading platforms: huge, sudden concurrency on a relatively small, hot data set. In-memory data grids such as Hazelcast, Apache Ignite and Oracle Coherence provide the grid pieces.

Choosing a caching topology comes down to four questions: how big is the data (replicated needs it to fit in every unit), how often does it change (frequent updates favour distributed), how many instances (more instances mean more collisions for replicated), and how fresh must reads be.

Caching and CAP. A replicated cache chooses availability: every unit answers from its own copy, even if it's briefly stale. A distributed cache with one copy per item is closer to consistency: everyone sees the same value, but a cache-node outage makes those items unavailable.

Say it in the interview

Two-sentence answers
  • "What is space-based architecture?" Processing units that keep their data in replicated in-memory grids, with the database updated asynchronously through data pumps. Removing the database from the request path gives extreme elasticity for bursts.
  • "Replicated or distributed cache?" Replicated for small, mostly-read data where read speed matters; distributed for large or frequently updated data, or many instances. Updates to replicated caches can collide.
  • "How do you handle the last-seat problem?" Partition so each seat or event is owned by one unit, or route updates for a key to its owner; otherwise accept and detect collisions. Pure replication can't prevent them.
Red flags interviewers notice
  • A replicated cache for data updated thousands of times a second.
  • Space-based architecture for a steady, ordinary workload.
  • No plan for what happens when the data pump's queue backs up.

6. Remember it

Memory hook: "A fridge at every bar"Each unit serves from its own memory and tells the others what changed; the storeroom catches up later.
How this hook works: what "a fridge at every bar" means

What the slogan means, phrase by phrase

"A fridge"
An in-memory data grid inside each processing unit.
"at every bar"
Every unit has one, so opening more bars (units) adds capacity without queuing at a storeroom.

Why this picture fits: it explains the elasticity (no trip to the storeroom) and the risk (two bars selling the last bottle before they've phoned each other).

The story → the concept

Festival barsSpace-based architecture
A bar with its own fridgeProcessing unit with an in-memory grid
Bars phoning each otherData grid replication
The overnight van to the storeroomData pump and data writer
Two bars selling the last bottleA data collision

The trap it saves you from

Scaling application servers for a burst while every one of them waits on the same database.

7. Common mistakes

Ignoring collisionsReplicated caches with frequent updates to popular items will overwrite each other. Measure the rate, partition ownership, or use a distributed cache.
Unbounded data in memorySpace-based needs a bounded working set. Archive or evict everything else.
Forgetting the pumpIf the database is down for an hour, the pump's queue must hold an hour of changes. Size and monitor it.
Using the style for its own sakeIts complexity is only justified by extreme or unpredictable bursts.

8. Design exercise (30 minutes)

  1. Size the working set (10 min). For a ticketing site, estimate the data one event's sale needs in memory: seats, prices, holds. Does it fit in one unit?
  2. Pick a topology (10 min). For seat availability, prices and customer profiles, choose a caching topology each, and say why.
  3. Handle the last seat (10 min). Design how two units avoid selling the same seat: ownership by partition, routing by key, or detection and repair.

9. Self-check quiz

Why does space-based architecture handle bursts so well?

Units serve from memory and the database is updated asynchronously.

Doubling the number of units with a replicated cache (from 4 to 8) changes collisions how?

N × (N − 1): 4 × 3 = 12 vs 8 × 7 = 56, about 4.7 times. Collisions grow roughly with the square of the number of units.

Large data, updated often, read by 30 instances. Which caching topology?

One copy avoids collisions and memory per instance, at the cost of a network hop.

What does a data pump do?

Pumps and writers keep the database eventually consistent with memory.

In CAP terms, what does a replicated cache favour?

Each unit answers from its own copy even while replication catches up.

10. Go deeper

ResourceTimeWhy
Fundamentals of Software Architecture, 2nd edition (Richards and Ford)bookThe space-based chapter: processing units, virtualised middleware, data pumps and collisions.
Hazelcast: choosing a deployment option15 minEmbedded vs client-server in-memory grids in a real product.
Two Hard Things (Martin Fowler)5 minCache invalidation, and why it's famously hard.