Capstone: Concurrent Multi-Format Report Generator
Track
Design-only
Brief
A finance analytics product generates the same approved report definition as PDF, CSV, and HTML for hundreds of scheduled and interactive requests. The current workers share mutable report templates, so one request occasionally inherits another request’s locale and filters. Repeated reports also rerun identical queries, and traffic spikes leave many workers rendering the same expensive result. Design a generation pipeline with one controlled lifecycle, interchangeable output formats, request-isolated definitions, and safe caching.
Constraints
- The service runs eight worker threads per instance and three instances behind an existing job queue.
- Interactive previews must return a cache hit within 300 ms at p95; cache misses may take up to 20 seconds.
- A three-person team must add XML next quarter without changing query acquisition, caching, or delivery steps.
- Reports on the same
asOftimestamp must use one immutable source snapshot, even when source rows change during rendering. - The shared cache has a 2 GB memory budget, and a cache entry must never contain a partial or failed render.
Required Patterns
- Template Method — WHY: cache lookup, snapshot acquisition, rendering, storage, and delivery form a fixed lifecycle whose invariant order should not be reimplemented by each format.
- Strategy — WHY: PDF, CSV, HTML, and future XML encoders are peer rendering algorithms selected per request while the lifecycle stays unchanged.
- Prototype — WHY: cloning an approved report definition gives each job an isolated configuration without rebuilding complex defaults or sharing mutable filters across workers.
Solution Sketch
Illustrative pseudocode — not runnable.
approvedDefinition = ReportDefinition(
name = "monthly-close", version = 7,
querySpec = validatedQuerySpec, defaults = sharedImmutableDefaults
)
onQueueMessage(message):
definition = approvedDefinition.clone()
definition.setRequestValues(message.filters, message.locale, message.timezone)
definition.freeze() # this worker alone owns the clone
renderer = RendererRegistry.forFormat(message.format)
ReportPipeline(renderer, cache, snapshotStore).run(definition, message.asOf)
ReportPipeline.run(definition, asOf):
key = sha256(tenantId, name, version, normalizedFilters,
locale, timezone, format, asOf)
hit = cache.getCompleteEntry(key)
if hit exists: deliver(hit); return CacheHit
return singleFlight(key): # one render; peers await its result
recheck cache after acquiring key leadership
snapshot = snapshotStore.openConsistentSnapshot(asOf)
data = query(snapshot, definition.querySpec)
artifact = renderer.render(data, definition) # Strategy hook
completed = artifact.sealWithChecksum()
cache.putIfWithinBudget(key, completed, ttlFor(format))
deliver(completed)
close(snapshot)
on error: close(snapshot); publish no cache entry; propagate failure
workers never mutate approvedDefinition, renderer instances, or cache values
Rubric
- Fixed lifecycle: Passes if the template explicitly orders lookup, single-flight control, snapshot acquisition, rendering, sealing, caching, and delivery; fails if format code can skip or reorder those steps.
- Format substitution: Passes if one registry chooses a stateless rendering strategy and adding XML requires only a new strategy plus registration.
- Request isolation: Passes if every job clones the approved definition, applies request values only to that clone, and freezes it before concurrent use.
- Cache correctness: Passes if the key includes tenant, definition version, normalized filters, locale, timezone, format, and
asOf, and only sealed successful artifacts are cached. - Snapshot consistency: Passes if all query and render inputs for a job come from the same
asOfsnapshot and the sketch closes that snapshot on success and failure. - Concurrency and budget: Passes if one leader renders each missing key, followers wait or receive the completed artifact, and the design names an eviction or admission rule for the 2 GB limit.