1. Introduction
Efficiently storing and retrieving data is a core challenge in software development. Developers want to depend on fast and reliable tools that manage data with minimal overhead.
DiceDB is an open-source, high-performance, and in-memory database. It delivers blazing-fast performance and a developer-friendly experience, all while supporting real-time applications. DiceDB utilizes familiar commands like SET and GET, offers modern features such as reactive queries, and, most importantly, remains fully open-source.
2. How to Install and Use It?
To install and use DiceDB, we need a running instance of the DiceDB server and DiceDB CLI.
2.1. Running Instance of DiceDB
We can install DiceDB either from Docker or we can build it from the source code. In this tutorial, we’ll set up the DiceDB using Docker by running the following command:
$ docker run -d -p 7379:7379 --name dicedb-container dicedb/dicedb:latest
The above command starts the DiceDB server in the background. It assigns the container name as dicedb-container. The server runs locally and is accessible on port 7379.
2.2. DiceDB CLI
DiceDB CLI is the command-line interface for DiceDB. This is the recommended method for connecting to the DiceDB server. We can install it by running the following command:
$ curl -sL https://raw.githubusercontent.com/dicedb/dicedb-cli/refs/heads/master/install.sh | sh
Upon successful completion, the installation script will print the following message on the terminal:
DiceDB CLI installation complete ✓
Now, to interact with the database, we can start the CLI by running the command dicedb-cli:
$ dicedb-cli
localhost:7379>
The above output indicates that we’re connected to the default DiceDB instance, which is running on port 7379 on our local machine.
3. A Simple Set-Get Example With DiceDB
Let’s take a look at the example, which confirms that our DiceDB setup is running correctly and helps us learn how to interact with the database.
In this example, we’ll use the SET command to store the key and then retrieve it using the GET command.
Let’s define a key-value pair using key1 as the key and assigning it the string value1 as its value:
localhost:7379> SET key1 value1
The DiceDB replies with OK, which indicates that it has successfully stored the key key1Â with the value value1.
Let’s now retrieve the value of key1 using GET:
localhost:7379> GET key1
The DiceDB responds with OK “value1”, which returns our value.
If we try to retrieve the value of a key that doesn’t exist in the database, the command returns an empty string “”:
localhost:7379> GET key2
The DiceDB responds with OK “”, which indicates that the key is not present.
4. Some Useful DiceDB Commands
Let’s take a look at some useful DiceDB commands:
4.1. DELÂ – Delete a Key
The DEL command removes all the specified keys and returns the number of keys deleted successfully. If the key doesn’t exist, it’s ignored.Â
Let’s take a look at the example where we’ll try to delete a key key3, which doesn’t exist in the database:
localhost:7379> SET key1 value1
OK
localhost:7379> SET key2 value2
OK
localhost:7379> DEL key1 key2 key3
OK 2
The DiceDB responds with OK 2, indicating that it deleted the two keys present in the database and ignored the third key.Â
The ECHO command returns whatever string we pass in. It’s useful for testing connectivity.
Let’s run the ECHOÂ command to return and display a message:
localhost:7379> ECHO "Hello from Baeldung!"
OK Hello from Baeldung!
4.3. PINGÂ – Check Server Responsiveness
The PING command checks if the server is alive and responsive. It’s used to verify if the server is running and reachable.
The PING commands check connectivity by returning PONG:
localhost:7379> PING
OK "PONG"
A PONG response confirms that DiceDB is up and running.
4.4. TYPEÂ – Identify the Value Type of a Key
The TYPEÂ command tells us the data type stored at a given key. It could return either a string or an int. It helps validate the data type before performing any operation.
Let’s set a couple of keys and check their types using the TYPEÂ command:
localhost:7379> SET key1 12
OK
localhost:7379> SET key2 Hello
OK
localhost:7379> TYPE key1
OK int
localhost:7379> TYPE key2
OK string
In the above example, key1 has an integer value of 12, whereas key2 has a string value of Hello.
4.5. EXISTS – Check If a Key Exists
We can use the EXISTS command to check whether a key is present in the database. If the key exists, it returns 1; otherwise, 0.
In the below example, we’ve only one key, i.e., key1, in the database:
localhost:7379> SET key1 value1
OK
localhost:7379> EXISTS key1
OK 1
localhost:7379> EXISTS key22
OK 0
When we checked for the existence of key1, DiceDB returned 1, indicating that the key exists in the database. In contrast, it returned 0 for key22, confirming that the key is not present.
5. Conclusion
In this article, we’ve established a connection to a DiceDB instance, saved a basic string, and successfully retrieved it.
The code backing this article is available on GitHub. Once you're
logged in as a Baeldung Pro Member, start learning and coding on the project.