Telethon Redis session
A Redis-backed session implementation for Telethon.
Multiple accounts can share a Redis database; each account uses a distinct session name.
Setup
Requires Python 3.10+ and uv.
The minimum Telethon version is 1.31.0. Earlier releases import imghdr,
which was removed in Python 3.13.
Version 1.31.0 provides the required session APIs without that dependency.
uv sync --locked
This installs this checkout and its development dependencies into .venv.
After updating the checkout, run the same command. After deliberately changing
dependencies, regenerate uv.lock with uv lock.
To install this package from PyPI in another uv-managed service:
uv add telethon-redis
Usage
Provide REDIS_URL, TELEGRAM_API_ID, and TELEGRAM_API_HASH through your
environment or secret manager. Use a stable, unique session name for each account.
import os
import redis
from telethon import TelegramClient
from teleredis import RedisSession
connection = redis.Redis.from_url(os.environ["REDIS_URL"], decode_responses=False)
session = RedisSession("account_name", connection)
client = TelegramClient(
session,
int(os.environ["TELEGRAM_API_ID"]),
os.environ["TELEGRAM_API_HASH"],
)
client.start()
Save the example as bot.py and run it with uv run --locked python bot.py.
Storage and startup
Under the default telethon:client hive:
| Key | Purpose |
|---|---|
telethon:client:<session>:active_dc |
Active data-center ID |
telethon:client:<session>:sessions:<dc_id> |
Connection metadata and authentication key |
Session loading uses two direct GET commands. Writes atomically update the
session record and active-DC pointer with MSET. Telethon's startup self-ID
lookups use one MGET per lookup for the possible user, chat, and channel entity
keys instead of scanning all entities. When IDs overlap, lookup prefers user,
then chat, then channel. Exact-ID lookups use one GET.
Existing accounts must be migrated before deploying this version. Startup never discovers old records or repairs pointers automatically:
- A missing pointer means a new, unauthenticated session.
- A pointer whose record is missing raises an error instead of selecting another DC.
- A record whose DC disagrees with its pointer also raises an error.
Phone, username, and name lookups still use KEYS; they are not part of ordinary
saved-session startup. The public list_sessions() maintenance method uses
incremental SCAN but is never called while loading a session.
Migrate existing sessions
Run from the repository root after uv sync --locked.
-
Back up Redis and verify your restore procedure. Stop every client writing this session hive, including supervisors, workers, and login services. Keep them stopped through migration and deployment. Do not mix old and new writers.
-
Export
REDIS_URLfor the correct host and database, preferably through your secret manager. For a local instance without authentication:export REDIS_URL='redis://localhost:6379/0'
-
Preview the migration. Without
--apply, nothing is written:uv run --locked python scripts/migrate_active_dc.py
The command traverses the database once with incremental
SCAN, fetching only matching session records, never entity values. It preserves a valid active pointer or selects the sole authenticated DC, ignoring empty-auth placeholders. Any validation or selection error prevents all writes. -
If an account has multiple authenticated DC records, determine its intended active DC from your account configuration or known working deployment. Create
active-dcs.jsonwith explicit choices:{ "account_name": 4, "another_account_name": 5 }
Use the exact session names passed to
RedisSession, including the leading+for phone-number session names. Explicit choices override existing pointers, so review the preview:uv run --locked python scripts/migrate_active_dc.py --dc-map active-dcs.json
-
Apply the reviewed plan:
uv run --locked python scripts/migrate_active_dc.py --apply # If explicit choices were required: uv run --locked python scripts/migrate_active_dc.py --dc-map active-dcs.json --apply
Only active-DC pointers are written, in one atomic
MSET. Authentication keys, session records, entities, and cached files are not rewritten or deleted. Keep the per-DC records: the runtime still uses them. -
Rerun the preview to check the pointers, deploy the patched package to every client, then start the clients. Ordinary startup performs no keyspace traversal. If a selected record is later deleted, stop its writers and repair the pointer explicitly with this procedure.
For a custom hive, pass --hive-prefix on every migration invocation.
For pickle-serialized sessions, explicitly pass --format pickle.
Only unpickle trusted Redis data: pickle can execute arbitrary code.
The default format is JSON.
Development
uv run --locked python -m unittest discover -s tests -v
uv build
Package metadata and dependencies live in pyproject.toml; uv.lock records the
resolved environment. The package version comes from teleredis/__init__.py.
Credits
Based on telethon-session-redis by Konstantin M. (@ezdev128).
Metadata
Release files for telethon-redis 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| telethon_redis-0.1.0.tar.gz | 14.4 kB | With provenance | Details for telethon_redis-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| telethon_redis-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for telethon_redis-0.1.0-py3-none-any.whl |
Total release size: 21.6 kB
Release files / telethon_redis-0.1.0.tar.gz
| Download URL | telethon_redis-0.1.0.tar.gz |
|---|---|
| Size | 14.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6121dd398478629716eb4c3651fc4ce3f8d6b1dcb332182a9f7ff170bff0690f
|
|
BLAKE2b-256 checksum How to use checksums |
47119686897a195bb462874642afa73b06f9c5f1714b1ecbc6ca5d147b97dfef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logRelease files / telethon_redis-0.1.0-py3-none-any.whl
| Download URL | telethon_redis-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
483524049382bf1d230ed3ed24b09a9f1ab4c56372a69faf02a0cd273ecff6a0
|
|
BLAKE2b-256 checksum How to use checksums |
c74486f79f991b5da9a8fa89e0bd469b7cb5054a77e34df3c343fe45fccf9087
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logSecurity for telethon-redis 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for telethon_redis-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.