Skip to content
JackSparrow414
Go back

Getting Started with Redis (Part 2): Basic Data Types and Commands

Table of contents

Open Table of contents

Learning Redis

Basic Usage

Starting Redis

# Start the Redis server.
redis-server --port 6379

# When there are multiple Redis instances, specify a configuration file at startup.
redis-server ./redis.conf
redis-server ./redis16379.conf

# Start the Redis client.
redis-cli -p 6379

Shutting Down

redis-cli shutdown

Checking the Redis Connection

ping
# A PONG response means the connection is working.

Getting Started

Basic Data Types

Strings

Each key of the string type corresponds to just one string.

Hashes

A hash key can contain $2^{32}-1$ fields. Hashes are suitable for storing objects, with an object’s properties stored in separate fields.

Lists

Lists are commonly used for adding elements at either end.

Sets

Elements in a set are unique; duplicates are not allowed. Lists, by contrast, can contain multiple identical elements.

Sorted Sets

Elements are automatically ordered by score.

Notes

  1. Redis commands are case-insensitive.
  2. A useful convention for Redis key names is object-type:object-ID:object-property.

Useful Resources

  1. Redis command reference: useful for a quick search when you forget a command.
  2. Redis documentation: official documentation is always my first recommendation for getting started and solving problems.

Terminology

  1. Time To Live (TTL): the time an item remains alive.
  2. Race Condition: a condition caused by competing operations.
  3. Round-trip delay time (round trip time): the delay for a round trip.

Business Atomicity and Code Atomicity When Using a Redis Java Client

When writing application code, some developers know to use Redis for atomic operations and assemble business logic through a Java client, but overlook the atomicity of the entire business operation. In such cases, use a Lua script rather than splitting critical business logic into individual lines of code, each making its own Redis call. Each Redis command is atomic, but the full code flow can still be affected by concurrent threads. Critical business operations that require atomicity therefore need to be implemented with Lua scripts.


Share this post:

Continue this series

Getting Started with Redis

  1. Getting Started with Redis (Part 1): Basic Setup on Windows
  2. Getting Started with Redis (Part 2): Basic Data Types and CommandsYou are here
  3. Getting Started with Redis (Part 3): Primary/Replica Replication
  4. Getting Started with Redis (Part 4): Monitoring and Automatic Recovery with Sentinel

Comments

Questions, corrections, and experiences are welcome. Sign in with GitHub to comment; both language versions share this discussion.

Comments are available on the live site only.