In Firebase Functions, would data be cached if 2 users hit the same instance?


Let’s imagine I have the following code which authenticates a user and then caches it so the next request can go faster without re-authenticating the user.

export const app = express();

let currentUser: User | undefined = undefined;

export function authenticateUser(
  req: express.Request,
  res: express.Response,
  next: express.NextFunction
) {
  if(!currentUser) {
    currentUser = authenticateUser();

app.get("/", authenticateUser, async (_req, res) => {
  send(`Hello, ${}!`);

app.listen(port, () => {
  debug(`Server running on port ${port}`);
export const myfunction = functions.https.onRequest(app);

In this code, I’m expecting that the same user will do the the first request, which loads the information and then performs another request which uses the cached information. Now, I’m worried whether this would be a problem if I have 2 users hitting the same instance almost at same time and I’ll have this problem:

Request from Output
User A Hello, User A!
User B Hello, User A!

So my questions are:

  1. Is it possible that 2 users could hit the same instance?
  2. If so, will the cached information be cleaned up somehow by Firebase or it’ll stay?
  3. If information will be cached, how can I solve this? I think my options could be:
    1. Make sure information is cleaned if the coming user is different
    2. Configure somehow cloud functions to ensure only exists one instance per user. Is that possible?


  1. Yes, instances may be re-used for multiple calls.
  2. Your script will NOT be reloaded between these calls, so currentUser will have the same value you gave it before.
  3. It depends on how you handle the authentication, but the common approach would be to pass the ID token from the client to the Cloud Function, and decode and verify it there. For an example of this, see this repo.

Answered By – Frank van Puffelen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published