Skip to content

Commit

Permalink
add and update Redis and NoSQL pages
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCurrin authored Aug 7, 2024
1 parent beaec93 commit 06f325f
Show file tree
Hide file tree
Showing 10 changed files with 666 additions and 13 deletions.
41 changes: 41 additions & 0 deletions cheatsheets/nosql/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
# NoSQL


### Types of DBs

The NoSQL database types, from most to least common:

| Type | Description | Examples |
|------|--------------|----------|
| Key-Value Store | Stores data as key-value pairs, with keys acting as unique identifiers and values containing the associated data. | Redis, DynamoDB, Riak |
| Document Store | Stores data as semi-structured documents, typically in JSON or XML format. Documents can have nested structures and are schema-less. | MongoDB, Couchbase, DocumentDB |
| Column-Family Store | Stores data in column families, which are containers for rows of data. Columns can be added dynamically, and data is stored and retrieved by column family. | Apache Cassandra, HBase, Bigtable |
| Graph Database | Stores data as nodes and relationships (edges) between nodes, optimized for traversing and querying highly connected data. | Neo4j, JanusGraph, Amazon Neptune |
| Time-Series Database | Optimized for storing and analyzing time-series data, such as sensor data, application metrics, and financial data. | InfluxDB, Prometheus, Timescale |
| Search Engine Database | Designed for full-text search and indexing large amounts of data. | Solr, Sphinx, Elasticsearch |
| Multi-Model Database | Supports multiple data models (e.g., key-value, document, graph) within a single database. | ArangoDB, OrientDB, MarkLogic |
| Object Store | Stores data as objects, with each object having a unique identifier and associated metadata. | Amazon S3, Azure Blob Storage, OpenStack Swift |
| Event Store | Designed to store and process event data, often used in event-driven architectures and event sourcing. | Event Store, Kafka Streams |


### Comparison of DBs

Redis, MongoDB, and other popular NoSQL databases, focusing on their differences relevant for cloud engineers:

| Database | Type | Performance | Storage | Persistence | Support | Cost |
|-----------|------|--------------|---------|--------------|----------|------|
| Redis | Key-Value | Extremely fast, in-memory | RAM, disk (optional) | Configurable, snapshots | Excellent, widely used | Open-source (free), paid enterprise support |
| AWS DynamoDB | Key-Value | High, fully managed | Cloud storage | Automatic, durable | Excellent, fully managed | Pay-per-use, can be expensive at scale |
| MongoDB | Document | High, with indexing | Disk | Automatic, journaling | Excellent, widely used | Open-source (free), paid enterprise support |
| Elasticsearch | Document | High, distributed | Disk | Automatic, replication | Excellent, widely used | Open-source (free), paid enterprise support |
| Couchbase | Document | High, with indexing | Disk, RAM (optional) | Automatic, replication | Good, widely used | Open-source (free), paid enterprise support |
| Cassandra | Wide Column | High, distributed | Disk | Automatic, replication | Good, widely used | Open-source (free), paid enterprise support |
| Neo4j | Graph | High for graph queries | Disk | Automatic, snapshots | Good, widely used | Open-source (free), paid enterprise support |

Key differences:

- **Type**: Redis is a key-value store, MongoDB and Couchbase are document stores, Cassandra is a wide column store, and Neo4j is a graph database.
- **Performance**: Redis is extremely fast due to its in-memory nature, while others trade some speed for durability by storing data on disk.
- **Storage**: Redis can use RAM or disk, while others primarily use disk storage.
- **Persistence**: Redis requires configuration for persistence, while others automatically handle it.
- **Support**: Most have excellent community support, with paid enterprise options available.
- **Cost**: Redis, MongoDB, Cassandra, Couchbase, and Elasticsearch have open-source versions, while DynamoDB is a fully managed service with pay-per-use pricing.
44 changes: 31 additions & 13 deletions cheatsheets/nosql/redis.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# Redis

A key-value NoSQL database.
A in-memory data structure store, known for high performance as a database, cache, and message broker. It can handel complex data types and large volumes at a speed and with reliability.

