Loading...

Redis online editor

Write, Run & Share Redis commands online using OneCompiler's Redis online editor for free. It's one of the robust, feature-rich online editors for Redis. Getting started with the OneCompiler's Redis editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'Redis' and start writing commands to learn and test online without worrying about tedious process of installation.

About Redis

Redis is an open-source, in-memory data structure store used as a database, cache, message broker, and streaming engine. It supports various data structures including strings, lists, sets, sorted sets, hashes, and more. Redis is known for its exceptional performance, simplicity, and versatility, making it one of the most popular NoSQL databases for real-time applications.

Syntax help

Strings - SET & GET

Strings are the most basic Redis data type, storing text or binary data up to 512MB. SET stores a value at a key, GET retrieves it. Redis strings can also store integers and floats, enabling atomic increment/decrement operations. Use strings for caching, counters, session data, and any simple key-value storage needs.

SET name "John"
GET name

SET greeting "Hello, World!"
GET greeting

-- Set with expiration (seconds)
SETEX session 3600 "session_data"

-- Set only if not exists
SETNX unique_key "value"

-- Set multiple keys
MSET key1 "value1" key2 "value2" key3 "value3"
MGET key1 key2 key3

Strings - Counters

Redis strings can store numeric values and perform atomic increment/decrement operations. INCR and DECR change the value by 1, while INCRBY and DECRBY allow custom increments. These operations are atomic, making Redis perfect for counters, rate limiters, and real-time statistics without race conditions.

SET counter 10
INCR counter
GET counter

DECR counter
GET counter

INCRBY counter 5
GET counter

DECRBY counter 3
GET counter

-- Float increment
SET price 10.50
INCRBYFLOAT price 0.25
GET price

Strings - Manipulation

Redis provides commands for manipulating string values without retrieving them first. APPEND adds to existing strings, STRLEN returns length, and GETRANGE extracts substrings. GETSET atomically sets a new value while returning the old one, useful for implementing locks and swaps.

SET message "Hello"
APPEND message " World"
GET message

STRLEN message

GETRANGE message 0 4

-- Get old value while setting new
SET status "active"
GETSET status "inactive"
GET status

Lists

Lists are ordered collections of strings, implemented as linked lists. Use LPUSH/RPUSH to add elements at the head/tail, LPOP/RPOP to remove and return elements. Lists are ideal for queues, stacks, timelines, and any ordered data. Redis lists can contain over 4 billion elements.

-- Add to list (left/head)
LPUSH tasks "task1"
LPUSH tasks "task2"
LPUSH tasks "task3"

-- Add to list (right/tail)
RPUSH tasks "task4"

-- Get all elements (0 to -1 means all)
LRANGE tasks 0 -1

-- Get specific range
LRANGE tasks 0 2

-- Get list length
LLEN tasks

-- Remove and return from left
LPOP tasks

-- Remove and return from right
RPOP tasks

LRANGE tasks 0 -1

Lists - Advanced Operations

Redis lists support element access by index, range trimming, and blocking operations. LINDEX retrieves elements by position, LSET updates them, and LTRIM keeps only a specified range. Blocking commands like BLPOP wait for elements, perfect for implementing job queues.

LPUSH mylist "a" "b" "c" "d" "e"

-- Get element by index (0-based)
LINDEX mylist 0
LINDEX mylist 2

-- Set element at index
LSET mylist 1 "updated"
LRANGE mylist 0 -1

-- Keep only elements in range (trim)
LTRIM mylist 0 2
LRANGE mylist 0 -1

-- Insert before/after element
LPUSH items "world"
LINSERT items BEFORE "world" "hello"
LRANGE items 0 -1

Hashes

Hashes store field-value pairs, perfect for representing objects. HSET sets fields, HGET retrieves them, and HGETALL returns all fields and values. Hashes are memory-efficient for storing objects with many fields and support atomic operations on individual fields like HINCRBY for counters.

-- Set single field
HSET user:1 name "Alice"
HSET user:1 age 30
HSET user:1 city "New York"

-- Set multiple fields at once
HSET user:2 name "Bob" age 25 city "Boston"

-- Get single field
HGET user:1 name

-- Get multiple fields
HMGET user:1 name age

-- Get all fields and values
HGETALL user:1

-- Check if field exists
HEXISTS user:1 email

-- Get all field names
HKEYS user:1

-- Get all values
HVALS user:1

-- Get number of fields
HLEN user:1

Hashes - Numeric Operations

Hash fields can store numeric values and support atomic increment operations. HINCRBY increments integer fields, HINCRBYFLOAT handles floating-point numbers. These operations are perfect for user statistics, product inventory, game scores, or any object with numeric properties that change frequently.

HSET product:1 name "Laptop" price 999 stock 50

-- Increment integer field
HINCRBY product:1 stock 10
HGET product:1 stock

-- Decrement (use negative value)
HINCRBY product:1 stock -5
HGET product:1 stock

-- Increment float field
HSET product:1 rating 4.5
HINCRBYFLOAT product:1 rating 0.1
HGET product:1 rating

HGETALL product:1

Sets

