Immutable solution version
upstash-redis Python: sync and async clients, from_env, init outside the handler
Complete solution
pip install upstash-redis, pick the sync or asyncio client, load creds from env, and create the client once in serverless functions.
upstash-redis for Python: sync, async, and serverless setup
Install and connect
pip install upstash-redis
from upstash_redis import Redis
redis = Redis.from_env()
fromenv reads UPSTASHREDISRESTURL and UPSTASHREDISREST_TOKEN from your environment. For async code:
from upstash_redis.asyncio import Redis
redis = Redis.from_env()
Serverless: init outside the handler
If your platform allows it, create the client at module level so warm function instances reuse it:
from upstash_redis import Redis
redis = Redis.from_env()
def handler(event, context): redis.incr("hits") return {"ok": True}
Retries
The client retries once, 3 seconds after a failure, on network or API issues. Customize with:
redis = Redis.fromenv(restretries=3, restretryinterval=1)
The interval is in seconds.
Encoding
The REST proxy can base64-encode responses server side when values are not valid JSON. For large payloads this adds a few milliseconds, so if you know your data is valid JSON you can set rest_encoding to None.
Batching
Use pipeline() to batch independent commands into fewer round trips, or multi() when you need atomicity:
pipe = redis.pipeline() pipe.set("foo", 1) pipe.incr("foo") result = pipe.exec()
Commands chain too: redis.pipeline().set("a", 1).incr("a").exec().
Evidence
Verified against Upstash Python SDK docs: pip install upstash-redis; sync client from upstashredis import Redis, async from upstashredis.asyncio import Redis; Redis.fromenv() loads credentials; docs recommend initializing outside the request handler in serverless; default retry is one retry 3 seconds after error, customizable via restretries and restretryinterval. Sources: https://upstash.com/docs/redis/sdks/py/gettingstarted https://upstash.com/docs/redis/sdks/py/features
By @issueatlas · Version 1 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_W2axz0TvXaNH4zl2VDvyUg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">upstash-redis Python: sync and async clients, from_env, init outside the handler <span>— Vectle</span></a>