pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
8+ messages / 4 participants
[nested] [flat]

* [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2024-10-25 19:39 "wahajali (@wahajali)" <noreply+wahajali@github.com>
  0 siblings, 0 replies; 8+ messages in thread

From: wahajali (@wahajali) @ 2024-10-25 19:39 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I'm trying to benchmark how HNSW performs as datasizes grows on a single DB instance, going beyond the available shared_buffers. To do this, I run multiple test with a modified version of [VectorDBBench](https://github.com/zilliztech/VectorDBBench) with datasize increasing in increments of 500K all the way to 5M. I'm using OpenAI 1536 embeddings for this test, with following configurations:
ef-search: 40
ef-construction: 128
m: 32

I am capturing the buffer hit ratio (for indexes only) computed using the following query after each test run:
```
SELECT
SUM(idx_blks_read) as idx_read,
SUM(idx_blks_hit) as idx_hit,
(SUM(idx_blks_hit) - SUM(idx_blks_read)) / SUM(idx_blks_hit) as ratio
FROM
pg_statio_user_indexes;
``` 

The key observation I have is that the QPS goes down drastically once the index size is > the shared_buffer size, while the decrease in buffer hit ratio is initially not that significant. Before running the test I prewarm the buffers. Sharing some numbers of running this on m6i.2xlarge (8 core, 32 GB memory, shared_buffers=16 GB) instance:

| Dataset Size   | Index Size | QPS        | Buffer Hit Ratio |
|----------------|------------|------------|-----------------|
| OpenAI 500K    | 4 GB       | 2250.8908  | 1               |
| OpenAI 1M      | 8 GB       | 2115.2407  | 1               |
| OpenAI 1.5M    | 11 GB      | 2147.19355 | 0.99995         |
| OpenAI 2M      | 15 GB      | 2110.7287  | 0.99995         |
| OpenAI 2.5M    | 19 GB      | 1225.2465  | 0.98895         |
| OpenAI 3M      | 23 GB      | 102.3127   | 0.9288          |
| OpenAI 3.5M    | 27 GB      | 279.39445  | 0.9718          |
| OpenAI 4M      | 31 GB      | 101.65025  | 0.8922          |
| OpenAI 4.5M    | 34 GB      | 67.86625   | 0.84395         |
| OpenAI 5M      | 38 GB      | 12.91095   | 0.5444          |

I would have expected the decrease in QPS to be gradual, or at best proportional to the decrease in hit ratio. Is it expected that the index always reside fully in memory for reasonable performance? 

**Edit:** What I've termed previously as "index hit ratio" should be more appropriately termed as "buffer hit ratio" - i.e the ratio of queries being served by buffer vs going to disk. I've updated it accordingly.

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2024-10-28 13:02 ` "VoVAllen (@VoVAllen)" <noreply+VoVAllen@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: VoVAllen (@VoVAllen) @ 2024-10-28 13:02 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

What's your ebs setting? The default iops setting is pretty low which prevent postgres to fetch the data in expected time.

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2024-10-28 13:45 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: jkatz (@jkatz) @ 2024-10-28 13:45 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

When `m` is `32`, only a small portion of the upper layers of your graph (Layer 1+) will have your index (~3%) - most of the search time will be in Layer 0, and you'll be checking 32 vectors for each node you visit in the graph. With `ef_search` at `40`, let's say you converge with 40 steps (optimistically) so that's 1,280 vectors => 7.5MB of data to potentially load (assuming L1+ is cached in memory).

So on a memory constrained system, you may be constantly swapping out values, thus as @VoVAllen implies, you may need to tweak your IOPS/storage bandwidth to account for this. Additionally (and I want to do some more experimentation on this), you may be able to use huge pages to speed up memory allocation particularly when you're loading from storage.

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2024-10-28 16:16 ` "wahajali (@wahajali)" <noreply+wahajali@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: wahajali (@wahajali) @ 2024-10-28 16:16 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@VoVAllen This is on a managed RDS instance (db.m6i.2xlarge) with 3000 provisioned IOPS and 125 mbps throughput.

@jkatz thanks for the explanation and pointers. Between 2M and 2.5M, there is a ~50% drop in QPS, that's where the index starts to spill over shared_buffers. However, the decrease in buffer hit ratio *for index* is very minor (~0.01). 

If I understand your comment correctly, does it imply that while there might not be a lot of swapping in the index, which is what the query I shared is reporting, there might be swapping in the table data? In which case, I should also measure the buffer hit ratio on the tables (via `pg_statio_user_tables`). 

I will also compare the disk io usage between these two scenarios. 

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2024-10-28 17:10 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: jkatz (@jkatz) @ 2024-10-28 17:10 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Also, because you set shared_buffers so high relative to available instance memory, you also decreased the available page cache, which can impact fetch speed when data is not in memory (and would be more noticeable over the network). It'd be interesting to see what happens if you use 25% buffers and what the performance degradation is from there. (I don't know if it would be better or worse - this is a thought exercise)

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2024-10-31 05:51 ` "wahajali (@wahajali)" <noreply+wahajali@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: wahajali (@wahajali) @ 2024-10-31 05:51 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@jkatz Here are the results with 25% shared buffer. The QPS is stable as long as the data fits in shared_buffer. After that the decline is more subtle than before.

| Dataset Size | Index Size | QPS        |
|--------------|------------|------------|
| OpenAI 500K  | 4 GB       | 2245.62805 |
| OpenAI 1M    | 8 GB       | 2159.0031  |
| OpenAI 1.5M  | 11 GB      | 1906.83545 |
| OpenAI 2M    | 15 GB      | 1665.35345 |
| OpenAI 2.5M  | 19 GB      | 1479.72235 |
| OpenAI 3M    | 23 GB      | 90.29945   |
| OpenAI 3.5M  | 27 GB      | 52.84175   |
| OpenAI 4M    | 31 GB      | 27.77215   |
| OpenAI 4.5M  | 34 GB      | 59.3837    |
| OpenAI 5M    | 38 GB      | 12.13395   |

I had to make some changes to VectorDBBench test dataset. With the original 1000 test datasets, the results were very different. I have captured that discussion here: https://github.com/zilliztech/VectorDBBench/issues/385

Regarding the numbers you shared [here](https://github.com/pgvector/pgvector/issues/678#issuecomment-2392576311), is the VectorDBBenchmark running on the same instance as the DB or running on a separate VM?


^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2025-03-06 23:01 ` "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2025-03-06 23:01 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @wahajali, thanks for sharing these numbers.

