Why did subscribing twice trigger two HTTP requests in Angular?
Understand the idea, explain it in your own words and use the rubric to find your next learning gap.
The idea behind the question
HttpClient observables are cold: subscribing starts work. Sharing or caching can avoid repeated work, but requires decisions about subscribers, cancellation, invalidation and the lifetime of retained data.
Two view regions subscribe to the same unshared request observable and each starts a separate network request.
A practical way to learn it
Use Angular HTTP testing to observe two subscriptions, then implement a documented sharing policy and verify both duplicate suppression and explicit invalidation.
Open Angular project studioThe related tool opens separately; your interview answer stays here. Written practice is self-guided, without automatic code grading.
Build an explanation you understand
- State what the concept does in plain language.
- Apply it to the example and make your assumptions explicit.
- Explain a trade-off, failure case and how you would test it.
- Distinguish your actual experience from a proposed approach.
How would you explain it?
Single-question timers reset on refresh. Full mock sessions can resume from this browser.
Strengthen your explanation
Compare reasoning, not identical wording. Reference answers describe a possible approach; replace hypothetical statements with truthful details from your own work.
One way to answer
I would confirm the subscription paths before changing the API. If the result should be shared, I would define the desired cache lifetime and choose explicit sharing behavior appropriate to the installed RxJS version. I would not assume shareReplay automatically creates a correct permanent cache. I would consider reference counting, errors, completion and invalidation after a mutation or account change. Tests should count requests and cover late subscribers, destroyed views and refetching after invalidation. For one consumer, removing an unnecessary second subscription may be clearer than adding caching machinery.
What a strong explanation should cover
Tick only the points your own answer addresses. This is self-assessment, not AI scoring.
0 / 3 points self-reportedWatch out for
- Adding shareReplay everywhere without a logout or mutation invalidation policy.
Now go one level deeper
- When should a new subscriber trigger a new request?
- How would you keep user-specific cached data from crossing account changes?
Further reading
Further-reading links support learning; the reference is Sukoaa-authored, not an official employer answer key.