Can TiKV achieve Redis's incrby and ensure atomic operations?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 使用TiKV可以实现Redis的incrby并保证原子操作吗?

| username: abelard2008

I am currently using Redis and want to replace the existing Redis with TiKV. However, I am using Redis’s atomic incrby as a distributed lock to ensure that only one thread operates on critical resources at the same time. I would like to ask if this atomic incrby can also be achieved using TiKV to get the same effect as using Redis? Thank you.

PS: I am currently using Tidis to access TiKV.

| username: xfworld | Original post link

Well, the atomicity of Redis and the transactional support provided by TiKV are different in their specific manifestations.

Redis relies on a single-threaded queue model to ensure that only one resource can be scheduled and executed at the same time.
TiKV needs to rely on some mechanisms to meet this requirement. It can inherently support atomicity because the upper layer of atomicity is transactional.
Mainly, TiKV consists of multiple nodes, and data can be distributed across different nodes.
Redis relies on the slot algorithm to find the point and node for reading (in cluster mode).

Therefore, if you want to use TiKV to implement Redis’s distributed lock, you will need to implement some additional functionalities yourself.

| username: abelard2008 | Original post link

You mentioned in the last sentence:

Can you specifically say what functions? I guess: since TiKV is multiple nodes, you need to implement a single-threaded queue mode similar to native Redis in the application. Considering that I am now using Tidis as middleware, can these functions be implemented in Tidis, right?

| username: xfworld | Original post link

I don’t know if middleware can meet your needs. If it can, then there shouldn’t be much of a problem.

| username: yongman | Original post link

The official PingCAP Tidis has not been open-sourced yet, but it is expected to be open next week. Please stay tuned.

Tidis currently implements the incrby command, and it can ensure atomicity in terms of semantics. However, in cases of high concurrency modifying the same key, transaction conflicts may occur, and it will automatically retry. This extreme scenario needs to be verified in specific contexts.

| username: abelard2008 | Original post link

Very much looking forward to it!

This is very challenging, but without rigorous testing, there is a concern about encountering extreme situations in production!

| username: yongman | Original post link

As long as the business can tolerate error returns and has retry logic, it should be fine.

| username: abelard2008 | Original post link

Yes, retry logic is worth considering. Additionally, incorporating the single-threaded concept of Redis into Tidis to create a specific custom version is also a feasible approach, right?

| username: 张雨齐0720 | Original post link

I think it’s entirely possible to use TiDB to implement distributed locks.
Implement distributed locks based on the database.
The “select for update” method can achieve this.
It can be divided into optimistic lock and pessimistic lock modes, depending on the tolerance of your application, you can implement it yourself.

Pessimistic lock: Use “select … where … for update” exclusive lock
Optimistic lock: Implement optimistic lock by adding an incrementing version number field

| username: abelard2008 | Original post link

My main consideration right now is to replace my current Redis with TiKV. Replacing Redis with TiDB might be considered in the future. However, you mentioned using TiDB for distributed locking, and I can refer to that idea when the time comes. :smile:

| username: 张雨齐0720 | Original post link

Using the features of the database, it can be achieved. There are definitely databases using this method now. Create a table and encapsulate an API, and it should work.

| username: yongman | Original post link

It’s open source now: GitHub - tidb-incubator/tidis: A distributed transactional large-scale NoSQL database powered by TiKV

| username: abelard2008 | Original post link

Got it, I’ll download it right away and test it. Here’s a star for now :slight_smile:

| username: jansu-dev | Original post link

Any updates?

| username: abelard2008 | Original post link

Update what?

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.