Refresh cache after update from remote server?

I have an instantsearch UI, an algolia index, and a database-backed API which is the single source of truth for the searchable records. The UI can update a record, which sends a PATCH request to the server. When the server successfully updates a record, it will then send the updated record to algolia for indexing.

The problem I’m having is reflecting the updated record in the search UI as soon as algolia updates the record. I’ve tried refreshing the cache as soon as the user receives a response from the API that the PATCH request succeeded, but it seems to just get stale data again because algolia hasn’t yet received the updated record from the API.

I’ve looked into waitTask, but the instantsearch UI isn’t updating algolia directly so there’s no client-originating operation for it to wait for. Does algolia have any recommendations or guidance for how to handle this type of situation?

Hi @will_too,

Updating records with Algolia is indeed done asynchronously.

Could you tell us a bit more on how you’re leveraging waitTask method?

You can find more information in our documentation:

Keep us posted!

Best,

I’m not using waitTask right now; as I mentioned, I’m not calling an Algolia update from the InstantSearch UI (that’s coming from the back-end API that stores the full records) so there’s nothing to call waitTask on as far as I’m aware.

The basic loop:

  • InstantSearch UI loads record from Algolia
  • User changes a record and hits save. The UI then sends an async HTTP request to the API server (a database connector and python algoliasearch) and awaits a response.
  • The API server updates the database record and batches a background job to update the algolia record. As soon as the job is batched, it sends a response to the InstantSearch UI
  • The InstantSearch UI receives the response from the API Server and sets the refresh prop for InstantSearch to true

And nothing changes on the UI because, as far as I can tell, Algolia hasn’t yet received the updated record from the API server.

Is there a pattern that I can follow to wait for Algolia to get that update message from the API server before setting refresh to false? Something like a record version key I can monitor and only cache results again once that key has changed, maybe?

Hi @will_too!

I might seem like I’m repeating what my colleague Valentin said but are you using waitTask on your backend? If you do so, your server will wait for the operation to be completely handled by the engine before sending back the updated data so you shouldn’t have any problems with async update.

Maybe you could share with us a code sandbox or code samples so we can better understand your back and front end implementation and find potential issues?

Have a good day,

Not an ideal solution for me, but I suppose that will work. Thanks for looking into this.

You’re welcome! Don’t hesitate if you still struggle after trying this solution