Sets are unordered collections of unique strings. SADD adds members, SMEMBERS lists all members, and SISMEMBER checks membership. Sets support powerful operations like union, intersection, and difference. Use sets for tags, unique visitors, friend lists, or any collection where uniqueness matters.

-- Add members
SADD tags "redis" "database" "nosql" "fast"

-- Try adding duplicate (ignored)
SADD tags "redis"

-- Get all members
SMEMBERS tags

-- Check membership
SISMEMBER tags "redis"
SISMEMBER tags "mysql"

-- Get set size
SCARD tags

-- Remove member
SREM tags "fast"
SMEMBERS tags

-- Get random member
SRANDMEMBER tags

Sets - Operations

Redis sets support mathematical set operations: union (all elements from all sets), intersection (common elements), and difference (elements in first set not in others). These operations can be performed and stored in a new set, enabling complex data analysis and relationship queries.

SADD set1 "a" "b" "c" "d"
SADD set2 "c" "d" "e" "f"

-- Union (all unique elements)
SUNION set1 set2

-- Intersection (common elements)
SINTER set1 set2

-- Difference (in set1 but not set2)
SDIFF set1 set2

-- Store results in new set
SINTERSTORE common set1 set2
SMEMBERS common

SUNIONSTORE all set1 set2
SMEMBERS all

Sorted Sets

Sorted sets combine sets with scores, maintaining elements in score order. ZADD adds members with scores, ZRANGE retrieves by rank, and ZSCORE gets a member's score. Sorted sets are perfect for leaderboards, priority queues, rate limiters, and any data needing both uniqueness and ordering.

-- Add members with scores
ZADD leaderboard 100 "Alice"
ZADD leaderboard 85 "Bob"
ZADD leaderboard 95 "Charlie"
ZADD leaderboard 90 "Diana"

-- Get by rank (ascending, 0-indexed)
ZRANGE leaderboard 0 -1

-- Get with scores
ZRANGE leaderboard 0 -1 WITHSCORES

-- Get by rank (descending - highest first)
ZREVRANGE leaderboard 0 -1 WITHSCORES

-- Get rank of member (0-indexed)
ZRANK leaderboard "Alice"
ZREVRANK leaderboard "Alice"

-- Get score
ZSCORE leaderboard "Alice"

-- Get count
ZCARD leaderboard

Sorted Sets - Range Queries

Sorted sets support powerful range queries by score or lexicographically. ZRANGEBYSCORE retrieves members within a score range, useful for time-based data or filtering by numeric criteria. ZINCRBY atomically updates scores, perfect for real-time leaderboards and ranking systems.

ZADD scores 10 "a" 20 "b" 30 "c" 40 "d" 50 "e"

-- Get members by score range
ZRANGEBYSCORE scores 15 35
ZRANGEBYSCORE scores 15 35 WITHSCORES

-- Get count in score range
ZCOUNT scores 15 35

-- Increment score
ZINCRBY leaderboard 10 "Bob"
ZSCORE leaderboard "Bob"

-- Remove by rank range
ZREMRANGEBYRANK scores 0 1
ZRANGE scores 0 -1

-- Remove by score range
ZREMRANGEBYSCORE scores 40 50
ZRANGE scores 0 -1

Key Expiration (TTL)

Redis supports automatic key expiration using TTL (Time To Live). EXPIRE sets expiration in seconds, PEXPIRE in milliseconds. SETEX combines SET and EXPIRE in one command. TTL checks remaining time, and PERSIST removes expiration. Use expiration for sessions, caches, rate limits, and temporary data.

SET session:123 "user_data"

-- Set expiration (seconds)
EXPIRE session:123 3600

-- Check remaining time
TTL session:123

-- Set with expiration in one command
SETEX cache:page 300 "page_content"
TTL cache:page

-- Set expiration (milliseconds)
PSETEX temp 5000 "expires in 5 seconds"
PTTL temp

-- Remove expiration (make permanent)
SET mykey "value"
EXPIRE mykey 100
PERSIST mykey
TTL mykey

Key Management

Redis provides commands for managing keys across all data types. KEYS finds keys matching patterns (use carefully in production), EXISTS checks if keys exist, DEL removes keys, and TYPE returns the data type. RENAME changes key names atomically.

SET user:1 "Alice"
SET user:2 "Bob"
LPUSH user:list "item1"
HSET user:hash field "value"

-- Check if key exists
EXISTS user:1
EXISTS nonexistent

-- Get key type
TYPE user:1
TYPE user:list
TYPE user:hash

-- Find keys by pattern
KEYS user:*

-- Rename key
RENAME user:1 customer:1
GET customer:1

-- Delete keys
DEL customer:1
DEL user:2 user:list user:hash

Transactions

Redis transactions group commands for atomic execution using MULTI/EXEC. All commands between MULTI and EXEC are queued and executed together. DISCARD cancels a transaction. WATCH enables optimistic locking - the transaction fails if watched keys change before EXEC. Use transactions for operations that must succeed or fail together.

-- Basic transaction
MULTI
SET account:1 1000
SET account:2 500
INCR account:1
DECR account:2
EXEC

GET account:1
GET account:2

-- Discard transaction
MULTI
SET temp "value"
DISCARD

-- Check temp was not set
EXISTS temp