Homepage: [https://redis.io/](https://redis.io/).

## Related

- [redis][] cheatsheet in the Python libraries section.

[redis]: {% link cheatsheets/python/libraries/database/redis/index.md %}


## Security tips

Protect unauthorized data access and manipulation.

- Authentication - use strong authentication mechanims so only autherized applications and user have access.
- Use TLS for data transfer between Redis and client apps.
- Network security - Apply firewalls to limit IP access and monitor network traffic.

## Redis commands

See [Commands](https://redis.io/docs/latest/commands/) on the Redis docs.
Expand All @@ -17,21 +32,23 @@ See [Install](https://redis.io/docs/latest/operate/oss_and_stack/install/install

As a NoSQL in-memory database with no schema, Redis is faster than SQL databases for IO.

Redis is useful for simple data types and structures.
### Use cases

Redis is useful for simple data types and structures where persistance of data is not a priority. Such as:

- cache (store frequently-accessed data to reduce response time)
- queue
- logged in users
- session storage for authenticated users
- message brokering for realtime applications
- counter

And where persistance of data is not a priority.

### Data types

- Strings (there are no numbers)
- Lists
- Sets (unique)
- Sorted sets (where order is important such as leaderboard)
- Hashes (such as attributes of a user)
- Strings (there are no numbers) - can store text or binary data. Useful for caching info or counters.
- Lists - good for queues or leaderboards where duplicates are allowed.
- Sets - efficient storage for unique items.
- Sorted sets - set but with order. For where order is important such as leaderboard.
- Hashes - represent objects. Attributes can be accessed without retrieving the etire object.
- Streams

More advanced:
Expand All @@ -42,15 +59,17 @@ More advanced:

### Start and stop Redis using the shell

Start server:
Add `.exe` for Windows.

#### Start server

```sh
$ redis-server
```

Or start as a service using a package manager for your OS.

Start CLI client:
### Start CLI client

```sh
$ redis-cli
Expand Down Expand Up @@ -139,7 +158,6 @@ A shopping cart of data for users. The cart is 123 and items are 1 and 2 here.
6
```


### Lists

They are implemented as doubly linked lists. Inserting and removing from either end has $$O(1)$$ time complexity. Retrieving an element in the middle has $$O(N)$$ time complexity since you cannot look up an element by index directly.
Expand Down
57 changes: 57 additions & 0 deletions cheatsheets/python/libraries/database/redis/data-types/hashes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Hash operations

## Get

```python
# Get all fields and values of a hash
user = r.hgetall('user:1000')
print(user) # {b'name': b'John Doe', b'email': b'[email protected]', b'age': b'30'}
```

```python
# Get the number of fields in a hash
count = r.hlen('user:1000')
print(count) # 3

# Get all field names
fields = r.hkeys('user:1000')
print(fields) # [b'name', b'email', b'visits']
```

```python
# Get the value of a specific field
name = r.hget('user:1000', 'name')
print(name) # b'John Doe'
# Get values of multiple fields
values = r.hmget('user:1000', ['name', 'age'])
print(values) # [b'John Doe', b'30']
```

```python
# Check if a field exists
exists = r.hexists('user:1000', 'email')
print(exists) # True
```

## Create/update

```python
# Set hash fields
r.hset('user:1000', mapping={'name': 'John Doe', 'email': '[email protected]', 'age': '30'})

# Set a field only if it doesn't exist
r.hsetnx('user:1000', 'visits', '10')
```

```python
# Increment the integer value of a field
r.hincrby('user:1000', 'visits', 1)
```


## Remove

```python
# Delete one or more fields
r.hdel('user:1000', 'age')
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Data types
63 changes: 63 additions & 0 deletions cheatsheets/python/libraries/database/redis/data-types/lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# List operations

## Get

```python
# Get list length
length = r.llen('fruits')
print(length) # 4
```

```python
# Get range of items (0-based indexing)
items = r.lrange('fruits', 0, -1)
print(items) # [b'date', b'apple', b'banana', b'cherry']
```

## Create/update

```python
# Push items to the right of a list
r.rpush('fruits', 'apple', 'banana', 'cherry')
```

```python
# Push items to the left of a list
r.lpush('fruits', 'date')
```

```python
# Insert item before or after specific value
r.linsert('fruits', 'BEFORE', 'banana', 'kiwi')
```

```python
# Set item at specific index
r.lset('fruits', 0, 'grape')
```

## Remove

```python
# Remove and return an item from the right
item = r.rpop('fruits')
print(item) # b'cherry'
```

```python
# Remove and return an item from the left
item = r.lpop('fruits')
print(item) # b'date'
```

```python
# Trim a list to specified range
r.ltrim('fruits', 0, 1) # Keep only first two items
```

## Block

```python
# Block and wait for item to be pushed to a list
item = r.brpop('fruits', timeout=5) # Wait up to 5 seconds
```
62 changes: 62 additions & 0 deletions cheatsheets/python/libraries/database/redis/data-types/sets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Set operations

## Get

```python
# Get the number of members in a set
count = r.scard('fruits')
print(count) # 2
```

```python
# Get all members of a set
members = r.smembers('fruits')
print(members) # {b'apple', b'banana', b'cherry'}
```

```python
# Check if a member exists in a set
exists = r.sismember('fruits', 'apple')
print(exists) # True
```

## Create/update

```python
# Add members to a set
r.sadd('fruits', 'apple', 'banana', 'cherry')
```

```python
# Move a member from one set to another
r.smove('tropical', 'fruits', 'banana')
```

## Remove

```python
# Remove a member from a set
r.srem('fruits', 'banana')
```


## Comparisons

```python
# Get the difference between sets
r.sadd('tropical', 'banana', 'mango')
diff = r.sdiff('fruits', 'tropical')
print(diff) # {b'apple', b'cherry'}
```

```python
# Get the intersection of sets
inter = r.sinter('fruits', 'tropical')
print(inter) # set()
```

```python
# Get the union of sets
union = r.sunion('fruits', 'tropical')
print(union) # {b'apple', b'cherry', b'banana', b'mango'}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Sorted set operations

## Get

```python
# Get the score of a member
score = r.zscore('leaderboard', 'Bob')
print(score) # 80.0
```

```python
# Get the rank of a member (0-based)
rank = r.zrank('leaderboard', 'Charlie')
print(rank) # 1
```

```python
# Get range of members by rank (0-based)
members = r.zrange('leaderboard', 0, -1, withscores=True)
print(members) # [(b'Bob', 80.0), (b'Charlie', 95.0), (b'Alice', 100.0)]
```

```python
# Get range of members by score
members = r.zrangebyscore('leaderboard', 90, 100, withscores=True)
print(members) # [(b'Charlie', 95.0), (b'Alice', 100.0)]
```

```python
# Get the number of members in a sorted set
count = r.zcard('leaderboard')
print(count) # 2
```

```python
# Count members with scores within a range
count = r.zcount('leaderboard', 90, 100)
print(count) # 2
```


## Create/update

```python
# Add members with scores to a sorted set
r.zadd('leaderboard', {'Alice': 100, 'Bob': 80, 'Charlie': 95})
```

```python
# Increment the score of a member
r.zincrby('leaderboard', 5, 'Bob')
```

## Remove

```python
# Remove members from a sorted set
r.zrem('leaderboard', 'Charlie')
```

Loading

0 comments on commit 06f325f

Please sign in to comment.