Hi @vs5
I came across similar issue during a POC while working on the caching behavior of Experience Fragments in a headless AEM setup [version was AEM6.5.21~]. The weird and strange part was, just like in your case, it didn’t happen consistently—sometimes the .model.json was complete, and sometimes certain attributes were missing, even though cache invalidation was working.
duringmy POC across AEM publish, dispatcher, and model serialization, here’s what I found and how we fixed it:
Possible Fixes
1. Race Condition in Cache Invalidation Acrosss Dispatchers
Since you mentioned some requests serve bad cache while others serve the correct one, it sounds like a timing issue between publish and dispatcher cache invalidation. Some dispatcher nodes might be serving stale cache while others have already updated.
solution : Try ading a short delay (1-2 sec) before cache invalidation to ensure the latest .model.json is fully available before dispatchers purge old cache.
2. Incomplete Model JSON Serialization in Publish Instance
Soluton : If the attributes are always present on Author but sometimes missing on Publish, the issue could be with how AEM serializes the model in a wrapped request. If modelFactory.getModelFromWrappedRequest() is used, it may not fully resolve nested models when caching is involved.
@Inject
@Named("customAttribute")
@JsonProperty("customAttribute")
private String customAttribute;This forces attributes to be always included, even when wrapped via modelFactory
3. Stale Dispatcher Cache Serving Partial JSON
Even though you already have cache invalidation in place, I’d suggest double-checking if all dispatchers purge at the same time.
Try manually purging the cached .model.json across all dispatchers and see if that eliminates the issue permanently. You can also enforce this in dispatcher.any:
/0001 { /glob "*.model.json" /type "allow" }ensures any request for model.json forces invalidation and prevents inconsistencies.