Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6113

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T03:31:08+00:00 2024-11-27T03:31:08+00:00

Using HTTP Headers for faster Responses

  • 60k

Intro

There is a history: You got a ticket because a specific API route is slow, and it's your job to fix that. Sounds familiar, right? Your first thought is to make whatever operation there fast, but it's not possible in this case. The second option is to give more resources to the service, but that does not sound like a real solution. The third option is to cache with Redis/memcached to avoid paying the penalty for every request. But what if I told you about a “free” solution that is as simple as adding some headers to the response?

The solution itself is tied to where the server is hosted, or at least where the domain exists. Each platform has its own ways to do so. For our example, we will use Cloudflare for the sake of simplicity.

Summary

  1. Intro
  2. Setup
  3. Explanation
  4. Closing thoughts
  5. References

Setup

For our example we need:

  • Cloudflare account
  • Hono project 🔗
  • Three routes
    • / to render a html page
    • /no-cache to return a data without cache
    • /cached to return cached data

After cloning the project, run npm run deploy to deploy the project to cloudflare workers.

Opening the page will you see a page with two dates.
Before

Waiting some few seconds and reloading the page.
After

Waiting some few seconds and reloading the page.
Gif

Without looking at the code, you may think that is a complex setup that is very specific. Not really. Here is the required code for caching the API response.

return new Response(new Date().toISOString(), {   headers: {     'Cache-Control': 'public, max-age=10' // keep the cache for 10 seconds   } }) 
Enter fullscreen mode Exit fullscreen mode

Just like that, we cached using http and without redis.


Explanation

Cloudflare has cache headers so it can cache fetch requests made to their own domain, in my case: https://[project].stneto.dev. These headers aren't tied to a specific provider, so each of them can have their own version, such as: Netlify, Vercel, AWS Api Gateway and the list can go on.

The code example did show some JavaScript code, but the caching itself isn't tied to a language or a framework, so it's possible to do something like this as well:

Go (native http handler)

func handler(w http.ResponseWriter, r *http.Request) {     w.Header().Set("Cache-Control", "max-age=3600") // Cache for 1 hour (in seconds)     w.Header().Set("Expires", "Tue, 01 Jan 2024 00:00:00 GMT") // Cache expiration date and time     w.Header().Set("ETag", "123456") // ETag for conditional requests       w.Write([]byte("Hello 🤠")) } 
Enter fullscreen mode Exit fullscreen mode

Rust (Axum)

async fn handler(_req: Request<Body>) -> Response<Body> {     // Set Cache-Control header to enable caching for 1 hour     Response::builder()         .header("Cache-Control", "max-age=3600")         .body(Body::from("Hello 🤠"))         .unwrap() } 
Enter fullscreen mode Exit fullscreen mode


Closing thoughts

The know-how to better use the platform can do wonders for you, as the solution is there “for free,” and you just need to know how to use it. There is a difference between using what the platform offers to you and a specific platform feature. Using response headers between platforms is quite simple, as you can just replace them, and you are good to go. But using some very specific behavior tied to a platform can be quite dangerous if you ever need to move away from there. It's about the diminishing returns; staying “platform-free” can be nice because you have more flexibility on where the service runs, but at the same time, you won't be using 100% of what the platform offers to you.


References

  • Cloudflare Origin Cache Control
  • Sanic

httpperformancerediswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.