Skip to content

Apollo Store lock contention #120

Closed
Closed
@ynkm169

Description

@ynkm169

Use case

Hello,

Did some systracing. Launched 50 duplicate API calls, result shows there are around 60 RxCachedThreadS and 60 DefaultDispatch threads all contending for monitor lock.

This is only theoretical tests. However, our app at peak (home feed) can have 200+ threads running. Even if there will be only dozen of remote API calls (not counting other Apollo store cache reads), if we migrate all APIs to GraphQL, this could present a performance penalty.

Also in RXJava world, it is not uncommon for dev to call an API to retrieve a list of items, and use flatmap to make a seperate API call for each item in the list, so 50 API calls is not so uncommon and it gets worse as user scroll quickly on a list.

Describe the solution you'd like

Question
Is it possible to optimize the locking a bit more? Possibly even sacrificing consistency for performance (or having it as a configurable option)?
MemoryCache has a monitor lock: CacheLock. (This theoretically should be very fast since synchronized block guards just a few LinkedHashMap calls)
DefaultApolloStore has a ReentrantReadWriteLock. (A single writer will block all readers, a single reader will block all writers)


Java/Kotlin method tracing
DefaultDispatch: It seems the ReentrantReadWriteLock does a couple of things such as Invoking JsonWriter, not just directly save data to a LinkedHashMap.
Screen Shot 2023-01-04 at 10 48 21 PM

RxCachedThreadS: It's harder to understand the Flame Chart and why the thread exist.
Screen Shot 2023-01-04 at 10 52 07 PM


systrace
Screen Shot 2023-01-04 at 9 33 42 PM
Screen Shot 2023-01-04 at 9 32 07 PM
Screen Shot 2023-01-04 at 9 32 01 PM
Screen Shot 2023-01-04 at 9 31 55 PM
Screen Shot 2023-01-04 at 9 31 49 PM
Screen Shot 2023-01-04 at 9 31 44 PM
Screen Shot 2023-01-04 at 9 31 38 PM
Screen Shot 2023-01-04 at 9 31 29 PM

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions