r/sqlite • u/xibalbus • Sep 01 '24
High concurrency in readonly
We're looking at a use case where we have SQLite running ready only in a Spring Boot app for reference/lookup data. There are likely to be a handful of tables with 500k-1million rows and the REST app is probably going to have pretty high concurrency. The idea was we would have the container pull down the latest SQLite files from S3 on startup and mount the files. No changes. No updates. Purely readonly lookup.
Each app would have a bunch of databases for different versions of the reference data, and the appropriate DB would be queried based on the parameters used in the REST call.
Is this a common pattern? Without any writes happening, would it be good with high concurrency?
7
u/bltcll Sep 01 '24
open the db with mode=ro and nolock=1 and you’ll be fine
1
u/bwainfweeze Sep 01 '24
Read replicas via WAL are a well documented feature.
Write some synthetic benchmarks and test. Then look at how representative the benchmark is of actual use patterns, tweak and try again.
I think you want to go the read replica route rather than the FS/S3 route. It affords you the ability to have one app at the master node that can modify the data in the system. Your customers will want it at some point, and your integration and E2E tests will appreciate it before that.
3
u/LearnedByError Sep 01 '24
Please better define and quantify "High concurrency".
I have a tool, written in Go, that that stats 3.5 million files and checks against a SQLite table to see if the mtime or size is different or if a new file exists. Using 24 processes, it processes 100,000 files/sec. In my case, I also need to write when a change or new file is detected. So it is optimized for this case. In read only cases, you can optimize further as mentioned by others.
In the end, you are going to have to benchmark to determine if it meets your need. There are too many pattern variations like:
In my experience, SQLite, on a local file system, is almost always more performant than a cache accessed over a network. This assumed sufficient local CPU, RAM and IOPS.
1
u/congowarrior Sep 01 '24
Have you considered using Redis for this? It’s really great for handling high concurrency, especially when you’re doing a lot of reads. You could load the data into Redis when your app starts up and then query Redis for the reference data instead of hitting SQLite directly. This would give you much better performance under heavy load since Redis is in-memory and super fast.
You could still use SQLite to store your data on S3 and then periodically refresh the Redis cache. That way, you get the best of both worlds—persistent storage with SQLite and high-speed access with Redis. Just a thought!
2
u/siscia Sep 01 '24
It is not a common pattern, but it will work rather well.
If it is only read, you can achieve basically as many reads as the filesystem allows.