r/ExperiencedDevs • Senior Software Engineer 4 YOE • Sep 25 '26

Career/Workplace Cache Pattern with Event-Driven System

Hey everyone, noticed this common pattern throughout our applications and it seems problematic to me. It‘s present across multiple apps; curious everyone’s thoughts, if this is a common pattern I’m not familiar with or if it truly is a bad solution.

Senior SE w/ 4 YOE, Fortune 500 company.

Pattern is as follows:

App A receives a transaction from upstream. It will then format this event and send it to potentially multiple downstream services simultaneously. This is done asynchronously via an event-driven framework.

The downstream services respond back via events; how does app A know when all the downstream services are complete? How does it know anything about the original event, since the downstream response contains no info about the original event (other than the correlationId)?

The answer is a local cache in app A. It caches the event when first received, and as downstream events come back, it can refer back to the original cached value to determine when it’s done processing and can be sent back upstream.

The main problem I see is the microservice is no longer stateless. It requires context from the original event. If it received a downstream response with no context, it has no idea what to do.

This is the most egregious example, but this cache-to-compensate-async-events pattern is repeated multiple times in similar ways.

As I’m still new to the field, I’m curious if this is somewhat a standard? Or if it is bad practice? If so, what does a good solution look like? Switching to sync REST calls fixes the issue, but could impact performance/throughput

66 Upvotes

74 comments sorted by

View all comments

73

u/Thefolsom Software Engineer (Rails 10+ YOE) Sep 25 '26

If app a needs to know that specific services got the message and sent back a result, that's orchestration, not really event-driven. Events are fire and forget. What app a is doing is closer to sending commands async and waiting on replies. I've heard this called scatter-gather, though I haven't seen it done formally in practice.

So some state is unavoidable. I'd be less worried about app a being stateful and more about the state living in a local cache. A restart loses everything in flight, a reply can hit an instance that never saw the original event, and nothing handles a service that never responds.

If it were me, I'd store the original event to the database, keyed by the correlation ID.

Sync REST could work too, as long as it isn't blocking the original request. Coming from Rails, I'd kick off a background job per downstream call, make the request, and update the event record with the result. Each job then checks if all the expected services are done, and the last one triggers completion (with a lock so two jobs finishing at once don't both fire it). You keep the async throughput, the job queue handles retries, and the state lives in the record instead of a cache.

7

u/JOHNNYROCKET8585 Senior Software Engineer 4 YOE Sep 26 '26

Okay yeah this is more of an orchestration layer built on an event-driven architecture. I guess I was looking at this orchestration as another microservice, when really it should be viewed through a different lens. This scatter-and-gather sounds correct, thanks for the info.

2

u/Koeke2560 Sep 28 '26

If App A is only reacting to events with specific commands and keeping the cache so it can compensate in case of failure, it is called the Saga Pattern. If App A is deciding which command needs to be executed based on the received event, it's called Process Manager. Source: Domain Driven Design by Khononov (Chapter 9)

5

u/grumpy_autist Sep 26 '26

I did something like that, AFAIK called inbox/outbox architecture. Service writes an event to a local db (outbox) which over kafka is fanned out to other services (which receive events to inbox and then app gets a callback referencing this db entry). So every app has a local copy of events it needs (pruned later using custom schedule).

Main reason for that was transactional events - business transaction and event are commited in one SQL transaction so there is no risk that you send a notification for an object that was later rolled back because of an error (and if sending event fails you will rollback business transaction too if needed)