paint-brush
What is Eventual Consistency and How Do You Deal with It?by@olvrng
520 reads
520 reads

What is Eventual Consistency and How Do You Deal with It?

by Oliver Nguyen
Oliver Nguyen HackerNoon profile picture

Oliver Nguyen

@olvrng

I’m a software maker working mostly in Go and JavaScript....

February 22nd, 2022
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Eventually consistency is a fancy name of doing something and only expecting the changed state after a while. When querying for a list of message templates from WhatsApp, we’ll cache the response for use later. It’s because message templates are not something that changes so frequently. Just cache it to reduce unnecessary API calls. After creating a template, we won’t see it for a while if we call the listing API. That's because of, you guessed it, eventually consistency.

Company Mentioned

Mention Thumbnail
Few
featured image - What is Eventual Consistency and How Do You Deal with It?
1x
Read by Dr. One voice-avatar

Listen to this story

Oliver Nguyen HackerNoon profile picture
Oliver Nguyen

Oliver Nguyen

@olvrng

I’m a software maker working mostly in Go and JavaScript. Share knowledge and thoughts during my journey.

About @olvrng
LEARN MORE ABOUT @OLVRNG'S
EXPERTISE AND PLACE ON THE INTERNET.

What Is Eventual Consistency?

Eventual consistency is a fancy name of doing something and only expecting the changed state after a while. And there is a guarantee that you will eventually get the expected state. Hence, consistency.


But that won’t work well with caching. And I had to deal with that recently when working with WhatsApp API.


When querying for a list of message templates from WhatsApp, we’ll cache the response for use later. It’s because message templates are not something that changes so frequently. Just cache it to reduce unnecessary API calls.


After creating a template, we won’t see it for a while if we call the listing API. That’s because of, you guessed it, eventual consistency.


Things will get worse because these responded message templates will be cached for a few minutes. Therefore, we won’t see the newly created template soon, even though it may be available on the WhatsApp API earlier.

How to Deal with Eventual Consistency

First, let’s simply ignore the cache for, like 1 minute, after creating a new template. That would be enough for the WhatsApp API to return the newly created template.


The next improvement would be reducing the cache expiration time to, like 5s, in the first 1 minute. Let’s call that the catching-up duration. After that, use the original expiration time.


Another improvement would be verifying that the new template is available in the response and starting caching at that point. That would be a bit more complex, as we need to store the template id and more code to extract it from the response.


Finally, while that new template is not available, we can still construct it and manually inject it into our own response.


Recap

In the end, that works well. We can have both cache and eventual consistency playing together, minimizing the waiting time and the number of requests to an external platform! 🎉

L O A D I N G
. . . comments & more!

About Author

Oliver Nguyen HackerNoon profile picture
Oliver Nguyen@olvrng
I’m a software maker working mostly in Go and JavaScript. Share knowledge and thoughts during my journey.

TOPICS

THIS ARTICLE WAS FEATURED IN...

Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Also published here
Buzzsumo
Newsbreak
Moomoo
Learnrepo
Coffee-web
Olivernguyen
Leftic
Newsbreak
Learnrepo
Hashnode
X REMOVE AD