2 ms·
Yes: use a different tool, such as Redis. Updating a SQL database row can be expensive because of the transaction overhead. Once you increment this counter, th
by immibis 2y ago
Yes: use a different tool, such as Redis.
Updating a SQL database row can be expensive because of the transaction overhead. Once you increment this counter, the row with the counter remains locked until the transaction is committed. But the actual useful work of incrementing a number is already quicker than selecting a random slot. If the increment can be a simple ADD instruction, without transactional semantics, then this random-slot-based approach is slower than just incrementing it. Especially if you don't care about missing a few increments and therefore don't need a mutex.
For even more scale, don't use Redis but have a shared memory block containing your integer.
For even more scale, you can have one such slot per CPU. Make sure they're in different cache lines, to avoid false sharing. Now you have slots again, but they're per-CPU and so you still avoid all the SQL database overhead.
Performance is all about structuring the task as close to the machine as possible.
- jeffrallen 2y agoSome people, when they have the problem of incrementing integers think, "I'll use Redis." Now they have two problems.
- esafak 2y agoCan you go into more detail? Redis supports counters: https://redis.io/glossary/storing-counters-in-redis/ https://redis.io/glossary/storing-counters-in-redis/
- jeffrallen 2y agohttps://regex.info/blog/2006-09-15/247 https://regex.info/blog/2006-09-15/247
- immibis 2y agoso... what's the problem with using Redis to store a shared counter?
- jeffrallen 2y agoIt's a joke about answering complexity with additional complexity. Laugh, it's ok. :)
- immibis 2y agoRedis is a data structure server, for a limited set of data structures. If that's what you want, then use Redis.