The Node Express server is returning "304 Not Modified" responses, but only after querying the database

18,492

The framework is:

  1. Generating the page internally for every request
  2. Using ETag headers with a content hash function to compare the generated page with the version sent before
  3. Either sending the full page, or a "Not Modified" response based on the hash comparison

Because your content is database generated, the framework has to assume that it is dynamic and can change with every request. Therefore it must execute all the queries and generate the page before it compare to the version the client actually has.

I'm not familiar with Express, but most frameworks have a mechanism where you can cache the results of SQL queries for some amount of time to reduce the load on the database.

Share:
18,492

Related videos on Youtube

Diligent Key Presser
Author by

Diligent Key Presser

Programming is all about self-restriction. Which language do you use? Why does it ever have such a wierd syntax? We choose appropriate languages and suitable patterns for different problems - just to escape from total uncertainty to cosiness of well-known behaviors. Art of programming is nothing but sense of proper way to restrict ability to shoot in your foot. Paradigms and languages are our tools to cut off those undesirable scenarios.

Updated on September 18, 2022

Comments

  • Diligent Key Presser
    Diligent Key Presser almost 2 years

    I'm just diving into web developent, my current passion is Express (Node.js).

    I've noticed then i often recieve HTTP/1.1 304 Not Modified either for static content and (!) for my json API requests (when data is still the same, so this is actually correct). But my node console log shows that SQL querys are executed every time API requested.

    It seems like requests from my front-end are cached, but the cache does not actually prevent my back-end from executing redundant SQL querys. Hmmm...

    Can anyone shed some light onto what is actually going on here?

  • Diligent Key Presser
    Diligent Key Presser almost 8 years
    Well, no actual server-side caching, which is actually nice. This makes me free to deploy a caching software on a separate machine. Thank you for the explanation!