Modern architecture patterns: event-driven, serverless and reactive
Study notes on three modern architecture patterns — Event-driven, Serverless and Reactive — their definitions, diagrams to draw, key rules (including Little's law), syntax/formulas to remember, worked exam-style examples, common mistakes and likely exam questions.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Modern cloud-scale applications must handle high concurrency, variable load and distributed teams. Event-driven, serverless and reactive architectures are used by engineers to build scalable, cost-efficient, loosely-coupled systems that respond to events and user demand with low operational overhead.
Key ideas
Start from basic terms.
- Event: a record that something happened. Example: "order-placed" with data (order id, timestamp). An event is immutable after creation.
- Event-driven architecture (EDA): an architectural style where components communicate by producing and consuming events rather than calling each other directly. Assumption: components can be decoupled by an event broker or message channel.
- Event producer (publisher): a component that creates events and sends them to a channel or broker.
- Event consumer (subscriber): a component that receives and processes events from a channel or broker.
- Event broker / message bus: middleware that accepts events from producers and delivers them to one or many consumers. It can be simple (topic-based) or complex (with partitions, persistence, ordering guarantees).
- Serverless: a cloud execution model where the cloud provider runs the server, dynamically manages resource allocation, and charges based on usage. Serverless functions are short-lived, stateless functions invoked by events (HTTP calls, queue messages, timers). Assumption: the provider handles OS, runtime, scaling; you manage code and configuration.
- Cold start: the delay when a serverless platform starts a new execution container to run a function the first time or after idle period. Important for latency-sensitive use-cases.
- Reactive systems: systems designed to be responsive, resilient, elastic and message-driven (this is the Reactive Manifesto). Key ideas: non-blocking communication, asynchronous message passing, and backpressure to control flow.
- Backpressure: a flow-control mechanism where a slower consumer signals the producer to reduce the rate of messages or data. Assumption: both sides agree to a protocol to signal capacity (credits, window sizes, explicit requests).
- Idempotency: a property of an operation where performing it multiple times has the same effect as performing it once. In EDA and serverless you must design handlers to be idempotent because events may be delivered more than once.
Exact statements and derivations universities ask for:
- Little's law (used in reactive / performance reasoning)
- Statement: For a stable system in steady state, the long-term average number of items in the system L equals the arrival rate λ multiplied by the average time W an item spends in the system: L = λ · W.
- Assumptions: system in steady state (averages stable), arrivals counted and departures counted over same time window, average values exist and are finite.
- Derivation (standard): Observe a time window T. Let N(T) be number of arrivals in T. Let S_i be time spent in system by i-th arrival. Total time spent by all arrivals = Σ_i S_i. Average number in system ≈ (Σ_i S_i) / T. Arrival rate λ ≈ N(T) / T. Average time per arrival W ≈ (Σ_i S_i) / N(T). Substitute to get L = λ·W.
- Event delivery semantics definitions (exact):
- At-most-once: each event is delivered zero or one time. No retries; risk of lost events.
- At-least-once: each event is delivered one or more times until acknowledged; duplicates possible; requires idempotent handlers.
- Exactly-once (end-to-end): each event is processed exactly once in effect (most expensive; often implemented with deduplication and transactional sinks). Achieving true exactly-once requires coordination and often storage of processed event ids.
- Diagram to draw for a simple event-driven system (exam instruction):
- Draw three boxes left-to-right labelled "Producer(s)" (one or more), "Event Broker / Topic" (middle), and "Consumer(s)" (one or more). Draw arrows from Producer(s) into the Broker, and arrows from Broker to Consumer(s). Label the arrows "events". Inside the Broker box, also draw a small disk labelled "event log" to indicate persistence when present. Add optional arrows from Consumers to an external "Datastore" box if consumers write results.
- Reactive Manifesto exact four traits (short university answer):
- Systems should be: Responsive, Resilient, Elastic, and Message-driven. Define each briefly: Responsive = quick and consistent response times; Resilient = remain responsive under failure; Elastic = maintain responsiveness under varying load by scaling; Message-driven = rely on asynchronous message-passing for loose coupling and isolation.
Formulas
(For programming/architecture, list syntax and simple performance formulas.)
Little's law:
L = λ · W- L = average number of items in system (dimensionless count)
- λ = arrival rate (items per second, s⁻¹)
- W = average time in system (seconds, s)
- Applies when system is in steady state and averages exist.
Serverless cost (symbolic):
TotalCost = Σ_i (Invocations_i × (Duration_i_seconds × Memory_i_MB) × Price_per_MB_s) + Σ_i (Invocations_i × Price_per_invocation)- Invocations_i = number of times function variant i was invoked (count)
- Duration_i_seconds = average billed execution time per invocation (s)
- Memory_i_MB = memory configuration in MB
- Price_per_MB_s = provider price per MB-second (currency per MB·s)
- Price_per_invocation = provider per-invocation charge (currency per invocation)
- Applies to provider billing models that charge per memory-second and per invocation (typical serverless models).
Flow backlog change (over short interval Δt):
ΔBacklog = (ArrivalRate - ServiceRate) × Δt- Positive ΔBacklog means queue grows. Units: items.
Common syntax patterns to remember (pseudocode):
- Event handler signature:
function onEvent(event) { /* idempotent processing */ } - Subscribe to topic:
broker.subscribe('topic', onEvent) - Publish event:
broker.publish('topic', { type: 'order-placed', data: {...} }) - Serverless handler (HTTP style):
exports.handler = async (event, context) => { return { statusCode: 200, body: 'OK' } }(node-style pseudocode)
- Event handler signature:
Worked examples
- Event-driven design (model exam answer): Question: Design an event-driven order processing flow where placing an order triggers payment, inventory update and notification. Include idempotency and failure handling. Answer (step-by-step):
- Components: Order Service (producer), Event Broker (topic
orders), Payment Service (consumer), Inventory Service (consumer), Notification Service (consumer), Dead-letter Queue (DLQ). - Sequence: Order Service publishes event
{ type: 'order.placed', orderId, items, amount }to topicorders. - Consumers subscribe to
orders. Each consumer must be idempotent: storeprocessedEventIdsand ignore events with same id if seen before. This satisfies at-least-once delivery. - Failure handling: if consumer fails to process an event after N retries, send the event to DLQ for manual inspection. Draw the diagram as instructed earlier and label a DLQ connected from broker's retry/failure arrow.
- Consistency: use eventual consistency: Payment and Inventory operate independently; if Payment succeeds and Inventory fails, compensate by issuing refund or restock through compensating events (e.g.,
order.refund-requested). - Notes: if ordering between Payment and Inventory is required, partition topic by orderId so messages for same order go to same partition and maintain ordering.
- Serverless cost numerical example (exam-style): Question: A function is configured with 256 MB memory. It runs 1,000,000 times in a month. Average billed duration per invocation is 200 ms. Provider charges $0.00001667 per GB-second and $0.20 per 1,000,000 invocations. Compute monthly cost (show steps). Assume 1 GB = 1024 MB for billing converted to GB-seconds. Answer (steps):
- Memory in GB = 256 MB / 1024 = 0.25 GB.
- Duration in seconds = 200 ms = 0.2 s.
- GB-seconds per invocation = 0.25 GB × 0.2 s = 0.05 GB·s.
- Compute cost per invocation for compute = 0.05 × $0.00001667 = $0.0000008335.
- For 1,000,000 invocations: compute cost = 1,000,000 × $0.0000008335 = $0.8335.
- Invocation charges = $0.20 (per 1,000,000).
- Total monthly cost = $0.8335 + $0.20 = $1.0335.
- (Note: I used price $0.00001667 per GB-second and $0.20 per million invocations — verify these numbers for your cloud provider and date.)
- Reactive pipeline trace (small input): Given stream: [1, 2, 3, 4] Operators: map(x => x*2), filter(x => x > 4), subscribe(print). Trace:
- Source emits 1 -> map -> 2 -> filter -> 2 > 4? no -> dropped.
- Emit 2 -> map -> 4 -> filter -> 4 > 4? no -> dropped.
- Emit 3 -> map -> 6 -> filter -> 6 > 4? yes -> subscriber prints 6.
- Emit 4 -> map -> 8 -> filter -> 8 > 4? yes -> subscriber prints 8. Output sequence printed: 6, 8.
Common mistakes
- Assuming serverless means zero latency cold starts — cold starts can introduce significant latency for some runtimes.
- Not planning for ordering: assuming events across different partitions/topics are ordered when they may not be.
- Misapplying Little's law when the system is not in steady state.
- Treating reactive programming as simply "async functions" instead of understanding flow-control and backpressure.
For your exam
Types of questions you will get:
- Definitions: Define Event-driven architecture, Serverless and Reactive systems.
- Short notes: Compare EDA vs synchronous RPC; advantages and disadvantages of serverless.
- Derivations: Derive Little's law and state its assumptions.
- Diagrams: Draw event flow with producers, broker, consumers and DLQ; label arrows and event log.
- Design/long questions: Design an order-processing system using event-driven architecture, explain idempotency, retries, and failure handling.
- Numericals: Compute serverless cost given memory, duration and invocation counts (show calculations).
- Small programs/traces: Trace a reactive stream with map/filter/reduce operators.
What full-marks answers include:
- Clear definitions with bold terms, list of assumptions, neat labelled diagrams, explanation of delivery semantics, discussion of idempotency and DLQs, correct use of Little's law with units, and step-by-step numeric calculation with units.
Quick check
- Define in one sentence: Event-driven architecture.
- What is backpressure and why is it needed in reactive systems?
- Using the serverless cost model in the notes, compute cost for 100,000 invocations, 128 MB memory, 500 ms average duration, price $0.00001667 per GB-s and $0.20 per 1,000,000 invocations. Show the final numeric answer.
Answers: 1) An architecture where components communicate by producing and consuming immutable events via channels or brokers, decoupling producers and consumers. 2) Backpressure is a flow-control technique where a slow consumer signals a producer to reduce the send rate, preventing unbounded queues and system overload. 3) Memory=128/1024=0.125 GB; duration=0.5 s; GB·s per invocation=0.125×0.5=0.0625; compute cost per invocation=0.0625×0.00001667=$0.0000010419; total compute=100,000×0.0000010419=$0.10419; invocation charge=100,000/1,000,000×$0.20=$0.02; total = $0.12419.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?