- [Narrator] With the circuit breaker in place,
service outages won't take down
our whole infrastructure anymore.
Still, our goal should be to keep
the user impact as low as possible.
And right now, we still get the somehow empty page
because we got no response from the service.
In this video, we will now add a simple local cache
that will return a cached result in case the service fails.
For that, let's open Speakers.js
on the Conference app, and there, at the top,
I have to bring in a few dependencies
that we will need later.
So add: const url equals require,
and we also will need crypto,
const crypto equals require crypto.
Now I head down to line 73
where the callService method is,
and we will extend the null a little bit.
I want to create a key, a cache key,
that, again, uniquely identifies an endpoint.
But in this case, I want it to be independent
of the port and IP because an endpoint
should return some result regardless
of the individual service instance it is running on.
So I will add:
const servicePath equals url dot parse
requestOptions dot url,
and from that we can now access the property of "path".
And this will hold just the path
without the IP address and the port.
Next we want to create a key
that uniquely identifies the endpoint.
For data add const cacheKey equals crypto dot createHash
and we want to create 'md5' file hash,
and we have to call update on that,
with it using the requestOptions dot method again,
plus the servicePath that we found out later.
And in the end I have to call digest,
hex on that to get a hexadecimal representation
of this hash.
Now we want to get result back
from the circuitBreaker callService call,
and for that we have to now change the return statement
to const result equals await.
Now if there was a result, we have to store it somewhere,
and for that we have to create the beta structure
and we will do that in the constructor.
So add this dot cache, should be an empty object.
And back in callService, I can now,
simply say, this dot cache cacheKey should be result,
and we want to return result.
Now we also have to handle the error case,
so this means: add if there is no result,
I will look into the cache,
so if there is: this dot cache
for the given cacheKey, I can return that.
We're returning the cache result,
and if this is not the case, we will return false.
That would be a cache miss,
so we don't have a result on the cache.
Let's fix the function a little bit, clean up,
and now we could try it out,
so run an instant start.
So start the Conference app, and, if you're not,
look at our website, it's still working.
So let's break something again.
For that I'm going to the speaker service,
and there I will again break the list.
I'll restart it.
And now let's reload the page.
We see it takes a little bit longer,
but then it returns a valid result.
But that looks very good, and it will,
as soon as the circuit breaker also goes to open,
as it's now, it will return very quickly,
because it's not waiting for it to call also 260.
Now we have open again, you get the trap.
So let's restart the app to see what happens
if the cache is empty, we see that,
in this case, we get an empty page back.
So it has to work at least once
so that cache can be populated with results.
Our application is now resilient
to a large range of problems,
and the user impact is minimized.
Still we have to do a little tweak here,
and we will do that in the next video.