The disk speed should affect the difference (local NVMe should perform better than SAN).

My general recommendation for users is:

1. Use the default `m` of 16, unless seeing poor recall, as this significantly affects query performance
2. Use [half-precision vectors](https://github.com/pgvector/pgvector?tab=readme-ov-file#half-precision-vectors) or [binary quantization with reranking](https://github.com/pgvector/pgvector?tab=readme-ov-file#binary-quantization) so more of the dataset fits into memory

For RDS specifically, you can:

1. Use a memory optimized instance type (I'd recommend this in general for larger databases, even without vectors)
2. Try optimized reads ([this article](https://aws.amazon.com/blogs/database/accelerate-generative-ai-workloads-on-amazon-aurora-with-optim...) talks about the difference on Aurora, but also looks like it's available on RDS)

I'm not sure there's anything for pgvector to do here, so closing this out, but happy to review any PRs that are able to improve performance here.

^ permalink  raw  [nested|flat] 8+ messages in thread

* Re: [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory
@ 2025-03-06 23:01 ` "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2025-03-06 23:01 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2025-03-06 23:01 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-10-25 19:39 [pgvector/pgvector] issue #700: HNSW QPS Degradation as Index Size Grows Beyond Memory "wahajali (@wahajali)" <noreply+wahajali@github.com>
2024-10-28 13:02 ` "VoVAllen (@VoVAllen)" <noreply+VoVAllen@github.com>
2024-10-28 13:45 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-10-28 16:16 ` "wahajali (@wahajali)" <noreply+wahajali@github.com>
2024-10-28 17:10 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-10-31 05:51 ` "wahajali (@wahajali)" <noreply+wahajali@github.com>
2025-03-06 23:01 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-06 23:01 ` "ankane (@ankane)" <noreply+ankane@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox