Firestore DB read/write latency is same from public route and private service connect endpoint in GCP

I have firestore DB in GCP. I was accessing the firestore DB via public route and the latency was around 300ms (280-300).

I create a private service connect endpoint to reduce the latency and access DB privately.

But event after PSC endpoint I’m getting same latency.

Below is my script to check endpoint latency, read and write latency.

const admin = require('firebase-admin');
const path = require('path');
const axios = require('axios');


// Initialize Firebase Admin SDK
const serviceAccount = require(path.resolve(__dirname, '/home/amityadav/sa.json')); 
// Replace with your own service account key path

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

console.log('Credentials After initializeApp:', serviceAccount.client_email);

const db = admin.firestore();
const collectionName = 'testCollection';
const documentId = 'testDoc';


const docRef = db.collection(collectionName).doc(documentId);
console.log('Firestore Path:', `projects/${serviceAccount.project_id}/databases/(default)/documents/${collectionName}/${documentId}`);


// Function to test network latency
async function testNetworkLatency(url) {
  const start = Date.now();
  try {
    await axios.get(url);
    const end = Date.now();
    return end - start;
  } catch (error) {
    console.error(`Error accessing ${url}:`, error);
    return -1;
  }
}


// Function to check if accessing DB privately or publicly
async function checkAccessMethod() {
  const privateUrl = 'http://metadata.google.internal'; // Known private endpoint
  const publicUrl = 'https://www.google.com'; // Public endpoint

  const privateLatency = await testNetworkLatency(privateUrl);
  const publicLatency = await testNetworkLatency(publicUrl);

  console.log(`Private Network Latency: ${privateLatency} ms`);
  console.log(`Public Network Latency: ${publicLatency} ms`);

  if (privateLatency > 0 && (privateLatency < publicLatency)) {
    console.log('Accessing Firestore privately.');
  } else {
    console.log('Accessing Firestore publicly.');
  }
}



// Function to write data to Firestore
async function writeToFirestore(data) {
  const startWrite = Date.now();
  try {
    await db.collection(collectionName).doc(documentId).set(data);
    const endWrite = Date.now();
    const writeLatency = endWrite - startWrite;
    console.log(`Write Latency: ${writeLatency} ms`);
  } catch (error) {
    console.error('Error writing to Firestore:', error);
  }
}

// Function to read data from Firestore
async function readFromFirestore() {
  const startRead = Date.now();
  try {
    const docRef = db.collection(collectionName).doc(documentId);
    const snapshot = await docRef.get();
    const endRead = Date.now();
    const readLatency = endRead - startRead;
    console.log(`Read Latency: ${readLatency} ms`);

    // Print the data fetched (optional)
    if (snapshot.exists) {
      console.log('Fetched Data:', snapshot.data());
    } else {
      console.log('No data found');
    }
  } catch (error) {
    console.error('Error reading from Firestore:', error);
  }
}

// Function to execute write and read operations every 2 seconds
async function executeOperations() {
  try {
    const testData = { message: 'Hello, Firestore!' };
    
    // Check if accessing DB privately or publicly
    await checkAccessMethod();

    // Write data to Firestore
    await writeToFirestore(testData);
    
    // Read data from Firestore
    await readFromFirestore();
  } catch (error) {
    console.error('Error executing operations:', error);
  }
}

// Start executing operations every 2 seconds
setInterval(executeOperations, 2000);

I give it a try from cloud shell instance.

Output from my VM shell

Output from Cloud shell

I wanted to achieve cloud shell latency (~80ms), Is it possible in GCP and If yes HOW ?

New contributor

Amit Yadav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật