My Other Social Media Hangouts

If you were anyone with a TV or electronic streaming device more than likely you were tuning into last night’s Golden Globe awards. And if you also happened to be someone who kept up with what’s…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




API Gateway Caching for ASP.NET Core WEB API

When you are building an API, you want to keep it simple and fast. Once the concurrent need to read the same data increase, you’ll face a few issues😐 where you might be considering introducing caching:

❌ There is latency on some API requests which is noticeably affecting the user’s experience.
❌ Fetching data from a database takes more time to respond.
❌ Availability of your API is threatened by the API’s high throughput.
❌ There are some network failures in getting frequently accessed information from your API.

Here is an overview of what we cover in this walkthrough:

Reverse Proxy Caching is yet another caching mechanism that is usually implemented inside API Gateway. It can reduce the number of calls made to your endpoint and also improve the latency of requests to your API by caching a response from the upstream. If the API Gateway cache has a fresh copy of the requested resource, it uses that copy to satisfy the request directly instead of making a request to the endpoint. If the cached data is not found, the request travels to the intended upstream services (backend services).

🙋🏼 With all this in mind, we’ll look next at an example of using proxy-cache plugin offered by Apache APISIX and apply it for ASP.NET Core Web API with a single endpoint.

💁 To execute and customize the example project per your need shown in this post, here are the minimum requirements you need to install in your system:

Let’s assume that this product list is usually updated only once a day and the endpoint receives repeated billions of requests every day to fetch the product list partially or all of them. In this scenario, using API caching technique with proxy-cache plugin might be really helpful🙌. For the demo purpose, we only enable caching for GET method.

Now let’s start with adding proxy-cache plugin to Apache APISIX declarative configuration file config.yaml in the project. Because in the current project, we have not registered yet the plugin we are going to use for this demo. We appended proxy-cache plugin's name to the end of plugins list:

Next, we can directly run apisix reload command to reload the latest plugin code without restarting Apache APISIX. See the command to reload the newly added plugin:

Then, we run two more curl commands to configure an Upstream and Route for the /api/products endpoint. The following command creates a sample upstream (that's our API Server):

Next, we will add a new route with caching ability by setting proxy-cache plugin in plugins property and giving a reference to the upstream service by its unique id to forward requests to the API server:

As you can see in the above configuration, we defined some plugin attributes that we want to cache only successful responses from the GET method of API.

Finally, we can test the proxy caching if it is working as it is expected.

We will send multiple requests to the /api/products path and we should receive HTTP 200 OK response each time. However, the Apisix-Cache-Status in the response shows MISS meaning that the response has not cached yet when the request hits the route for the first time. Now, if you make another request, you will see that you get a cached response with the caching indicator as HIT.

Now we can make an initial request:

The response looks like as below:

When you do the next call to the service, the route responds to the request with a cached response since it has already cached in the previous request:

Or if you try again to hit the endpoint after the time-to-live (TTL) period for the cache ends, you will get:

Excellent! We enabled caching for our API endpoint😎

💁🏼 Optionally, you can also add some delay in the Product controller code and measure response time properly with and without cache:

The curl command to check response time would be:

Community⤵️

Add a comment

Related posts:

Review youtube secrets

This is going to be a review of a ClickBank product called YouTube Secrets. When I first saw this product, I thought it is just a rehashed Viral Cash Application. The main reason was that both…

What My Minimalist Phone Looks Like

This is not the first time I’ve written about digital minimalism. It feels like I can’t go a week without spotting someone’s phone out of the corner of my eye, overflowing with hundreds of unseen…

5 reasons owning a Tesla Model 3 will change your life

Aniseh Sharifi is a self-proclaimed electric vehicle nerd who admits, “My EV has changed my life — and yes I realize how cheesy that sounds.” After 10+ years in the auto industry, Aniseh’s bold…