Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert queries take an enormous amount of time with replicas #1375

Open
ilshm opened this issue May 6, 2024 · 1 comment
Open

Insert queries take an enormous amount of time with replicas #1375

ilshm opened this issue May 6, 2024 · 1 comment

Comments

@ilshm
Copy link

ilshm commented May 6, 2024

tursoClient.js

import { createClient } from "@libsql/client"

const tursoClient = createClient({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
})

export default tursoClient

app.js

import tursoClient from './lib/tursoClient.js'

async function testDB() {
    const iterations = 1000 // Number of iterations for stress testing

    // Variables to store performance metrics
    let totalQueryTime = 0
    let totalWriteTime = 0

    // Read operation
    for (let i = 0; i < iterations; i++) {
        const startQueryTime = Date.now()
        const employees = await tursoClient.execute("SELECT * FROM Employees")
        const endQueryTime = Date.now()
        totalQueryTime += endQueryTime - startQueryTime
    }

    // Write operation (inserting a new employee)
    for (let i = 0; i < iterations; i++) {
        const startWriteTime = Date.now()
        // Generate random data for the new employee
        const newEmployeeFirstName = await getRandomString()
        const newEmployeeLastName = await getRandomString()
        const newEmployeeDepartmentID = 1
        // Execute the INSERT query
        await tursoClient.execute({
            sql: "INSERT INTO Employees (FirstName, LastName, DepartmentID) VALUES (?, ?, ?)",
            args: [newEmployeeFirstName, newEmployeeLastName, newEmployeeDepartmentID]
        })
        const endWriteTime = Date.now()
        totalWriteTime += endWriteTime - startWriteTime
    }

    // Print performance reports
    const averageQueryTime = totalQueryTime / iterations
    console.log(`Average query time for SELECT operation: ${averageQueryTime} ms`)

    const averageWriteTime = totalWriteTime / iterations
    console.log(`Average time taken for INSERT operation: ${averageWriteTime} ms`)
}

async function getRandomString() {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  let randomString = ''
  for (let i = 0; i < 5; i++) {
    randomString += characters.charAt(Math.floor(Math.random() * characters.length))
  }
  return randomString
}

await testDB()

Results:

Primary only:

root@ubuntu-s-4vcpu-16gb-amd-ams3-01:~/pastebin# node --env-file=.env app.js 
Average query time for SELECT operation: 20.641 ms
Average time taken for INSERT operation: 19.172 ms

Primary + 2 replicas:

root@ubuntu-s-4vcpu-16gb-amd-ams3-01:~/pastebin# node --env-file=.env app.js 
Average query time for SELECT operation: 13.753 ms
Average time taken for INSERT operation: 132.646 ms

Is this acceptable? Is there a way to disable replication acknowledgment for insert operations, similar to MongoDB?

@haaawk
Copy link
Contributor

haaawk commented May 6, 2024

Replication is async so it shouldn't impact the performance. This is something we will have to debug to understand why replication has such a big impact on performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants