Package: memphis-py
Memphis.devis a highly scalable, painless, and effortless data streaming platform.Made to enable developers and data teams to collaborate and buildreal-time and streaming apps fast.Installation$pip3installmemphis-pyNotice: you may receive an error about the "mmh3" package, to solve it please install python3-devel$sudoyuminstallpython3-develImportingfrommemphisimportMemphis,Headersfrommemphis.typesimportRetention,StorageimportasyncioConnecting to MemphisFirst, we need to create Memphisobjectand then connect with Memphis by usingmemphis.connect.asyncdefmain():try:memphis=Memphis()awaitmemphis.connect(host="<memphis-host>",username="<application-type username>",account_id=<account_id>,# You can find it on the profile page in the Memphis UI. This field should be sent only on the cloud version of Memphis, otherwise it will be ignoredconnection_token="<broker-token>",# you will get it on application type user creationpassword="<string>",# depends on how Memphis deployed - default is connection token-based authenticationport=<port>,# defaults to 6666reconnect=True,# defaults to Truemax_reconnect=10,# defaults to -1 which means reconnect indefinitelyreconnect_interval_ms=1500,# defaults to 1500timeout_ms=1500,# defaults to 1500# for TLS connection:key_file='<key-client.pem>',cert_file='<cert-client.pem>',ca_file='<rootCA.pem>')...exceptExceptionase:print(e)finally:awaitmemphis.close()if__name__=='__main__':asyncio.run(main())asyncdefconnect(self,host:str,username:str,account_id:int=1,# Cloud use only, ignored otherwiseconnection_token:str="",# JWT token given when creating client accountspassword:str="",# For password-based connectionsport:int=6666,reconnect:bool=True,max_reconnect:int=10,reconnect_interval_ms:int=1500,timeout_ms:int=2000,# For TLS connections:cert_file:str="",key_file:str="",ca_file:str="",)The connect function in the Memphis class allows for the connection to Memphis. Connecting to Memphis (cloud or open-source) will be needed in order to use any of the other functionality of the Memphis class. Upon connection, all of Memphis' features are available.What arguments are used with the Memphis.connect function change depending on the type of connection being made.For details on deploying memphis open-source with different types of connections see thedocs.A password-based connection would look like this (using the defualt root memphis login with Memphis open-source):# Imports hidden. See other examplesasyncdefmain():try:memphis=Memphis()awaitmemphis.connect(host="localhost",username="root",password="memphis",# port = 6666, default port# reconnect = True, default reconnect setting# max_reconnect = 10, default number of reconnect attempts# reconnect_interval_ms = 1500, default reconnect interval# timeout_ms = 2000, default duration of time for the connection to timeout)exceptExceptionase:print(e)finally:awaitmemphis.close()if__name__=='__main__':asyncio.run(main())If you wanted to connect to Memphis cloud instead, simply add your account ID and change the host. The host and account_id can be found on the Overview page in the Memphis cloud UI under your name at the top. Here is an example to connecting to a cloud broker that is located in US East:# Imports hidden. See other examplesasyncdefmain():try:memphis=Memphis()awaitmemphis.connect(host="aws-us-east-1.cloud.memphis.dev",username="my_client_username",password="my_client_password",account_id="123456789"# port = 6666, default port# reconnect = True, default reconnect setting# max_reconnect = 10, default number of reconnect attempts# reconnect_interval_ms = 1500, default reconnect interval# timeout_ms = 2000, default duration of time for the connection to timeout)exceptExceptionase:print(e)finally:awaitmemphis.close()if__name__=='__main__':asyncio.run(main())It is possible to use a token-based connection to memphis as well, where multiple users can share the same token to connect to memphis. Here is an example of using memphis.connect with a token:# Imports hidden. See other examplesasyncdefmain():try:memphis=Memphis()awaitmemphis.connect(host="localhost",username="user",connection_token="token",# port = 6666, default port# reconnect = True, default reconnect setting# max_reconnect = 10, default number of reconnect attempts# reconnect_interval_ms = 1500, default reconnect interval# timeout_ms = 2000, default duration of time for the connection to timeout)exceptExceptionase:print(e)finally:awaitmemphis.close()if__name__=='__main__':asyncio.run(main())The token will be presented when creating new users.Memphis needs to be configured to use a token based connection. See thedocsfor help doing this.For the rest of the examples, the try-except statement and the asyncio runtime call will be withheld to assist with the succinctness of the examples.A TLS based connection would look like this:# Imports hidden. See other examplestry:memphis=Memphis()awaitmemphis.connect(host="localhost",username="user",key_file="~/tls_file_path.key",cert_file="~/tls_cert_file_path.crt",ca_file="~/tls_ca_file_path.crt",# port = 6666, default port# reconnect = True, default reconnect setting# max_reconnect = 10, default number of reconnect attempts# reconnect_interval_ms = 1500, default reconnect interval# timeout_ms = 2000, default duration of time for the connection to timeout)exceptExceptionase:print(e)finally:Memphis needs to configured for these use cases. To configure memphis to use TLS see thedocs.Disconnecting from MemphisTo disconnect from Memphis, callclose()on the memphis object.awaitmemphis.close()Creating a StationStations are distributed units that store messages. Producers add messages to stations and Consumers take messages from them. Each station stores messages until their retention policy causes them to either delete the messages or move them toremote storage.A station will be automatically created for the user when a consumer or producer is used if no stations with the given station name exist.If the station trying to be created exists when this function is called, nothing will change with the exisitng stationasyncdefstation(self,name:str,retention_type:Retention=Retention.MAX_MESSAGE_AGE_SECONDS,# MAX_MESSAGE_AGE_SECONDS/MESSAGES/BYTES/ACK_BASED(cloud only). Defaults to MAX_MESSAGE_AGE_SECONDSretention_value:int=3600,# defaults to 3600storage_type:Storage=Storage.DISK,# Storage.DISK/Storage.MEMORY. Defaults to DISKreplicas:int=1,idempotency_window_ms:int=120000,# defaults to 2 minutesschema_name:str="",# defaults to "" (no schema)send_poison_msg_to_dls:bool=True,# defaults to truesend_schema_failed_msg_to_dls:bool=True,# defaults to truetiered_storage_enabled:bool=False,# defaults to falsepartitions_number:int=1,# defaults to 1dls_station:str="",# defaults to "" (no DLS station). If given, both poison and schema failed events will be sent to the DLS)The station function is used to create a station. Using the different arguemnts, one can programically create many different types of stations. The Memphis UI can also be used to create stations to the same effect.Creating a station with just a name name would create a station with that named and containing the default options provided above:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station")Stations with RetentionTo change what criteria the station uses to decide if a message should be retained in the station, change the retention type. The different types of retention are documentedherein the python README.The unit of the rentention value will vary depending on the retention_type. Theprevious linkalso describes what units will be used.Here is an example of a station which will only hold up to 10 messages:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",retention_type=Retention.MESSAGES,retention_value=10)Station storage typesMemphis stations can either store Messages on disk or in memory. A comparison of those types of storage can be foundhere.Here is an example of how to create a station that uses Memory as its storage type:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",storage_type=Storage.MEMORY)Station ReplicasIn order to make a station more redundant, replicas can be used. Read more about replicashere. Note that replicas are only available in cluster mode. Cluster mode can be enabled in theHelm settingswhen deploying Memphis with Kubernetes.Here is an example of creating a station with 3 replicas:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",replicas=3)Station idempotencyIdempotency defines how Memphis will prevent duplicate messages from being stored or consumed. The duration of time the message ID's will be stored in the station can be set with idempotency_window_ms. If the environment Memphis is deployed in has unreliably connection and/or a lot of latency, increasing this value might be desiriable. The default duration of time is set to two minutes. Read more about idempotencyhere.Here is an example of changing the idempotency window to 3 seconds:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",idempotency_window_ms=180000)Enforcing a schemaThe schema name is used to set a schema to be enforced by the station. The default value of "" ensures that no schema is enforced. Here is an example of changing the schema to a defined schema in schemaverse called "sensor_logs":memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",schema="sensor_logs")Dead Letter StationsThere are two parameters for sending messages to thedead-letter station(DLS). These are send_poison_msg_to_dls and send_schema_failed_msg_to_dls.Here is an example of sending poison messages to the DLS but not messages which fail to conform to the given schema.memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",schema="sensor_logs",send_poison_msg_to_dls=True,send_schema_failed_msg_to_dls=False)When either of the DLS flags are set to True, a station can also be set to handle these events. To set a station as the station to where schema failed or poison messages will be set to, use the dls_station parameter:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",schema="sensor_logs",send_poison_msg_to_dls=True,send_schema_failed_msg_to_dls=False,dls_station="bad_sensor_messages_station")Station Tiered StorageWhen the retention value is met, Mempihs by default will delete old messages. If tiered storage is setup, Memphis can instead move messages to tier 2 storage. Read more about tiered storagehere. Enable this setting with the respective flag:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",tiered_storage_enabled=True)Station PartitionsPartitioningmight be useful for a station. To have a station partitioned, simply change the partitions number:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.station(name="my_station",partitions_number=3)Retention typesRetention types define the methodology behind how a station behaves with its messages. Memphis currently supports the following retention types:memphis.types.Retention.MAX_MESSAGE_AGE_SECONDSWhen the retention type is set to MAX_MESSAGE_AGE_SECONDS, messages will persist in the station for the number of seconds specified in the retention_value.memphis.types.Retention.MESSAGESWhen the retention type is set to MESSAGES, the station will only hold up to retention_value messages. The station will delete the oldest messsages to maintain a retention_value number of messages.memphis.types.Retention.BYTESWhen the retention type is set to BYTES, the station will only hold up to retention_value BYTES. The oldest messages will be deleted in order to maintain at maximum retention_vlaue BYTES in the station.memphis.types.Retention.ACK_BASED# for cloud users onlyWhen the retention type is set to ACK_BASED, messages in the station will be deleted after they are acked by all subscribed consumer groups.Retention ValuesThe unit of theretention_valuechanges depending on theretention_typespecified.All retention values are of typeint. The following units are used based on the respective retention type:memphis.types.Retention.MAX_MESSAGE_AGE_SECONDSisin seconds,memphis.types.Retention.MESSAGESis anumber of messages,memphis.types.Retention.BYTESis anumber of bytes,Withmemphis.ACK_BASED, theretention_typeis ignoredStorage typesMemphis currently supports the following types of messages storage:memphis.types.Storage.DISKWhen storage is set to DISK, messages are stored on disk.memphis.types.Storage.MEMORYWhen storage is set to MEMORY, messages are stored in the system memory.Destroying a StationDestroying a station will remove all its resources (including producers/consumers)station.destroy()Creating a New SchemaIn case schema is already exist a new version will be createdawaitmemphis.create_schema("<schema-name>","<schema-type>","<schema-file-path>")Current available schema types - Protobuf / JSON schema / GraphQL schema / AvroEnforcing a Schema on an Existing Stationasyncdefenforce_schema(self,name,station_name)To add a schema to an already created station, enforce_schema can be used. Here is an example using enforce_schmea to add a schema to a station:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.enforce_schmea(name="my_schmea",station_name="my_station")Deprecated  - Attaching a Schema, use enforce_schema insteadawaitmemphis.attach_schema("<schema-name>","<station-name>")Detaching a Schema from Stationasyncdefdetach_schema(self,station_name)To remove a schema from an already created station, detach_schema can be used. Here is an example of removing a schmea from a station:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.detach_schmea(station_name="my_station")Produce and Consume messagesThe most common client operations are usingproduceto send messages andconsumeto
receive messages.Messages are published to a station with a Producer and consumed from it by a Consumer.Consumers are poll based and consume all the messages in a station. Consumers can also be grouped into consumer groups. When consuming with a consumer group, all consumers in the group will receive each message.Memphis messages are payload agnostic. Payloads are alwaysbytearrays.In order to stop getting messages, you have to callconsumer.destroy(). Destroy will terminate the consumer even if messages are currently being sent to the consumer.If a station is created with more than one partition, producing to and consuming from the station will happen in a round robin fashion.Creating a Producerasyncdefproducer(self,station_name:str,producer_name:str,generate_random_suffix:bool=False,#Depreicated)Use the Memphis producer function to create a producer. Here is an example of creating a producer for a given station:memphis=Memphis()awaitmemphis.connect(...)producer=awaitmemphis.producer(station_name="my_station",producer_name="new_producer")Producing a messageasyncdefproduce(self,message,ack_wait_sec:int=15,headers:Union[Headers,None]=None,async_produce:Union[bool,None]=None,nonblocking:bool=False,msg_id:Union[str,None]=None,concurrent_task_limit:Union[int,None]=None,producer_partition_key:Union[str,None]=None,producer_partition_number:Union[int,-1]=-1):Both producers and connections can use the produce function. To produce a message from a connection, simply callmemphis.produce. This function will create a producer if none with the given name exists, otherwise it will pull the producer from a cache and use it to produce the message.awaitmemphis.produce(station_name='test_station_py',producer_name='prod_py',message='bytearray/protobuf class/dict/string/graphql.language.ast.DocumentNode',# bytearray / protobuf class (schema validated station - protobuf) or bytearray/dict (schema validated station - json schema) or string/bytearray/graphql.language.ast.DocumentNode (schema validated station - graphql schema) or bytearray/dict (schema validated station - avro schema)ack_wait_sec=15,# defaults to 15headers=headers,# default to {}nonblocking=False,#defaults to falsemsg_id="123",producer_partition_key="key"#default to None)Creating a producer and calling produce on it will increase the performance of producing messages as it removes the overhead of pulling created producers from the cache.awaitproducer.produce(message='bytearray/protobuf class/dict/string/graphql.language.ast.DocumentNode',# bytearray / protobuf class (schema validated station - protobuf) or bytearray/dict (schema validated station - json schema) or string/bytearray/graphql.language.ast.DocumentNode (schema validated station - graphql schema) or or bytearray/dict (schema validated station - avro schema)ack_wait_sec=15)# defaults to 15Here is an example of a produce function call that waits up to 30 seconds for an acknowledgement from memphis and does so in an nonblocking manner:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.produce(station_name="some_station",producer_name="temp_producer",message={'some':'message'},ack_wait_sec=30,nonblocking=True)Producing with idempotencyAs discussed before in the station section, idempotency is an important feature of memphis. To achieve idempotency, an id must be assigned to messages that are being produced. Use the msg_id parameter for this purpose.memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.produce(station_name="some_station",producer_name="temp_producer",message={'some':'message'},msg_id='42')Producing with headersTo add message headers to the message, use the headers parameter. Headers can help with observability when using certain 3rd party to help monitor the behavior of memphis. Seeherefor more details.memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.produce(station_name="some_station",producer_name="temp_producer",message={'some':'message'},headers={'trace_header':'track_me_123'})Producing to a partitionLastly, memphis can produce to a specific partition in a station. To do so, use the producer_partition_key parameter:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.produce(station_name="some_station",producer_name="temp_producer",message={'some':'message'},producer_partition_key="2nd_partition")Or, alternatively, use the producer_partition_number parameter:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.produce(station_name="some_station",producer_name="temp_producer",message={'some':'message'},producer_partition_number=2)Non-blocking Produce with Task LimitsFor better performance, the client won't block requests while waiting for an acknowledgment.
If you are producing a large number of messages very quickly, there maybe some timeout errors, then you may need to limit the number of concurrent tasks to get around this:awaitproducer.produce(message='bytearray/protobuf class/dict/string/graphql.language.ast.DocumentNode',# bytearray / protobuf class (schema validated station - protobuf) or bytearray/dict (schema validated station - json schema) or string/bytearray/graphql.language.ast.DocumentNode (schema validated station - graphql schema)headers={},nonblocking=True,limit_concurrent_tasks=500)You may read more about thishereon the memphis.dev blog.Produce to multiple stationsProducing to multiple stations can be done by creating a producer with multiple stations and then calling produce on that producer.memphis=Memphis()awaitmemphis.connect(...)producer=awaitmemphis.producer(station_name=["station_1","station_2"],producer_name="new_producer")awaitproducer.produce(message="some message")Alternatively, it also possible to produce to multiple stations using the connection:memphis=Memphis()awaitmemphis.connect(...)awaitmemphis.produce(station_name=["station_1","station_2"],producer_name="new_producer",message="some message")Destroying a Producerproducer.destroy()Creating a Consumerconsumer=awaitmemphis.consumer(station_name="<station-name>",consumer_name="<consumer-name>",consumer_group="<group-name>",# defaults to the consumer namepull_interval_ms=1000,# defaults to 1000batch_size=10,# defaults to 10batch_max_time_to_wait_ms=100,# defaults to 100max_ack_time_ms=30000,# defaults to 30000max_msg_deliveries=2,# defaults to 2start_consume_from_sequence=1,# start consuming from a specific sequence. defaults to 1last_messages=-1# consume the last N messages, defaults to -1 (all messages in the station))Consumers are used to pull messages from a station. Here is how to create a consumer with all of the default parameters:memphis=Memphis()awaitmemphis.connect(...)consumer=awaitMemphis.consumer(station_name="my_station",consumer_name:"new_consumer",)To create a consumer in a consumer group, add the consumer_group parameter:memphis=Memphis()awaitmemphis.connect(...)consumer=awaitMemphis.consumer(station_name="my_station",consumer_name:"new_consumer",consumer_group:"consumer_group_1")When using Consumer.consume, the consumer will continue to consume in an infinite loop. To change the rate at which the consumer polls the station for new messages, change the pull_interval_ms parameter:memphis=Memphis()awaitmemphis.connect(...)consumer=awaitMemphis.consumer(station_name="my_station",consumer_name="new_consumer",pull_interval_ms=2000)Every time the consumer pulls from the station, the consumer will try to take batch_size number of elements from the station. However, sometimes there are not enough messages in the station for the consumer to consume a full batch. In this case, the consumer will continue to wait until either batch_size messages are gathered or the time in milliseconds specified by batch_max_time_to_wait_ms is reached.Here is an example of a consumer that will try to poll 100 messages every 10 seconds while waiting up to 15 seconds for all messages to reach the consumer.memphis=Memphis()awaitmemphis.connect(...)consumer=awaitMemphis.consumer(station_name="my_station",consumer_name="new_consumer",pull_interval_ms=10000,batch_size=100,batch_max_time_to_wait_ms=100)The max_msg_deliveries parameter allows the user how many messages the consumer is able to consume before consuming more. The max_ack_time_ms Here is an example where the consumer will only hold up to one batch of messages at a time:memphis=Memphis()awaitmemphis.connect(...)consumer=awaitMemphis.consumer(station_name="my_station",consumer_name="new_consumer",pull_interval_ms=10000,batch_size=100,batch_max_time_to_wait_ms=100,max_msg_deliveries=2)Consume using a partition keyThe key will be used to consume from a specific partitionconsumer.consume(msg_handler,consumer_partition_key="key"#consume from a specific partition)Consume using a partition numberThe number will be used to consume from a specific partitionconsumer.consume(msg_handler,consumer_partition_number=-1#consume from a specific partition)Setting a context for message handler functioncontext={"key":"value"}consumer.set_context(context)Processing messagesTo use a consumer to process messages, use the consume function. The consume function will have a consumer poll a station for new messages as discussed in previous sections. The consumer will stop polling the statoin once all the messages in the station were consumed, and the msg_handler will receive aMemphis: TimeoutError.asyncdefmsg_handler(msgs,error,context):formsginmsgs:print("message: ",msg.get_data())awaitmsg.ack()iferror:print(error)consumer.consume(msg_handler)Processing schema deserialized messagesTo get messages deserialized, usemsg.get_data_deserialized().asyncdefmsg_handler(msgs,error,context):formsginmsgs:print("message: ",awaitmsg.get_data_deserialized())awaitmsg.ack()iferror:print(error)consumer.consume(msg_handler)There may be some instances where you apply a schemaaftera station has received some messages. In order to consume those messages get_data_deserialized may be used to consume the messages without trying to apply the schema to them. As an example, if you produced a string to a station and then attached a protobuf schema, using get_data_deserialized will not try to deserialize the string as a protobuf-formatted message.Fetch a single batch of messagesUsing fetch_messages or fetch will allow the user to remove a specific number of messages from a given station. This behavior could be beneficial if the user does not want to have a consumer actively poll from a station indefinetly.msgs=awaitmemphis.fetch_messages(station_name="<station-name>",consumer_name="<consumer-name>",consumer_group="<group-name>",# defaults to the consumer namebatch_size=10,# defaults to 10batch_max_time_to_wait_ms=100,# defaults to 100max_ack_time_ms=30000,# defaults to 30000max_msg_deliveries=2,# defaults to 2start_consume_from_sequence=1,# start consuming from a specific sequence. defaults to 1last_messages=-1,# consume the last N messages, defaults to -1 (all messages in the station))consumer_partition_key="key",# used to consume from a specific partition, default to Noneconsumer_partition_number=-1# used to consume from a specific partition, default to -1)Fetch a single batch of messages after creating a consumermsgs=awaitconsumer.fetch(batch_size=10)# defaults to 10prefetch = truewill prefetch next batch of messages and save it in memory for future fetch() requestmsgs=awaitconsumer.fetch(batch_size=10,prefetch=True)# defaults to FalseAcknowledge a messageAcknowledge a message indicates the Memphis server to not re-send the same message again to the same consumer / consumers groupawaitmessage.ack()Nacking a MessageMark the message as not acknowledged - the broker will resend the message immediately to the same consumers group, instead of waiting to the max ack time configured.awaitmessage.nack();Sending a message to the dead-letterSending the message to the dead-letter station (DLS) - the broker won't resend the message again to the same consumers group and will place the message inside the dead-letter station (DLS) with the given reason.
The message will still be available to other consumer groupsawaitmessage.dead_letter("reason");Delay the message after a given durationDelay the message and tell Memphis server to re-send the same message again to the same consumer group. The message will be redelivered only in caseconsumer.max_msg_deliveriesis not reached yet.awaitmessage.delay(delay_in_seconds)Get headersGet headers per messageheaders=message.get_headers()Get message sequence numberGet message sequence numbersequence_number=msg.get_sequence_number()Get message time sentGet message time senttime_sent=msg.get_timesent()Destroying a Consumerconsumer.destroy()Check connection statusmemphis.is_connected()